/src/openssl/crypto/cms/cms_env.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2008-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include "internal/cryptlib.h" |
11 | | #include <openssl/asn1t.h> |
12 | | #include <openssl/pem.h> |
13 | | #include <openssl/x509v3.h> |
14 | | #include <openssl/err.h> |
15 | | #include <openssl/cms.h> |
16 | | #include <openssl/aes.h> |
17 | | #include "cms_lcl.h" |
18 | | #include "internal/asn1_int.h" |
19 | | #include "internal/evp_int.h" |
20 | | |
21 | | /* CMS EnvelopedData Utilities */ |
22 | | |
23 | | CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms) |
24 | 0 | { |
25 | 0 | if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) { |
26 | 0 | CMSerr(CMS_F_CMS_GET0_ENVELOPED, |
27 | 0 | CMS_R_CONTENT_TYPE_NOT_ENVELOPED_DATA); |
28 | 0 | return NULL; |
29 | 0 | } |
30 | 0 | return cms->d.envelopedData; |
31 | 0 | } |
32 | | |
33 | | static CMS_EnvelopedData *cms_enveloped_data_init(CMS_ContentInfo *cms) |
34 | 0 | { |
35 | 0 | if (cms->d.other == NULL) { |
36 | 0 | cms->d.envelopedData = M_ASN1_new_of(CMS_EnvelopedData); |
37 | 0 | if (!cms->d.envelopedData) { |
38 | 0 | CMSerr(CMS_F_CMS_ENVELOPED_DATA_INIT, ERR_R_MALLOC_FAILURE); |
39 | 0 | return NULL; |
40 | 0 | } |
41 | 0 | cms->d.envelopedData->version = 0; |
42 | 0 | cms->d.envelopedData->encryptedContentInfo->contentType = |
43 | 0 | OBJ_nid2obj(NID_pkcs7_data); |
44 | 0 | ASN1_OBJECT_free(cms->contentType); |
45 | 0 | cms->contentType = OBJ_nid2obj(NID_pkcs7_enveloped); |
46 | 0 | return cms->d.envelopedData; |
47 | 0 | } |
48 | 0 | return cms_get0_enveloped(cms); |
49 | 0 | } |
50 | | |
51 | | int cms_env_asn1_ctrl(CMS_RecipientInfo *ri, int cmd) |
52 | 0 | { |
53 | 0 | EVP_PKEY *pkey; |
54 | 0 | int i; |
55 | 0 | if (ri->type == CMS_RECIPINFO_TRANS) |
56 | 0 | pkey = ri->d.ktri->pkey; |
57 | 0 | else if (ri->type == CMS_RECIPINFO_AGREE) { |
58 | 0 | EVP_PKEY_CTX *pctx = ri->d.kari->pctx; |
59 | 0 | if (!pctx) |
60 | 0 | return 0; |
61 | 0 | pkey = EVP_PKEY_CTX_get0_pkey(pctx); |
62 | 0 | if (!pkey) |
63 | 0 | return 0; |
64 | 0 | } else |
65 | 0 | return 0; |
66 | 0 | if (!pkey->ameth || !pkey->ameth->pkey_ctrl) |
67 | 0 | return 1; |
68 | 0 | i = pkey->ameth->pkey_ctrl(pkey, ASN1_PKEY_CTRL_CMS_ENVELOPE, cmd, ri); |
69 | 0 | if (i == -2) { |
70 | 0 | CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, |
71 | 0 | CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); |
72 | 0 | return 0; |
73 | 0 | } |
74 | 0 | if (i <= 0) { |
75 | 0 | CMSerr(CMS_F_CMS_ENV_ASN1_CTRL, CMS_R_CTRL_FAILURE); |
76 | 0 | return 0; |
77 | 0 | } |
78 | 0 | return 1; |
79 | 0 | } |
80 | | |
81 | | STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms) |
82 | 0 | { |
83 | 0 | CMS_EnvelopedData *env; |
84 | 0 | env = cms_get0_enveloped(cms); |
85 | 0 | if (!env) |
86 | 0 | return NULL; |
87 | 0 | return env->recipientInfos; |
88 | 0 | } |
89 | | |
90 | | int CMS_RecipientInfo_type(CMS_RecipientInfo *ri) |
91 | 0 | { |
92 | 0 | return ri->type; |
93 | 0 | } |
94 | | |
95 | | EVP_PKEY_CTX *CMS_RecipientInfo_get0_pkey_ctx(CMS_RecipientInfo *ri) |
96 | 0 | { |
97 | 0 | if (ri->type == CMS_RECIPINFO_TRANS) |
98 | 0 | return ri->d.ktri->pctx; |
99 | 0 | else if (ri->type == CMS_RECIPINFO_AGREE) |
100 | 0 | return ri->d.kari->pctx; |
101 | 0 | return NULL; |
102 | 0 | } |
103 | | |
104 | | CMS_ContentInfo *CMS_EnvelopedData_create(const EVP_CIPHER *cipher) |
105 | 0 | { |
106 | 0 | CMS_ContentInfo *cms; |
107 | 0 | CMS_EnvelopedData *env; |
108 | 0 | cms = CMS_ContentInfo_new(); |
109 | 0 | if (cms == NULL) |
110 | 0 | goto merr; |
111 | 0 | env = cms_enveloped_data_init(cms); |
112 | 0 | if (env == NULL) |
113 | 0 | goto merr; |
114 | 0 | if (!cms_EncryptedContent_init(env->encryptedContentInfo, |
115 | 0 | cipher, NULL, 0)) |
116 | 0 | goto merr; |
117 | 0 | return cms; |
118 | 0 | merr: |
119 | 0 | CMS_ContentInfo_free(cms); |
120 | 0 | CMSerr(CMS_F_CMS_ENVELOPEDDATA_CREATE, ERR_R_MALLOC_FAILURE); |
121 | 0 | return NULL; |
122 | 0 | } |
123 | | |
124 | | /* Key Transport Recipient Info (KTRI) routines */ |
125 | | |
126 | | /* Initialise a ktri based on passed certificate and key */ |
127 | | |
128 | | static int cms_RecipientInfo_ktri_init(CMS_RecipientInfo *ri, X509 *recip, |
129 | | EVP_PKEY *pk, unsigned int flags) |
130 | 0 | { |
131 | 0 | CMS_KeyTransRecipientInfo *ktri; |
132 | 0 | int idtype; |
133 | 0 |
|
134 | 0 | ri->d.ktri = M_ASN1_new_of(CMS_KeyTransRecipientInfo); |
135 | 0 | if (!ri->d.ktri) |
136 | 0 | return 0; |
137 | 0 | ri->type = CMS_RECIPINFO_TRANS; |
138 | 0 |
|
139 | 0 | ktri = ri->d.ktri; |
140 | 0 |
|
141 | 0 | if (flags & CMS_USE_KEYID) { |
142 | 0 | ktri->version = 2; |
143 | 0 | idtype = CMS_RECIPINFO_KEYIDENTIFIER; |
144 | 0 | } else { |
145 | 0 | ktri->version = 0; |
146 | 0 | idtype = CMS_RECIPINFO_ISSUER_SERIAL; |
147 | 0 | } |
148 | 0 |
|
149 | 0 | /* |
150 | 0 | * Not a typo: RecipientIdentifier and SignerIdentifier are the same |
151 | 0 | * structure. |
152 | 0 | */ |
153 | 0 |
|
154 | 0 | if (!cms_set1_SignerIdentifier(ktri->rid, recip, idtype)) |
155 | 0 | return 0; |
156 | 0 | |
157 | 0 | X509_up_ref(recip); |
158 | 0 | EVP_PKEY_up_ref(pk); |
159 | 0 |
|
160 | 0 | ktri->pkey = pk; |
161 | 0 | ktri->recip = recip; |
162 | 0 |
|
163 | 0 | if (flags & CMS_KEY_PARAM) { |
164 | 0 | ktri->pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL); |
165 | 0 | if (ktri->pctx == NULL) |
166 | 0 | return 0; |
167 | 0 | if (EVP_PKEY_encrypt_init(ktri->pctx) <= 0) |
168 | 0 | return 0; |
169 | 0 | } else if (!cms_env_asn1_ctrl(ri, 0)) |
170 | 0 | return 0; |
171 | 0 | return 1; |
172 | 0 | } |
173 | | |
174 | | /* |
175 | | * Add a recipient certificate using appropriate type of RecipientInfo |
176 | | */ |
177 | | |
178 | | CMS_RecipientInfo *CMS_add1_recipient_cert(CMS_ContentInfo *cms, |
179 | | X509 *recip, unsigned int flags) |
180 | 0 | { |
181 | 0 | CMS_RecipientInfo *ri = NULL; |
182 | 0 | CMS_EnvelopedData *env; |
183 | 0 | EVP_PKEY *pk = NULL; |
184 | 0 | env = cms_get0_enveloped(cms); |
185 | 0 | if (!env) |
186 | 0 | goto err; |
187 | 0 | |
188 | 0 | /* Initialize recipient info */ |
189 | 0 | ri = M_ASN1_new_of(CMS_RecipientInfo); |
190 | 0 | if (!ri) |
191 | 0 | goto merr; |
192 | 0 | |
193 | 0 | pk = X509_get0_pubkey(recip); |
194 | 0 | if (!pk) { |
195 | 0 | CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, CMS_R_ERROR_GETTING_PUBLIC_KEY); |
196 | 0 | goto err; |
197 | 0 | } |
198 | 0 |
|
199 | 0 | switch (cms_pkey_get_ri_type(pk)) { |
200 | 0 |
|
201 | 0 | case CMS_RECIPINFO_TRANS: |
202 | 0 | if (!cms_RecipientInfo_ktri_init(ri, recip, pk, flags)) |
203 | 0 | goto err; |
204 | 0 | break; |
205 | 0 |
|
206 | 0 | case CMS_RECIPINFO_AGREE: |
207 | 0 | if (!cms_RecipientInfo_kari_init(ri, recip, pk, flags)) |
208 | 0 | goto err; |
209 | 0 | break; |
210 | 0 |
|
211 | 0 | default: |
212 | 0 | CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, |
213 | 0 | CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE); |
214 | 0 | goto err; |
215 | 0 |
|
216 | 0 | } |
217 | 0 |
|
218 | 0 | if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri)) |
219 | 0 | goto merr; |
220 | 0 | |
221 | 0 | return ri; |
222 | 0 | |
223 | 0 | merr: |
224 | 0 | CMSerr(CMS_F_CMS_ADD1_RECIPIENT_CERT, ERR_R_MALLOC_FAILURE); |
225 | 0 | err: |
226 | 0 | M_ASN1_free_of(ri, CMS_RecipientInfo); |
227 | 0 | return NULL; |
228 | 0 |
|
229 | 0 | } |
230 | | |
231 | | int CMS_RecipientInfo_ktri_get0_algs(CMS_RecipientInfo *ri, |
232 | | EVP_PKEY **pk, X509 **recip, |
233 | | X509_ALGOR **palg) |
234 | 0 | { |
235 | 0 | CMS_KeyTransRecipientInfo *ktri; |
236 | 0 | if (ri->type != CMS_RECIPINFO_TRANS) { |
237 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS, |
238 | 0 | CMS_R_NOT_KEY_TRANSPORT); |
239 | 0 | return 0; |
240 | 0 | } |
241 | 0 |
|
242 | 0 | ktri = ri->d.ktri; |
243 | 0 |
|
244 | 0 | if (pk) |
245 | 0 | *pk = ktri->pkey; |
246 | 0 | if (recip) |
247 | 0 | *recip = ktri->recip; |
248 | 0 | if (palg) |
249 | 0 | *palg = ktri->keyEncryptionAlgorithm; |
250 | 0 | return 1; |
251 | 0 | } |
252 | | |
253 | | int CMS_RecipientInfo_ktri_get0_signer_id(CMS_RecipientInfo *ri, |
254 | | ASN1_OCTET_STRING **keyid, |
255 | | X509_NAME **issuer, |
256 | | ASN1_INTEGER **sno) |
257 | 0 | { |
258 | 0 | CMS_KeyTransRecipientInfo *ktri; |
259 | 0 | if (ri->type != CMS_RECIPINFO_TRANS) { |
260 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID, |
261 | 0 | CMS_R_NOT_KEY_TRANSPORT); |
262 | 0 | return 0; |
263 | 0 | } |
264 | 0 | ktri = ri->d.ktri; |
265 | 0 |
|
266 | 0 | return cms_SignerIdentifier_get0_signer_id(ktri->rid, keyid, issuer, sno); |
267 | 0 | } |
268 | | |
269 | | int CMS_RecipientInfo_ktri_cert_cmp(CMS_RecipientInfo *ri, X509 *cert) |
270 | 0 | { |
271 | 0 | if (ri->type != CMS_RECIPINFO_TRANS) { |
272 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_CERT_CMP, |
273 | 0 | CMS_R_NOT_KEY_TRANSPORT); |
274 | 0 | return -2; |
275 | 0 | } |
276 | 0 | return cms_SignerIdentifier_cert_cmp(ri->d.ktri->rid, cert); |
277 | 0 | } |
278 | | |
279 | | int CMS_RecipientInfo_set0_pkey(CMS_RecipientInfo *ri, EVP_PKEY *pkey) |
280 | 0 | { |
281 | 0 | if (ri->type != CMS_RECIPINFO_TRANS) { |
282 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY, CMS_R_NOT_KEY_TRANSPORT); |
283 | 0 | return 0; |
284 | 0 | } |
285 | 0 | EVP_PKEY_free(ri->d.ktri->pkey); |
286 | 0 | ri->d.ktri->pkey = pkey; |
287 | 0 | return 1; |
288 | 0 | } |
289 | | |
290 | | /* Encrypt content key in key transport recipient info */ |
291 | | |
292 | | static int cms_RecipientInfo_ktri_encrypt(CMS_ContentInfo *cms, |
293 | | CMS_RecipientInfo *ri) |
294 | 0 | { |
295 | 0 | CMS_KeyTransRecipientInfo *ktri; |
296 | 0 | CMS_EncryptedContentInfo *ec; |
297 | 0 | EVP_PKEY_CTX *pctx; |
298 | 0 | unsigned char *ek = NULL; |
299 | 0 | size_t eklen; |
300 | 0 |
|
301 | 0 | int ret = 0; |
302 | 0 |
|
303 | 0 | if (ri->type != CMS_RECIPINFO_TRANS) { |
304 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_NOT_KEY_TRANSPORT); |
305 | 0 | return 0; |
306 | 0 | } |
307 | 0 | ktri = ri->d.ktri; |
308 | 0 | ec = cms->d.envelopedData->encryptedContentInfo; |
309 | 0 |
|
310 | 0 | pctx = ktri->pctx; |
311 | 0 |
|
312 | 0 | if (pctx) { |
313 | 0 | if (!cms_env_asn1_ctrl(ri, 0)) |
314 | 0 | goto err; |
315 | 0 | } else { |
316 | 0 | pctx = EVP_PKEY_CTX_new(ktri->pkey, NULL); |
317 | 0 | if (pctx == NULL) |
318 | 0 | return 0; |
319 | 0 | |
320 | 0 | if (EVP_PKEY_encrypt_init(pctx) <= 0) |
321 | 0 | goto err; |
322 | 0 | } |
323 | 0 | |
324 | 0 | if (EVP_PKEY_CTX_ctrl(pctx, -1, EVP_PKEY_OP_ENCRYPT, |
325 | 0 | EVP_PKEY_CTRL_CMS_ENCRYPT, 0, ri) <= 0) { |
326 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, CMS_R_CTRL_ERROR); |
327 | 0 | goto err; |
328 | 0 | } |
329 | 0 |
|
330 | 0 | if (EVP_PKEY_encrypt(pctx, NULL, &eklen, ec->key, ec->keylen) <= 0) |
331 | 0 | goto err; |
332 | 0 | |
333 | 0 | ek = OPENSSL_malloc(eklen); |
334 | 0 |
|
335 | 0 | if (ek == NULL) { |
336 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT, ERR_R_MALLOC_FAILURE); |
337 | 0 | goto err; |
338 | 0 | } |
339 | 0 |
|
340 | 0 | if (EVP_PKEY_encrypt(pctx, ek, &eklen, ec->key, ec->keylen) <= 0) |
341 | 0 | goto err; |
342 | 0 | |
343 | 0 | ASN1_STRING_set0(ktri->encryptedKey, ek, eklen); |
344 | 0 | ek = NULL; |
345 | 0 |
|
346 | 0 | ret = 1; |
347 | 0 |
|
348 | 0 | err: |
349 | 0 | EVP_PKEY_CTX_free(pctx); |
350 | 0 | ktri->pctx = NULL; |
351 | 0 | OPENSSL_free(ek); |
352 | 0 | return ret; |
353 | 0 |
|
354 | 0 | } |
355 | | |
356 | | /* Decrypt content key from KTRI */ |
357 | | |
358 | | static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms, |
359 | | CMS_RecipientInfo *ri) |
360 | 0 | { |
361 | 0 | CMS_KeyTransRecipientInfo *ktri = ri->d.ktri; |
362 | 0 | EVP_PKEY *pkey = ktri->pkey; |
363 | 0 | unsigned char *ek = NULL; |
364 | 0 | size_t eklen; |
365 | 0 | int ret = 0; |
366 | 0 | CMS_EncryptedContentInfo *ec; |
367 | 0 | ec = cms->d.envelopedData->encryptedContentInfo; |
368 | 0 |
|
369 | 0 | if (ktri->pkey == NULL) { |
370 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_NO_PRIVATE_KEY); |
371 | 0 | return 0; |
372 | 0 | } |
373 | 0 |
|
374 | 0 | ktri->pctx = EVP_PKEY_CTX_new(pkey, NULL); |
375 | 0 | if (ktri->pctx == NULL) |
376 | 0 | return 0; |
377 | 0 | |
378 | 0 | if (EVP_PKEY_decrypt_init(ktri->pctx) <= 0) |
379 | 0 | goto err; |
380 | 0 | |
381 | 0 | if (!cms_env_asn1_ctrl(ri, 1)) |
382 | 0 | goto err; |
383 | 0 | |
384 | 0 | if (EVP_PKEY_CTX_ctrl(ktri->pctx, -1, EVP_PKEY_OP_DECRYPT, |
385 | 0 | EVP_PKEY_CTRL_CMS_DECRYPT, 0, ri) <= 0) { |
386 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CTRL_ERROR); |
387 | 0 | goto err; |
388 | 0 | } |
389 | 0 |
|
390 | 0 | if (EVP_PKEY_decrypt(ktri->pctx, NULL, &eklen, |
391 | 0 | ktri->encryptedKey->data, |
392 | 0 | ktri->encryptedKey->length) <= 0) |
393 | 0 | goto err; |
394 | 0 | |
395 | 0 | ek = OPENSSL_malloc(eklen); |
396 | 0 |
|
397 | 0 | if (ek == NULL) { |
398 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, ERR_R_MALLOC_FAILURE); |
399 | 0 | goto err; |
400 | 0 | } |
401 | 0 |
|
402 | 0 | if (EVP_PKEY_decrypt(ktri->pctx, ek, &eklen, |
403 | 0 | ktri->encryptedKey->data, |
404 | 0 | ktri->encryptedKey->length) <= 0) { |
405 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KTRI_DECRYPT, CMS_R_CMS_LIB); |
406 | 0 | goto err; |
407 | 0 | } |
408 | 0 |
|
409 | 0 | ret = 1; |
410 | 0 |
|
411 | 0 | OPENSSL_clear_free(ec->key, ec->keylen); |
412 | 0 | ec->key = ek; |
413 | 0 | ec->keylen = eklen; |
414 | 0 |
|
415 | 0 | err: |
416 | 0 | EVP_PKEY_CTX_free(ktri->pctx); |
417 | 0 | ktri->pctx = NULL; |
418 | 0 | if (!ret) |
419 | 0 | OPENSSL_free(ek); |
420 | 0 |
|
421 | 0 | return ret; |
422 | 0 | } |
423 | | |
424 | | /* Key Encrypted Key (KEK) RecipientInfo routines */ |
425 | | |
426 | | int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri, |
427 | | const unsigned char *id, size_t idlen) |
428 | 0 | { |
429 | 0 | ASN1_OCTET_STRING tmp_os; |
430 | 0 | CMS_KEKRecipientInfo *kekri; |
431 | 0 | if (ri->type != CMS_RECIPINFO_KEK) { |
432 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ID_CMP, CMS_R_NOT_KEK); |
433 | 0 | return -2; |
434 | 0 | } |
435 | 0 | kekri = ri->d.kekri; |
436 | 0 | tmp_os.type = V_ASN1_OCTET_STRING; |
437 | 0 | tmp_os.flags = 0; |
438 | 0 | tmp_os.data = (unsigned char *)id; |
439 | 0 | tmp_os.length = (int)idlen; |
440 | 0 | return ASN1_OCTET_STRING_cmp(&tmp_os, kekri->kekid->keyIdentifier); |
441 | 0 | } |
442 | | |
443 | | /* For now hard code AES key wrap info */ |
444 | | |
445 | | static size_t aes_wrap_keylen(int nid) |
446 | 0 | { |
447 | 0 | switch (nid) { |
448 | 0 | case NID_id_aes128_wrap: |
449 | 0 | return 16; |
450 | 0 |
|
451 | 0 | case NID_id_aes192_wrap: |
452 | 0 | return 24; |
453 | 0 |
|
454 | 0 | case NID_id_aes256_wrap: |
455 | 0 | return 32; |
456 | 0 |
|
457 | 0 | default: |
458 | 0 | return 0; |
459 | 0 | } |
460 | 0 | } |
461 | | |
462 | | CMS_RecipientInfo *CMS_add0_recipient_key(CMS_ContentInfo *cms, int nid, |
463 | | unsigned char *key, size_t keylen, |
464 | | unsigned char *id, size_t idlen, |
465 | | ASN1_GENERALIZEDTIME *date, |
466 | | ASN1_OBJECT *otherTypeId, |
467 | | ASN1_TYPE *otherType) |
468 | 0 | { |
469 | 0 | CMS_RecipientInfo *ri = NULL; |
470 | 0 | CMS_EnvelopedData *env; |
471 | 0 | CMS_KEKRecipientInfo *kekri; |
472 | 0 | env = cms_get0_enveloped(cms); |
473 | 0 | if (!env) |
474 | 0 | goto err; |
475 | 0 | |
476 | 0 | if (nid == NID_undef) { |
477 | 0 | switch (keylen) { |
478 | 0 | case 16: |
479 | 0 | nid = NID_id_aes128_wrap; |
480 | 0 | break; |
481 | 0 |
|
482 | 0 | case 24: |
483 | 0 | nid = NID_id_aes192_wrap; |
484 | 0 | break; |
485 | 0 |
|
486 | 0 | case 32: |
487 | 0 | nid = NID_id_aes256_wrap; |
488 | 0 | break; |
489 | 0 |
|
490 | 0 | default: |
491 | 0 | CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH); |
492 | 0 | goto err; |
493 | 0 | } |
494 | 0 |
|
495 | 0 | } else { |
496 | 0 |
|
497 | 0 | size_t exp_keylen = aes_wrap_keylen(nid); |
498 | 0 |
|
499 | 0 | if (!exp_keylen) { |
500 | 0 | CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, |
501 | 0 | CMS_R_UNSUPPORTED_KEK_ALGORITHM); |
502 | 0 | goto err; |
503 | 0 | } |
504 | 0 |
|
505 | 0 | if (keylen != exp_keylen) { |
506 | 0 | CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, CMS_R_INVALID_KEY_LENGTH); |
507 | 0 | goto err; |
508 | 0 | } |
509 | 0 |
|
510 | 0 | } |
511 | 0 |
|
512 | 0 | /* Initialize recipient info */ |
513 | 0 | ri = M_ASN1_new_of(CMS_RecipientInfo); |
514 | 0 | if (!ri) |
515 | 0 | goto merr; |
516 | 0 | |
517 | 0 | ri->d.kekri = M_ASN1_new_of(CMS_KEKRecipientInfo); |
518 | 0 | if (!ri->d.kekri) |
519 | 0 | goto merr; |
520 | 0 | ri->type = CMS_RECIPINFO_KEK; |
521 | 0 |
|
522 | 0 | kekri = ri->d.kekri; |
523 | 0 |
|
524 | 0 | if (otherTypeId) { |
525 | 0 | kekri->kekid->other = M_ASN1_new_of(CMS_OtherKeyAttribute); |
526 | 0 | if (kekri->kekid->other == NULL) |
527 | 0 | goto merr; |
528 | 0 | } |
529 | 0 | |
530 | 0 | if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri)) |
531 | 0 | goto merr; |
532 | 0 | |
533 | 0 | /* After this point no calls can fail */ |
534 | 0 | |
535 | 0 | kekri->version = 4; |
536 | 0 |
|
537 | 0 | kekri->key = key; |
538 | 0 | kekri->keylen = keylen; |
539 | 0 |
|
540 | 0 | ASN1_STRING_set0(kekri->kekid->keyIdentifier, id, idlen); |
541 | 0 |
|
542 | 0 | kekri->kekid->date = date; |
543 | 0 |
|
544 | 0 | if (kekri->kekid->other) { |
545 | 0 | kekri->kekid->other->keyAttrId = otherTypeId; |
546 | 0 | kekri->kekid->other->keyAttr = otherType; |
547 | 0 | } |
548 | 0 |
|
549 | 0 | X509_ALGOR_set0(kekri->keyEncryptionAlgorithm, |
550 | 0 | OBJ_nid2obj(nid), V_ASN1_UNDEF, NULL); |
551 | 0 |
|
552 | 0 | return ri; |
553 | 0 |
|
554 | 0 | merr: |
555 | 0 | CMSerr(CMS_F_CMS_ADD0_RECIPIENT_KEY, ERR_R_MALLOC_FAILURE); |
556 | 0 | err: |
557 | 0 | M_ASN1_free_of(ri, CMS_RecipientInfo); |
558 | 0 | return NULL; |
559 | 0 |
|
560 | 0 | } |
561 | | |
562 | | int CMS_RecipientInfo_kekri_get0_id(CMS_RecipientInfo *ri, |
563 | | X509_ALGOR **palg, |
564 | | ASN1_OCTET_STRING **pid, |
565 | | ASN1_GENERALIZEDTIME **pdate, |
566 | | ASN1_OBJECT **potherid, |
567 | | ASN1_TYPE **pothertype) |
568 | 0 | { |
569 | 0 | CMS_KEKIdentifier *rkid; |
570 | 0 | if (ri->type != CMS_RECIPINFO_KEK) { |
571 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_GET0_ID, CMS_R_NOT_KEK); |
572 | 0 | return 0; |
573 | 0 | } |
574 | 0 | rkid = ri->d.kekri->kekid; |
575 | 0 | if (palg) |
576 | 0 | *palg = ri->d.kekri->keyEncryptionAlgorithm; |
577 | 0 | if (pid) |
578 | 0 | *pid = rkid->keyIdentifier; |
579 | 0 | if (pdate) |
580 | 0 | *pdate = rkid->date; |
581 | 0 | if (potherid) { |
582 | 0 | if (rkid->other) |
583 | 0 | *potherid = rkid->other->keyAttrId; |
584 | 0 | else |
585 | 0 | *potherid = NULL; |
586 | 0 | } |
587 | 0 | if (pothertype) { |
588 | 0 | if (rkid->other) |
589 | 0 | *pothertype = rkid->other->keyAttr; |
590 | 0 | else |
591 | 0 | *pothertype = NULL; |
592 | 0 | } |
593 | 0 | return 1; |
594 | 0 | } |
595 | | |
596 | | int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri, |
597 | | unsigned char *key, size_t keylen) |
598 | 0 | { |
599 | 0 | CMS_KEKRecipientInfo *kekri; |
600 | 0 | if (ri->type != CMS_RECIPINFO_KEK) { |
601 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_KEY, CMS_R_NOT_KEK); |
602 | 0 | return 0; |
603 | 0 | } |
604 | 0 |
|
605 | 0 | kekri = ri->d.kekri; |
606 | 0 | kekri->key = key; |
607 | 0 | kekri->keylen = keylen; |
608 | 0 | return 1; |
609 | 0 | } |
610 | | |
611 | | /* Encrypt content key in KEK recipient info */ |
612 | | |
613 | | static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms, |
614 | | CMS_RecipientInfo *ri) |
615 | 0 | { |
616 | 0 | CMS_EncryptedContentInfo *ec; |
617 | 0 | CMS_KEKRecipientInfo *kekri; |
618 | 0 | AES_KEY actx; |
619 | 0 | unsigned char *wkey = NULL; |
620 | 0 | int wkeylen; |
621 | 0 | int r = 0; |
622 | 0 |
|
623 | 0 | ec = cms->d.envelopedData->encryptedContentInfo; |
624 | 0 |
|
625 | 0 | kekri = ri->d.kekri; |
626 | 0 |
|
627 | 0 | if (!kekri->key) { |
628 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_KEY); |
629 | 0 | return 0; |
630 | 0 | } |
631 | 0 |
|
632 | 0 | if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx)) { |
633 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, |
634 | 0 | CMS_R_ERROR_SETTING_KEY); |
635 | 0 | goto err; |
636 | 0 | } |
637 | 0 |
|
638 | 0 | wkey = OPENSSL_malloc(ec->keylen + 8); |
639 | 0 |
|
640 | 0 | if (wkey == NULL) { |
641 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, ERR_R_MALLOC_FAILURE); |
642 | 0 | goto err; |
643 | 0 | } |
644 | 0 |
|
645 | 0 | wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen); |
646 | 0 |
|
647 | 0 | if (wkeylen <= 0) { |
648 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR); |
649 | 0 | goto err; |
650 | 0 | } |
651 | 0 |
|
652 | 0 | ASN1_STRING_set0(kekri->encryptedKey, wkey, wkeylen); |
653 | 0 |
|
654 | 0 | r = 1; |
655 | 0 |
|
656 | 0 | err: |
657 | 0 |
|
658 | 0 | if (!r) |
659 | 0 | OPENSSL_free(wkey); |
660 | 0 | OPENSSL_cleanse(&actx, sizeof(actx)); |
661 | 0 |
|
662 | 0 | return r; |
663 | 0 |
|
664 | 0 | } |
665 | | |
666 | | /* Decrypt content key in KEK recipient info */ |
667 | | |
668 | | static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms, |
669 | | CMS_RecipientInfo *ri) |
670 | 0 | { |
671 | 0 | CMS_EncryptedContentInfo *ec; |
672 | 0 | CMS_KEKRecipientInfo *kekri; |
673 | 0 | AES_KEY actx; |
674 | 0 | unsigned char *ukey = NULL; |
675 | 0 | int ukeylen; |
676 | 0 | int r = 0, wrap_nid; |
677 | 0 |
|
678 | 0 | ec = cms->d.envelopedData->encryptedContentInfo; |
679 | 0 |
|
680 | 0 | kekri = ri->d.kekri; |
681 | 0 |
|
682 | 0 | if (!kekri->key) { |
683 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_KEY); |
684 | 0 | return 0; |
685 | 0 | } |
686 | 0 |
|
687 | 0 | wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm); |
688 | 0 | if (aes_wrap_keylen(wrap_nid) != kekri->keylen) { |
689 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, |
690 | 0 | CMS_R_INVALID_KEY_LENGTH); |
691 | 0 | return 0; |
692 | 0 | } |
693 | 0 |
|
694 | 0 | /* If encrypted key length is invalid don't bother */ |
695 | 0 |
|
696 | 0 | if (kekri->encryptedKey->length < 16) { |
697 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, |
698 | 0 | CMS_R_INVALID_ENCRYPTED_KEY_LENGTH); |
699 | 0 | goto err; |
700 | 0 | } |
701 | 0 |
|
702 | 0 | if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx)) { |
703 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, |
704 | 0 | CMS_R_ERROR_SETTING_KEY); |
705 | 0 | goto err; |
706 | 0 | } |
707 | 0 |
|
708 | 0 | ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8); |
709 | 0 |
|
710 | 0 | if (ukey == NULL) { |
711 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, ERR_R_MALLOC_FAILURE); |
712 | 0 | goto err; |
713 | 0 | } |
714 | 0 |
|
715 | 0 | ukeylen = AES_unwrap_key(&actx, NULL, ukey, |
716 | 0 | kekri->encryptedKey->data, |
717 | 0 | kekri->encryptedKey->length); |
718 | 0 |
|
719 | 0 | if (ukeylen <= 0) { |
720 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR); |
721 | 0 | goto err; |
722 | 0 | } |
723 | 0 |
|
724 | 0 | ec->key = ukey; |
725 | 0 | ec->keylen = ukeylen; |
726 | 0 |
|
727 | 0 | r = 1; |
728 | 0 |
|
729 | 0 | err: |
730 | 0 |
|
731 | 0 | if (!r) |
732 | 0 | OPENSSL_free(ukey); |
733 | 0 | OPENSSL_cleanse(&actx, sizeof(actx)); |
734 | 0 |
|
735 | 0 | return r; |
736 | 0 |
|
737 | 0 | } |
738 | | |
739 | | int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri) |
740 | 0 | { |
741 | 0 | switch (ri->type) { |
742 | 0 | case CMS_RECIPINFO_TRANS: |
743 | 0 | return cms_RecipientInfo_ktri_decrypt(cms, ri); |
744 | 0 |
|
745 | 0 | case CMS_RECIPINFO_KEK: |
746 | 0 | return cms_RecipientInfo_kekri_decrypt(cms, ri); |
747 | 0 |
|
748 | 0 | case CMS_RECIPINFO_PASS: |
749 | 0 | return cms_RecipientInfo_pwri_crypt(cms, ri, 0); |
750 | 0 |
|
751 | 0 | default: |
752 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT, |
753 | 0 | CMS_R_UNSUPPORTED_RECIPIENTINFO_TYPE); |
754 | 0 | return 0; |
755 | 0 | } |
756 | 0 | } |
757 | | |
758 | | int CMS_RecipientInfo_encrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri) |
759 | 0 | { |
760 | 0 | switch (ri->type) { |
761 | 0 | case CMS_RECIPINFO_TRANS: |
762 | 0 | return cms_RecipientInfo_ktri_encrypt(cms, ri); |
763 | 0 |
|
764 | 0 | case CMS_RECIPINFO_AGREE: |
765 | 0 | return cms_RecipientInfo_kari_encrypt(cms, ri); |
766 | 0 |
|
767 | 0 | case CMS_RECIPINFO_KEK: |
768 | 0 | return cms_RecipientInfo_kekri_encrypt(cms, ri); |
769 | 0 |
|
770 | 0 | case CMS_RECIPINFO_PASS: |
771 | 0 | return cms_RecipientInfo_pwri_crypt(cms, ri, 1); |
772 | 0 |
|
773 | 0 | default: |
774 | 0 | CMSerr(CMS_F_CMS_RECIPIENTINFO_ENCRYPT, |
775 | 0 | CMS_R_UNSUPPORTED_RECIPIENT_TYPE); |
776 | 0 | return 0; |
777 | 0 | } |
778 | 0 | } |
779 | | |
780 | | /* Check structures and fixup version numbers (if necessary) */ |
781 | | |
782 | | static void cms_env_set_originfo_version(CMS_EnvelopedData *env) |
783 | 0 | { |
784 | 0 | CMS_OriginatorInfo *org = env->originatorInfo; |
785 | 0 | int i; |
786 | 0 | if (org == NULL) |
787 | 0 | return; |
788 | 0 | for (i = 0; i < sk_CMS_CertificateChoices_num(org->certificates); i++) { |
789 | 0 | CMS_CertificateChoices *cch; |
790 | 0 | cch = sk_CMS_CertificateChoices_value(org->certificates, i); |
791 | 0 | if (cch->type == CMS_CERTCHOICE_OTHER) { |
792 | 0 | env->version = 4; |
793 | 0 | return; |
794 | 0 | } else if (cch->type == CMS_CERTCHOICE_V2ACERT) { |
795 | 0 | if (env->version < 3) |
796 | 0 | env->version = 3; |
797 | 0 | } |
798 | 0 | } |
799 | 0 |
|
800 | 0 | for (i = 0; i < sk_CMS_RevocationInfoChoice_num(org->crls); i++) { |
801 | 0 | CMS_RevocationInfoChoice *rch; |
802 | 0 | rch = sk_CMS_RevocationInfoChoice_value(org->crls, i); |
803 | 0 | if (rch->type == CMS_REVCHOICE_OTHER) { |
804 | 0 | env->version = 4; |
805 | 0 | return; |
806 | 0 | } |
807 | 0 | } |
808 | 0 | } |
809 | | |
810 | | static void cms_env_set_version(CMS_EnvelopedData *env) |
811 | 0 | { |
812 | 0 | int i; |
813 | 0 | CMS_RecipientInfo *ri; |
814 | 0 |
|
815 | 0 | /* |
816 | 0 | * Can't set version higher than 4 so if 4 or more already nothing to do. |
817 | 0 | */ |
818 | 0 | if (env->version >= 4) |
819 | 0 | return; |
820 | 0 | |
821 | 0 | cms_env_set_originfo_version(env); |
822 | 0 |
|
823 | 0 | if (env->version >= 3) |
824 | 0 | return; |
825 | 0 | |
826 | 0 | for (i = 0; i < sk_CMS_RecipientInfo_num(env->recipientInfos); i++) { |
827 | 0 | ri = sk_CMS_RecipientInfo_value(env->recipientInfos, i); |
828 | 0 | if (ri->type == CMS_RECIPINFO_PASS || ri->type == CMS_RECIPINFO_OTHER) { |
829 | 0 | env->version = 3; |
830 | 0 | return; |
831 | 0 | } else if (ri->type != CMS_RECIPINFO_TRANS |
832 | 0 | || ri->d.ktri->version != 0) { |
833 | 0 | env->version = 2; |
834 | 0 | } |
835 | 0 | } |
836 | 0 | if (env->originatorInfo || env->unprotectedAttrs) |
837 | 0 | env->version = 2; |
838 | 0 | if (env->version == 2) |
839 | 0 | return; |
840 | 0 | env->version = 0; |
841 | 0 | } |
842 | | |
843 | | BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms) |
844 | 0 | { |
845 | 0 | CMS_EncryptedContentInfo *ec; |
846 | 0 | STACK_OF(CMS_RecipientInfo) *rinfos; |
847 | 0 | CMS_RecipientInfo *ri; |
848 | 0 | int i, ok = 0; |
849 | 0 | BIO *ret; |
850 | 0 |
|
851 | 0 | /* Get BIO first to set up key */ |
852 | 0 |
|
853 | 0 | ec = cms->d.envelopedData->encryptedContentInfo; |
854 | 0 | ret = cms_EncryptedContent_init_bio(ec); |
855 | 0 |
|
856 | 0 | /* If error or no cipher end of processing */ |
857 | 0 |
|
858 | 0 | if (!ret || !ec->cipher) |
859 | 0 | return ret; |
860 | 0 | |
861 | 0 | /* Now encrypt content key according to each RecipientInfo type */ |
862 | 0 | |
863 | 0 | rinfos = cms->d.envelopedData->recipientInfos; |
864 | 0 |
|
865 | 0 | for (i = 0; i < sk_CMS_RecipientInfo_num(rinfos); i++) { |
866 | 0 | ri = sk_CMS_RecipientInfo_value(rinfos, i); |
867 | 0 | if (CMS_RecipientInfo_encrypt(cms, ri) <= 0) { |
868 | 0 | CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO, |
869 | 0 | CMS_R_ERROR_SETTING_RECIPIENTINFO); |
870 | 0 | goto err; |
871 | 0 | } |
872 | 0 | } |
873 | 0 | cms_env_set_version(cms->d.envelopedData); |
874 | 0 |
|
875 | 0 | ok = 1; |
876 | 0 |
|
877 | 0 | err: |
878 | 0 | ec->cipher = NULL; |
879 | 0 | OPENSSL_clear_free(ec->key, ec->keylen); |
880 | 0 | ec->key = NULL; |
881 | 0 | ec->keylen = 0; |
882 | 0 | if (ok) |
883 | 0 | return ret; |
884 | 0 | BIO_free(ret); |
885 | 0 | return NULL; |
886 | 0 |
|
887 | 0 | } |
888 | | |
889 | | /* |
890 | | * Get RecipientInfo type (if any) supported by a key (public or private). To |
891 | | * retain compatibility with previous behaviour if the ctrl value isn't |
892 | | * supported we assume key transport. |
893 | | */ |
894 | | int cms_pkey_get_ri_type(EVP_PKEY *pk) |
895 | 0 | { |
896 | 0 | if (pk->ameth && pk->ameth->pkey_ctrl) { |
897 | 0 | int i, r; |
898 | 0 | i = pk->ameth->pkey_ctrl(pk, ASN1_PKEY_CTRL_CMS_RI_TYPE, 0, &r); |
899 | 0 | if (i > 0) |
900 | 0 | return r; |
901 | 0 | } |
902 | 0 | return CMS_RECIPINFO_TRANS; |
903 | 0 | } |