1 package com.android.hotspot2.asn1;
2 
3 import java.util.HashMap;
4 import java.util.HashSet;
5 import java.util.Map;
6 import java.util.Set;
7 
8 public class OidMappings {
9     public static class SigEntry {
10         private final String mSigAlgo;
11         private final Asn1Oid mKeyAlgo;
12 
SigEntry(String sigAlgo, Asn1Oid keyAlgo)13         private SigEntry(String sigAlgo, Asn1Oid keyAlgo) {
14             mSigAlgo = sigAlgo;
15             mKeyAlgo = keyAlgo;
16         }
17 
getSigAlgo()18         public String getSigAlgo() {
19             return mSigAlgo;
20         }
21 
getKeyAlgo()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 {
sCryptoMapping.put(sAlgo_DSA, "DSA")89         sCryptoMapping.put(sAlgo_DSA, "DSA");
sCryptoMapping.put(sAlgo_RSA, "RSA")90         sCryptoMapping.put(sAlgo_RSA, "RSA");
sCryptoMapping.put(sAlgo_EC, "EC")91         sCryptoMapping.put(sAlgo_EC, "EC");
92 
sSigAlgos.put(sAlgo_SHA1withDSA, new SigEntry("SHA1withDSA", sAlgo_DSA))93         sSigAlgos.put(sAlgo_SHA1withDSA, new SigEntry("SHA1withDSA", sAlgo_DSA));
94 
sSigAlgos.put(sAlgo_MD2withRSA, new SigEntry("MD2withRSA", sAlgo_RSA))95         sSigAlgos.put(sAlgo_MD2withRSA, new SigEntry("MD2withRSA", sAlgo_RSA));
sSigAlgos.put(sAlgo_MD5withRSA, new SigEntry("MD5withRSA", sAlgo_RSA))96         sSigAlgos.put(sAlgo_MD5withRSA, new SigEntry("MD5withRSA", sAlgo_RSA));
sSigAlgos.put(sAlgo_SHA1withRSA, new SigEntry("SHA1withRSA", sAlgo_RSA))97         sSigAlgos.put(sAlgo_SHA1withRSA, new SigEntry("SHA1withRSA", sAlgo_RSA));
sSigAlgos.put(sAlgo_SHA224withRSA, new SigEntry(null, sAlgo_RSA))98         sSigAlgos.put(sAlgo_SHA224withRSA, new SigEntry(null, sAlgo_RSA));
sSigAlgos.put(sAlgo_SHA256withRSA, new SigEntry("SHA256withRSA", sAlgo_RSA))99         sSigAlgos.put(sAlgo_SHA256withRSA, new SigEntry("SHA256withRSA", sAlgo_RSA));
sSigAlgos.put(sAlgo_SHA384withRSA, new SigEntry("SHA384withRSA", sAlgo_RSA))100         sSigAlgos.put(sAlgo_SHA384withRSA, new SigEntry("SHA384withRSA", sAlgo_RSA));
sSigAlgos.put(sAlgo_SHA512withRSA, new SigEntry("SHA512withRSA", sAlgo_RSA))101         sSigAlgos.put(sAlgo_SHA512withRSA, new SigEntry("SHA512withRSA", sAlgo_RSA));
102 
sSigAlgos.put(sAlgo_SHA1withECDSA, new SigEntry("SHA1withECDSA", sAlgo_EC))103         sSigAlgos.put(sAlgo_SHA1withECDSA, new SigEntry("SHA1withECDSA", sAlgo_EC));
sSigAlgos.put(sAlgo_SHA224withECDSA, new SigEntry(null, sAlgo_EC))104         sSigAlgos.put(sAlgo_SHA224withECDSA, new SigEntry(null, sAlgo_EC));
sSigAlgos.put(sAlgo_SHA256withECDSA, new SigEntry("SHA256withECDSA", sAlgo_EC))105         sSigAlgos.put(sAlgo_SHA256withECDSA, new SigEntry("SHA256withECDSA", sAlgo_EC));
sSigAlgos.put(sAlgo_SHA384withECDSA, new SigEntry("SHA384withECDSA", sAlgo_EC))106         sSigAlgos.put(sAlgo_SHA384withECDSA, new SigEntry("SHA384withECDSA", sAlgo_EC));
sSigAlgos.put(sAlgo_SHA512withECDSA, new SigEntry("SHA512withECDSA", 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()) {
entry.getKey()115             sNameMapping.put(entry.getKey(), entry.getValue());
116         }
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 1L), "sect163k1")117         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 1L), "sect163k1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 2L), "sect163r1")118         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 2L), "sect163r1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 3L), "sect239k1")119         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 3L), "sect239k1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 4L), "sect113r1")120         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 4L), "sect113r1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 5L), "sect113r2")121         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 5L), "sect113r2");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 6L), "secp112r1")122         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 6L), "secp112r1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 7L), "secp112r2")123         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 7L), "secp112r2");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 8L), "secp160r1")124         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 8L), "secp160r1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 9L), "secp160k1")125         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 9L), "secp160k1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 10L), "secp256k1")126         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 10L), "secp256k1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 15L), "sect163r2")127         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 15L), "sect163r2");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 16L), "sect283k1")128         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 16L), "sect283k1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 17L), "sect283r1")129         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 17L), "sect283r1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 22L), "sect131r1")130         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 22L), "sect131r1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 23L), "sect131r2")131         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 23L), "sect131r2");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 24L), "sect193r1")132         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 24L), "sect193r1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 25L), "sect193r2")133         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 25L), "sect193r2");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 26L), "sect233k1")134         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 26L), "sect233k1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 27L), "sect233r1")135         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 27L), "sect233r1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 28L), "secp128r1")136         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 28L), "secp128r1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 29L), "secp128r2")137         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 29L), "secp128r2");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 30L), "secp160r2")138         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 30L), "secp160r2");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 31L), "secp192k1")139         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 31L), "secp192k1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 32L), "secp224k1")140         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 32L), "secp224k1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 33L), "secp224r1")141         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 33L), "secp224r1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 34L), "secp384r1")142         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 34L), "secp384r1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 35L), "secp521r1")143         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 35L), "secp521r1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 36L), "sect409k1")144         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 36L), "sect409k1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 37L), "sect409r1")145         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 37L), "sect409r1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 38L), "sect571k1")146         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 38L), "sect571k1");
sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 39L), "sect571r1")147         sNameMapping.put(new Asn1Oid(1L, 3L, 132L, 0L, 39L), "sect571r1");
sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 1L), "secp192r1")148         sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 1L), "secp192r1");
sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 7L), "secp256r1")149         sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 7L), "secp256r1");
sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 2L), "prime192v2")150         sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 2L), "prime192v2");    // X9.62
sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 3L), "prime192v3")151         sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 3L), "prime192v3");    // X9.62
sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 4L), "prime239v1")152         sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 4L), "prime239v1");    // X9.62
sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 5L), "prime239v2")153         sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 5L), "prime239v2");    // X9.62
sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 6L), "prime239v3")154         sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 1L, 6L), "prime239v3");    // X9.62
sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 5L), "c2tnb191v1")155         sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 5L), "c2tnb191v1");    // X9.62
sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 6L), "c2tnb191v2")156         sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 6L), "c2tnb191v2");    // X9.62
sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 7L), "c2tnb191v3")157         sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 7L), "c2tnb191v3");    // X9.62
sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 11L), "c2tnb239v1")158         sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 11L), "c2tnb239v1");   // X9.62
sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 12L), "c2tnb239v2")159         sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 12L), "c2tnb239v2");   // X9.62
sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 13L), "c2tnb239v3")160         sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 13L), "c2tnb239v3");   // X9.62
sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 18L), "c2tnb359v1")161         sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 18L), "c2tnb359v1");   // X9.62
sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 20L), "c2tnb431r1")162         sNameMapping.put(new Asn1Oid(1L, 2L, 840L, 10045L, 3L, 0L, 20L), "c2tnb431r1");   // X9.62
163 
sNameMapping.put(sAlgo_MD2, "MD2")164         sNameMapping.put(sAlgo_MD2, "MD2");
sNameMapping.put(sAlgo_MD5, "MD5")165         sNameMapping.put(sAlgo_MD5, "MD5");
sNameMapping.put(sAlgo_SHA1, "SHA-1")166         sNameMapping.put(sAlgo_SHA1, "SHA-1");
sNameMapping.put(sAlgo_SHA256, "SHA-256")167         sNameMapping.put(sAlgo_SHA256, "SHA-256");
sNameMapping.put(sAlgo_SHA384, "SHA-384")168         sNameMapping.put(sAlgo_SHA384, "SHA-384");
sNameMapping.put(sAlgo_SHA512, "SHA-512")169         sNameMapping.put(sAlgo_SHA512, "SHA-512");
170     }
171 
getSigEntry(Asn1Oid oid)172     public static SigEntry getSigEntry(Asn1Oid oid) {
173         return sSigAlgos.get(oid);
174     }
175 
getCryptoID(Asn1Oid oid)176     public static String getCryptoID(Asn1Oid oid) {
177         return sCryptoMapping.get(oid);
178     }
179 
getJCEName(Asn1Oid oid)180     public static String getJCEName(Asn1Oid oid) {
181         return sNameMapping.get(oid);
182     }
183 
getSigAlgoName(Asn1Oid oid)184     public static String getSigAlgoName(Asn1Oid oid) {
185         SigEntry sigEntry = sSigAlgos.get(oid);
186         return sigEntry != null ? sigEntry.getSigAlgo() : null;
187     }
188 
getKeyAlgoName(Asn1Oid oid)189     public static String getKeyAlgoName(Asn1Oid oid) {
190         SigEntry sigEntry = sSigAlgos.get(oid);
191         return sigEntry != null ? sNameMapping.get(sigEntry.getKeyAlgo()) : null;
192     }
193 
isIDAttribute(Asn1Oid oid)194     public static boolean isIDAttribute(Asn1Oid oid) {
195         return sIDMapping.contains(oid);
196     }
197 }
198