| 1 | package pkcs7scip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.14 bytes/" |
| 5 | "cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/" |
| 6 | "crypto/aesscip-go gomod github.com/golang/go/src go1.14 `crypto/aes`/" |
| 7 | "crypto/cipherscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/" |
| 8 | "crypto/desscip-go gomod github.com/golang/go/src go1.14 `crypto/des`/" |
| 9 | "crypto/randscip-go gomod github.com/golang/go/src go1.14 `crypto/rand`/" |
| 10 | "crypto/rsascip-go gomod github.com/golang/go/src go1.14 `crypto/rsa`/" |
| 11 | "crypto/x509scip-go gomod github.com/golang/go/src go1.14 `crypto/x509`/" |
| 12 | "crypto/x509/pkixscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/" |
| 13 | "encoding/asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/" |
| 14 | "errorsscip-go gomod github.com/golang/go/src go1.14 errors/" |
| 15 | "fmtscip-go gomod github.com/golang/go/src go1.14 fmt/" |
| 16 | ) |
| 17 | |
| 18 | // ErrUnsupportedAlgorithm tells you when our quick dev assumptions have failed |
| 19 | var ErrUnsupportedAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrUnsupportedAlgorithm. = errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("pkcs7: cannot decrypt data: only RSA, DES, DES-EDE3, AES-256-CBC and AES-128-GCM supported") |
| 20 | |
| 21 | // ErrUnsupportedAsymmetricEncryptionAlgorithm is returned when attempting to use an unknown asymmetric encryption algorithm |
| 22 | var ErrUnsupportedAsymmetricEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrUnsupportedAsymmetricEncryptionAlgorithm. = errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("pkcs7: cannot decrypt data: only RSA PKCS#1 v1.5 and RSA OAEP are supported") |
| 23 | |
| 24 | // ErrUnsupportedKeyType is returned when attempting to encrypting keys using a key that's not an RSA key |
| 25 | var ErrUnsupportedKeyTypescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrUnsupportedKeyType. = errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("pkcs7: only RSA keys are supported") |
| 26 | |
| 27 | // ErrNotEncryptedContent is returned when attempting to Decrypt data that is not encrypted data |
| 28 | var ErrNotEncryptedContentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrNotEncryptedContent. = errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("pkcs7: content data is a decryptable data type") |
| 29 | |
| 30 | // Decrypt decrypts encrypted content info for recipient cert and private key |
| 31 | func (p7 *PKCS7scip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/PKCS7#) Decryptscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/PKCS7#Decrypt().(cert *x509scip-go gomod github.com/golang/go/src go1.14 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.14 `crypto/x509`/Certificate#, pkey cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.PrivateKeyscip-go gomod github.com/golang/go/src go1.14 crypto/PrivateKey#) ([]byte, error) { |
| 32 | data, ok := p7.rawscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/PKCS7#raw..(envelopedDatascip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/envelopedData#) |
| 33 | if !ok { |
| 34 | return nil, ErrNotEncryptedContentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrNotEncryptedContent. |
| 35 | } |
| 36 | recipient := selectRecipientForCertificatescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/selectRecipientForCertificate().(data.RecipientInfosscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/envelopedData#RecipientInfos., cert) |
| 37 | if recipient.EncryptedKeyscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#EncryptedKey. == nil { |
| 38 | return nil, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("pkcs7: no enveloped recipient for provided certificate") |
| 39 | } |
| 40 | switch pkey := pkey.(type) { |
| 41 | case cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.Decrypterscip-go gomod github.com/golang/go/src go1.14 crypto/Decrypter#: |
| 42 | var opts cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.DecrypterOptsscip-go gomod github.com/golang/go/src go1.14 crypto/DecrypterOpts# |
| 43 | switch algorithm := recipient.KeyEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#KeyEncryptionAlgorithm..Algorithmscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm.; { |
| 44 | case algorithm.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmRSAESOAEPscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmRSAESOAEP.): |
| 45 | hashFunc, err := getHashFuncForKeyEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/getHashFuncForKeyEncryptionAlgorithm().(recipient.KeyEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#KeyEncryptionAlgorithm.) |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | opts = &rsascip-go gomod github.com/golang/go/src go1.14 `crypto/rsa`/.OAEPOptionsscip-go gomod github.com/golang/go/src go1.14 `crypto/rsa`/OAEPOptions#{Hashscip-go gomod github.com/golang/go/src go1.14 `crypto/rsa`/OAEPOptions#Hash.: hashFunc} |
| 50 | case algorithm.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmRSAscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmRSA.): |
| 51 | opts = &rsascip-go gomod github.com/golang/go/src go1.14 `crypto/rsa`/.PKCS1v15DecryptOptionsscip-go gomod github.com/golang/go/src go1.14 `crypto/rsa`/PKCS1v15DecryptOptions#{} |
| 52 | default: |
| 53 | return nil, ErrUnsupportedAsymmetricEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrUnsupportedAsymmetricEncryptionAlgorithm. |
| 54 | } |
| 55 | contentKey, err := pkey.Decryptscip-go gomod github.com/golang/go/src go1.14 crypto/Decrypter#Decrypt().(randscip-go gomod github.com/golang/go/src go1.14 `crypto/rand`/.Readerscip-go gomod github.com/golang/go/src go1.14 `crypto/rand`/Reader., recipient.EncryptedKeyscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#EncryptedKey., opts) |
| 56 | if err != nil { |
| 57 | return nil, err |
| 58 | } |
| 59 | return data.EncryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/envelopedData#EncryptedContentInfo..decryptscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#decrypt().(contentKey) |
| 60 | } |
| 61 | return nil, ErrUnsupportedAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrUnsupportedAlgorithm. |
| 62 | } |
| 63 | |
| 64 | // RFC 4055, 4.1 |
| 65 | // The current ASN.1 parser does not support non-integer defaults so the 'default:' tags here do nothing. |
| 66 | type rsaOAEPAlgorithmParametersscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/rsaOAEPAlgorithmParameters# struct { |
| 67 | HashFuncscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/rsaOAEPAlgorithmParameters#HashFunc. pkixscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/.AlgorithmIdentifierscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier# `asn1:"optional,explicit,tag:0,default:sha1Identifier"` |
| 68 | MaskGenFuncscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/rsaOAEPAlgorithmParameters#MaskGenFunc. pkixscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/.AlgorithmIdentifierscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier# `asn1:"optional,explicit,tag:1,default:mgf1SHA1Identifier"` |
| 69 | PSourceFuncscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/rsaOAEPAlgorithmParameters#PSourceFunc. pkixscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/.AlgorithmIdentifierscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier# `asn1:"optional,explicit,tag:2,default:pSpecifiedEmptyIdentifier"` |
| 70 | } |
| 71 | |
| 72 | func getHashFuncForKeyEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/getHashFuncForKeyEncryptionAlgorithm().(keyEncryptionAlgorithm pkixscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/.AlgorithmIdentifierscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#) (cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.Hashscip-go gomod github.com/golang/go/src go1.14 crypto/Hash#, error) { |
| 73 | invalidHashFunc := cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.Hashscip-go gomod github.com/golang/go/src go1.14 crypto/Hash#(0) |
| 74 | params := &rsaOAEPAlgorithmParametersscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/rsaOAEPAlgorithmParameters#{ |
| 75 | HashFuncscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/rsaOAEPAlgorithmParameters#HashFunc.: pkixscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/.AlgorithmIdentifierscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#{Algorithmscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm.: OIDDigestAlgorithmSHA1scip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDDigestAlgorithmSHA1.}, // set default hash algorithm to SHA1 |
| 76 | } |
| 77 | var rest []byte |
| 78 | rest, err := asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.Unmarshalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/Unmarshal().(keyEncryptionAlgorithm.Parametersscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Parameters..FullBytesscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#FullBytes., params) |
| 79 | if err != nil { |
| 80 | return invalidHashFunc, fmtscip-go gomod github.com/golang/go/src go1.14 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.14 fmt/Errorf().("pkcs7: failed unmarshaling key encryption algorithm parameters: %v", err) |
| 81 | } |
| 82 | if len(rest) != 0 { |
| 83 | return invalidHashFunc, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("pkcs7: trailing data after RSA OAEP parameters") |
| 84 | } |
| 85 | |
| 86 | switch { |
| 87 | case params.HashFuncscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/rsaOAEPAlgorithmParameters#HashFunc..Algorithmscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm..Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDDigestAlgorithmSHA1scip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDDigestAlgorithmSHA1.): |
| 88 | return cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.SHA1scip-go gomod github.com/golang/go/src go1.14 crypto/SHA1., nil |
| 89 | case params.HashFuncscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/rsaOAEPAlgorithmParameters#HashFunc..Algorithmscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm..Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDDigestAlgorithmSHA224scip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDDigestAlgorithmSHA224.): |
| 90 | return cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.SHA224scip-go gomod github.com/golang/go/src go1.14 crypto/SHA224., nil |
| 91 | case params.HashFuncscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/rsaOAEPAlgorithmParameters#HashFunc..Algorithmscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm..Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDDigestAlgorithmSHA256scip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDDigestAlgorithmSHA256.): |
| 92 | return cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.SHA256scip-go gomod github.com/golang/go/src go1.14 crypto/SHA256., nil |
| 93 | case params.HashFuncscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/rsaOAEPAlgorithmParameters#HashFunc..Algorithmscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm..Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDDigestAlgorithmSHA384scip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDDigestAlgorithmSHA384.): |
| 94 | return cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.SHA384scip-go gomod github.com/golang/go/src go1.14 crypto/SHA384., nil |
| 95 | case params.HashFuncscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/rsaOAEPAlgorithmParameters#HashFunc..Algorithmscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm..Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDDigestAlgorithmSHA512scip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDDigestAlgorithmSHA512.): |
| 96 | return cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.SHA512scip-go gomod github.com/golang/go/src go1.14 crypto/SHA512., nil |
| 97 | default: |
| 98 | return invalidHashFunc, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("pkcs7: unsupported hash function for RSA OAEP") |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // DecryptUsingPSK decrypts encrypted data using caller provided |
| 103 | // pre-shared secret |
| 104 | func (p7 *PKCS7scip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/PKCS7#) DecryptUsingPSKscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/PKCS7#DecryptUsingPSK().(key []byte) ([]byte, error) { |
| 105 | data, ok := p7.rawscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/PKCS7#raw..(encryptedDatascip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedData#) |
| 106 | if !ok { |
| 107 | return nil, ErrNotEncryptedContentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrNotEncryptedContent. |
| 108 | } |
| 109 | return data.EncryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedData#EncryptedContentInfo..decryptscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#decrypt().(key) |
| 110 | } |
| 111 | |
| 112 | func (eci encryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#) decryptscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#decrypt().(key []byte) ([]byte, error) { |
| 113 | alg := eci.ContentEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#ContentEncryptionAlgorithm..Algorithmscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm. |
| 114 | if !alg.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmDESCBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmDESCBC.) && |
| 115 | !alg.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmDESEDE3CBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmDESEDE3CBC.) && |
| 116 | !alg.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmAES256CBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmAES256CBC.) && |
| 117 | !alg.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmAES128CBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmAES128CBC.) && |
| 118 | !alg.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmAES128GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmAES128GCM.) && |
| 119 | !alg.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmAES256GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmAES256GCM.) { |
| 120 | return nil, ErrUnsupportedAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrUnsupportedAlgorithm. |
| 121 | } |
| 122 | |
| 123 | // EncryptedContent can either be constructed of multple OCTET STRINGs |
| 124 | // or _be_ a tagged OCTET STRING |
| 125 | var cyphertext []byte |
| 126 | if eci.EncryptedContentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#EncryptedContent..IsCompoundscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#IsCompound. { |
| 127 | // Complex case to concat all of the children OCTET STRINGs |
| 128 | var buf bytesscip-go gomod github.com/golang/go/src go1.14 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.14 bytes/Buffer# |
| 129 | cypherbytes := eci.EncryptedContentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#EncryptedContent..Bytesscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Bytes. |
| 130 | for { |
| 131 | var part []byte |
| 132 | cypherbytes, _ = asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.Unmarshalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/Unmarshal().(cypherbytes, &part) |
| 133 | buf.Writescip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#Write().(part) |
| 134 | if cypherbytes == nil { |
| 135 | break |
| 136 | } |
| 137 | } |
| 138 | cyphertext = buf.Bytesscip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#Bytes().() |
| 139 | } else { |
| 140 | // Simple case, the bytes _are_ the cyphertext |
| 141 | cyphertext = eci.EncryptedContentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#EncryptedContent..Bytesscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Bytes. |
| 142 | } |
| 143 | |
| 144 | var block cipherscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/.Blockscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/Block# |
| 145 | var err error |
| 146 | |
| 147 | switch { |
| 148 | case alg.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmDESCBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmDESCBC.): |
| 149 | block, err = desscip-go gomod github.com/golang/go/src go1.14 `crypto/des`/.NewCipherscip-go gomod github.com/golang/go/src go1.14 `crypto/des`/NewCipher().(key) |
| 150 | case alg.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmDESEDE3CBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmDESEDE3CBC.): |
| 151 | block, err = desscip-go gomod github.com/golang/go/src go1.14 `crypto/des`/.NewTripleDESCipherscip-go gomod github.com/golang/go/src go1.14 `crypto/des`/NewTripleDESCipher().(key) |
| 152 | case alg.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmAES256CBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmAES256CBC.), alg.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmAES256GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmAES256GCM.): |
| 153 | fallthrough |
| 154 | case alg.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmAES128GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmAES128GCM.), alg.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmAES128CBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmAES128CBC.): |
| 155 | block, err = aesscip-go gomod github.com/golang/go/src go1.14 `crypto/aes`/.NewCipherscip-go gomod github.com/golang/go/src go1.14 `crypto/aes`/NewCipher().(key) |
| 156 | } |
| 157 | |
| 158 | if err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | |
| 162 | if alg.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmAES128GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmAES128GCM.) || alg.Equalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#Equal().(OIDEncryptionAlgorithmAES256GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmAES256GCM.) { |
| 163 | params := aesGCMParametersscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/aesGCMParameters#{} |
| 164 | paramBytes := eci.ContentEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#ContentEncryptionAlgorithm..Parametersscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Parameters..Bytesscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Bytes. |
| 165 | |
| 166 | _, err := asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.Unmarshalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/Unmarshal().(paramBytes, ¶ms) |
| 167 | if err != nil { |
| 168 | return nil, err |
| 169 | } |
| 170 | |
| 171 | gcm, err := cipherscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/.NewGCMscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/NewGCM().(block) |
| 172 | if err != nil { |
| 173 | return nil, err |
| 174 | } |
| 175 | |
| 176 | if len(params.Noncescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/aesGCMParameters#Nonce.) != gcm.NonceSizescip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/AEAD#NonceSize().() { |
| 177 | return nil, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("pkcs7: encryption algorithm parameters are incorrect") |
| 178 | } |
| 179 | if params.ICVLenscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/aesGCMParameters#ICVLen. != gcm.Overheadscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/AEAD#Overhead().() { |
| 180 | return nil, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("pkcs7: encryption algorithm parameters are incorrect") |
| 181 | } |
| 182 | |
| 183 | plaintext, err := gcm.Openscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/AEAD#Open().(nil, params.Noncescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/aesGCMParameters#Nonce., cyphertext, nil) |
| 184 | if err != nil { |
| 185 | return nil, err |
| 186 | } |
| 187 | |
| 188 | return plaintext, nil |
| 189 | } |
| 190 | |
| 191 | iv := eci.ContentEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#ContentEncryptionAlgorithm..Parametersscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Parameters..Bytesscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Bytes. |
| 192 | if len(iv) != block.BlockSizescip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/Block#BlockSize().() { |
| 193 | return nil, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("pkcs7: encryption algorithm parameters are malformed") |
| 194 | } |
| 195 | mode := cipherscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/.NewCBCDecrypterscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/NewCBCDecrypter().(block, iv) |
| 196 | plaintext := make([]byte, len(cyphertext)) |
| 197 | mode.CryptBlocksscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/BlockMode#CryptBlocks().(plaintext, cyphertext) |
| 198 | if plaintext, err = unpadscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/unpad().(plaintext, mode.BlockSizescip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/BlockMode#BlockSize().()); err != nil { |
| 199 | return nil, err |
| 200 | } |
| 201 | return plaintext, nil |
| 202 | } |
| 203 | |
| 204 | func unpadscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/unpad().(data []byte, blocklen int) ([]byte, error) { |
| 205 | if blocklen < 1 { |
| 206 | return nil, fmtscip-go gomod github.com/golang/go/src go1.14 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.14 fmt/Errorf().("pkcs7: invalid blocklen %d", blocklen) |
| 207 | } |
| 208 | if len(data)%blocklen != 0 || len(data) == 0 { |
| 209 | return nil, fmtscip-go gomod github.com/golang/go/src go1.14 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.14 fmt/Errorf().("pkcs7: invalid data len %d", len(data)) |
| 210 | } |
| 211 | |
| 212 | // the last byte is the length of padding |
| 213 | padlen := int(data[len(data)-1]) |
| 214 | |
| 215 | // check padding integrity, all bytes should be the same |
| 216 | pad := data[len(data)-padlen:] |
| 217 | for _, padbyte := range pad { |
| 218 | if padbyte != byte(padlen) { |
| 219 | return nil, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("pkcs7: invalid padding") |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return data[:len(data)-padlen], nil |
| 224 | } |
| 225 | |
| 226 | func selectRecipientForCertificatescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/selectRecipientForCertificate().(recipients []recipientInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#, cert *x509scip-go gomod github.com/golang/go/src go1.14 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.14 `crypto/x509`/Certificate#) recipientInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo# { |
| 227 | for _, recp := range recipients { |
| 228 | if isCertMatchForIssuerAndSerialscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/isCertMatchForIssuerAndSerial().(cert, recp.IssuerAndSerialNumberscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#IssuerAndSerialNumber.) { |
| 229 | return recp |
| 230 | } |
| 231 | } |
| 232 | return recipientInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#{} |
| 233 | } |