summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/cms
diff options
context:
space:
mode:
authordjm <>2012-10-13 21:23:50 +0000
committerdjm <>2012-10-13 21:23:50 +0000
commite9d65189905c6e99c1062d65e26bf83eebb0a26a (patch)
tree10ebe51c3542099b0ab8325d8f322372375dc3b4 /src/lib/libcrypto/cms
parent59625e84c89bf82e1c6d20c55785b618eb56ea72 (diff)
parent228cae30b117c2493f69ad3c195341cd6ec8d430 (diff)
downloadopenbsd-e9d65189905c6e99c1062d65e26bf83eebb0a26a.tar.gz
openbsd-e9d65189905c6e99c1062d65e26bf83eebb0a26a.tar.bz2
openbsd-e9d65189905c6e99c1062d65e26bf83eebb0a26a.zip
This commit was generated by cvs2git to track changes on a CVS vendor
branch.
Diffstat (limited to 'src/lib/libcrypto/cms')
-rw-r--r--src/lib/libcrypto/cms/cms.h22
-rw-r--r--src/lib/libcrypto/cms/cms_asn1.c9
-rw-r--r--src/lib/libcrypto/cms/cms_enc.c60
-rw-r--r--src/lib/libcrypto/cms/cms_env.c22
-rw-r--r--src/lib/libcrypto/cms/cms_err.c13
-rw-r--r--src/lib/libcrypto/cms/cms_lcl.h12
-rw-r--r--src/lib/libcrypto/cms/cms_lib.c3
-rw-r--r--src/lib/libcrypto/cms/cms_pwri.c454
-rw-r--r--src/lib/libcrypto/cms/cms_sd.c3
9 files changed, 575 insertions, 23 deletions
diff --git a/src/lib/libcrypto/cms/cms.h b/src/lib/libcrypto/cms/cms.h
index 09c45d0412..36994fa6a2 100644
--- a/src/lib/libcrypto/cms/cms.h
+++ b/src/lib/libcrypto/cms/cms.h
@@ -111,6 +111,7 @@ DECLARE_ASN1_PRINT_FUNCTION(CMS_ContentInfo)
111#define CMS_PARTIAL 0x4000 111#define CMS_PARTIAL 0x4000
112#define CMS_REUSE_DIGEST 0x8000 112#define CMS_REUSE_DIGEST 0x8000
113#define CMS_USE_KEYID 0x10000 113#define CMS_USE_KEYID 0x10000
114#define CMS_DEBUG_DECRYPT 0x20000
114 115
115const ASN1_OBJECT *CMS_get0_type(CMS_ContentInfo *cms); 116const ASN1_OBJECT *CMS_get0_type(CMS_ContentInfo *cms);
116 117
@@ -184,6 +185,8 @@ int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert);
184int CMS_decrypt_set1_key(CMS_ContentInfo *cms, 185int CMS_decrypt_set1_key(CMS_ContentInfo *cms,
185 unsigned char *key, size_t keylen, 186 unsigned char *key, size_t keylen,
186 unsigned char *id, size_t idlen); 187 unsigned char *id, size_t idlen);
188int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
189 unsigned char *pass, ossl_ssize_t passlen);
187 190
188STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms); 191STACK_OF(CMS_RecipientInfo) *CMS_get0_RecipientInfos(CMS_ContentInfo *cms);
189int CMS_RecipientInfo_type(CMS_RecipientInfo *ri); 192int CMS_RecipientInfo_type(CMS_RecipientInfo *ri);
@@ -219,6 +222,16 @@ int CMS_RecipientInfo_set0_key(CMS_RecipientInfo *ri,
219int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri, 222int CMS_RecipientInfo_kekri_id_cmp(CMS_RecipientInfo *ri,
220 const unsigned char *id, size_t idlen); 223 const unsigned char *id, size_t idlen);
221 224
225int CMS_RecipientInfo_set0_password(CMS_RecipientInfo *ri,
226 unsigned char *pass,
227 ossl_ssize_t passlen);
228
229CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
230 int iter, int wrap_nid, int pbe_nid,
231 unsigned char *pass,
232 ossl_ssize_t passlen,
233 const EVP_CIPHER *kekciph);
234
222int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri); 235int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri);
223 236
224int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out, 237int CMS_uncompress(CMS_ContentInfo *cms, BIO *dcont, BIO *out,
@@ -330,6 +343,7 @@ void ERR_load_CMS_strings(void);
330#define CMS_F_CHECK_CONTENT 99 343#define CMS_F_CHECK_CONTENT 99
331#define CMS_F_CMS_ADD0_CERT 164 344#define CMS_F_CMS_ADD0_CERT 164
332#define CMS_F_CMS_ADD0_RECIPIENT_KEY 100 345#define CMS_F_CMS_ADD0_RECIPIENT_KEY 100
346#define CMS_F_CMS_ADD0_RECIPIENT_PASSWORD 165
333#define CMS_F_CMS_ADD1_RECEIPTREQUEST 158 347#define CMS_F_CMS_ADD1_RECEIPTREQUEST 158
334#define CMS_F_CMS_ADD1_RECIPIENT_CERT 101 348#define CMS_F_CMS_ADD1_RECIPIENT_CERT 101
335#define CMS_F_CMS_ADD1_SIGNER 102 349#define CMS_F_CMS_ADD1_SIGNER 102
@@ -344,6 +358,7 @@ void ERR_load_CMS_strings(void);
344#define CMS_F_CMS_DATAINIT 111 358#define CMS_F_CMS_DATAINIT 111
345#define CMS_F_CMS_DECRYPT 112 359#define CMS_F_CMS_DECRYPT 112
346#define CMS_F_CMS_DECRYPT_SET1_KEY 113 360#define CMS_F_CMS_DECRYPT_SET1_KEY 113
361#define CMS_F_CMS_DECRYPT_SET1_PASSWORD 166
347#define CMS_F_CMS_DECRYPT_SET1_PKEY 114 362#define CMS_F_CMS_DECRYPT_SET1_PKEY 114
348#define CMS_F_CMS_DIGESTALGORITHM_FIND_CTX 115 363#define CMS_F_CMS_DIGESTALGORITHM_FIND_CTX 115
349#define CMS_F_CMS_DIGESTALGORITHM_INIT_BIO 116 364#define CMS_F_CMS_DIGESTALGORITHM_INIT_BIO 116
@@ -378,7 +393,9 @@ void ERR_load_CMS_strings(void);
378#define CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT 141 393#define CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT 141
379#define CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS 142 394#define CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS 142
380#define CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID 143 395#define CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID 143
396#define CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT 167
381#define CMS_F_CMS_RECIPIENTINFO_SET0_KEY 144 397#define CMS_F_CMS_RECIPIENTINFO_SET0_KEY 144
398#define CMS_F_CMS_RECIPIENTINFO_SET0_PASSWORD 168
382#define CMS_F_CMS_RECIPIENTINFO_SET0_PKEY 145 399#define CMS_F_CMS_RECIPIENTINFO_SET0_PKEY 145
383#define CMS_F_CMS_SET1_SIGNERIDENTIFIER 146 400#define CMS_F_CMS_SET1_SIGNERIDENTIFIER 146
384#define CMS_F_CMS_SET_DETACHED 147 401#define CMS_F_CMS_SET_DETACHED 147
@@ -419,6 +436,7 @@ void ERR_load_CMS_strings(void);
419#define CMS_R_ERROR_SETTING_KEY 115 436#define CMS_R_ERROR_SETTING_KEY 115
420#define CMS_R_ERROR_SETTING_RECIPIENTINFO 116 437#define CMS_R_ERROR_SETTING_RECIPIENTINFO 116
421#define CMS_R_INVALID_ENCRYPTED_KEY_LENGTH 117 438#define CMS_R_INVALID_ENCRYPTED_KEY_LENGTH 117
439#define CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER 176
422#define CMS_R_INVALID_KEY_LENGTH 118 440#define CMS_R_INVALID_KEY_LENGTH 118
423#define CMS_R_MD_BIO_INIT_ERROR 119 441#define CMS_R_MD_BIO_INIT_ERROR 119
424#define CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH 120 442#define CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH 120
@@ -431,6 +449,7 @@ void ERR_load_CMS_strings(void);
431#define CMS_R_NOT_ENCRYPTED_DATA 122 449#define CMS_R_NOT_ENCRYPTED_DATA 122
432#define CMS_R_NOT_KEK 123 450#define CMS_R_NOT_KEK 123
433#define CMS_R_NOT_KEY_TRANSPORT 124 451#define CMS_R_NOT_KEY_TRANSPORT 124
452#define CMS_R_NOT_PWRI 177
434#define CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE 125 453#define CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE 125
435#define CMS_R_NO_CIPHER 126 454#define CMS_R_NO_CIPHER 126
436#define CMS_R_NO_CONTENT 127 455#define CMS_R_NO_CONTENT 127
@@ -443,6 +462,7 @@ void ERR_load_CMS_strings(void);
443#define CMS_R_NO_MATCHING_RECIPIENT 132 462#define CMS_R_NO_MATCHING_RECIPIENT 132
444#define CMS_R_NO_MATCHING_SIGNATURE 166 463#define CMS_R_NO_MATCHING_SIGNATURE 166
445#define CMS_R_NO_MSGSIGDIGEST 167 464#define CMS_R_NO_MSGSIGDIGEST 167
465#define CMS_R_NO_PASSWORD 178
446#define CMS_R_NO_PRIVATE_KEY 133 466#define CMS_R_NO_PRIVATE_KEY 133
447#define CMS_R_NO_PUBLIC_KEY 134 467#define CMS_R_NO_PUBLIC_KEY 134
448#define CMS_R_NO_RECEIPT_REQUEST 168 468#define CMS_R_NO_RECEIPT_REQUEST 168
@@ -466,10 +486,12 @@ void ERR_load_CMS_strings(void);
466#define CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM 151 486#define CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM 151
467#define CMS_R_UNSUPPORTED_CONTENT_TYPE 152 487#define CMS_R_UNSUPPORTED_CONTENT_TYPE 152
468#define CMS_R_UNSUPPORTED_KEK_ALGORITHM 153 488#define CMS_R_UNSUPPORTED_KEK_ALGORITHM 153
489#define CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM 179
469#define CMS_R_UNSUPPORTED_RECIPIENT_TYPE 154 490#define CMS_R_UNSUPPORTED_RECIPIENT_TYPE 154
470#define CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE 155 491#define CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE 155
471#define CMS_R_UNSUPPORTED_TYPE 156 492#define CMS_R_UNSUPPORTED_TYPE 156
472#define CMS_R_UNWRAP_ERROR 157 493#define CMS_R_UNWRAP_ERROR 157
494#define CMS_R_UNWRAP_FAILURE 180
473#define CMS_R_VERIFICATION_FAILURE 158 495#define CMS_R_VERIFICATION_FAILURE 158
474#define CMS_R_WRAP_ERROR 159 496#define CMS_R_WRAP_ERROR 159
475 497
diff --git a/src/lib/libcrypto/cms/cms_asn1.c b/src/lib/libcrypto/cms/cms_asn1.c
index fcba4dcbcc..cfe67fb6c1 100644
--- a/src/lib/libcrypto/cms/cms_asn1.c
+++ b/src/lib/libcrypto/cms/cms_asn1.c
@@ -237,6 +237,15 @@ static int cms_ri_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
237 OPENSSL_free(kekri->key); 237 OPENSSL_free(kekri->key);
238 } 238 }
239 } 239 }
240 else if (ri->type == CMS_RECIPINFO_PASS)
241 {
242 CMS_PasswordRecipientInfo *pwri = ri->d.pwri;
243 if (pwri->pass)
244 {
245 OPENSSL_cleanse(pwri->pass, pwri->passlen);
246 OPENSSL_free(pwri->pass);
247 }
248 }
240 } 249 }
241 return 1; 250 return 1;
242 } 251 }
diff --git a/src/lib/libcrypto/cms/cms_enc.c b/src/lib/libcrypto/cms/cms_enc.c
index bab26235bd..f873ce3794 100644
--- a/src/lib/libcrypto/cms/cms_enc.c
+++ b/src/lib/libcrypto/cms/cms_enc.c
@@ -73,6 +73,8 @@ BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec)
73 const EVP_CIPHER *ciph; 73 const EVP_CIPHER *ciph;
74 X509_ALGOR *calg = ec->contentEncryptionAlgorithm; 74 X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
75 unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL; 75 unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL;
76 unsigned char *tkey = NULL;
77 size_t tkeylen;
76 78
77 int ok = 0; 79 int ok = 0;
78 80
@@ -137,32 +139,57 @@ BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec)
137 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR); 139 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
138 goto err; 140 goto err;
139 } 141 }
140 142 tkeylen = EVP_CIPHER_CTX_key_length(ctx);
141 143 /* Generate random session key */
142 if (enc && !ec->key) 144 if (!enc || !ec->key)
143 { 145 {
144 /* Generate random key */ 146 tkey = OPENSSL_malloc(tkeylen);
145 if (!ec->keylen) 147 if (!tkey)
146 ec->keylen = EVP_CIPHER_CTX_key_length(ctx);
147 ec->key = OPENSSL_malloc(ec->keylen);
148 if (!ec->key)
149 { 148 {
150 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, 149 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
151 ERR_R_MALLOC_FAILURE); 150 ERR_R_MALLOC_FAILURE);
152 goto err; 151 goto err;
153 } 152 }
154 if (EVP_CIPHER_CTX_rand_key(ctx, ec->key) <= 0) 153 if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
155 goto err; 154 goto err;
156 keep_key = 1;
157 } 155 }
158 else if (ec->keylen != (unsigned int)EVP_CIPHER_CTX_key_length(ctx)) 156
157 if (!ec->key)
158 {
159 ec->key = tkey;
160 ec->keylen = tkeylen;
161 tkey = NULL;
162 if (enc)
163 keep_key = 1;
164 else
165 ERR_clear_error();
166
167 }
168
169 if (ec->keylen != tkeylen)
159 { 170 {
160 /* If necessary set key length */ 171 /* If necessary set key length */
161 if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) 172 if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0)
162 { 173 {
163 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, 174 /* Only reveal failure if debugging so we don't
164 CMS_R_INVALID_KEY_LENGTH); 175 * leak information which may be useful in MMA.
165 goto err; 176 */
177 if (enc || ec->debug)
178 {
179 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
180 CMS_R_INVALID_KEY_LENGTH);
181 goto err;
182 }
183 else
184 {
185 /* Use random key */
186 OPENSSL_cleanse(ec->key, ec->keylen);
187 OPENSSL_free(ec->key);
188 ec->key = tkey;
189 ec->keylen = tkeylen;
190 tkey = NULL;
191 ERR_clear_error();
192 }
166 } 193 }
167 } 194 }
168 195
@@ -198,6 +225,11 @@ BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec)
198 OPENSSL_free(ec->key); 225 OPENSSL_free(ec->key);
199 ec->key = NULL; 226 ec->key = NULL;
200 } 227 }
228 if (tkey)
229 {
230 OPENSSL_cleanse(tkey, tkeylen);
231 OPENSSL_free(tkey);
232 }
201 if (ok) 233 if (ok)
202 return b; 234 return b;
203 BIO_free(b); 235 BIO_free(b);
diff --git a/src/lib/libcrypto/cms/cms_env.c b/src/lib/libcrypto/cms/cms_env.c
index b3237d4b94..be20b1c024 100644
--- a/src/lib/libcrypto/cms/cms_env.c
+++ b/src/lib/libcrypto/cms/cms_env.c
@@ -65,14 +65,13 @@
65/* CMS EnvelopedData Utilities */ 65/* CMS EnvelopedData Utilities */
66 66
67DECLARE_ASN1_ITEM(CMS_EnvelopedData) 67DECLARE_ASN1_ITEM(CMS_EnvelopedData)
68DECLARE_ASN1_ITEM(CMS_RecipientInfo)
69DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo) 68DECLARE_ASN1_ITEM(CMS_KeyTransRecipientInfo)
70DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo) 69DECLARE_ASN1_ITEM(CMS_KEKRecipientInfo)
71DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute) 70DECLARE_ASN1_ITEM(CMS_OtherKeyAttribute)
72 71
73DECLARE_STACK_OF(CMS_RecipientInfo) 72DECLARE_STACK_OF(CMS_RecipientInfo)
74 73
75static CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms) 74CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms)
76 { 75 {
77 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped) 76 if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_enveloped)
78 { 77 {
@@ -371,6 +370,8 @@ static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
371 unsigned char *ek = NULL; 370 unsigned char *ek = NULL;
372 size_t eklen; 371 size_t eklen;
373 int ret = 0; 372 int ret = 0;
373 CMS_EncryptedContentInfo *ec;
374 ec = cms->d.envelopedData->encryptedContentInfo;
374 375
375 if (ktri->pkey == NULL) 376 if (ktri->pkey == NULL)
376 { 377 {
@@ -417,8 +418,14 @@ static int cms_RecipientInfo_ktri_decrypt(CMS_ContentInfo *cms,
417 418
418 ret = 1; 419 ret = 1;
419 420
420 cms->d.envelopedData->encryptedContentInfo->key = ek; 421 if (ec->key)
421 cms->d.envelopedData->encryptedContentInfo->keylen = eklen; 422 {
423 OPENSSL_cleanse(ec->key, ec->keylen);
424 OPENSSL_free(ec->key);
425 }
426
427 ec->key = ek;
428 ec->keylen = eklen;
422 429
423 err: 430 err:
424 if (pctx) 431 if (pctx)
@@ -786,6 +793,9 @@ int CMS_RecipientInfo_decrypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri)
786 case CMS_RECIPINFO_KEK: 793 case CMS_RECIPINFO_KEK:
787 return cms_RecipientInfo_kekri_decrypt(cms, ri); 794 return cms_RecipientInfo_kekri_decrypt(cms, ri);
788 795
796 case CMS_RECIPINFO_PASS:
797 return cms_RecipientInfo_pwri_crypt(cms, ri, 0);
798
789 default: 799 default:
790 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT, 800 CMSerr(CMS_F_CMS_RECIPIENTINFO_DECRYPT,
791 CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE); 801 CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE);
@@ -829,6 +839,10 @@ BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms)
829 r = cms_RecipientInfo_kekri_encrypt(cms, ri); 839 r = cms_RecipientInfo_kekri_encrypt(cms, ri);
830 break; 840 break;
831 841
842 case CMS_RECIPINFO_PASS:
843 r = cms_RecipientInfo_pwri_crypt(cms, ri, 1);
844 break;
845
832 default: 846 default:
833 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO, 847 CMSerr(CMS_F_CMS_ENVELOPEDDATA_INIT_BIO,
834 CMS_R_UNSUPPORTED_RECIPIENT_TYPE); 848 CMS_R_UNSUPPORTED_RECIPIENT_TYPE);
diff --git a/src/lib/libcrypto/cms/cms_err.c b/src/lib/libcrypto/cms/cms_err.c
index ff7b0309e5..8330ead7ed 100644
--- a/src/lib/libcrypto/cms/cms_err.c
+++ b/src/lib/libcrypto/cms/cms_err.c
@@ -1,6 +1,6 @@
1/* crypto/cms/cms_err.c */ 1/* crypto/cms/cms_err.c */
2/* ==================================================================== 2/* ====================================================================
3 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved. 3 * Copyright (c) 1999-2009 The OpenSSL Project. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
@@ -73,6 +73,7 @@ static ERR_STRING_DATA CMS_str_functs[]=
73{ERR_FUNC(CMS_F_CHECK_CONTENT), "CHECK_CONTENT"}, 73{ERR_FUNC(CMS_F_CHECK_CONTENT), "CHECK_CONTENT"},
74{ERR_FUNC(CMS_F_CMS_ADD0_CERT), "CMS_add0_cert"}, 74{ERR_FUNC(CMS_F_CMS_ADD0_CERT), "CMS_add0_cert"},
75{ERR_FUNC(CMS_F_CMS_ADD0_RECIPIENT_KEY), "CMS_add0_recipient_key"}, 75{ERR_FUNC(CMS_F_CMS_ADD0_RECIPIENT_KEY), "CMS_add0_recipient_key"},
76{ERR_FUNC(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD), "CMS_add0_recipient_password"},
76{ERR_FUNC(CMS_F_CMS_ADD1_RECEIPTREQUEST), "CMS_add1_ReceiptRequest"}, 77{ERR_FUNC(CMS_F_CMS_ADD1_RECEIPTREQUEST), "CMS_add1_ReceiptRequest"},
77{ERR_FUNC(CMS_F_CMS_ADD1_RECIPIENT_CERT), "CMS_add1_recipient_cert"}, 78{ERR_FUNC(CMS_F_CMS_ADD1_RECIPIENT_CERT), "CMS_add1_recipient_cert"},
78{ERR_FUNC(CMS_F_CMS_ADD1_SIGNER), "CMS_add1_signer"}, 79{ERR_FUNC(CMS_F_CMS_ADD1_SIGNER), "CMS_add1_signer"},
@@ -87,6 +88,7 @@ static ERR_STRING_DATA CMS_str_functs[]=
87{ERR_FUNC(CMS_F_CMS_DATAINIT), "CMS_dataInit"}, 88{ERR_FUNC(CMS_F_CMS_DATAINIT), "CMS_dataInit"},
88{ERR_FUNC(CMS_F_CMS_DECRYPT), "CMS_decrypt"}, 89{ERR_FUNC(CMS_F_CMS_DECRYPT), "CMS_decrypt"},
89{ERR_FUNC(CMS_F_CMS_DECRYPT_SET1_KEY), "CMS_decrypt_set1_key"}, 90{ERR_FUNC(CMS_F_CMS_DECRYPT_SET1_KEY), "CMS_decrypt_set1_key"},
91{ERR_FUNC(CMS_F_CMS_DECRYPT_SET1_PASSWORD), "CMS_decrypt_set1_password"},
90{ERR_FUNC(CMS_F_CMS_DECRYPT_SET1_PKEY), "CMS_decrypt_set1_pkey"}, 92{ERR_FUNC(CMS_F_CMS_DECRYPT_SET1_PKEY), "CMS_decrypt_set1_pkey"},
91{ERR_FUNC(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX), "cms_DigestAlgorithm_find_ctx"}, 93{ERR_FUNC(CMS_F_CMS_DIGESTALGORITHM_FIND_CTX), "cms_DigestAlgorithm_find_ctx"},
92{ERR_FUNC(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO), "cms_DigestAlgorithm_init_bio"}, 94{ERR_FUNC(CMS_F_CMS_DIGESTALGORITHM_INIT_BIO), "cms_DigestAlgorithm_init_bio"},
@@ -105,7 +107,7 @@ static ERR_STRING_DATA CMS_str_functs[]=
105{ERR_FUNC(CMS_F_CMS_GET0_CERTIFICATE_CHOICES), "CMS_GET0_CERTIFICATE_CHOICES"}, 107{ERR_FUNC(CMS_F_CMS_GET0_CERTIFICATE_CHOICES), "CMS_GET0_CERTIFICATE_CHOICES"},
106{ERR_FUNC(CMS_F_CMS_GET0_CONTENT), "CMS_get0_content"}, 108{ERR_FUNC(CMS_F_CMS_GET0_CONTENT), "CMS_get0_content"},
107{ERR_FUNC(CMS_F_CMS_GET0_ECONTENT_TYPE), "CMS_GET0_ECONTENT_TYPE"}, 109{ERR_FUNC(CMS_F_CMS_GET0_ECONTENT_TYPE), "CMS_GET0_ECONTENT_TYPE"},
108{ERR_FUNC(CMS_F_CMS_GET0_ENVELOPED), "CMS_GET0_ENVELOPED"}, 110{ERR_FUNC(CMS_F_CMS_GET0_ENVELOPED), "cms_get0_enveloped"},
109{ERR_FUNC(CMS_F_CMS_GET0_REVOCATION_CHOICES), "CMS_GET0_REVOCATION_CHOICES"}, 111{ERR_FUNC(CMS_F_CMS_GET0_REVOCATION_CHOICES), "CMS_GET0_REVOCATION_CHOICES"},
110{ERR_FUNC(CMS_F_CMS_GET0_SIGNED), "CMS_GET0_SIGNED"}, 112{ERR_FUNC(CMS_F_CMS_GET0_SIGNED), "CMS_GET0_SIGNED"},
111{ERR_FUNC(CMS_F_CMS_MSGSIGDIGEST_ADD1), "cms_msgSigDigest_add1"}, 113{ERR_FUNC(CMS_F_CMS_MSGSIGDIGEST_ADD1), "cms_msgSigDigest_add1"},
@@ -121,7 +123,9 @@ static ERR_STRING_DATA CMS_str_functs[]=
121{ERR_FUNC(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT), "CMS_RECIPIENTINFO_KTRI_ENCRYPT"}, 123{ERR_FUNC(CMS_F_CMS_RECIPIENTINFO_KTRI_ENCRYPT), "CMS_RECIPIENTINFO_KTRI_ENCRYPT"},
122{ERR_FUNC(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS), "CMS_RecipientInfo_ktri_get0_algs"}, 124{ERR_FUNC(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_ALGS), "CMS_RecipientInfo_ktri_get0_algs"},
123{ERR_FUNC(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID), "CMS_RecipientInfo_ktri_get0_signer_id"}, 125{ERR_FUNC(CMS_F_CMS_RECIPIENTINFO_KTRI_GET0_SIGNER_ID), "CMS_RecipientInfo_ktri_get0_signer_id"},
126{ERR_FUNC(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT), "cms_RecipientInfo_pwri_crypt"},
124{ERR_FUNC(CMS_F_CMS_RECIPIENTINFO_SET0_KEY), "CMS_RecipientInfo_set0_key"}, 127{ERR_FUNC(CMS_F_CMS_RECIPIENTINFO_SET0_KEY), "CMS_RecipientInfo_set0_key"},
128{ERR_FUNC(CMS_F_CMS_RECIPIENTINFO_SET0_PASSWORD), "CMS_RecipientInfo_set0_password"},
125{ERR_FUNC(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY), "CMS_RecipientInfo_set0_pkey"}, 129{ERR_FUNC(CMS_F_CMS_RECIPIENTINFO_SET0_PKEY), "CMS_RecipientInfo_set0_pkey"},
126{ERR_FUNC(CMS_F_CMS_SET1_SIGNERIDENTIFIER), "cms_set1_SignerIdentifier"}, 130{ERR_FUNC(CMS_F_CMS_SET1_SIGNERIDENTIFIER), "cms_set1_SignerIdentifier"},
127{ERR_FUNC(CMS_F_CMS_SET_DETACHED), "CMS_set_detached"}, 131{ERR_FUNC(CMS_F_CMS_SET_DETACHED), "CMS_set_detached"},
@@ -165,6 +169,7 @@ static ERR_STRING_DATA CMS_str_reasons[]=
165{ERR_REASON(CMS_R_ERROR_SETTING_KEY) ,"error setting key"}, 169{ERR_REASON(CMS_R_ERROR_SETTING_KEY) ,"error setting key"},
166{ERR_REASON(CMS_R_ERROR_SETTING_RECIPIENTINFO),"error setting recipientinfo"}, 170{ERR_REASON(CMS_R_ERROR_SETTING_RECIPIENTINFO),"error setting recipientinfo"},
167{ERR_REASON(CMS_R_INVALID_ENCRYPTED_KEY_LENGTH),"invalid encrypted key length"}, 171{ERR_REASON(CMS_R_INVALID_ENCRYPTED_KEY_LENGTH),"invalid encrypted key length"},
172{ERR_REASON(CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER),"invalid key encryption parameter"},
168{ERR_REASON(CMS_R_INVALID_KEY_LENGTH) ,"invalid key length"}, 173{ERR_REASON(CMS_R_INVALID_KEY_LENGTH) ,"invalid key length"},
169{ERR_REASON(CMS_R_MD_BIO_INIT_ERROR) ,"md bio init error"}, 174{ERR_REASON(CMS_R_MD_BIO_INIT_ERROR) ,"md bio init error"},
170{ERR_REASON(CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH),"messagedigest attribute wrong length"}, 175{ERR_REASON(CMS_R_MESSAGEDIGEST_ATTRIBUTE_WRONG_LENGTH),"messagedigest attribute wrong length"},
@@ -177,6 +182,7 @@ static ERR_STRING_DATA CMS_str_reasons[]=
177{ERR_REASON(CMS_R_NOT_ENCRYPTED_DATA) ,"not encrypted data"}, 182{ERR_REASON(CMS_R_NOT_ENCRYPTED_DATA) ,"not encrypted data"},
178{ERR_REASON(CMS_R_NOT_KEK) ,"not kek"}, 183{ERR_REASON(CMS_R_NOT_KEK) ,"not kek"},
179{ERR_REASON(CMS_R_NOT_KEY_TRANSPORT) ,"not key transport"}, 184{ERR_REASON(CMS_R_NOT_KEY_TRANSPORT) ,"not key transport"},
185{ERR_REASON(CMS_R_NOT_PWRI) ,"not pwri"},
180{ERR_REASON(CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE),"not supported for this key type"}, 186{ERR_REASON(CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE),"not supported for this key type"},
181{ERR_REASON(CMS_R_NO_CIPHER) ,"no cipher"}, 187{ERR_REASON(CMS_R_NO_CIPHER) ,"no cipher"},
182{ERR_REASON(CMS_R_NO_CONTENT) ,"no content"}, 188{ERR_REASON(CMS_R_NO_CONTENT) ,"no content"},
@@ -189,6 +195,7 @@ static ERR_STRING_DATA CMS_str_reasons[]=
189{ERR_REASON(CMS_R_NO_MATCHING_RECIPIENT) ,"no matching recipient"}, 195{ERR_REASON(CMS_R_NO_MATCHING_RECIPIENT) ,"no matching recipient"},
190{ERR_REASON(CMS_R_NO_MATCHING_SIGNATURE) ,"no matching signature"}, 196{ERR_REASON(CMS_R_NO_MATCHING_SIGNATURE) ,"no matching signature"},
191{ERR_REASON(CMS_R_NO_MSGSIGDIGEST) ,"no msgsigdigest"}, 197{ERR_REASON(CMS_R_NO_MSGSIGDIGEST) ,"no msgsigdigest"},
198{ERR_REASON(CMS_R_NO_PASSWORD) ,"no password"},
192{ERR_REASON(CMS_R_NO_PRIVATE_KEY) ,"no private key"}, 199{ERR_REASON(CMS_R_NO_PRIVATE_KEY) ,"no private key"},
193{ERR_REASON(CMS_R_NO_PUBLIC_KEY) ,"no public key"}, 200{ERR_REASON(CMS_R_NO_PUBLIC_KEY) ,"no public key"},
194{ERR_REASON(CMS_R_NO_RECEIPT_REQUEST) ,"no receipt request"}, 201{ERR_REASON(CMS_R_NO_RECEIPT_REQUEST) ,"no receipt request"},
@@ -212,10 +219,12 @@ static ERR_STRING_DATA CMS_str_reasons[]=
212{ERR_REASON(CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM),"unsupported compression algorithm"}, 219{ERR_REASON(CMS_R_UNSUPPORTED_COMPRESSION_ALGORITHM),"unsupported compression algorithm"},
213{ERR_REASON(CMS_R_UNSUPPORTED_CONTENT_TYPE),"unsupported content type"}, 220{ERR_REASON(CMS_R_UNSUPPORTED_CONTENT_TYPE),"unsupported content type"},
214{ERR_REASON(CMS_R_UNSUPPORTED_KEK_ALGORITHM),"unsupported kek algorithm"}, 221{ERR_REASON(CMS_R_UNSUPPORTED_KEK_ALGORITHM),"unsupported kek algorithm"},
222{ERR_REASON(CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM),"unsupported key encryption algorithm"},
215{ERR_REASON(CMS_R_UNSUPPORTED_RECIPIENT_TYPE),"unsupported recipient type"}, 223{ERR_REASON(CMS_R_UNSUPPORTED_RECIPIENT_TYPE),"unsupported recipient type"},
216{ERR_REASON(CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE),"unsupported recpientinfo type"}, 224{ERR_REASON(CMS_R_UNSUPPORTED_RECPIENTINFO_TYPE),"unsupported recpientinfo type"},
217{ERR_REASON(CMS_R_UNSUPPORTED_TYPE) ,"unsupported type"}, 225{ERR_REASON(CMS_R_UNSUPPORTED_TYPE) ,"unsupported type"},
218{ERR_REASON(CMS_R_UNWRAP_ERROR) ,"unwrap error"}, 226{ERR_REASON(CMS_R_UNWRAP_ERROR) ,"unwrap error"},
227{ERR_REASON(CMS_R_UNWRAP_FAILURE) ,"unwrap failure"},
219{ERR_REASON(CMS_R_VERIFICATION_FAILURE) ,"verification failure"}, 228{ERR_REASON(CMS_R_VERIFICATION_FAILURE) ,"verification failure"},
220{ERR_REASON(CMS_R_WRAP_ERROR) ,"wrap error"}, 229{ERR_REASON(CMS_R_WRAP_ERROR) ,"wrap error"},
221{0,NULL} 230{0,NULL}
diff --git a/src/lib/libcrypto/cms/cms_lcl.h b/src/lib/libcrypto/cms/cms_lcl.h
index c8ecfa724a..a9f9730157 100644
--- a/src/lib/libcrypto/cms/cms_lcl.h
+++ b/src/lib/libcrypto/cms/cms_lcl.h
@@ -175,6 +175,8 @@ struct CMS_EncryptedContentInfo_st
175 const EVP_CIPHER *cipher; 175 const EVP_CIPHER *cipher;
176 unsigned char *key; 176 unsigned char *key;
177 size_t keylen; 177 size_t keylen;
178 /* Set to 1 if we are debugging decrypt and don't fake keys for MMA */
179 int debug;
178 }; 180 };
179 181
180struct CMS_RecipientInfo_st 182struct CMS_RecipientInfo_st
@@ -273,6 +275,9 @@ struct CMS_PasswordRecipientInfo_st
273 X509_ALGOR *keyDerivationAlgorithm; 275 X509_ALGOR *keyDerivationAlgorithm;
274 X509_ALGOR *keyEncryptionAlgorithm; 276 X509_ALGOR *keyEncryptionAlgorithm;
275 ASN1_OCTET_STRING *encryptedKey; 277 ASN1_OCTET_STRING *encryptedKey;
278 /* Extra info: password to use */
279 unsigned char *pass;
280 size_t passlen;
276 }; 281 };
277 282
278struct CMS_OtherRecipientInfo_st 283struct CMS_OtherRecipientInfo_st
@@ -411,6 +416,8 @@ DECLARE_ASN1_ITEM(CMS_SignerInfo)
411DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber) 416DECLARE_ASN1_ITEM(CMS_IssuerAndSerialNumber)
412DECLARE_ASN1_ITEM(CMS_Attributes_Sign) 417DECLARE_ASN1_ITEM(CMS_Attributes_Sign)
413DECLARE_ASN1_ITEM(CMS_Attributes_Verify) 418DECLARE_ASN1_ITEM(CMS_Attributes_Verify)
419DECLARE_ASN1_ITEM(CMS_RecipientInfo)
420DECLARE_ASN1_ITEM(CMS_PasswordRecipientInfo)
414DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber) 421DECLARE_ASN1_ALLOC_FUNCTIONS(CMS_IssuerAndSerialNumber)
415 422
416#define CMS_SIGNERINFO_ISSUER_SERIAL 0 423#define CMS_SIGNERINFO_ISSUER_SERIAL 0
@@ -454,6 +461,11 @@ int cms_msgSigDigest_add1(CMS_SignerInfo *dest, CMS_SignerInfo *src);
454ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si); 461ASN1_OCTET_STRING *cms_encode_Receipt(CMS_SignerInfo *si);
455 462
456BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms); 463BIO *cms_EnvelopedData_init_bio(CMS_ContentInfo *cms);
464CMS_EnvelopedData *cms_get0_enveloped(CMS_ContentInfo *cms);
465
466/* PWRI routines */
467int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
468 int en_de);
457 469
458#ifdef __cplusplus 470#ifdef __cplusplus
459} 471}
diff --git a/src/lib/libcrypto/cms/cms_lib.c b/src/lib/libcrypto/cms/cms_lib.c
index d00fe0f87b..f88e8f3b52 100644
--- a/src/lib/libcrypto/cms/cms_lib.c
+++ b/src/lib/libcrypto/cms/cms_lib.c
@@ -412,8 +412,7 @@ int cms_DigestAlgorithm_find_ctx(EVP_MD_CTX *mctx, BIO *chain,
412 */ 412 */
413 || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid) 413 || EVP_MD_pkey_type(EVP_MD_CTX_md(mtmp)) == nid)
414 { 414 {
415 EVP_MD_CTX_copy_ex(mctx, mtmp); 415 return EVP_MD_CTX_copy_ex(mctx, mtmp);
416 return 1;
417 } 416 }
418 chain = BIO_next(chain); 417 chain = BIO_next(chain);
419 } 418 }
diff --git a/src/lib/libcrypto/cms/cms_pwri.c b/src/lib/libcrypto/cms/cms_pwri.c
new file mode 100644
index 0000000000..b79612a12d
--- /dev/null
+++ b/src/lib/libcrypto/cms/cms_pwri.c
@@ -0,0 +1,454 @@
1/* crypto/cms/cms_pwri.c */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project.
4 */
5/* ====================================================================
6 * Copyright (c) 2009 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 */
53
54#include "cryptlib.h"
55#include <openssl/asn1t.h>
56#include <openssl/pem.h>
57#include <openssl/x509v3.h>
58#include <openssl/err.h>
59#include <openssl/cms.h>
60#include <openssl/rand.h>
61#include <openssl/aes.h>
62#include "cms_lcl.h"
63#include "asn1_locl.h"
64
65int CMS_RecipientInfo_set0_password(CMS_RecipientInfo *ri,
66 unsigned char *pass, ossl_ssize_t passlen)
67 {
68 CMS_PasswordRecipientInfo *pwri;
69 if (ri->type != CMS_RECIPINFO_PASS)
70 {
71 CMSerr(CMS_F_CMS_RECIPIENTINFO_SET0_PASSWORD, CMS_R_NOT_PWRI);
72 return 0;
73 }
74
75 pwri = ri->d.pwri;
76 pwri->pass = pass;
77 if (pass && passlen < 0)
78 passlen = strlen((char *)pass);
79 pwri->passlen = passlen;
80 return 1;
81 }
82
83CMS_RecipientInfo *CMS_add0_recipient_password(CMS_ContentInfo *cms,
84 int iter, int wrap_nid, int pbe_nid,
85 unsigned char *pass,
86 ossl_ssize_t passlen,
87 const EVP_CIPHER *kekciph)
88 {
89 CMS_RecipientInfo *ri = NULL;
90 CMS_EnvelopedData *env;
91 CMS_PasswordRecipientInfo *pwri;
92 EVP_CIPHER_CTX ctx;
93 X509_ALGOR *encalg = NULL;
94 unsigned char iv[EVP_MAX_IV_LENGTH];
95 int ivlen;
96 env = cms_get0_enveloped(cms);
97 if (!env)
98 goto err;
99
100 if (wrap_nid <= 0)
101 wrap_nid = NID_id_alg_PWRI_KEK;
102
103 if (pbe_nid <= 0)
104 pbe_nid = NID_id_pbkdf2;
105
106 /* Get from enveloped data */
107 if (kekciph == NULL)
108 kekciph = env->encryptedContentInfo->cipher;
109
110 if (kekciph == NULL)
111 {
112 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, CMS_R_NO_CIPHER);
113 return NULL;
114 }
115 if (wrap_nid != NID_id_alg_PWRI_KEK)
116 {
117 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
118 CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
119 return NULL;
120 }
121
122 /* Setup algorithm identifier for cipher */
123 encalg = X509_ALGOR_new();
124 EVP_CIPHER_CTX_init(&ctx);
125
126 if (EVP_EncryptInit_ex(&ctx, kekciph, NULL, NULL, NULL) <= 0)
127 {
128 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_EVP_LIB);
129 goto err;
130 }
131
132 ivlen = EVP_CIPHER_CTX_iv_length(&ctx);
133
134 if (ivlen > 0)
135 {
136 if (RAND_pseudo_bytes(iv, ivlen) <= 0)
137 goto err;
138 if (EVP_EncryptInit_ex(&ctx, NULL, NULL, NULL, iv) <= 0)
139 {
140 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
141 ERR_R_EVP_LIB);
142 goto err;
143 }
144 encalg->parameter = ASN1_TYPE_new();
145 if (!encalg->parameter)
146 {
147 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
148 ERR_R_MALLOC_FAILURE);
149 goto err;
150 }
151 if (EVP_CIPHER_param_to_asn1(&ctx, encalg->parameter) <= 0)
152 {
153 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD,
154 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
155 goto err;
156 }
157 }
158
159
160 encalg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(&ctx));
161
162 EVP_CIPHER_CTX_cleanup(&ctx);
163
164 /* Initialize recipient info */
165 ri = M_ASN1_new_of(CMS_RecipientInfo);
166 if (!ri)
167 goto merr;
168
169 ri->d.pwri = M_ASN1_new_of(CMS_PasswordRecipientInfo);
170 if (!ri->d.pwri)
171 goto merr;
172 ri->type = CMS_RECIPINFO_PASS;
173
174 pwri = ri->d.pwri;
175 /* Since this is overwritten, free up empty structure already there */
176 X509_ALGOR_free(pwri->keyEncryptionAlgorithm);
177 pwri->keyEncryptionAlgorithm = X509_ALGOR_new();
178 if (!pwri->keyEncryptionAlgorithm)
179 goto merr;
180 pwri->keyEncryptionAlgorithm->algorithm = OBJ_nid2obj(wrap_nid);
181 pwri->keyEncryptionAlgorithm->parameter = ASN1_TYPE_new();
182 if (!pwri->keyEncryptionAlgorithm->parameter)
183 goto merr;
184
185 if(!ASN1_item_pack(encalg, ASN1_ITEM_rptr(X509_ALGOR),
186 &pwri->keyEncryptionAlgorithm->parameter->value.sequence))
187 goto merr;
188 pwri->keyEncryptionAlgorithm->parameter->type = V_ASN1_SEQUENCE;
189
190 X509_ALGOR_free(encalg);
191 encalg = NULL;
192
193 /* Setup PBE algorithm */
194
195 pwri->keyDerivationAlgorithm = PKCS5_pbkdf2_set(iter, NULL, 0, -1, -1);
196
197 if (!pwri->keyDerivationAlgorithm)
198 goto err;
199
200 CMS_RecipientInfo_set0_password(ri, pass, passlen);
201 pwri->version = 0;
202
203 if (!sk_CMS_RecipientInfo_push(env->recipientInfos, ri))
204 goto merr;
205
206 return ri;
207
208 merr:
209 CMSerr(CMS_F_CMS_ADD0_RECIPIENT_PASSWORD, ERR_R_MALLOC_FAILURE);
210 err:
211 EVP_CIPHER_CTX_cleanup(&ctx);
212 if (ri)
213 M_ASN1_free_of(ri, CMS_RecipientInfo);
214 if (encalg)
215 X509_ALGOR_free(encalg);
216 return NULL;
217
218 }
219
220/* This is an implementation of the key wrapping mechanism in RFC3211,
221 * at some point this should go into EVP.
222 */
223
224static int kek_unwrap_key(unsigned char *out, size_t *outlen,
225 const unsigned char *in, size_t inlen, EVP_CIPHER_CTX *ctx)
226 {
227 size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);
228 unsigned char *tmp;
229 int outl, rv = 0;
230 if (inlen < 2 * blocklen)
231 {
232 /* too small */
233 return 0;
234 }
235 if (inlen % blocklen)
236 {
237 /* Invalid size */
238 return 0;
239 }
240 tmp = OPENSSL_malloc(inlen);
241 /* setup IV by decrypting last two blocks */
242 EVP_DecryptUpdate(ctx, tmp + inlen - 2 * blocklen, &outl,
243 in + inlen - 2 * blocklen, blocklen * 2);
244 /* Do a decrypt of last decrypted block to set IV to correct value
245 * output it to start of buffer so we don't corrupt decrypted block
246 * this works because buffer is at least two block lengths long.
247 */
248 EVP_DecryptUpdate(ctx, tmp, &outl,
249 tmp + inlen - blocklen, blocklen);
250 /* Can now decrypt first n - 1 blocks */
251 EVP_DecryptUpdate(ctx, tmp, &outl, in, inlen - blocklen);
252
253 /* Reset IV to original value */
254 EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, NULL);
255 /* Decrypt again */
256 EVP_DecryptUpdate(ctx, tmp, &outl, tmp, inlen);
257 /* Check check bytes */
258 if (((tmp[1] ^ tmp[4]) & (tmp[2] ^ tmp[5]) & (tmp[3] ^ tmp[6])) != 0xff)
259 {
260 /* Check byte failure */
261 goto err;
262 }
263 if (inlen < (size_t)(tmp[0] - 4 ))
264 {
265 /* Invalid length value */
266 goto err;
267 }
268 *outlen = (size_t)tmp[0];
269 memcpy(out, tmp + 4, *outlen);
270 rv = 1;
271 err:
272 OPENSSL_cleanse(tmp, inlen);
273 OPENSSL_free(tmp);
274 return rv;
275
276 }
277
278static int kek_wrap_key(unsigned char *out, size_t *outlen,
279 const unsigned char *in, size_t inlen, EVP_CIPHER_CTX *ctx)
280 {
281 size_t blocklen = EVP_CIPHER_CTX_block_size(ctx);
282 size_t olen;
283 int dummy;
284 /* First decide length of output buffer: need header and round up to
285 * multiple of block length.
286 */
287 olen = (inlen + 4 + blocklen - 1)/blocklen;
288 olen *= blocklen;
289 if (olen < 2 * blocklen)
290 {
291 /* Key too small */
292 return 0;
293 }
294 if (inlen > 0xFF)
295 {
296 /* Key too large */
297 return 0;
298 }
299 if (out)
300 {
301 /* Set header */
302 out[0] = (unsigned char)inlen;
303 out[1] = in[0] ^ 0xFF;
304 out[2] = in[1] ^ 0xFF;
305 out[3] = in[2] ^ 0xFF;
306 memcpy(out + 4, in, inlen);
307 /* Add random padding to end */
308 if (olen > inlen + 4)
309 RAND_pseudo_bytes(out + 4 + inlen, olen - 4 - inlen);
310 /* Encrypt twice */
311 EVP_EncryptUpdate(ctx, out, &dummy, out, olen);
312 EVP_EncryptUpdate(ctx, out, &dummy, out, olen);
313 }
314
315 *outlen = olen;
316
317 return 1;
318 }
319
320/* Encrypt/Decrypt content key in PWRI recipient info */
321
322int cms_RecipientInfo_pwri_crypt(CMS_ContentInfo *cms, CMS_RecipientInfo *ri,
323 int en_de)
324 {
325 CMS_EncryptedContentInfo *ec;
326 CMS_PasswordRecipientInfo *pwri;
327 const unsigned char *p = NULL;
328 int plen;
329 int r = 0;
330 X509_ALGOR *algtmp, *kekalg = NULL;
331 EVP_CIPHER_CTX kekctx;
332 const EVP_CIPHER *kekcipher;
333 unsigned char *key = NULL;
334 size_t keylen;
335
336 ec = cms->d.envelopedData->encryptedContentInfo;
337
338 pwri = ri->d.pwri;
339 EVP_CIPHER_CTX_init(&kekctx);
340
341 if (!pwri->pass)
342 {
343 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, CMS_R_NO_PASSWORD);
344 return 0;
345 }
346 algtmp = pwri->keyEncryptionAlgorithm;
347
348 if (!algtmp || OBJ_obj2nid(algtmp->algorithm) != NID_id_alg_PWRI_KEK)
349 {
350 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
351 CMS_R_UNSUPPORTED_KEY_ENCRYPTION_ALGORITHM);
352 return 0;
353 }
354
355 if (algtmp->parameter->type == V_ASN1_SEQUENCE)
356 {
357 p = algtmp->parameter->value.sequence->data;
358 plen = algtmp->parameter->value.sequence->length;
359 kekalg = d2i_X509_ALGOR(NULL, &p, plen);
360 }
361 if (kekalg == NULL)
362 {
363 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
364 CMS_R_INVALID_KEY_ENCRYPTION_PARAMETER);
365 return 0;
366 }
367
368 kekcipher = EVP_get_cipherbyobj(kekalg->algorithm);
369
370 if(!kekcipher)
371 {
372 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
373 CMS_R_UNKNOWN_CIPHER);
374 goto err;
375 }
376
377 /* Fixup cipher based on AlgorithmIdentifier to set IV etc */
378 if (!EVP_CipherInit_ex(&kekctx, kekcipher, NULL, NULL, NULL, en_de))
379 goto err;
380 EVP_CIPHER_CTX_set_padding(&kekctx, 0);
381 if(EVP_CIPHER_asn1_to_param(&kekctx, kekalg->parameter) < 0)
382 {
383 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
384 CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
385 goto err;
386 }
387
388 algtmp = pwri->keyDerivationAlgorithm;
389
390 /* Finish password based key derivation to setup key in "ctx" */
391
392 if (EVP_PBE_CipherInit(algtmp->algorithm,
393 (char *)pwri->pass, pwri->passlen,
394 algtmp->parameter, &kekctx, en_de) < 0)
395 {
396 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT, ERR_R_EVP_LIB);
397 goto err;
398 }
399
400 /* Finally wrap/unwrap the key */
401
402 if (en_de)
403 {
404
405 if (!kek_wrap_key(NULL, &keylen, ec->key, ec->keylen, &kekctx))
406 goto err;
407
408 key = OPENSSL_malloc(keylen);
409
410 if (!key)
411 goto err;
412
413 if (!kek_wrap_key(key, &keylen, ec->key, ec->keylen, &kekctx))
414 goto err;
415 pwri->encryptedKey->data = key;
416 pwri->encryptedKey->length = keylen;
417 }
418 else
419 {
420 key = OPENSSL_malloc(pwri->encryptedKey->length);
421
422 if (!key)
423 {
424 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
425 ERR_R_MALLOC_FAILURE);
426 goto err;
427 }
428 if (!kek_unwrap_key(key, &keylen,
429 pwri->encryptedKey->data,
430 pwri->encryptedKey->length, &kekctx))
431 {
432 CMSerr(CMS_F_CMS_RECIPIENTINFO_PWRI_CRYPT,
433 CMS_R_UNWRAP_FAILURE);
434 goto err;
435 }
436
437 ec->key = key;
438 ec->keylen = keylen;
439
440 }
441
442 r = 1;
443
444 err:
445
446 EVP_CIPHER_CTX_cleanup(&kekctx);
447
448 if (!r && key)
449 OPENSSL_free(key);
450 X509_ALGOR_free(kekalg);
451
452 return r;
453
454 }
diff --git a/src/lib/libcrypto/cms/cms_sd.c b/src/lib/libcrypto/cms/cms_sd.c
index e3192b9c57..77fbd13596 100644
--- a/src/lib/libcrypto/cms/cms_sd.c
+++ b/src/lib/libcrypto/cms/cms_sd.c
@@ -641,7 +641,8 @@ static int cms_SignerInfo_content_sign(CMS_ContentInfo *cms,
641 cms->d.signedData->encapContentInfo->eContentType; 641 cms->d.signedData->encapContentInfo->eContentType;
642 unsigned char md[EVP_MAX_MD_SIZE]; 642 unsigned char md[EVP_MAX_MD_SIZE];
643 unsigned int mdlen; 643 unsigned int mdlen;
644 EVP_DigestFinal_ex(&mctx, md, &mdlen); 644 if (!EVP_DigestFinal_ex(&mctx, md, &mdlen))
645 goto err;
645 if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest, 646 if (!CMS_signed_add1_attr_by_NID(si, NID_pkcs9_messageDigest,
646 V_ASN1_OCTET_STRING, 647 V_ASN1_OCTET_STRING,
647 md, mdlen)) 648 md, mdlen))