algorithm_identifier.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package util
  2. import (
  3. "bytes"
  4. "encoding/asn1"
  5. "errors"
  6. "fmt"
  7. "github.com/zmap/zcrypto/x509"
  8. "golang.org/x/crypto/cryptobyte"
  9. cryptobyte_asn1 "golang.org/x/crypto/cryptobyte/asn1"
  10. )
  11. // additional OIDs not provided by the x509 package.
  12. var (
  13. // 1.2.840.10045.4.3.1 is SHA224withECDSA
  14. OidSignatureSHA224withECDSA = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 1}
  15. )
  16. // RSAAlgorithmIDToDER contains DER representations of pkix.AlgorithmIdentifier for different RSA OIDs with Parameters as asn1.NULL.
  17. var RSAAlgorithmIDToDER = map[string][]byte{
  18. // rsaEncryption
  19. "1.2.840.113549.1.1.1": {0x30, 0x0d, 0x6, 0x9, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0xd, 0x1, 0x1, 0x1, 0x5, 0x0},
  20. // md2WithRSAEncryption
  21. "1.2.840.113549.1.1.2": {0x30, 0x0d, 0x6, 0x9, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0xd, 0x1, 0x1, 0x2, 0x5, 0x0},
  22. // md5WithRSAEncryption
  23. "1.2.840.113549.1.1.4": {0x30, 0x0d, 0x6, 0x9, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0xd, 0x1, 0x1, 0x4, 0x5, 0x0},
  24. // sha-1WithRSAEncryption
  25. "1.2.840.113549.1.1.5": {0x30, 0x0d, 0x6, 0x9, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0xd, 0x1, 0x1, 0x5, 0x5, 0x0},
  26. // sha224WithRSAEncryption
  27. "1.2.840.113549.1.1.14": {0x30, 0x0d, 0x6, 0x9, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0xd, 0x1, 0x1, 0xe, 0x5, 0x0},
  28. // sha256WithRSAEncryption
  29. "1.2.840.113549.1.1.11": {0x30, 0x0d, 0x6, 0x9, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0xd, 0x1, 0x1, 0xb, 0x5, 0x0},
  30. // sha384WithRSAEncryption
  31. "1.2.840.113549.1.1.12": {0x30, 0x0d, 0x6, 0x9, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0xd, 0x1, 0x1, 0xc, 0x5, 0x0},
  32. // sha512WithRSAEncryption
  33. "1.2.840.113549.1.1.13": {0x30, 0x0d, 0x6, 0x9, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0xd, 0x1, 0x1, 0xd, 0x5, 0x0},
  34. }
  35. // CheckAlgorithmIDParamNotNULL parses an AlgorithmIdentifier with algorithm OID rsaEncryption to check the Param field is asn1.NULL
  36. // Expects DER-encoded AlgorithmIdentifier including tag and length.
  37. func CheckAlgorithmIDParamNotNULL(algorithmIdentifier []byte, requiredAlgoID asn1.ObjectIdentifier) error {
  38. expectedAlgoIDBytes, ok := RSAAlgorithmIDToDER[requiredAlgoID.String()]
  39. if !ok {
  40. return errors.New("error algorithmID to check is not RSA")
  41. }
  42. algorithmSequence := cryptobyte.String(algorithmIdentifier)
  43. // byte comparison of algorithm sequence and checking no trailing data is present
  44. var algorithmBytes []byte
  45. if algorithmSequence.ReadBytes(&algorithmBytes, len(expectedAlgoIDBytes)) {
  46. if bytes.Equal(algorithmBytes, expectedAlgoIDBytes) && algorithmSequence.Empty() {
  47. return nil
  48. }
  49. }
  50. // re-parse to get an error message detailing what did not match in the byte comparison
  51. algorithmSequence = cryptobyte.String(algorithmIdentifier)
  52. var algorithm cryptobyte.String
  53. if !algorithmSequence.ReadASN1(&algorithm, cryptobyte_asn1.SEQUENCE) {
  54. return errors.New("error reading algorithm")
  55. }
  56. encryptionOID := asn1.ObjectIdentifier{}
  57. if !algorithm.ReadASN1ObjectIdentifier(&encryptionOID) {
  58. return errors.New("error reading algorithm OID")
  59. }
  60. if !encryptionOID.Equal(requiredAlgoID) {
  61. return fmt.Errorf("algorithm OID is not equal to %s", requiredAlgoID.String())
  62. }
  63. if algorithm.Empty() {
  64. return errors.New("RSA algorithm identifier missing required NULL parameter")
  65. }
  66. var nullValue cryptobyte.String
  67. if !algorithm.ReadASN1(&nullValue, cryptobyte_asn1.NULL) {
  68. return errors.New("RSA algorithm identifier with non-NULL parameter")
  69. }
  70. if len(nullValue) != 0 {
  71. return errors.New("RSA algorithm identifier with NULL parameter containing data")
  72. }
  73. // ensure algorithm is empty and no trailing data is present
  74. if !algorithm.Empty() {
  75. return errors.New("RSA algorithm identifier with trailing data")
  76. }
  77. return errors.New("RSA algorithm appears correct, but didn't match byte-wise comparison")
  78. }
  79. // Returns the signature field of the tbsCertificate of this certificate in a DER encoded form or an error
  80. // if the signature field could not be extracted. The encoded form contains the tag and the length.
  81. //
  82. // TBSCertificate ::= SEQUENCE {
  83. // version [0] EXPLICIT Version DEFAULT v1,
  84. // serialNumber CertificateSerialNumber,
  85. // signature AlgorithmIdentifier,
  86. // issuer Name,
  87. // validity Validity,
  88. // subject Name,
  89. // subjectPublicKeyInfo SubjectPublicKeyInfo,
  90. // issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
  91. // -- If present, version MUST be v2 or v3
  92. // subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
  93. // -- If present, version MUST be v2 or v3
  94. // extensions [3] EXPLICIT Extensions OPTIONAL
  95. // -- If present, version MUST be v3
  96. // }
  97. func GetSignatureAlgorithmInTBSEncoded(c *x509.Certificate) ([]byte, error) {
  98. input := cryptobyte.String(c.RawTBSCertificate)
  99. var tbsCert cryptobyte.String
  100. if !input.ReadASN1(&tbsCert, cryptobyte_asn1.SEQUENCE) {
  101. return nil, errors.New("error reading tbsCertificate")
  102. }
  103. if !tbsCert.SkipOptionalASN1(cryptobyte_asn1.Tag(0).Constructed().ContextSpecific()) {
  104. return nil, errors.New("error reading tbsCertificate.version")
  105. }
  106. if !tbsCert.SkipASN1(cryptobyte_asn1.INTEGER) {
  107. return nil, errors.New("error reading tbsCertificate.serialNumber")
  108. }
  109. var signatureAlgoID cryptobyte.String
  110. var tag cryptobyte_asn1.Tag
  111. // use ReadAnyElement to preserve tag and length octets
  112. if !tbsCert.ReadAnyASN1Element(&signatureAlgoID, &tag) {
  113. return nil, errors.New("error reading tbsCertificate.signature")
  114. }
  115. return signatureAlgoID, nil
  116. }
  117. // Returns the algorithm field of the SubjectPublicKeyInfo of the certificate or an error
  118. // if the algorithm field could not be extracted.
  119. //
  120. // SubjectPublicKeyInfo ::= SEQUENCE {
  121. // algorithm AlgorithmIdentifier,
  122. // subjectPublicKey BIT STRING }
  123. //
  124. func GetPublicKeyOID(c *x509.Certificate) (asn1.ObjectIdentifier, error) {
  125. input := cryptobyte.String(c.RawSubjectPublicKeyInfo)
  126. var publicKeyInfo cryptobyte.String
  127. if !input.ReadASN1(&publicKeyInfo, cryptobyte_asn1.SEQUENCE) {
  128. return nil, errors.New("error reading pkixPublicKey")
  129. }
  130. var algorithm cryptobyte.String
  131. if !publicKeyInfo.ReadASN1(&algorithm, cryptobyte_asn1.SEQUENCE) {
  132. return nil, errors.New("error reading public key algorithm identifier")
  133. }
  134. publicKeyOID := asn1.ObjectIdentifier{}
  135. if !algorithm.ReadASN1ObjectIdentifier(&publicKeyOID) {
  136. return nil, errors.New("error reading public key OID")
  137. }
  138. return publicKeyOID, nil
  139. }
  140. // Returns the algorithm field of the SubjectPublicKeyInfo of the certificate in its encoded form (containing Tag
  141. // and Length) or an error if the algorithm field could not be extracted.
  142. //
  143. // SubjectPublicKeyInfo ::= SEQUENCE {
  144. // algorithm AlgorithmIdentifier,
  145. // subjectPublicKey BIT STRING }
  146. //
  147. func GetPublicKeyAidEncoded(c *x509.Certificate) ([]byte, error) {
  148. input := cryptobyte.String(c.RawSubjectPublicKeyInfo)
  149. var spkiContent cryptobyte.String
  150. if !input.ReadASN1(&spkiContent, cryptobyte_asn1.SEQUENCE) {
  151. return nil, errors.New("error reading pkixPublicKey")
  152. }
  153. var algorithm cryptobyte.String
  154. var tag cryptobyte_asn1.Tag
  155. if !spkiContent.ReadAnyASN1Element(&algorithm, &tag) {
  156. return nil, errors.New("error reading public key algorithm identifier")
  157. }
  158. return algorithm, nil
  159. }