x509.go 19 KB

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