| 1 | // Package pkcs7 implements parsing and generation of some PKCS#7 structures. |
| 2 | package pkcs7 |
| 3 | |
| 4 | import ( |
| 5 | "bytes" |
| 6 | "crypto" |
| 7 | "crypto/dsa" |
| 8 | "crypto/ecdsa" |
| 9 | "crypto/rsa" |
| 10 | "crypto/x509" |
| 11 | "crypto/x509/pkix" |
| 12 | "encoding/asn1" |
| 13 | "errors" |
| 14 | "fmt" |
| 15 | "io" |
| 16 | "sort" |
| 17 | "sync" |
| 18 | |
| 19 | _ "crypto/sha1" // for crypto.SHA1 |
| 20 | |
| 21 | legacyx509 "github.com/smallstep/pkcs7/internal/legacy/x509" |
| 22 | ) |
| 23 | |
| 24 | // PKCS7 Represents a PKCS7 structure |
| 25 | type PKCS7 struct { |
| 26 | Content []byte |
| 27 | Certificates []*x509.Certificate |
| 28 | CRLs []pkix.CertificateList |
| 29 | Signers []signerInfo |
| 30 | Hasher Hasher |
| 31 | raw interface{} |
| 32 | } |
| 33 | |
| 34 | // Hasher is an interface defining a custom hash calculator. |
| 35 | type Hasher interface { |
| 36 | Hash(crypto.Hash, io.Reader) ([]byte, error) |
| 37 | } |
| 38 | |
| 39 | type contentInfo struct { |
| 40 | ContentType asn1.ObjectIdentifier |
| 41 | Content asn1.RawValue `asn1:"explicit,optional,tag:0"` |
| 42 | } |
| 43 | |
| 44 | // ErrUnsupportedContentType is returned when a PKCS7 content type is not supported. |
| 45 | // Currently only Data (1.2.840.113549.1.7.1), Signed Data (1.2.840.113549.1.7.2), |
| 46 | // and Enveloped Data are supported (1.2.840.113549.1.7.3) |
| 47 | var ErrUnsupportedContentType = errors.New("pkcs7: cannot parse data: unimplemented content type") |
| 48 | |
| 49 | type unsignedData []byte |
| 50 | |
| 51 | var ( |
| 52 | // Signed Data OIDs |
| 53 | OIDData = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 1} |
| 54 | OIDSignedData = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 2} |
| 55 | OIDEnvelopedData = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 3} |
| 56 | OIDEncryptedData = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 7, 6} |
| 57 | OIDAttributeContentType = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 3} |
| 58 | OIDAttributeMessageDigest = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 4} |
| 59 | OIDAttributeSigningTime = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 5} |
| 60 | |
| 61 | // Digest Algorithms |
| 62 | OIDDigestAlgorithmSHA1 = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 26} |
| 63 | OIDDigestAlgorithmSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 1} |
| 64 | OIDDigestAlgorithmSHA384 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 2} |
| 65 | OIDDigestAlgorithmSHA512 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 3} |
| 66 | OIDDigestAlgorithmSHA224 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 2, 4} |
| 67 | |
| 68 | OIDDigestAlgorithmDSA = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 1} |
| 69 | OIDDigestAlgorithmDSASHA1 = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 3} |
| 70 | |
| 71 | OIDDigestAlgorithmECDSASHA1 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 1} |
| 72 | OIDDigestAlgorithmECDSASHA256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2} |
| 73 | OIDDigestAlgorithmECDSASHA384 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 3} |
| 74 | OIDDigestAlgorithmECDSASHA512 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 4} |
| 75 | |
| 76 | // Signature Algorithms |
| 77 | OIDEncryptionAlgorithmRSAMD5 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 4} // see https://www.rfc-editor.org/rfc/rfc8017#appendix-A.2.4 |
| 78 | OIDEncryptionAlgorithmRSASHA1 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 5} // ditto |
| 79 | OIDEncryptionAlgorithmRSASHA256 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 11} // ditto |
| 80 | OIDEncryptionAlgorithmRSASHA384 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 12} // ditto |
| 81 | OIDEncryptionAlgorithmRSASHA512 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 13} // ditto |
| 82 | OIDEncryptionAlgorithmRSASHA224 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 14} // ditto |
| 83 | |
| 84 | OIDEncryptionAlgorithmECDSAP256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 3, 1, 7} |
| 85 | OIDEncryptionAlgorithmECDSAP384 = asn1.ObjectIdentifier{1, 3, 132, 0, 34} |
| 86 | OIDEncryptionAlgorithmECDSAP521 = asn1.ObjectIdentifier{1, 3, 132, 0, 35} |
| 87 | |
| 88 | // Asymmetric Encryption Algorithms |
| 89 | OIDEncryptionAlgorithmRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1} // see https://www.rfc-editor.org/rfc/rfc8017#appendix-A.2.2 |
| 90 | OIDEncryptionAlgorithmRSAESOAEP = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 7} // see https://www.rfc-editor.org/rfc/rfc8017#appendix-A.2.1 |
| 91 | |
| 92 | // Symmetric Encryption Algorithms |
| 93 | OIDEncryptionAlgorithmDESCBC = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 7} // see https://www.rfc-editor.org/rfc/rfc8018.html#appendix-B.2.1 |
| 94 | OIDEncryptionAlgorithmDESEDE3CBC = asn1.ObjectIdentifier{1, 2, 840, 113549, 3, 7} // see https://www.rfc-editor.org/rfc/rfc8018.html#appendix-B.2.2 |
| 95 | OIDEncryptionAlgorithmAES256CBC = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 1, 42} // see https://www.rfc-editor.org/rfc/rfc3565.html#section-4.1 |
| 96 | OIDEncryptionAlgorithmAES128GCM = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 1, 6} // see https://www.rfc-editor.org/rfc/rfc5084.html#section-3.2 |
| 97 | OIDEncryptionAlgorithmAES128CBC = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 1, 2} // see https://www.rfc-editor.org/rfc/rfc8018.html#appendix-B.2.5 |
| 98 | OIDEncryptionAlgorithmAES256GCM = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 1, 46} // see https://www.rfc-editor.org/rfc/rfc5084.html#section-3.2 |
| 99 | ) |
| 100 | |
| 101 | func getHashForOID(oid asn1.ObjectIdentifier) (crypto.Hash, error) { |
| 102 | switch { |
| 103 | case oid.Equal(OIDDigestAlgorithmSHA1), oid.Equal(OIDDigestAlgorithmECDSASHA1), |
| 104 | oid.Equal(OIDDigestAlgorithmDSA), oid.Equal(OIDDigestAlgorithmDSASHA1), |
| 105 | oid.Equal(OIDEncryptionAlgorithmRSA): |
| 106 | return crypto.SHA1, nil |
| 107 | case oid.Equal(OIDDigestAlgorithmSHA256), oid.Equal(OIDDigestAlgorithmECDSASHA256): |
| 108 | return crypto.SHA256, nil |
| 109 | case oid.Equal(OIDDigestAlgorithmSHA384), oid.Equal(OIDDigestAlgorithmECDSASHA384): |
| 110 | return crypto.SHA384, nil |
| 111 | case oid.Equal(OIDDigestAlgorithmSHA512), oid.Equal(OIDDigestAlgorithmECDSASHA512): |
| 112 | return crypto.SHA512, nil |
| 113 | } |
| 114 | return crypto.Hash(0), ErrUnsupportedAlgorithm |
| 115 | } |
| 116 | |
| 117 | // getDigestOIDForSignatureAlgorithm takes an x509.SignatureAlgorithm |
| 118 | // and returns the corresponding OID digest algorithm |
| 119 | func getDigestOIDForSignatureAlgorithm(digestAlg x509.SignatureAlgorithm) (asn1.ObjectIdentifier, error) { |
| 120 | switch digestAlg { |
| 121 | case x509.SHA1WithRSA, x509.ECDSAWithSHA1: |
| 122 | return OIDDigestAlgorithmSHA1, nil |
| 123 | case x509.SHA256WithRSA, x509.ECDSAWithSHA256: |
| 124 | return OIDDigestAlgorithmSHA256, nil |
| 125 | case x509.SHA384WithRSA, x509.ECDSAWithSHA384: |
| 126 | return OIDDigestAlgorithmSHA384, nil |
| 127 | case x509.SHA512WithRSA, x509.ECDSAWithSHA512: |
| 128 | return OIDDigestAlgorithmSHA512, nil |
| 129 | } |
| 130 | return nil, fmt.Errorf("pkcs7: cannot convert hash to oid, unknown hash algorithm") |
| 131 | } |
| 132 | |
| 133 | // getOIDForEncryptionAlgorithm takes the public or private key type of the signer and |
| 134 | // the OID of a digest algorithm to return the appropriate signerInfo.DigestEncryptionAlgorithm |
| 135 | func getOIDForEncryptionAlgorithm(pkey interface{}, OIDDigestAlg asn1.ObjectIdentifier) (asn1.ObjectIdentifier, error) { |
| 136 | switch k := pkey.(type) { |
| 137 | case *rsa.PrivateKey, *rsa.PublicKey: |
| 138 | switch { |
| 139 | default: |
| 140 | return OIDEncryptionAlgorithmRSA, nil |
| 141 | case OIDDigestAlg.Equal(OIDEncryptionAlgorithmRSA): |
| 142 | return OIDEncryptionAlgorithmRSA, nil |
| 143 | case OIDDigestAlg.Equal(OIDDigestAlgorithmSHA1): |
| 144 | return OIDEncryptionAlgorithmRSASHA1, nil |
| 145 | case OIDDigestAlg.Equal(OIDDigestAlgorithmSHA256): |
| 146 | return OIDEncryptionAlgorithmRSASHA256, nil |
| 147 | case OIDDigestAlg.Equal(OIDDigestAlgorithmSHA384): |
| 148 | return OIDEncryptionAlgorithmRSASHA384, nil |
| 149 | case OIDDigestAlg.Equal(OIDDigestAlgorithmSHA512): |
| 150 | return OIDEncryptionAlgorithmRSASHA512, nil |
| 151 | } |
| 152 | case *ecdsa.PrivateKey, *ecdsa.PublicKey: |
| 153 | switch { |
| 154 | case OIDDigestAlg.Equal(OIDDigestAlgorithmSHA1): |
| 155 | return OIDDigestAlgorithmECDSASHA1, nil |
| 156 | case OIDDigestAlg.Equal(OIDDigestAlgorithmSHA256): |
| 157 | return OIDDigestAlgorithmECDSASHA256, nil |
| 158 | case OIDDigestAlg.Equal(OIDDigestAlgorithmSHA384): |
| 159 | return OIDDigestAlgorithmECDSASHA384, nil |
| 160 | case OIDDigestAlg.Equal(OIDDigestAlgorithmSHA512): |
| 161 | return OIDDigestAlgorithmECDSASHA512, nil |
| 162 | } |
| 163 | case *dsa.PrivateKey, *dsa.PublicKey: |
| 164 | return OIDDigestAlgorithmDSA, nil |
| 165 | case crypto.Signer: |
| 166 | // This generic case is here to cover types from other packages. It |
| 167 | // was specifically added to handle the private keyRSA type in the |
| 168 | // github.com/go-piv/piv-go/piv package. |
| 169 | return getOIDForEncryptionAlgorithm(k.Public(), OIDDigestAlg) |
| 170 | } |
| 171 | return nil, fmt.Errorf("pkcs7: cannot convert encryption algorithm to oid, unknown private key type %T", pkey) |
| 172 | |
| 173 | } |
| 174 | |
| 175 | // Parse decodes a DER encoded PKCS7 package |
| 176 | func Parse(data []byte) (p7 *PKCS7, err error) { |
| 177 | if len(data) == 0 { |
| 178 | return nil, errors.New("pkcs7: input data is empty") |
| 179 | } |
| 180 | var info contentInfo |
| 181 | der, err := ber2der(data) |
| 182 | if err != nil { |
| 183 | return nil, err |
| 184 | } |
| 185 | rest, err := asn1.Unmarshal(der, &info) |
| 186 | if len(rest) > 0 { |
| 187 | err = asn1.SyntaxError{Msg: "trailing data"} |
| 188 | return |
| 189 | } |
| 190 | if err != nil { |
| 191 | return |
| 192 | } |
| 193 | |
| 194 | // fmt.Printf("--> Content Type: %s", info.ContentType) |
| 195 | switch { |
| 196 | case info.ContentType.Equal(OIDSignedData): |
| 197 | return parseSignedData(info.Content.Bytes) |
| 198 | case info.ContentType.Equal(OIDEnvelopedData): |
| 199 | return parseEnvelopedData(info.Content.Bytes) |
| 200 | case info.ContentType.Equal(OIDEncryptedData): |
| 201 | return parseEncryptedData(info.Content.Bytes) |
| 202 | } |
| 203 | return nil, ErrUnsupportedContentType |
| 204 | } |
| 205 | |
| 206 | func parseEnvelopedData(data []byte) (*PKCS7, error) { |
| 207 | var ed envelopedData |
| 208 | if _, err := asn1.Unmarshal(data, &ed); err != nil { |
| 209 | return nil, err |
| 210 | } |
| 211 | return &PKCS7{ |
| 212 | raw: ed, |
| 213 | }, nil |
| 214 | } |
| 215 | |
| 216 | func parseEncryptedData(data []byte) (*PKCS7, error) { |
| 217 | var ed encryptedData |
| 218 | if _, err := asn1.Unmarshal(data, &ed); err != nil { |
| 219 | return nil, err |
| 220 | } |
| 221 | return &PKCS7{ |
| 222 | raw: ed, |
| 223 | }, nil |
| 224 | } |
| 225 | |
| 226 | // SetFallbackLegacyX509CertificateParserEnabled enables parsing certificates |
| 227 | // embedded in a PKCS7 message using the logic from crypto/x509 from before |
| 228 | // Go 1.23. Go 1.23 introduced a breaking change in case a certificate contains |
| 229 | // a critical authority key identifier, which is the correct thing to do based |
| 230 | // on RFC 5280, but it breaks Windows devices performing the Simple Certificate |
| 231 | // Enrolment Protocol (SCEP), as the certificates embedded in those requests |
| 232 | // apparently have authority key identifier extensions marked critical. |
| 233 | // |
| 234 | // See https://go-review.googlesource.com/c/go/+/562341 for the change in the |
| 235 | // Go source. |
| 236 | // |
| 237 | // When [SetFallbackLegacyX509CertificateParserEnabled] is called with true, it |
| 238 | // enables parsing using the legacy crypto/x509 certificate parser. It'll first |
| 239 | // try to parse the certificates using the regular Go crypto/x509 package, but |
| 240 | // if it fails on the above case, it'll retry parsing the certificates using a |
| 241 | // copy of the crypto/x509 package based on Go 1.23, but skips checking the |
| 242 | // authority key identifier extension being critical or not. |
| 243 | func SetFallbackLegacyX509CertificateParserEnabled(v bool) { |
| 244 | legacyX509CertificateParser.Lock() |
| 245 | legacyX509CertificateParser.enabled = v |
| 246 | legacyX509CertificateParser.Unlock() |
| 247 | } |
| 248 | |
| 249 | var legacyX509CertificateParser struct { |
| 250 | sync.RWMutex |
| 251 | enabled bool |
| 252 | } |
| 253 | |
| 254 | func isLegacyX509ParserEnabled() bool { |
| 255 | legacyX509CertificateParser.RLock() |
| 256 | defer legacyX509CertificateParser.RUnlock() |
| 257 | return legacyX509CertificateParser.enabled |
| 258 | } |
| 259 | |
| 260 | func (raw rawCertificates) Parse() ([]*x509.Certificate, error) { |
| 261 | if len(raw.Raw) == 0 { |
| 262 | return nil, nil |
| 263 | } |
| 264 | |
| 265 | var val asn1.RawValue |
| 266 | if _, err := asn1.Unmarshal(raw.Raw, &val); err != nil { |
| 267 | return nil, err |
| 268 | } |
| 269 | |
| 270 | certificates, err := x509.ParseCertificates(val.Bytes) |
| 271 | if err != nil && err.Error() == "x509: authority key identifier incorrectly marked critical" { |
| 272 | if isLegacyX509ParserEnabled() { |
| 273 | certificates, err = legacyx509.ParseCertificates(val.Bytes) |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return certificates, err |
| 278 | } |
| 279 | |
| 280 | func isCertMatchForIssuerAndSerial(cert *x509.Certificate, ias issuerAndSerial) bool { |
| 281 | return cert.SerialNumber.Cmp(ias.SerialNumber) == 0 && bytes.Equal(cert.RawIssuer, ias.IssuerName.FullBytes) |
| 282 | } |
| 283 | |
| 284 | // Attribute represents a key value pair attribute. Value must be marshalable byte |
| 285 | // `encoding/asn1` |
| 286 | type Attribute struct { |
| 287 | Type asn1.ObjectIdentifier |
| 288 | Value interface{} |
| 289 | } |
| 290 | |
| 291 | type attributes struct { |
| 292 | types []asn1.ObjectIdentifier |
| 293 | values []interface{} |
| 294 | } |
| 295 | |
| 296 | // Add adds the attribute, maintaining insertion order |
| 297 | func (attrs *attributes) Add(attrType asn1.ObjectIdentifier, value interface{}) { |
| 298 | attrs.types = append(attrs.types, attrType) |
| 299 | attrs.values = append(attrs.values, value) |
| 300 | } |
| 301 | |
| 302 | type sortableAttribute struct { |
| 303 | SortKey []byte |
| 304 | Attribute attribute |
| 305 | } |
| 306 | |
| 307 | type attributeSet []sortableAttribute |
| 308 | |
| 309 | func (sa attributeSet) Len() int { |
| 310 | return len(sa) |
| 311 | } |
| 312 | |
| 313 | func (sa attributeSet) Less(i, j int) bool { |
| 314 | return bytes.Compare(sa[i].SortKey, sa[j].SortKey) < 0 |
| 315 | } |
| 316 | |
| 317 | func (sa attributeSet) Swap(i, j int) { |
| 318 | sa[i], sa[j] = sa[j], sa[i] |
| 319 | } |
| 320 | |
| 321 | func (sa attributeSet) Attributes() []attribute { |
| 322 | attrs := make([]attribute, len(sa)) |
| 323 | for i, attr := range sa { |
| 324 | attrs[i] = attr.Attribute |
| 325 | } |
| 326 | return attrs |
| 327 | } |
| 328 | |
| 329 | func (attrs *attributes) ForMarshalling() ([]attribute, error) { |
| 330 | sortables := make(attributeSet, len(attrs.types)) |
| 331 | for i := range sortables { |
| 332 | attrType := attrs.types[i] |
| 333 | attrValue := attrs.values[i] |
| 334 | asn1Value, err := asn1.Marshal(attrValue) |
| 335 | if err != nil { |
| 336 | return nil, err |
| 337 | } |
| 338 | attr := attribute{ |
| 339 | Type: attrType, |
| 340 | Value: asn1.RawValue{Tag: 17, IsCompound: true, Bytes: asn1Value}, // 17 == SET tag |
| 341 | } |
| 342 | encoded, err := asn1.Marshal(attr) |
| 343 | if err != nil { |
| 344 | return nil, err |
| 345 | } |
| 346 | sortables[i] = sortableAttribute{ |
| 347 | SortKey: encoded, |
| 348 | Attribute: attr, |
| 349 | } |
| 350 | } |
| 351 | sort.Sort(sortables) |
| 352 | return sortables.Attributes(), nil |
| 353 | } |