001/*
002 * Copyright 2015-2024 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.constants;
012
013import java.util.HashMap;
014import java.util.Map;
015
016public final class DnssecConstants {
017    /**
018     * Do not allow to instantiate DNSSECConstants
019     */
020    private DnssecConstants() {
021    }
022
023    private static final Map<Byte, SignatureAlgorithm> SIGNATURE_ALGORITHM_LUT = new HashMap<>();
024
025    /**
026     * DNSSEC Signature Algorithms.
027     * 
028     * @see <a href=
029     *      "http://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xhtml">
030     *      IANA DNSSEC Algorithm Numbers</a>
031     */
032    public enum SignatureAlgorithm {
033        @Deprecated
034        RSAMD5(1, "RSA/MD5"),
035        DH(2, "Diffie-Hellman"),
036        DSA(3, "DSA/SHA1"),
037        RSASHA1(5, "RSA/SHA-1"),
038        DSA_NSEC3_SHA1(6, "DSA_NSEC3-SHA1"),
039        RSASHA1_NSEC3_SHA1(7, "RSASHA1-NSEC3-SHA1"),
040        RSASHA256(8, "RSA/SHA-256"),
041        RSASHA512(10, "RSA/SHA-512"),
042        ECC_GOST(12, "GOST R 34.10-2001"),
043        ECDSAP256SHA256(13, "ECDSA Curve P-256 with SHA-256"),
044        ECDSAP384SHA384(14, "ECDSA Curve P-384 with SHA-384"),
045        INDIRECT(252, "Reserved for Indirect Keys"),
046        PRIVATEDNS(253, "private algorithm"),
047        PRIVATEOID(254, "private algorithm oid"),
048        ;
049
050        SignatureAlgorithm(int number, String description) {
051            if (number < 0 || number > 255) {
052                throw new IllegalArgumentException();
053            }
054            this.number = (byte) number;
055            this.description = description;
056            SIGNATURE_ALGORITHM_LUT.put(this.number, this);
057        }
058
059        public final byte number;
060        public final String description;
061
062        public static SignatureAlgorithm forByte(byte b) {
063            return SIGNATURE_ALGORITHM_LUT.get(b);
064        }
065    }
066
067    private static final Map<Byte, DigestAlgorithm> DELEGATION_DIGEST_LUT = new HashMap<>();
068
069    /**
070     * DNSSEC Digest Algorithms.
071     * 
072     * @see <a href=
073     *      "https://www.iana.org/assignments/ds-rr-types/ds-rr-types.xhtml">
074     *      IANA Delegation Signer (DS) Resource Record (RR)</a>
075     */
076    public enum DigestAlgorithm {
077        SHA1(1, "SHA-1"),
078        SHA256(2, "SHA-256"),
079        GOST(3, "GOST R 34.11-94"),
080        SHA384(4, "SHA-384"),
081        ;
082
083        DigestAlgorithm(int value, String description) {
084            if (value < 0 || value > 255) {
085                throw new IllegalArgumentException();
086            }
087            this.value = (byte) value;
088            this.description = description;
089            DELEGATION_DIGEST_LUT.put(this.value, this);
090        }
091
092        public final byte value;
093        public final String description;
094
095        public static DigestAlgorithm forByte(byte b) {
096            return DELEGATION_DIGEST_LUT.get(b);
097        }
098    }
099}