001/*
002 * Copyright 2015-2020 the original author or authors
003 *
004 * This software is licensed under the Apache License, Version 2.0,
005 * the GNU Lesser General Public License version 2 or later ("LGPL")
006 * and the WTFPL.
007 * You may choose either license to govern your use of this software only
008 * upon the condition that you accept all of the terms of either
009 * the Apache License 2.0, the LGPL 2.1+ or the WTFPL.
010 */
011package org.minidns.dnssec;
012
013import org.minidns.dnsmessage.DnsMessage;
014import org.minidns.dnsmessage.Question;
015import org.minidns.record.Data;
016import org.minidns.record.Record;
017
018import java.io.IOException;
019import java.security.spec.InvalidKeySpecException;
020import java.util.List;
021
022public class DnssecValidationFailedException extends IOException {
023    private static final long serialVersionUID = 5413184667629832742L;
024
025    public DnssecValidationFailedException(Question question, String reason) {
026        super("Validation of request to " + question + " failed: " + reason);
027    }
028
029    public DnssecValidationFailedException(String message) {
030        super(message);
031    }
032
033    public DnssecValidationFailedException(String message, Throwable cause) {
034        super(message, cause);
035    }
036
037    public DnssecValidationFailedException(Record<? extends Data> record, String reason) {
038        super("Validation of record " + record + " failed: " + reason);
039    }
040
041    public DnssecValidationFailedException(List<Record<? extends Data>> records, String reason) {
042        super("Validation of " + records.size() + " " + records.get(0).type + " record" + (records.size() > 1 ? "s" : "") + " failed: " + reason);
043    }
044
045    public static class DataMalformedException extends DnssecValidationFailedException {
046
047        /**
048         * 
049         */
050        private static final long serialVersionUID = 1L;
051
052        private final byte[] data;
053
054        public DataMalformedException(IOException exception, byte[] data) {
055            super("Malformed data", exception);
056            this.data = data;
057        }
058
059        public DataMalformedException(String message, IOException exception, byte[] data) {
060            super(message, exception);
061            this.data = data;
062        }
063
064        public byte[] getData() {
065            return data;
066        }
067    }
068
069    public static class DnssecInvalidKeySpecException extends DnssecValidationFailedException {
070
071        /**
072         * 
073         */
074        private static final long serialVersionUID = 1L;
075
076        public DnssecInvalidKeySpecException(InvalidKeySpecException exception) {
077            super("Invalid key spec", exception);
078        }
079
080        public DnssecInvalidKeySpecException(String message, InvalidKeySpecException exception, byte[] data) {
081            super(message, exception);
082        }
083
084    }
085
086    public static class AuthorityDoesNotContainSoa extends DnssecValidationFailedException {
087
088        /**
089         *
090         */
091        private static final long serialVersionUID = 1L;
092
093        private final DnsMessage response;
094
095        public AuthorityDoesNotContainSoa(DnsMessage response) {
096            super("Autority does not contain SOA");
097            this.response = response;
098        }
099
100        public DnsMessage getResponse() {
101            return response;
102        }
103    }
104}