Debian Codegraph @

encrypt.go

raw symbols SCIP
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 type envelopedDatascip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/envelopedData# struct {
19 Versionscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/envelopedData#Version. int
20 RecipientInfosscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/envelopedData#RecipientInfos. []recipientInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo# `asn1:"set"`
21 EncryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/envelopedData#EncryptedContentInfo. encryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#
22 }
23
24 type encryptedDatascip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedData# struct {
25 Versionscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedData#Version. int
26 EncryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedData#EncryptedContentInfo. encryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#
27 }
28
29 type recipientInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo# struct {
30 Versionscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#Version. int
31 IssuerAndSerialNumberscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#IssuerAndSerialNumber. issuerAndSerialscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/issuerAndSerial#
32 KeyEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#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#
33 EncryptedKeyscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#EncryptedKey. []byte
34 }
35
36 type encryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo# struct {
37 ContentTypescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#ContentType. asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.ObjectIdentifierscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#
38 ContentEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#ContentEncryptionAlgorithm. 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#
39 EncryptedContentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#EncryptedContent. asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue# `asn1:"tag:0,optional"`
40 }
41
42 const (
43 // EncryptionAlgorithmDESCBC is the DES CBC encryption algorithm
44 EncryptionAlgorithmDESCBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmDESCBC. = iota
45
46 // EncryptionAlgorithmAES128CBC is the AES 128 bits with CBC encryption algorithm
47 // Avoid this algorithm unless required for interoperability; use AES GCM instead.
48 EncryptionAlgorithmAES128CBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmAES128CBC.
49
50 // EncryptionAlgorithmAES256CBC is the AES 256 bits with CBC encryption algorithm
51 // Avoid this algorithm unless required for interoperability; use AES GCM instead.
52 EncryptionAlgorithmAES256CBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmAES256CBC.
53
54 // EncryptionAlgorithmAES128GCM is the AES 128 bits with GCM encryption algorithm
55 EncryptionAlgorithmAES128GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmAES128GCM.
56
57 // EncryptionAlgorithmAES256GCM is the AES 256 bits with GCM encryption algorithm
58 EncryptionAlgorithmAES256GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmAES256GCM.
59 )
60
61 // ContentEncryptionAlgorithm determines the algorithm used to encrypt the
62 // plaintext message. Change the value of this variable to change which
63 // algorithm is used in the Encrypt() function.
64 var ContentEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ContentEncryptionAlgorithm. = EncryptionAlgorithmDESCBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmDESCBC.
65
66 // ErrUnsupportedEncryptionAlgorithm is returned when attempting to encrypt
67 // content with an unsupported algorithm.
68 var ErrUnsupportedEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrUnsupportedEncryptionAlgorithm. = 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 encrypt content: only DES-CBC, AES-CBC, and AES-GCM supported")
69
70 // KeyEncryptionAlgorithm determines the algorithm used to encrypt a
71 // content key. Change the value of this variable to change which
72 // algorithm is used in the Encrypt() function.
73 var KeyEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/KeyEncryptionAlgorithm. = OIDEncryptionAlgorithmRSAscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmRSA.
74
75 // ErrUnsupportedKeyEncryptionAlgorithm is returned when an
76 // unsupported key encryption algorithm OID is provided.
77 var ErrUnsupportedKeyEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrUnsupportedKeyEncryptionAlgorithm. = 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 key encryption algorithm provided")
78
79 // KeyEncryptionHash determines the crypto.Hash algorithm to use
80 // when encrypting a content key. Change the value of this variable
81 // to change which algorithm is used in the Encrypt() function.
82 var KeyEncryptionHashscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/KeyEncryptionHash. = cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.SHA256scip-go gomod github.com/golang/go/src go1.14 crypto/SHA256.
83
84 // ErrUnsupportedKeyEncryptionHash is returned when an
85 // unsupported key encryption hash is provided.
86 var ErrUnsupportedKeyEncryptionHashscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrUnsupportedKeyEncryptionHash. = 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 key encryption hash provided")
87
88 // ErrPSKNotProvided is returned when attempting to encrypt
89 // using a PSK without actually providing the PSK.
90 var ErrPSKNotProvidedscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrPSKNotProvided. = 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 encrypt content: PSK not provided")
91
92 const nonceSizescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/nonceSize. = 12
93
94 type aesGCMParametersscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/aesGCMParameters# struct {
95 Noncescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/aesGCMParameters#Nonce. []byte `asn1:"tag:4"`
96 ICVLenscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/aesGCMParameters#ICVLen. int
97 }
98
99 func encryptAESGCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptAESGCM().(content []byte, key []byte) ([]byte, *encryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#, error) {
100 var keyLen int
101 var algID asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.ObjectIdentifierscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#
102 switch ContentEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ContentEncryptionAlgorithm. {
103 case EncryptionAlgorithmAES128GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmAES128GCM.:
104 keyLen = 16
105 algID = OIDEncryptionAlgorithmAES128GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmAES128GCM.
106 case EncryptionAlgorithmAES256GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmAES256GCM.:
107 keyLen = 32
108 algID = OIDEncryptionAlgorithmAES256GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmAES256GCM.
109 default:
110 return nil, nil, fmtscip-go gomod github.com/golang/go/src go1.14 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.14 fmt/Errorf().("invalid ContentEncryptionAlgorithm in encryptAESGCM: %d", ContentEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ContentEncryptionAlgorithm.)
111 }
112 if key == nil {
113 // Create AES key
114 key = make([]byte, keyLen)
115
116 _, err := randscip-go gomod github.com/golang/go/src go1.14 `crypto/rand`/.Readscip-go gomod github.com/golang/go/src go1.14 `crypto/rand`/Read().(key)
117 if err != nil {
118 return nil, nil, err
119 }
120 }
121
122 // Create nonce
123 nonce := make([]byte, nonceSizescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/nonceSize.)
124
125 _, err := randscip-go gomod github.com/golang/go/src go1.14 `crypto/rand`/.Readscip-go gomod github.com/golang/go/src go1.14 `crypto/rand`/Read().(nonce)
126 if err != nil {
127 return nil, nil, err
128 }
129
130 // Encrypt content
131 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)
132 if err != nil {
133 return nil, nil, err
134 }
135
136 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)
137 if err != nil {
138 return nil, nil, err
139 }
140
141 ciphertext := gcm.Sealscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/AEAD#Seal().(nil, nonce, content, nil)
142
143 // Prepare ASN.1 Encrypted Content Info
144 paramSeq := aesGCMParametersscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/aesGCMParameters#{
145 Noncescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/aesGCMParameters#Nonce.: nonce,
146 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().(),
147 }
148
149 paramBytes, err := asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.Marshalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/Marshal().(paramSeq)
150 if err != nil {
151 return nil, nil, err
152 }
153
154 eci := encryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#{
155 ContentTypescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#ContentType.: OIDDatascip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDData.,
156 ContentEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#ContentEncryptionAlgorithm.: 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#{
157 Algorithmscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm.: algID,
158 Parametersscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Parameters.: asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#{
159 Tagscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Tag.: asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.TagSequencescip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/TagSequence.,
160 Bytesscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Bytes.: paramBytes,
161 },
162 },
163 EncryptedContentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#EncryptedContent.: marshalEncryptedContentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/marshalEncryptedContent().(ciphertext),
164 }
165
166 return key, &eci, nil
167 }
168
169 func encryptDESCBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptDESCBC().(content []byte, key []byte) ([]byte, *encryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#, error) {
170 if key == nil {
171 // Create DES key
172 key = make([]byte, 8)
173
174 _, err := randscip-go gomod github.com/golang/go/src go1.14 `crypto/rand`/.Readscip-go gomod github.com/golang/go/src go1.14 `crypto/rand`/Read().(key)
175 if err != nil {
176 return nil, nil, err
177 }
178 }
179
180 // Create CBC IV
181 iv := make([]byte, desscip-go gomod github.com/golang/go/src go1.14 `crypto/des`/.BlockSizescip-go gomod github.com/golang/go/src go1.14 `crypto/des`/BlockSize.)
182 _, err := randscip-go gomod github.com/golang/go/src go1.14 `crypto/rand`/.Readscip-go gomod github.com/golang/go/src go1.14 `crypto/rand`/Read().(iv)
183 if err != nil {
184 return nil, nil, err
185 }
186
187 // Encrypt padded content
188 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)
189 if err != nil {
190 return nil, nil, err
191 }
192 mode := cipherscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/.NewCBCEncrypterscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/NewCBCEncrypter().(block, iv)
193 plaintext, err := padscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/pad().(content, mode.BlockSizescip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/BlockMode#BlockSize().())
194 if err != nil {
195 return nil, nil, err
196 }
197 cyphertext := make([]byte, len(plaintext))
198 mode.CryptBlocksscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/BlockMode#CryptBlocks().(cyphertext, plaintext)
199
200 // Prepare ASN.1 Encrypted Content Info
201 eci := encryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#{
202 ContentTypescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#ContentType.: OIDDatascip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDData.,
203 ContentEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#ContentEncryptionAlgorithm.: 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#{
204 Algorithmscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm.: OIDEncryptionAlgorithmDESCBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmDESCBC.,
205 Parametersscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Parameters.: asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#{Tagscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Tag.: 4, Bytesscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Bytes.: iv},
206 },
207 EncryptedContentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#EncryptedContent.: marshalEncryptedContentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/marshalEncryptedContent().(cyphertext),
208 }
209
210 return key, &eci, nil
211 }
212
213 func encryptAESCBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptAESCBC().(content []byte, key []byte) ([]byte, *encryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#, error) {
214 var keyLen int
215 var algID asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.ObjectIdentifierscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#
216 switch ContentEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ContentEncryptionAlgorithm. {
217 case EncryptionAlgorithmAES128CBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmAES128CBC.:
218 keyLen = 16
219 algID = OIDEncryptionAlgorithmAES128CBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmAES128CBC.
220 case EncryptionAlgorithmAES256CBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmAES256CBC.:
221 keyLen = 32
222 algID = OIDEncryptionAlgorithmAES256CBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptionAlgorithmAES256CBC.
223 default:
224 return nil, nil, fmtscip-go gomod github.com/golang/go/src go1.14 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.14 fmt/Errorf().("invalid ContentEncryptionAlgorithm in encryptAESCBC: %d", ContentEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ContentEncryptionAlgorithm.)
225 }
226
227 if key == nil {
228 // Create AES key
229 key = make([]byte, keyLen)
230
231 _, err := randscip-go gomod github.com/golang/go/src go1.14 `crypto/rand`/.Readscip-go gomod github.com/golang/go/src go1.14 `crypto/rand`/Read().(key)
232 if err != nil {
233 return nil, nil, err
234 }
235 }
236
237 // Create CBC IV
238 iv := make([]byte, aesscip-go gomod github.com/golang/go/src go1.14 `crypto/aes`/.BlockSizescip-go gomod github.com/golang/go/src go1.14 `crypto/aes`/BlockSize.)
239 _, err := randscip-go gomod github.com/golang/go/src go1.14 `crypto/rand`/.Readscip-go gomod github.com/golang/go/src go1.14 `crypto/rand`/Read().(iv)
240 if err != nil {
241 return nil, nil, err
242 }
243
244 // Encrypt padded content
245 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)
246 if err != nil {
247 return nil, nil, err
248 }
249 mode := cipherscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/.NewCBCEncrypterscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/NewCBCEncrypter().(block, iv)
250 plaintext, err := padscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/pad().(content, mode.BlockSizescip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/BlockMode#BlockSize().())
251 if err != nil {
252 return nil, nil, err
253 }
254 cyphertext := make([]byte, len(plaintext))
255 mode.CryptBlocksscip-go gomod github.com/golang/go/src go1.14 `crypto/cipher`/BlockMode#CryptBlocks().(cyphertext, plaintext)
256
257 // Prepare ASN.1 Encrypted Content Info
258 eci := encryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#{
259 ContentTypescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#ContentType.: OIDDatascip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDData.,
260 ContentEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#ContentEncryptionAlgorithm.: 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#{
261 Algorithmscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm.: algID,
262 Parametersscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Parameters.: asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#{Tagscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Tag.: 4, Bytesscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Bytes.: iv},
263 },
264 EncryptedContentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#EncryptedContent.: marshalEncryptedContentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/marshalEncryptedContent().(cyphertext),
265 }
266
267 return key, &eci, nil
268 }
269
270 // Encrypt creates and returns an envelope data PKCS7 structure with encrypted
271 // recipient keys for each recipient public key.
272 //
273 // The algorithm used to perform encryption is determined by the current value
274 // of the global ContentEncryptionAlgorithm package variable. By default, the
275 // value is EncryptionAlgorithmDESCBC. To use a different algorithm, change the
276 // value before calling Encrypt(). For example:
277 //
278 // ContentEncryptionAlgorithm = EncryptionAlgorithmAES256GCM
279 //
280 // TODO(fullsailor): Add support for encrypting content with other algorithms
281 func Encryptscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/Encrypt().(content []byte, recipients []*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#) ([]byte, error) {
282 var eci *encryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#
283 var key []byte
284 var err error
285
286 // Apply chosen symmetric encryption method
287 switch ContentEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ContentEncryptionAlgorithm. {
288 case EncryptionAlgorithmDESCBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmDESCBC.:
289 key, eci, err = encryptDESCBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptDESCBC().(content, nil)
290 case EncryptionAlgorithmAES128CBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmAES128CBC.:
291 fallthrough
292 case EncryptionAlgorithmAES256CBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmAES256CBC.:
293 key, eci, err = encryptAESCBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptAESCBC().(content, nil)
294 case EncryptionAlgorithmAES128GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmAES128GCM.:
295 fallthrough
296 case EncryptionAlgorithmAES256GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmAES256GCM.:
297 key, eci, err = encryptAESGCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptAESGCM().(content, nil)
298
299 default:
300 return nil, ErrUnsupportedEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrUnsupportedEncryptionAlgorithm.
301 }
302
303 if err != nil {
304 return nil, err
305 }
306
307 // Prepare each recipient's encrypted cipher key
308 recipientInfos := make([]recipientInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#, len(recipients))
309 for i, recipient := range recipients {
310 algorithm := KeyEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/KeyEncryptionAlgorithm.
311 hash := KeyEncryptionHashscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/KeyEncryptionHash.
312 var kea 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#
313 switch {
314 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.):
315 parameters, err := getParametersForKeyEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/getParametersForKeyEncryptionAlgorithm().(algorithm, hash)
316 if err != nil {
317 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().("failed to get parameters for key encryption: %v", err)
318 }
319 kea = 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#{
320 Algorithmscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm.: algorithm,
321 Parametersscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Parameters.: parameters,
322 }
323 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.):
324 kea = 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#{
325 Algorithmscip-go gomod github.com/golang/go/src go1.14 `crypto/x509/pkix`/AlgorithmIdentifier#Algorithm.: algorithm,
326 }
327 default:
328 return nil, ErrUnsupportedKeyEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrUnsupportedKeyEncryptionAlgorithm.
329 }
330 encrypted, err := encryptKeyscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptKey().(key, recipient, algorithm, hash)
331 if err != nil {
332 return nil, err
333 }
334 ias, err := cert2issuerAndSerialscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/cert2issuerAndSerial().(recipient)
335 if err != nil {
336 return nil, err
337 }
338 info := recipientInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#{
339 Versionscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#Version.: 0,
340 IssuerAndSerialNumberscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#IssuerAndSerialNumber.: ias,
341 KeyEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#KeyEncryptionAlgorithm.: kea,
342 EncryptedKeyscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/recipientInfo#EncryptedKey.: encrypted,
343 }
344 recipientInfos[i] = info
345 }
346
347 // Prepare envelope content
348 envelope := envelopedDatascip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/envelopedData#{
349 EncryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/envelopedData#EncryptedContentInfo.: *eci,
350 Versionscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/envelopedData#Version.: 0,
351 RecipientInfosscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/envelopedData#RecipientInfos.: recipientInfos,
352 }
353 innerContent, err := asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.Marshalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/Marshal().(envelope)
354 if err != nil {
355 return nil, err
356 }
357
358 // Prepare outer payload structure
359 wrapper := contentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/contentInfo#{
360 ContentTypescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/contentInfo#ContentType.: OIDEnvelopedDatascip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEnvelopedData.,
361 Contentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/contentInfo#Content.: asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#{Classscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Class.: 2, Tagscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Tag.: 0, IsCompoundscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#IsCompound.: true, Bytesscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Bytes.: innerContent},
362 }
363
364 return asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.Marshalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/Marshal().(wrapper)
365 }
366
367 func getParametersForKeyEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/getParametersForKeyEncryptionAlgorithm().(algorithm asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.ObjectIdentifierscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#, hash cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.Hashscip-go gomod github.com/golang/go/src go1.14 crypto/Hash#) (asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#, error) {
368 if !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.) {
369 return asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#{}, nil // return empty; not used
370 }
371
372 params := rsaOAEPAlgorithmParametersscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/rsaOAEPAlgorithmParameters#{}
373 switch hash {
374 case cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.SHA1scip-go gomod github.com/golang/go/src go1.14 crypto/SHA1.:
375 params.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.}
376 case cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.SHA224scip-go gomod github.com/golang/go/src go1.14 crypto/SHA224.:
377 params.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.: OIDDigestAlgorithmSHA224scip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDDigestAlgorithmSHA224.}
378 case cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.SHA256scip-go gomod github.com/golang/go/src go1.14 crypto/SHA256.:
379 params.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.: OIDDigestAlgorithmSHA256scip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDDigestAlgorithmSHA256.}
380 case cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.SHA384scip-go gomod github.com/golang/go/src go1.14 crypto/SHA384.:
381 params.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.: OIDDigestAlgorithmSHA384scip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDDigestAlgorithmSHA384.}
382 case cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.SHA512scip-go gomod github.com/golang/go/src go1.14 crypto/SHA512.:
383 params.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.: OIDDigestAlgorithmSHA512scip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDDigestAlgorithmSHA512.}
384 default:
385 return asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#{}, ErrUnsupportedAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrUnsupportedAlgorithm.
386 }
387
388 b, err := asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.Marshalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/Marshal().(params)
389 if err != nil {
390 return asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#{}, fmtscip-go gomod github.com/golang/go/src go1.14 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.14 fmt/Errorf().("failed marshaling key encryption parameters: %v", err)
391 }
392
393 return asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#{
394 FullBytesscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#FullBytes.: b,
395 }, nil
396 }
397
398 // EncryptUsingPSK creates and returns an encrypted data PKCS7 structure,
399 // encrypted using caller provided pre-shared secret.
400 func EncryptUsingPSKscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptUsingPSK().(content []byte, key []byte) ([]byte, error) {
401 var eci *encryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedContentInfo#
402 var err error
403
404 if key == nil {
405 return nil, ErrPSKNotProvidedscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrPSKNotProvided.
406 }
407
408 // Apply chosen symmetric encryption method
409 switch ContentEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ContentEncryptionAlgorithm. {
410 case EncryptionAlgorithmDESCBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmDESCBC.:
411 _, eci, err = encryptDESCBCscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptDESCBC().(content, key)
412
413 case EncryptionAlgorithmAES128GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmAES128GCM.:
414 fallthrough
415 case EncryptionAlgorithmAES256GCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/EncryptionAlgorithmAES256GCM.:
416 _, eci, err = encryptAESGCMscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptAESGCM().(content, key)
417
418 default:
419 return nil, ErrUnsupportedEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrUnsupportedEncryptionAlgorithm.
420 }
421
422 if err != nil {
423 return nil, err
424 }
425
426 // Prepare encrypted-data content
427 ed := encryptedDatascip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedData#{
428 Versionscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedData#Version.: 0,
429 EncryptedContentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptedData#EncryptedContentInfo.: *eci,
430 }
431 innerContent, err := asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.Marshalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/Marshal().(ed)
432 if err != nil {
433 return nil, err
434 }
435
436 // Prepare outer payload structure
437 wrapper := contentInfoscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/contentInfo#{
438 ContentTypescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/contentInfo#ContentType.: OIDEncryptedDatascip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/OIDEncryptedData.,
439 Contentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/contentInfo#Content.: asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#{Classscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Class.: 2, Tagscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Tag.: 0, IsCompoundscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#IsCompound.: true, Bytesscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Bytes.: innerContent},
440 }
441
442 return asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.Marshalscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/Marshal().(wrapper)
443 }
444
445 func marshalEncryptedContentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/marshalEncryptedContent().(content []byte) asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue# {
446 return asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#{Bytesscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Bytes.: content, Classscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#Class.: 2, IsCompoundscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/RawValue#IsCompound.: false}
447 }
448
449 func encryptKeyscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encryptKey().(key []byte, recipient *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#, algorithm asn1scip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/.ObjectIdentifierscip-go gomod github.com/golang/go/src go1.14 `encoding/asn1`/ObjectIdentifier#, hash cryptoscip-go gomod github.com/golang/go/src go1.14 crypto/.Hashscip-go gomod github.com/golang/go/src go1.14 crypto/Hash#) ([]byte, error) {
450 pub, ok := recipient.PublicKeyscip-go gomod github.com/golang/go/src go1.14 `crypto/x509`/Certificate#PublicKey..(*rsascip-go gomod github.com/golang/go/src go1.14 `crypto/rsa`/.PublicKeyscip-go gomod github.com/golang/go/src go1.14 `crypto/rsa`/PublicKey#)
451 if !ok {
452 return nil, ErrUnsupportedKeyTypescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrUnsupportedKeyType.
453 }
454
455 switch {
456 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.):
457 return rsascip-go gomod github.com/golang/go/src go1.14 `crypto/rsa`/.EncryptPKCS1v15scip-go gomod github.com/golang/go/src go1.14 `crypto/rsa`/EncryptPKCS1v15().(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., pub, key)
458 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.):
459 return rsascip-go gomod github.com/golang/go/src go1.14 `crypto/rsa`/.EncryptOAEPscip-go gomod github.com/golang/go/src go1.14 `crypto/rsa`/EncryptOAEP().(hash.Newscip-go gomod github.com/golang/go/src go1.14 crypto/Hash#New().(), 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., pub, key, nil)
460 default:
461 return nil, ErrUnsupportedKeyEncryptionAlgorithmscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ErrUnsupportedKeyEncryptionAlgorithm.
462 }
463 }
464
465 func padscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/pad().(data []byte, blocklen int) ([]byte, error) {
466 if blocklen < 1 {
467 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().("invalid blocklen %d", blocklen)
468 }
469 padlen := blocklen - (len(data) % blocklen)
470 if padlen == 0 {
471 padlen = blocklen
472 }
473 pad := bytesscip-go gomod github.com/golang/go/src go1.14 bytes/.Repeatscip-go gomod github.com/golang/go/src go1.14 bytes/Repeat().([]byte{byte(padlen)}, padlen)
474 return append(data, pad...), nil
475 }