pkcs7.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Package pkcs7 implements the subset of the CMS PKCS #7 datatype that is typically
  2. // used to package certificates and CRLs. Using openssl, every certificate converted
  3. // to PKCS #7 format from another encoding such as PEM conforms to this implementation.
  4. // reference: https://www.openssl.org/docs/man1.1.0/apps/crl2pkcs7.html
  5. //
  6. // PKCS #7 Data type, reference: https://tools.ietf.org/html/rfc2315
  7. //
  8. // The full pkcs#7 cryptographic message syntax allows for cryptographic enhancements,
  9. // for example data can be encrypted and signed and then packaged through pkcs#7 to be
  10. // sent over a network and then verified and decrypted. It is asn1, and the type of
  11. // PKCS #7 ContentInfo, which comprises the PKCS #7 structure, is:
  12. //
  13. // ContentInfo ::= SEQUENCE {
  14. // contentType ContentType,
  15. // content [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL
  16. // }
  17. //
  18. // There are 6 possible ContentTypes, data, signedData, envelopedData,
  19. // signedAndEnvelopedData, digestedData, and encryptedData. Here signedData, Data, and encrypted
  20. // Data are implemented, as the degenerate case of signedData without a signature is the typical
  21. // format for transferring certificates and CRLS, and Data and encryptedData are used in PKCS #12
  22. // formats.
  23. // The ContentType signedData has the form:
  24. //
  25. // signedData ::= SEQUENCE {
  26. // version Version,
  27. // digestAlgorithms DigestAlgorithmIdentifiers,
  28. // contentInfo ContentInfo,
  29. // certificates [0] IMPLICIT ExtendedCertificatesAndCertificates OPTIONAL
  30. // crls [1] IMPLICIT CertificateRevocationLists OPTIONAL,
  31. // signerInfos SignerInfos
  32. // }
  33. //
  34. // As of yet signerInfos and digestAlgorithms are not parsed, as they are not relevant to
  35. // this system's use of PKCS #7 data. Version is an integer type, note that PKCS #7 is
  36. // recursive, this second layer of ContentInfo is similar ignored for our degenerate
  37. // usage. The ExtendedCertificatesAndCertificates type consists of a sequence of choices
  38. // between PKCS #6 extended certificates and x509 certificates. Any sequence consisting
  39. // of any number of extended certificates is not yet supported in this implementation.
  40. //
  41. // The ContentType Data is simply a raw octet string and is parsed directly into a Go []byte slice.
  42. //
  43. // The ContentType encryptedData is the most complicated and its form can be gathered by
  44. // the go type below. It essentially contains a raw octet string of encrypted data and an
  45. // algorithm identifier for use in decrypting this data.
  46. package pkcs7
  47. import (
  48. "crypto/x509"
  49. "crypto/x509/pkix"
  50. "encoding/asn1"
  51. "errors"
  52. cferr "github.com/cloudflare/cfssl/errors"
  53. )
  54. // Types used for asn1 Unmarshaling.
  55. type signedData struct {
  56. Version int
  57. DigestAlgorithms asn1.RawValue
  58. ContentInfo asn1.RawValue
  59. Certificates asn1.RawValue `asn1:"optional" asn1:"tag:0"`
  60. Crls asn1.RawValue `asn1:"optional"`
  61. SignerInfos asn1.RawValue
  62. }
  63. type initPKCS7 struct {
  64. Raw asn1.RawContent
  65. ContentType asn1.ObjectIdentifier
  66. Content asn1.RawValue `asn1:"tag:0,explicit,optional"`
  67. }
  68. // Object identifier strings of the three implemented PKCS7 types.
  69. const (
  70. ObjIDData = "1.2.840.113549.1.7.1"
  71. ObjIDSignedData = "1.2.840.113549.1.7.2"
  72. ObjIDEncryptedData = "1.2.840.113549.1.7.6"
  73. )
  74. // PKCS7 represents the ASN1 PKCS #7 Content type. It contains one of three
  75. // possible types of Content objects, as denoted by the object identifier in
  76. // the ContentInfo field, the other two being nil. SignedData
  77. // is the degenerate SignedData Content info without signature used
  78. // to hold certificates and crls. Data is raw bytes, and EncryptedData
  79. // is as defined in PKCS #7 standard.
  80. type PKCS7 struct {
  81. Raw asn1.RawContent
  82. ContentInfo string
  83. Content Content
  84. }
  85. // Content implements three of the six possible PKCS7 data types. Only one is non-nil.
  86. type Content struct {
  87. Data []byte
  88. SignedData SignedData
  89. EncryptedData EncryptedData
  90. }
  91. // SignedData defines the typical carrier of certificates and crls.
  92. type SignedData struct {
  93. Raw asn1.RawContent
  94. Version int
  95. Certificates []*x509.Certificate
  96. Crl *pkix.CertificateList
  97. }
  98. // Data contains raw bytes. Used as a subtype in PKCS12.
  99. type Data struct {
  100. Bytes []byte
  101. }
  102. // EncryptedData contains encrypted data. Used as a subtype in PKCS12.
  103. type EncryptedData struct {
  104. Raw asn1.RawContent
  105. Version int
  106. EncryptedContentInfo EncryptedContentInfo
  107. }
  108. // EncryptedContentInfo is a subtype of PKCS7EncryptedData.
  109. type EncryptedContentInfo struct {
  110. Raw asn1.RawContent
  111. ContentType asn1.ObjectIdentifier
  112. ContentEncryptionAlgorithm pkix.AlgorithmIdentifier
  113. EncryptedContent []byte `asn1:"tag:0,optional"`
  114. }
  115. // ParsePKCS7 attempts to parse the DER encoded bytes of a
  116. // PKCS7 structure.
  117. func ParsePKCS7(raw []byte) (msg *PKCS7, err error) {
  118. var pkcs7 initPKCS7
  119. _, err = asn1.Unmarshal(raw, &pkcs7)
  120. if err != nil {
  121. return nil, cferr.Wrap(cferr.CertificateError, cferr.ParseFailed, err)
  122. }
  123. msg = new(PKCS7)
  124. msg.Raw = pkcs7.Raw
  125. msg.ContentInfo = pkcs7.ContentType.String()
  126. switch {
  127. case msg.ContentInfo == ObjIDData:
  128. msg.ContentInfo = "Data"
  129. _, err = asn1.Unmarshal(pkcs7.Content.Bytes, &msg.Content.Data)
  130. if err != nil {
  131. return nil, cferr.Wrap(cferr.CertificateError, cferr.ParseFailed, err)
  132. }
  133. case msg.ContentInfo == ObjIDSignedData:
  134. msg.ContentInfo = "SignedData"
  135. var signedData signedData
  136. _, err = asn1.Unmarshal(pkcs7.Content.Bytes, &signedData)
  137. if err != nil {
  138. return nil, cferr.Wrap(cferr.CertificateError, cferr.ParseFailed, err)
  139. }
  140. if len(signedData.Certificates.Bytes) != 0 {
  141. msg.Content.SignedData.Certificates, err = x509.ParseCertificates(signedData.Certificates.Bytes)
  142. if err != nil {
  143. return nil, cferr.Wrap(cferr.CertificateError, cferr.ParseFailed, err)
  144. }
  145. }
  146. if len(signedData.Crls.Bytes) != 0 {
  147. msg.Content.SignedData.Crl, err = x509.ParseDERCRL(signedData.Crls.Bytes)
  148. if err != nil {
  149. return nil, cferr.Wrap(cferr.CertificateError, cferr.ParseFailed, err)
  150. }
  151. }
  152. msg.Content.SignedData.Version = signedData.Version
  153. msg.Content.SignedData.Raw = pkcs7.Content.Bytes
  154. case msg.ContentInfo == ObjIDEncryptedData:
  155. msg.ContentInfo = "EncryptedData"
  156. var encryptedData EncryptedData
  157. _, err = asn1.Unmarshal(pkcs7.Content.Bytes, &encryptedData)
  158. if err != nil {
  159. return nil, cferr.Wrap(cferr.CertificateError, cferr.ParseFailed, err)
  160. }
  161. if encryptedData.Version != 0 {
  162. return nil, cferr.Wrap(cferr.CertificateError, cferr.ParseFailed, errors.New("Only support for PKCS #7 encryptedData version 0"))
  163. }
  164. msg.Content.EncryptedData = encryptedData
  165. default:
  166. return nil, cferr.Wrap(cferr.CertificateError, cferr.ParseFailed, errors.New("Attempt to parse PKCS# 7 Content not of type data, signed data or encrypted data"))
  167. }
  168. return msg, nil
  169. }