1# 2# This file is part of pyasn1-modules. 3# 4# Copyright (c) 2017, Danielle Madeley <danielle@madeley.id.au> 5# License: http://snmplabs.com/pyasn1/license.html 6# 7# Modified by Russ Housley to add maps for use with opentypes. 8# 9# Algorithms and Identifiers for Internet X.509 Certificates and CRLs 10# 11# Derived from RFC 3279: 12# https://www.rfc-editor.org/rfc/rfc3279.txt 13# 14from pyasn1.type import namedtype 15from pyasn1.type import namedval 16from pyasn1.type import univ 17 18from pyasn1_modules import rfc5280 19 20 21def _OID(*components): 22 output = [] 23 for x in tuple(components): 24 if isinstance(x, univ.ObjectIdentifier): 25 output.extend(list(x)) 26 else: 27 output.append(int(x)) 28 29 return univ.ObjectIdentifier(output) 30 31 32md2 = _OID(1, 2, 840, 113549, 2, 2) 33md5 = _OID(1, 2, 840, 113549, 2, 5) 34id_sha1 = _OID(1, 3, 14, 3, 2, 26) 35id_dsa = _OID(1, 2, 840, 10040, 4, 1) 36 37 38class DSAPublicKey(univ.Integer): 39 pass 40 41 42class Dss_Parms(univ.Sequence): 43 componentType = namedtype.NamedTypes( 44 namedtype.NamedType('p', univ.Integer()), 45 namedtype.NamedType('q', univ.Integer()), 46 namedtype.NamedType('g', univ.Integer()) 47 ) 48 49 50id_dsa_with_sha1 = _OID(1, 2, 840, 10040, 4, 3) 51 52 53class Dss_Sig_Value(univ.Sequence): 54 componentType = namedtype.NamedTypes( 55 namedtype.NamedType('r', univ.Integer()), 56 namedtype.NamedType('s', univ.Integer()) 57 ) 58 59 60pkcs_1 = _OID(1, 2, 840, 113549, 1, 1) 61rsaEncryption = _OID(pkcs_1, 1) 62md2WithRSAEncryption = _OID(pkcs_1, 2) 63md5WithRSAEncryption = _OID(pkcs_1, 4) 64sha1WithRSAEncryption = _OID(pkcs_1, 5) 65 66 67class RSAPublicKey(univ.Sequence): 68 componentType = namedtype.NamedTypes( 69 namedtype.NamedType('modulus', univ.Integer()), 70 namedtype.NamedType('publicExponent', univ.Integer()) 71 ) 72 73 74dhpublicnumber = _OID(1, 2, 840, 10046, 2, 1) 75 76 77class DHPublicKey(univ.Integer): 78 pass 79 80 81class ValidationParms(univ.Sequence): 82 componentType = namedtype.NamedTypes( 83 namedtype.NamedType('seed', univ.BitString()), 84 namedtype.NamedType('pgenCounter', univ.Integer()) 85 ) 86 87 88class DomainParameters(univ.Sequence): 89 componentType = namedtype.NamedTypes( 90 namedtype.NamedType('p', univ.Integer()), 91 namedtype.NamedType('g', univ.Integer()), 92 namedtype.NamedType('q', univ.Integer()), 93 namedtype.OptionalNamedType('j', univ.Integer()), 94 namedtype.OptionalNamedType('validationParms', ValidationParms()) 95 ) 96 97 98id_keyExchangeAlgorithm = _OID(2, 16, 840, 1, 101, 2, 1, 1, 22) 99 100 101class KEA_Parms_Id(univ.OctetString): 102 pass 103 104 105ansi_X9_62 = _OID(1, 2, 840, 10045) 106 107 108class FieldID(univ.Sequence): 109 componentType = namedtype.NamedTypes( 110 namedtype.NamedType('fieldType', univ.ObjectIdentifier()), 111 namedtype.NamedType('parameters', univ.Any()) 112 ) 113 114 115id_ecSigType = _OID(ansi_X9_62, 4) 116ecdsa_with_SHA1 = _OID(id_ecSigType, 1) 117 118 119class ECDSA_Sig_Value(univ.Sequence): 120 componentType = namedtype.NamedTypes( 121 namedtype.NamedType('r', univ.Integer()), 122 namedtype.NamedType('s', univ.Integer()) 123 ) 124 125 126id_fieldType = _OID(ansi_X9_62, 1) 127prime_field = _OID(id_fieldType, 1) 128 129 130class Prime_p(univ.Integer): 131 pass 132 133 134characteristic_two_field = _OID(id_fieldType, 2) 135 136 137class Characteristic_two(univ.Sequence): 138 componentType = namedtype.NamedTypes( 139 namedtype.NamedType('m', univ.Integer()), 140 namedtype.NamedType('basis', univ.ObjectIdentifier()), 141 namedtype.NamedType('parameters', univ.Any()) 142 ) 143 144 145id_characteristic_two_basis = _OID(characteristic_two_field, 3) 146gnBasis = _OID(id_characteristic_two_basis, 1) 147tpBasis = _OID(id_characteristic_two_basis, 2) 148 149 150class Trinomial(univ.Integer): 151 pass 152 153 154ppBasis = _OID(id_characteristic_two_basis, 3) 155 156 157class Pentanomial(univ.Sequence): 158 componentType = namedtype.NamedTypes( 159 namedtype.NamedType('k1', univ.Integer()), 160 namedtype.NamedType('k2', univ.Integer()), 161 namedtype.NamedType('k3', univ.Integer()) 162 ) 163 164 165class FieldElement(univ.OctetString): 166 pass 167 168 169class ECPoint(univ.OctetString): 170 pass 171 172 173class Curve(univ.Sequence): 174 componentType = namedtype.NamedTypes( 175 namedtype.NamedType('a', FieldElement()), 176 namedtype.NamedType('b', FieldElement()), 177 namedtype.OptionalNamedType('seed', univ.BitString()) 178 ) 179 180 181class ECPVer(univ.Integer): 182 namedValues = namedval.NamedValues( 183 ('ecpVer1', 1) 184 ) 185 186 187class ECParameters(univ.Sequence): 188 componentType = namedtype.NamedTypes( 189 namedtype.NamedType('version', ECPVer()), 190 namedtype.NamedType('fieldID', FieldID()), 191 namedtype.NamedType('curve', Curve()), 192 namedtype.NamedType('base', ECPoint()), 193 namedtype.NamedType('order', univ.Integer()), 194 namedtype.OptionalNamedType('cofactor', univ.Integer()) 195 ) 196 197 198class EcpkParameters(univ.Choice): 199 componentType = namedtype.NamedTypes( 200 namedtype.NamedType('ecParameters', ECParameters()), 201 namedtype.NamedType('namedCurve', univ.ObjectIdentifier()), 202 namedtype.NamedType('implicitlyCA', univ.Null()) 203 ) 204 205 206id_publicKeyType = _OID(ansi_X9_62, 2) 207id_ecPublicKey = _OID(id_publicKeyType, 1) 208 209ellipticCurve = _OID(ansi_X9_62, 3) 210 211c_TwoCurve = _OID(ellipticCurve, 0) 212c2pnb163v1 = _OID(c_TwoCurve, 1) 213c2pnb163v2 = _OID(c_TwoCurve, 2) 214c2pnb163v3 = _OID(c_TwoCurve, 3) 215c2pnb176w1 = _OID(c_TwoCurve, 4) 216c2tnb191v1 = _OID(c_TwoCurve, 5) 217c2tnb191v2 = _OID(c_TwoCurve, 6) 218c2tnb191v3 = _OID(c_TwoCurve, 7) 219c2onb191v4 = _OID(c_TwoCurve, 8) 220c2onb191v5 = _OID(c_TwoCurve, 9) 221c2pnb208w1 = _OID(c_TwoCurve, 10) 222c2tnb239v1 = _OID(c_TwoCurve, 11) 223c2tnb239v2 = _OID(c_TwoCurve, 12) 224c2tnb239v3 = _OID(c_TwoCurve, 13) 225c2onb239v4 = _OID(c_TwoCurve, 14) 226c2onb239v5 = _OID(c_TwoCurve, 15) 227c2pnb272w1 = _OID(c_TwoCurve, 16) 228c2pnb304w1 = _OID(c_TwoCurve, 17) 229c2tnb359v1 = _OID(c_TwoCurve, 18) 230c2pnb368w1 = _OID(c_TwoCurve, 19) 231c2tnb431r1 = _OID(c_TwoCurve, 20) 232 233primeCurve = _OID(ellipticCurve, 1) 234prime192v1 = _OID(primeCurve, 1) 235prime192v2 = _OID(primeCurve, 2) 236prime192v3 = _OID(primeCurve, 3) 237prime239v1 = _OID(primeCurve, 4) 238prime239v2 = _OID(primeCurve, 5) 239prime239v3 = _OID(primeCurve, 6) 240prime256v1 = _OID(primeCurve, 7) 241 242 243# Map of Algorithm Identifier OIDs to Parameters added to the 244# ones in rfc5280.py. Do not add OIDs with absent paramaters. 245 246_algorithmIdentifierMapUpdate = { 247 md2: univ.Null(""), 248 md5: univ.Null(""), 249 id_sha1: univ.Null(""), 250 id_dsa: Dss_Parms(), 251 rsaEncryption: univ.Null(""), 252 md2WithRSAEncryption: univ.Null(""), 253 md5WithRSAEncryption: univ.Null(""), 254 sha1WithRSAEncryption: univ.Null(""), 255 dhpublicnumber: DomainParameters(), 256 id_keyExchangeAlgorithm: KEA_Parms_Id(), 257 id_ecPublicKey: EcpkParameters(), 258} 259 260rfc5280.algorithmIdentifierMap.update(_algorithmIdentifierMapUpdate) 261