aboutsummaryrefslogtreecommitdiffstats
path: root/security-utils/src/main/java/com/yahoo/security/SubjectAlternativeName.java
blob: c93dd59ea9804c82e604955924d8854707a0cfc9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.security;

import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1IA5String;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x509.GeneralName;
import org.bouncycastle.asn1.x509.GeneralNames;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;


/**
 * @author bjorncs
 */
public class SubjectAlternativeName {

    private final Type type;
    private final String value;

    public SubjectAlternativeName(Type type, String value) {
        this.type = type;
        this.value = value;
    }

    SubjectAlternativeName(GeneralName bcGeneralName) {
        this.type = Type.fromTag(bcGeneralName.getTagNo());
        this.value = getValue(bcGeneralName);
    }

    public Type getType() {
        return type;
    }

    public String getValue() {
        return value;
    }

    GeneralName toGeneralName() {
        return new GeneralName(type.tag, value);
    }

    public SubjectAlternativeName decode() {
        return new SubjectAlternativeName(new GeneralName(type.tag, value));
    }

    static List<SubjectAlternativeName> fromGeneralNames(GeneralNames generalNames) {
        return Arrays.stream(generalNames.getNames()).map(SubjectAlternativeName::new).toList();
    }

    private String getValue(GeneralName bcGeneralName) {
        ASN1Encodable name = bcGeneralName.getName();
        switch (bcGeneralName.getTagNo()) {
            case GeneralName.rfc822Name:
            case GeneralName.dNSName:
            case GeneralName.uniformResourceIdentifier:
                return ASN1IA5String.getInstance(name).getString();
            case GeneralName.directoryName:
                return X500Name.getInstance(name).toString();
            case GeneralName.iPAddress:
                byte[] octets = DEROctetString.getInstance(name.toASN1Primitive()).getOctets();
                try {
                    return InetAddress.getByAddress(octets).getHostAddress();
                } catch (UnknownHostException e) {
                    // Only thrown if IP address is of invalid length, which is an illegal argument
                    throw new IllegalArgumentException(e);
                }
            default:
                return name.toString();
        }
    }

    @Override
    public String toString() {
        return "SubjectAlternativeName{" +
                "type=" + type +
                ", value='" + value + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        SubjectAlternativeName that = (SubjectAlternativeName) o;
        return type == that.type &&
                Objects.equals(value, that.value);
    }

    @Override
    public int hashCode() {
        return Objects.hash(type, value);
    }

    public enum Type {
        OTHER(0),
        EMAIL(1),
        DNS(2),
        X400(3),
        DIRECTORY(4),
        EDI_PARITY(5),
        URI(6),
        IP(7),
        REGISTERED(8);

        final int tag;

        Type(int tag) {
            this.tag = tag;
        }

        public static Type fromTag(int tag) {
            return Arrays.stream(Type.values())
                    .filter(type -> type.tag == tag)
                    .findAny()
                    .orElseThrow(() -> new IllegalArgumentException("Invalid tag: " + tag));
        }

        public int getTag() {
            return tag;
        }
    }
}