| 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package x509 implements a subset of the X.509 standard. |
| 6 | // |
| 7 | // It allows parsing and generating certificates, certificate signing |
| 8 | // requests, certificate revocation lists, and encoded public and private keys. |
| 9 | // It provides a certificate verifier, complete with a chain builder. |
| 10 | // |
| 11 | // The package targets the X.509 technical profile defined by the IETF (RFC |
| 12 | // 2459/3280/5280), and as further restricted by the CA/Browser Forum Baseline |
| 13 | // Requirements. There is minimal support for features outside of these |
| 14 | // profiles, as the primary goal of the package is to provide compatibility |
| 15 | // with the publicly trusted TLS certificate ecosystem and its policies and |
| 16 | // constraints. |
| 17 | // |
| 18 | // On macOS and Windows, certificate verification is handled by system APIs, but |
| 19 | // the package aims to apply consistent validation rules across operating |
| 20 | // systems. |
| 21 | package legacyx509 |
| 22 | |
| 23 | import ( |
| 24 | "bytes" |
| 25 | "crypto" |
| 26 | "crypto/elliptic" |
| 27 | stdx509 "crypto/x509" |
| 28 | "crypto/x509/pkix" |
| 29 | "encoding/asn1" |
| 30 | "fmt" |
| 31 | "strconv" |
| 32 | "unicode" |
| 33 | |
| 34 | // Explicitly import these for their crypto.RegisterHash init side-effects. |
| 35 | // Keep these as blank imports, even if they're imported above. |
| 36 | _ "crypto/sha1" |
| 37 | _ "crypto/sha256" |
| 38 | _ "crypto/sha512" |
| 39 | ) |
| 40 | |
| 41 | type publicKeyInfo struct { |
| 42 | Raw asn1.RawContent |
| 43 | Algorithm pkix.AlgorithmIdentifier |
| 44 | PublicKey asn1.BitString |
| 45 | } |
| 46 | |
| 47 | type SignatureAlgorithm int |
| 48 | |
| 49 | const ( |
| 50 | UnknownSignatureAlgorithm SignatureAlgorithm = iota |
| 51 | |
| 52 | MD2WithRSA // Unsupported. |
| 53 | MD5WithRSA // Only supported for signing, not verification. |
| 54 | SHA1WithRSA // Only supported for signing, and verification of CRLs, CSRs, and OCSP responses. |
| 55 | SHA256WithRSA |
| 56 | SHA384WithRSA |
| 57 | SHA512WithRSA |
| 58 | DSAWithSHA1 // Unsupported. |
| 59 | DSAWithSHA256 // Unsupported. |
| 60 | ECDSAWithSHA1 // Only supported for signing, and verification of CRLs, CSRs, and OCSP responses. |
| 61 | ECDSAWithSHA256 |
| 62 | ECDSAWithSHA384 |
| 63 | ECDSAWithSHA512 |
| 64 | SHA256WithRSAPSS |
| 65 | SHA384WithRSAPSS |
| 66 | SHA512WithRSAPSS |
| 67 | PureEd25519 |
| 68 | ) |
| 69 | |
| 70 | func (algo SignatureAlgorithm) String() string { |
| 71 | for _, details := range signatureAlgorithmDetails { |
| 72 | if details.algo == algo { |
| 73 | return details.name |
| 74 | } |
| 75 | } |
| 76 | return strconv.Itoa(int(algo)) |
| 77 | } |
| 78 | |
| 79 | type PublicKeyAlgorithm int |
| 80 | |
| 81 | const ( |
| 82 | UnknownPublicKeyAlgorithm PublicKeyAlgorithm = iota |
| 83 | RSA |
| 84 | DSA // Only supported for parsing. |
| 85 | ECDSA |
| 86 | Ed25519 |
| 87 | ) |
| 88 | |
| 89 | var publicKeyAlgoName = [...]string{ |
| 90 | RSA: "RSA", |
| 91 | DSA: "DSA", |
| 92 | ECDSA: "ECDSA", |
| 93 | Ed25519: "Ed25519", |
| 94 | } |
| 95 | |
| 96 | func (algo PublicKeyAlgorithm) String() string { |
| 97 | if 0 < algo && int(algo) < len(publicKeyAlgoName) { |
| 98 | return publicKeyAlgoName[algo] |
| 99 | } |
| 100 | return strconv.Itoa(int(algo)) |
| 101 | } |
| 102 | |
| 103 | // OIDs for signature algorithms |
| 104 | // |
| 105 | // pkcs-1 OBJECT IDENTIFIER ::= { |
| 106 | // iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) 1 } |
| 107 | // |
| 108 | // RFC 3279 2.2.1 RSA Signature Algorithms |
| 109 | // |
| 110 | // md5WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 4 } |
| 111 | // |
| 112 | // sha-1WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 5 } |
| 113 | // |
| 114 | // dsaWithSha1 OBJECT IDENTIFIER ::= { |
| 115 | // iso(1) member-body(2) us(840) x9-57(10040) x9cm(4) 3 } |
| 116 | // |
| 117 | // RFC 3279 2.2.3 ECDSA Signature Algorithm |
| 118 | // |
| 119 | // ecdsa-with-SHA1 OBJECT IDENTIFIER ::= { |
| 120 | // iso(1) member-body(2) us(840) ansi-x962(10045) |
| 121 | // signatures(4) ecdsa-with-SHA1(1)} |
| 122 | // |
| 123 | // RFC 4055 5 PKCS #1 Version 1.5 |
| 124 | // |
| 125 | // sha256WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 11 } |
| 126 | // |
| 127 | // sha384WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 12 } |
| 128 | // |
| 129 | // sha512WithRSAEncryption OBJECT IDENTIFIER ::= { pkcs-1 13 } |
| 130 | // |
| 131 | // RFC 5758 3.1 DSA Signature Algorithms |
| 132 | // |
| 133 | // dsaWithSha256 OBJECT IDENTIFIER ::= { |
| 134 | // joint-iso-ccitt(2) country(16) us(840) organization(1) gov(101) |
| 135 | // csor(3) algorithms(4) id-dsa-with-sha2(3) 2} |
| 136 | // |
| 137 | // RFC 5758 3.2 ECDSA Signature Algorithm |
| 138 | // |
| 139 | // ecdsa-with-SHA256 OBJECT IDENTIFIER ::= { iso(1) member-body(2) |
| 140 | // us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 2 } |
| 141 | // |
| 142 | // ecdsa-with-SHA384 OBJECT IDENTIFIER ::= { iso(1) member-body(2) |
| 143 | // us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 3 } |
| 144 | // |
| 145 | // ecdsa-with-SHA512 OBJECT IDENTIFIER ::= { iso(1) member-body(2) |
| 146 | // us(840) ansi-X9-62(10045) signatures(4) ecdsa-with-SHA2(3) 4 } |
| 147 | // |
| 148 | // RFC 8410 3 Curve25519 and Curve448 Algorithm Identifiers |
| 149 | // |
| 150 | // id-Ed25519 OBJECT IDENTIFIER ::= { 1 3 101 112 } |
| 151 | var ( |
| 152 | oidSignatureMD5WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 4} |
| 153 | oidSignatureSHA1WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 5} |
| 154 | oidSignatureSHA256WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 11} |
| 155 | oidSignatureSHA384WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 12} |
| 156 | oidSignatureSHA512WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 13} |
| 157 | oidSignatureRSAPSS = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 10} |
| 158 | oidSignatureDSAWithSHA1 = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 3} |
| 159 | oidSignatureDSAWithSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 3, 2} |
| 160 | oidSignatureECDSAWithSHA1 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 1} |
| 161 | oidSignatureECDSAWithSHA256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2} |
| 162 | oidSignatureECDSAWithSHA384 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 3} |
| 163 | oidSignatureECDSAWithSHA512 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 4} |
| 164 | oidSignatureEd25519 = asn1.ObjectIdentifier{1, 3, 101, 112} |
| 165 | |
| 166 | oidSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1} |
| 167 | oidSHA384 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 2} |
| 168 | oidSHA512 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 3} |
| 169 | |
| 170 | oidMGF1 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 8} |
| 171 | |
| 172 | // oidISOSignatureSHA1WithRSA means the same as oidSignatureSHA1WithRSA |
| 173 | // but it's specified by ISO. Microsoft's makecert.exe has been known |
| 174 | // to produce certificates with this OID. |
| 175 | oidISOSignatureSHA1WithRSA = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 29} |
| 176 | ) |
| 177 | |
| 178 | var signatureAlgorithmDetails = []struct { |
| 179 | algo SignatureAlgorithm |
| 180 | name string |
| 181 | oid asn1.ObjectIdentifier |
| 182 | params asn1.RawValue |
| 183 | pubKeyAlgo PublicKeyAlgorithm |
| 184 | hash crypto.Hash |
| 185 | isRSAPSS bool |
| 186 | }{ |
| 187 | {MD5WithRSA, "MD5-RSA", oidSignatureMD5WithRSA, asn1.NullRawValue, RSA, crypto.MD5, false}, |
| 188 | {SHA1WithRSA, "SHA1-RSA", oidSignatureSHA1WithRSA, asn1.NullRawValue, RSA, crypto.SHA1, false}, |
| 189 | {SHA1WithRSA, "SHA1-RSA", oidISOSignatureSHA1WithRSA, asn1.NullRawValue, RSA, crypto.SHA1, false}, |
| 190 | {SHA256WithRSA, "SHA256-RSA", oidSignatureSHA256WithRSA, asn1.NullRawValue, RSA, crypto.SHA256, false}, |
| 191 | {SHA384WithRSA, "SHA384-RSA", oidSignatureSHA384WithRSA, asn1.NullRawValue, RSA, crypto.SHA384, false}, |
| 192 | {SHA512WithRSA, "SHA512-RSA", oidSignatureSHA512WithRSA, asn1.NullRawValue, RSA, crypto.SHA512, false}, |
| 193 | {SHA256WithRSAPSS, "SHA256-RSAPSS", oidSignatureRSAPSS, pssParametersSHA256, RSA, crypto.SHA256, true}, |
| 194 | {SHA384WithRSAPSS, "SHA384-RSAPSS", oidSignatureRSAPSS, pssParametersSHA384, RSA, crypto.SHA384, true}, |
| 195 | {SHA512WithRSAPSS, "SHA512-RSAPSS", oidSignatureRSAPSS, pssParametersSHA512, RSA, crypto.SHA512, true}, |
| 196 | {DSAWithSHA1, "DSA-SHA1", oidSignatureDSAWithSHA1, emptyRawValue, DSA, crypto.SHA1, false}, |
| 197 | {DSAWithSHA256, "DSA-SHA256", oidSignatureDSAWithSHA256, emptyRawValue, DSA, crypto.SHA256, false}, |
| 198 | {ECDSAWithSHA1, "ECDSA-SHA1", oidSignatureECDSAWithSHA1, emptyRawValue, ECDSA, crypto.SHA1, false}, |
| 199 | {ECDSAWithSHA256, "ECDSA-SHA256", oidSignatureECDSAWithSHA256, emptyRawValue, ECDSA, crypto.SHA256, false}, |
| 200 | {ECDSAWithSHA384, "ECDSA-SHA384", oidSignatureECDSAWithSHA384, emptyRawValue, ECDSA, crypto.SHA384, false}, |
| 201 | {ECDSAWithSHA512, "ECDSA-SHA512", oidSignatureECDSAWithSHA512, emptyRawValue, ECDSA, crypto.SHA512, false}, |
| 202 | {PureEd25519, "Ed25519", oidSignatureEd25519, emptyRawValue, Ed25519, crypto.Hash(0) /* no pre-hashing */, false}, |
| 203 | } |
| 204 | |
| 205 | var emptyRawValue = asn1.RawValue{} |
| 206 | |
| 207 | // DER encoded RSA PSS parameters for the |
| 208 | // SHA256, SHA384, and SHA512 hashes as defined in RFC 3447, Appendix A.2.3. |
| 209 | // The parameters contain the following values: |
| 210 | // - hashAlgorithm contains the associated hash identifier with NULL parameters |
| 211 | // - maskGenAlgorithm always contains the default mgf1SHA1 identifier |
| 212 | // - saltLength contains the length of the associated hash |
| 213 | // - trailerField always contains the default trailerFieldBC value |
| 214 | var ( |
| 215 | pssParametersSHA256 = asn1.RawValue{FullBytes: []byte{48, 52, 160, 15, 48, 13, 6, 9, 96, 134, 72, 1, 101, 3, 4, 2, 1, 5, 0, 161, 28, 48, 26, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 8, 48, 13, 6, 9, 96, 134, 72, 1, 101, 3, 4, 2, 1, 5, 0, 162, 3, 2, 1, 32}} |
| 216 | pssParametersSHA384 = asn1.RawValue{FullBytes: []byte{48, 52, 160, 15, 48, 13, 6, 9, 96, 134, 72, 1, 101, 3, 4, 2, 2, 5, 0, 161, 28, 48, 26, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 8, 48, 13, 6, 9, 96, 134, 72, 1, 101, 3, 4, 2, 2, 5, 0, 162, 3, 2, 1, 48}} |
| 217 | pssParametersSHA512 = asn1.RawValue{FullBytes: []byte{48, 52, 160, 15, 48, 13, 6, 9, 96, 134, 72, 1, 101, 3, 4, 2, 3, 5, 0, 161, 28, 48, 26, 6, 9, 42, 134, 72, 134, 247, 13, 1, 1, 8, 48, 13, 6, 9, 96, 134, 72, 1, 101, 3, 4, 2, 3, 5, 0, 162, 3, 2, 1, 64}} |
| 218 | ) |
| 219 | |
| 220 | // pssParameters reflects the parameters in an AlgorithmIdentifier that |
| 221 | // specifies RSA PSS. See RFC 3447, Appendix A.2.3. |
| 222 | type pssParameters struct { |
| 223 | // The following three fields are not marked as |
| 224 | // optional because the default values specify SHA-1, |
| 225 | // which is no longer suitable for use in signatures. |
| 226 | Hash pkix.AlgorithmIdentifier `asn1:"explicit,tag:0"` |
| 227 | MGF pkix.AlgorithmIdentifier `asn1:"explicit,tag:1"` |
| 228 | SaltLength int `asn1:"explicit,tag:2"` |
| 229 | TrailerField int `asn1:"optional,explicit,tag:3,default:1"` |
| 230 | } |
| 231 | |
| 232 | func getSignatureAlgorithmFromAI(ai pkix.AlgorithmIdentifier) stdx509.SignatureAlgorithm { |
| 233 | if ai.Algorithm.Equal(oidSignatureEd25519) { |
| 234 | // RFC 8410, Section 3 |
| 235 | // > For all of the OIDs, the parameters MUST be absent. |
| 236 | if len(ai.Parameters.FullBytes) != 0 { |
| 237 | return stdx509.UnknownSignatureAlgorithm |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if !ai.Algorithm.Equal(oidSignatureRSAPSS) { |
| 242 | for _, details := range signatureAlgorithmDetails { |
| 243 | if ai.Algorithm.Equal(details.oid) { |
| 244 | return stdx509.SignatureAlgorithm(details.algo) |
| 245 | } |
| 246 | } |
| 247 | return stdx509.UnknownSignatureAlgorithm |
| 248 | } |
| 249 | |
| 250 | // RSA PSS is special because it encodes important parameters |
| 251 | // in the Parameters. |
| 252 | |
| 253 | var params pssParameters |
| 254 | if _, err := asn1.Unmarshal(ai.Parameters.FullBytes, ¶ms); err != nil { |
| 255 | return stdx509.UnknownSignatureAlgorithm |
| 256 | } |
| 257 | |
| 258 | var mgf1HashFunc pkix.AlgorithmIdentifier |
| 259 | if _, err := asn1.Unmarshal(params.MGF.Parameters.FullBytes, &mgf1HashFunc); err != nil { |
| 260 | return stdx509.UnknownSignatureAlgorithm |
| 261 | } |
| 262 | |
| 263 | // PSS is greatly overburdened with options. This code forces them into |
| 264 | // three buckets by requiring that the MGF1 hash function always match the |
| 265 | // message hash function (as recommended in RFC 3447, Section 8.1), that the |
| 266 | // salt length matches the hash length, and that the trailer field has the |
| 267 | // default value. |
| 268 | if (len(params.Hash.Parameters.FullBytes) != 0 && !bytes.Equal(params.Hash.Parameters.FullBytes, asn1.NullBytes)) || |
| 269 | !params.MGF.Algorithm.Equal(oidMGF1) || |
| 270 | !mgf1HashFunc.Algorithm.Equal(params.Hash.Algorithm) || |
| 271 | (len(mgf1HashFunc.Parameters.FullBytes) != 0 && !bytes.Equal(mgf1HashFunc.Parameters.FullBytes, asn1.NullBytes)) || |
| 272 | params.TrailerField != 1 { |
| 273 | return stdx509.UnknownSignatureAlgorithm |
| 274 | } |
| 275 | |
| 276 | switch { |
| 277 | case params.Hash.Algorithm.Equal(oidSHA256) && params.SaltLength == 32: |
| 278 | return stdx509.SHA256WithRSAPSS |
| 279 | case params.Hash.Algorithm.Equal(oidSHA384) && params.SaltLength == 48: |
| 280 | return stdx509.SHA384WithRSAPSS |
| 281 | case params.Hash.Algorithm.Equal(oidSHA512) && params.SaltLength == 64: |
| 282 | return stdx509.SHA512WithRSAPSS |
| 283 | } |
| 284 | |
| 285 | return stdx509.UnknownSignatureAlgorithm |
| 286 | } |
| 287 | |
| 288 | var ( |
| 289 | // RFC 3279, 2.3 Public Key Algorithms |
| 290 | // |
| 291 | // pkcs-1 OBJECT IDENTIFIER ::== { iso(1) member-body(2) us(840) |
| 292 | // rsadsi(113549) pkcs(1) 1 } |
| 293 | // |
| 294 | // rsaEncryption OBJECT IDENTIFIER ::== { pkcs1-1 1 } |
| 295 | // |
| 296 | // id-dsa OBJECT IDENTIFIER ::== { iso(1) member-body(2) us(840) |
| 297 | // x9-57(10040) x9cm(4) 1 } |
| 298 | oidPublicKeyRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1} |
| 299 | oidPublicKeyDSA = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 1} |
| 300 | // RFC 5480, 2.1.1 Unrestricted Algorithm Identifier and Parameters |
| 301 | // |
| 302 | // id-ecPublicKey OBJECT IDENTIFIER ::= { |
| 303 | // iso(1) member-body(2) us(840) ansi-X9-62(10045) keyType(2) 1 } |
| 304 | oidPublicKeyECDSA = asn1.ObjectIdentifier{1, 2, 840, 10045, 2, 1} |
| 305 | // RFC 8410, Section 3 |
| 306 | // |
| 307 | // id-X25519 OBJECT IDENTIFIER ::= { 1 3 101 110 } |
| 308 | // id-Ed25519 OBJECT IDENTIFIER ::= { 1 3 101 112 } |
| 309 | oidPublicKeyX25519 = asn1.ObjectIdentifier{1, 3, 101, 110} |
| 310 | oidPublicKeyEd25519 = asn1.ObjectIdentifier{1, 3, 101, 112} |
| 311 | ) |
| 312 | |
| 313 | // getPublicKeyAlgorithmFromOID returns the exposed PublicKeyAlgorithm |
| 314 | // identifier for public key types supported in certificates and CSRs. Marshal |
| 315 | // and Parse functions may support a different set of public key types. |
| 316 | func getPublicKeyAlgorithmFromOID(oid asn1.ObjectIdentifier) stdx509.PublicKeyAlgorithm { |
| 317 | switch { |
| 318 | case oid.Equal(oidPublicKeyRSA): |
| 319 | return stdx509.RSA |
| 320 | case oid.Equal(oidPublicKeyDSA): |
| 321 | return stdx509.DSA |
| 322 | case oid.Equal(oidPublicKeyECDSA): |
| 323 | return stdx509.ECDSA |
| 324 | case oid.Equal(oidPublicKeyEd25519): |
| 325 | return stdx509.Ed25519 |
| 326 | } |
| 327 | return stdx509.UnknownPublicKeyAlgorithm |
| 328 | } |
| 329 | |
| 330 | // RFC 5480, 2.1.1.1. Named Curve |
| 331 | // |
| 332 | // secp224r1 OBJECT IDENTIFIER ::= { |
| 333 | // iso(1) identified-organization(3) certicom(132) curve(0) 33 } |
| 334 | // |
| 335 | // secp256r1 OBJECT IDENTIFIER ::= { |
| 336 | // iso(1) member-body(2) us(840) ansi-X9-62(10045) curves(3) |
| 337 | // prime(1) 7 } |
| 338 | // |
| 339 | // secp384r1 OBJECT IDENTIFIER ::= { |
| 340 | // iso(1) identified-organization(3) certicom(132) curve(0) 34 } |
| 341 | // |
| 342 | // secp521r1 OBJECT IDENTIFIER ::= { |
| 343 | // iso(1) identified-organization(3) certicom(132) curve(0) 35 } |
| 344 | // |
| 345 | // NB: secp256r1 is equivalent to prime256v1 |
| 346 | var ( |
| 347 | oidNamedCurveP224 = asn1.ObjectIdentifier{1, 3, 132, 0, 33} |
| 348 | oidNamedCurveP256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 7} |
| 349 | oidNamedCurveP384 = asn1.ObjectIdentifier{1, 3, 132, 0, 34} |
| 350 | oidNamedCurveP521 = asn1.ObjectIdentifier{1, 3, 132, 0, 35} |
| 351 | ) |
| 352 | |
| 353 | func namedCurveFromOID(oid asn1.ObjectIdentifier) elliptic.Curve { |
| 354 | switch { |
| 355 | case oid.Equal(oidNamedCurveP224): |
| 356 | return elliptic.P224() |
| 357 | case oid.Equal(oidNamedCurveP256): |
| 358 | return elliptic.P256() |
| 359 | case oid.Equal(oidNamedCurveP384): |
| 360 | return elliptic.P384() |
| 361 | case oid.Equal(oidNamedCurveP521): |
| 362 | return elliptic.P521() |
| 363 | } |
| 364 | return nil |
| 365 | } |
| 366 | |
| 367 | // KeyUsage represents the set of actions that are valid for a given key. It's |
| 368 | // a bitmap of the KeyUsage* constants. |
| 369 | type KeyUsage int |
| 370 | |
| 371 | const ( |
| 372 | KeyUsageDigitalSignature KeyUsage = 1 << iota |
| 373 | KeyUsageContentCommitment |
| 374 | KeyUsageKeyEncipherment |
| 375 | KeyUsageDataEncipherment |
| 376 | KeyUsageKeyAgreement |
| 377 | KeyUsageCertSign |
| 378 | KeyUsageCRLSign |
| 379 | KeyUsageEncipherOnly |
| 380 | KeyUsageDecipherOnly |
| 381 | ) |
| 382 | |
| 383 | // RFC 5280, 4.2.1.12 Extended Key Usage |
| 384 | // |
| 385 | // anyExtendedKeyUsage OBJECT IDENTIFIER ::= { id-ce-extKeyUsage 0 } |
| 386 | // |
| 387 | // id-kp OBJECT IDENTIFIER ::= { id-pkix 3 } |
| 388 | // |
| 389 | // id-kp-serverAuth OBJECT IDENTIFIER ::= { id-kp 1 } |
| 390 | // id-kp-clientAuth OBJECT IDENTIFIER ::= { id-kp 2 } |
| 391 | // id-kp-codeSigning OBJECT IDENTIFIER ::= { id-kp 3 } |
| 392 | // id-kp-emailProtection OBJECT IDENTIFIER ::= { id-kp 4 } |
| 393 | // id-kp-timeStamping OBJECT IDENTIFIER ::= { id-kp 8 } |
| 394 | // id-kp-OCSPSigning OBJECT IDENTIFIER ::= { id-kp 9 } |
| 395 | var ( |
| 396 | oidExtKeyUsageAny = asn1.ObjectIdentifier{2, 5, 29, 37, 0} |
| 397 | oidExtKeyUsageServerAuth = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 1} |
| 398 | oidExtKeyUsageClientAuth = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 2} |
| 399 | oidExtKeyUsageCodeSigning = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 3} |
| 400 | oidExtKeyUsageEmailProtection = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 4} |
| 401 | oidExtKeyUsageIPSECEndSystem = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 5} |
| 402 | oidExtKeyUsageIPSECTunnel = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 6} |
| 403 | oidExtKeyUsageIPSECUser = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 7} |
| 404 | oidExtKeyUsageTimeStamping = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 8} |
| 405 | oidExtKeyUsageOCSPSigning = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 9} |
| 406 | oidExtKeyUsageMicrosoftServerGatedCrypto = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 10, 3, 3} |
| 407 | oidExtKeyUsageNetscapeServerGatedCrypto = asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 4, 1} |
| 408 | oidExtKeyUsageMicrosoftCommercialCodeSigning = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 2, 1, 22} |
| 409 | oidExtKeyUsageMicrosoftKernelCodeSigning = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 61, 1, 1} |
| 410 | ) |
| 411 | |
| 412 | // ExtKeyUsage represents an extended set of actions that are valid for a given key. |
| 413 | // Each of the ExtKeyUsage* constants define a unique action. |
| 414 | type ExtKeyUsage int |
| 415 | |
| 416 | const ( |
| 417 | ExtKeyUsageAny ExtKeyUsage = iota |
| 418 | ExtKeyUsageServerAuth |
| 419 | ExtKeyUsageClientAuth |
| 420 | ExtKeyUsageCodeSigning |
| 421 | ExtKeyUsageEmailProtection |
| 422 | ExtKeyUsageIPSECEndSystem |
| 423 | ExtKeyUsageIPSECTunnel |
| 424 | ExtKeyUsageIPSECUser |
| 425 | ExtKeyUsageTimeStamping |
| 426 | ExtKeyUsageOCSPSigning |
| 427 | ExtKeyUsageMicrosoftServerGatedCrypto |
| 428 | ExtKeyUsageNetscapeServerGatedCrypto |
| 429 | ExtKeyUsageMicrosoftCommercialCodeSigning |
| 430 | ExtKeyUsageMicrosoftKernelCodeSigning |
| 431 | ) |
| 432 | |
| 433 | // extKeyUsageOIDs contains the mapping between an ExtKeyUsage and its OID. |
| 434 | var extKeyUsageOIDs = []struct { |
| 435 | extKeyUsage ExtKeyUsage |
| 436 | oid asn1.ObjectIdentifier |
| 437 | }{ |
| 438 | {ExtKeyUsageAny, oidExtKeyUsageAny}, |
| 439 | {ExtKeyUsageServerAuth, oidExtKeyUsageServerAuth}, |
| 440 | {ExtKeyUsageClientAuth, oidExtKeyUsageClientAuth}, |
| 441 | {ExtKeyUsageCodeSigning, oidExtKeyUsageCodeSigning}, |
| 442 | {ExtKeyUsageEmailProtection, oidExtKeyUsageEmailProtection}, |
| 443 | {ExtKeyUsageIPSECEndSystem, oidExtKeyUsageIPSECEndSystem}, |
| 444 | {ExtKeyUsageIPSECTunnel, oidExtKeyUsageIPSECTunnel}, |
| 445 | {ExtKeyUsageIPSECUser, oidExtKeyUsageIPSECUser}, |
| 446 | {ExtKeyUsageTimeStamping, oidExtKeyUsageTimeStamping}, |
| 447 | {ExtKeyUsageOCSPSigning, oidExtKeyUsageOCSPSigning}, |
| 448 | {ExtKeyUsageMicrosoftServerGatedCrypto, oidExtKeyUsageMicrosoftServerGatedCrypto}, |
| 449 | {ExtKeyUsageNetscapeServerGatedCrypto, oidExtKeyUsageNetscapeServerGatedCrypto}, |
| 450 | {ExtKeyUsageMicrosoftCommercialCodeSigning, oidExtKeyUsageMicrosoftCommercialCodeSigning}, |
| 451 | {ExtKeyUsageMicrosoftKernelCodeSigning, oidExtKeyUsageMicrosoftKernelCodeSigning}, |
| 452 | } |
| 453 | |
| 454 | func extKeyUsageFromOID(oid asn1.ObjectIdentifier) (eku ExtKeyUsage, ok bool) { |
| 455 | for _, pair := range extKeyUsageOIDs { |
| 456 | if oid.Equal(pair.oid) { |
| 457 | return pair.extKeyUsage, true |
| 458 | } |
| 459 | } |
| 460 | return |
| 461 | } |
| 462 | |
| 463 | const ( |
| 464 | nameTypeEmail = 1 |
| 465 | nameTypeDNS = 2 |
| 466 | nameTypeURI = 6 |
| 467 | nameTypeIP = 7 |
| 468 | ) |
| 469 | |
| 470 | var ( |
| 471 | oidExtensionAuthorityInfoAccess = []int{1, 3, 6, 1, 5, 5, 7, 1, 1} |
| 472 | ) |
| 473 | |
| 474 | var ( |
| 475 | oidAuthorityInfoAccessOcsp = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 48, 1} |
| 476 | oidAuthorityInfoAccessIssuers = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 48, 2} |
| 477 | ) |
| 478 | |
| 479 | func isIA5String(s string) error { |
| 480 | for _, r := range s { |
| 481 | // Per RFC5280 "IA5String is limited to the set of ASCII characters" |
| 482 | if r > unicode.MaxASCII { |
| 483 | return fmt.Errorf("x509: %q cannot be encoded as an IA5String", s) |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | return nil |
| 488 | } |