1package com.android.hotspot2.asn1;
2
3import java.util.HashMap;
4import java.util.HashSet;
5import java.util.Map;
6import java.util.Set;
7
8public class OidMappings {
9    public static class SigEntry {
10        private final String mSigAlgo;
11        private final Asn1Oid mKeyAlgo;
12
13        private SigEntry(String sigAlgo, Asn1Oid keyAlgo) {
14            mSigAlgo = sigAlgo;
15            mKeyAlgo = keyAlgo;
16        }
17
18        public String getSigAlgo() {
19            return mSigAlgo;
20        }
21
22        public Asn1Oid getKeyAlgo() {
23            return mKeyAlgo;
24        }
25    }
26
27    public static final String IdPeLogotype = "1.3.6.1.5.5.7.1.12";
28    public static final String IdCeSubjectAltName = "2.5.29.17";
29
30    private static final Map<Asn1Oid, String> sCryptoMapping = new HashMap<>();
31    private static final Map<Asn1Oid, String> sNameMapping = new HashMap<>();
32    private static final Set<Asn1Oid> sIDMapping = new HashSet<>();
33    private static final Map<Asn1Oid, SigEntry> sSigAlgos = new HashMap<>();
34
35    // DSA
36    private static final Asn1Oid sAlgo_DSA = new Asn1Oid(1L, 2L, 840L, 10040L, 4L, 1L);
37    private static final Asn1Oid sAlgo_SHA1withDSA = new Asn1Oid(1L, 2L, 840L, 10040L, 4L, 3L);
38
39    // RSA
40    public static final Asn1Oid sAlgo_RSA = new Asn1Oid(1L, 2L, 840L, 113549L, 1L, 1L, 1L);
41    private static final Asn1Oid sAlgo_MD2withRSA = new Asn1Oid(1L, 2L, 840L, 113549L, 1L, 1L, 2L);
42    private static final Asn1Oid sAlgo_MD5withRSA = new Asn1Oid(1L, 2L, 840L, 113549L, 1L, 1L, 4L);
43    private static final Asn1Oid sAlgo_SHA1withRSA = new Asn1Oid(1L, 2L, 840L, 113549L, 1L, 1L, 5L);
44    private static final Asn1Oid sAlgo_SHA224withRSA =
45            new Asn1Oid(1L, 2L, 840L, 113549L, 1L, 1L, 14L);   // n/a
46    private static final Asn1Oid sAlgo_SHA256withRSA =
47            new Asn1Oid(1L, 2L, 840L, 113549L, 1L, 1L, 11L);
48    private static final Asn1Oid sAlgo_SHA384withRSA =
49            new Asn1Oid(1L, 2L, 840L, 113549L, 1L, 1L, 12L);
50    private static final Asn1Oid sAlgo_SHA512withRSA =
51            new Asn1Oid(1L, 2L, 840L, 113549L, 1L, 1L, 13L);
52
53    // ECC
54    public static final Asn1Oid sAlgo_EC = new Asn1Oid(1L, 2L, 840L, 10045L, 2L, 1L);
55    private static final Asn1Oid sAlgo_SHA1withECDSA = new Asn1Oid(1L, 2L, 840L, 10045L, 4L, 1L);
56    private static final Asn1Oid sAlgo_SHA224withECDSA =
57            new Asn1Oid(1L, 2L, 840L, 10045L, 4L, 3L, 1L);     // n/a
58    private static final Asn1Oid sAlgo_SHA256withECDSA =
59            new Asn1Oid(1L, 2L, 840L, 10045L, 4L, 3L, 2L);
60    private static final Asn1Oid sAlgo_SHA384withECDSA =
61            new Asn1Oid(1L, 2L, 840L, 10045L, 4L, 3L, 3L);
62    private static final Asn1Oid sAlgo_SHA512withECDSA =
63            new Asn1Oid(1L, 2L, 840L, 10045L, 4L, 3L, 4L);
64
65    private static final Asn1Oid sAlgo_MD2 = new Asn1Oid(1L, 2L, 840L, 113549L, 2L, 2L);
66    private static final Asn1Oid sAlgo_MD5 = new Asn1Oid(1L, 2L, 840L, 113549L, 2L, 5L);
67    private static final Asn1Oid sAlgo_SHA1 = new Asn1Oid(1L, 3L, 14L, 3L, 2L, 26L);
68    private static final Asn1Oid sAlgo_SHA256 =
69            new Asn1Oid(2L, 16L, 840L, 1L, 101L, 3L, 4L, 2L, 1L);
70    private static final Asn1Oid sAlgo_SHA384 =
71            new Asn1Oid(2L, 16L, 840L, 1L, 101L, 3L, 4L, 2L, 2L);
72    private static final Asn1Oid sAlgo_SHA512 =
73            new Asn1Oid(2L, 16L, 840L, 1L, 101L, 3L, 4L, 2L, 3L);
74
75    // HS2.0 stuff:
76    public static final Asn1Oid sPkcs9AtChallengePassword =
77            new Asn1Oid(1L, 2L, 840L, 113549L, 1L, 9L, 7L);
78    public static final Asn1Oid sExtensionRequest = new Asn1Oid(1L, 2L, 840L, 113549L, 1L, 9L, 14L);
79
80    public static final Asn1Oid sMAC = new Asn1Oid(1L, 3L, 6L, 1L, 1L, 1L, 1L, 22L);
81    public static final Asn1Oid sIMEI = new Asn1Oid(1L, 3L, 6L, 1L, 4L, 1L, 40808L, 1L, 1L, 3L);
82    public static final Asn1Oid sMEID = new Asn1Oid(1L, 3L, 6L, 1L, 4L, 1L, 40808L, 1L, 1L, 4L);
83    public static final Asn1Oid sDevID = new Asn1Oid(1L, 3L, 6L, 1L, 4L, 1L, 40808L, 1L, 1L, 5L);
84
85    public static final Asn1Oid sIdWfaHotspotFriendlyName =
86            new Asn1Oid(1L, 3L, 6L, 1L, 4L, 1L, 40808L, 1L, 1L, 1L);
87
88    static {
89        sCryptoMapping.put(sAlgo_DSA, "DSA");
90        sCryptoMapping.put(sAlgo_RSA, "RSA");
91        sCryptoMapping.put(sAlgo_EC, "EC");
92
93        sSigAlgos.put(sAlgo_SHA1withDSA, new SigEntry("SHA1withDSA", sAlgo_DSA));
94
95        sSigAlgos.put(sAlgo_MD2withRSA, new SigEntry("MD2withRSA", sAlgo_RSA));
96        sSigAlgos.put(sAlgo_MD5withRSA, new SigEntry("MD5withRSA", sAlgo_RSA));
97        sSigAlgos.put(sAlgo_SHA1withRSA, new SigEntry("SHA1withRSA", sAlgo_RSA));
98        sSigAlgos.put(sAlgo_SHA224withRSA, new SigEntry(null, sAlgo_RSA));
99        sSigAlgos.put(sAlgo_SHA256withRSA, new SigEntry("SHA256withRSA", sAlgo_RSA));
100        sSigAlgos.put(sAlgo_SHA384withRSA, new SigEntry("SHA384withRSA", sAlgo_RSA));
101        sSigAlgos.put(sAlgo_SHA512withRSA, new SigEntry("SHA512withRSA", sAlgo_RSA));
102
103        sSigAlgos.put(sAlgo_SHA1withECDSA, new SigEntry("SHA1withECDSA", sAlgo_EC));
104        sSigAlgos.put(sAlgo_SHA224withECDSA, new SigEntry(null, sAlgo_EC));
105        sSigAlgos.put(sAlgo_SHA256withECDSA, new SigEntry("SHA256withECDSA", sAlgo_EC));
106        sSigAlgos.put(sAlgo_SHA384withECDSA, new SigEntry("SHA384withECDSA", sAlgo_EC));
107        sSigAlgos.put(sAlgo_SHA512withECDSA, new SigEntry("SHA512withECDSA", sAlgo_EC));
108
109        sIDMapping.add(sMAC);
110        sIDMapping.add(sIMEI);
111        sIDMapping.add(sMEID);
112        sIDMapping.add(sDevID);
113
114        for (Map.Entry<Asn1Oid, String> entry : sCryptoMapping.entrySet()) {
115            sNameMapping.put(entry.getKey(), entry.getValue());
116        }
117        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 1L), "sect163k1");
118        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 2L), "sect163r1");
119        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 3L), "sect239k1");
120        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 4L), "sect113r1");
121        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 5L), "sect113r2");
122        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 6L), "secp112r1");
123        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 7L), "secp112r2");
124        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 8L), "secp160r1");
125        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 9L), "secp160k1");
126        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 10L), "secp256k1");
127        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 15L), "sect163r2");
128        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 16L), "sect283k1");
129        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 17L), "sect283r1");
130        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 22L), "sect131r1");
131        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 23L), "sect131r2");
132        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 24L), "sect193r1");
133        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 25L), "sect193r2");
134        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 26L), "sect233k1");
135        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 27L), "sect233r1");
136        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 28L), "secp128r1");
137        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 29L), "secp128r2");
138        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 30L), "secp160r2");
139        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 31L), "secp192k1");
140        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 32L), "secp224k1");
141        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 33L), "secp224r1");
142        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 34L), "secp384r1");
143        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 35L), "secp521r1");
144        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 36L), "sect409k1");
145        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 37L), "sect409r1");
146        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 38L), "sect571k1");
147        sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 39L), "sect571r1");
148        sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 1L), "secp192r1");
149        sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 7L), "secp256r1");
150        sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 2L), "prime192v2");    // X9.62
151        sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 3L), "prime192v3");    // X9.62
152        sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 4L), "prime239v1");    // X9.62
153        sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 5L), "prime239v2");    // X9.62
154        sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 6L), "prime239v3");    // X9.62
155        sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 5L), "c2tnb191v1");    // X9.62
156        sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 6L), "c2tnb191v2");    // X9.62
157        sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 7L), "c2tnb191v3");    // X9.62
158        sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 11L), "c2tnb239v1");   // X9.62
159        sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 12L), "c2tnb239v2");   // X9.62
160        sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 13L), "c2tnb239v3");   // X9.62
161        sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 18L), "c2tnb359v1");   // X9.62
162        sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 20L), "c2tnb431r1");   // X9.62
163
164        sNameMapping.put(sAlgo_MD2, "MD2");
165        sNameMapping.put(sAlgo_MD5, "MD5");
166        sNameMapping.put(sAlgo_SHA1, "SHA-1");
167        sNameMapping.put(sAlgo_SHA256, "SHA-256");
168        sNameMapping.put(sAlgo_SHA384, "SHA-384");
169        sNameMapping.put(sAlgo_SHA512, "SHA-512");
170    }
171
172    public static SigEntry getSigEntry(Asn1Oid oid) {
173        return sSigAlgos.get(oid);
174    }
175
176    public static String getCryptoID(Asn1Oid oid) {
177        return sCryptoMapping.get(oid);
178    }
179
180    public static String getJCEName(Asn1Oid oid) {
181        return sNameMapping.get(oid);
182    }
183
184    public static String getSigAlgoName(Asn1Oid oid) {
185        SigEntry sigEntry = sSigAlgos.get(oid);
186        return sigEntry != null ? sigEntry.getSigAlgo() : null;
187    }
188
189    public static String getKeyAlgoName(Asn1Oid oid) {
190        SigEntry sigEntry = sSigAlgos.get(oid);
191        return sigEntry != null ? sNameMapping.get(sigEntry.getKeyAlgo()) : null;
192    }
193
194    public static boolean isIDAttribute(Asn1Oid oid) {
195        return sIDMapping.contains(oid);
196    }
197}
198