Fix CMS so that it still works with non fetchable algorithms.
[openssl.git] / crypto / cms / cms_enc.c
1 /*
2  * Copyright 2008-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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/rand.h>
17 #include "cms_local.h"
18
19 /* CMS EncryptedData Utilities */
20
21 /* Return BIO based on EncryptedContentInfo and key */
22
23 BIO *cms_EncryptedContent_init_bio(CMS_EncryptedContentInfo *ec,
24                                    const CMS_CTX *cms_ctx)
25 {
26     BIO *b;
27     EVP_CIPHER_CTX *ctx;
28     EVP_CIPHER *fetched_ciph = NULL;
29     const EVP_CIPHER *cipher = NULL;
30     X509_ALGOR *calg = ec->contentEncryptionAlgorithm;
31     unsigned char iv[EVP_MAX_IV_LENGTH], *piv = NULL;
32     unsigned char *tkey = NULL;
33     int len;
34     size_t tkeylen = 0;
35     int ok = 0;
36     int enc, keep_key = 0;
37
38     enc = ec->cipher ? 1 : 0;
39
40     b = BIO_new(BIO_f_cipher());
41     if (b == NULL) {
42         CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
43         return NULL;
44     }
45
46     BIO_get_cipher_ctx(b, &ctx);
47
48     (void)ERR_set_mark();
49     if (enc) {
50         cipher = ec->cipher;
51         /*
52          * If not keeping key set cipher to NULL so subsequent calls decrypt.
53          */
54         if (ec->key != NULL)
55             ec->cipher = NULL;
56     } else {
57         cipher = EVP_get_cipherbyobj(calg->algorithm);
58     }
59     if (cipher != NULL) {
60         fetched_ciph = EVP_CIPHER_fetch(cms_ctx->libctx, EVP_CIPHER_name(cipher),
61                                         cms_ctx->propq);
62         if (fetched_ciph != NULL)
63             cipher = fetched_ciph;
64     }
65     if (cipher == NULL) {
66         (void)ERR_clear_last_mark();
67         CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, CMS_R_UNKNOWN_CIPHER);
68         goto err;
69     }
70     (void)ERR_pop_to_mark();
71
72     if (EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc) <= 0) {
73         CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
74                CMS_R_CIPHER_INITIALISATION_ERROR);
75         goto err;
76     }
77
78     if (enc) {
79         int ivlen;
80         calg->algorithm = OBJ_nid2obj(EVP_CIPHER_CTX_type(ctx));
81         /* Generate a random IV if we need one */
82         ivlen = EVP_CIPHER_CTX_iv_length(ctx);
83         if (ivlen > 0) {
84             if (RAND_bytes_ex(cms_ctx->libctx, iv, ivlen) <= 0)
85                 goto err;
86             piv = iv;
87         }
88     } else if (EVP_CIPHER_asn1_to_param(ctx, calg->parameter) <= 0) {
89         CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
90                CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
91         goto err;
92     }
93     len = EVP_CIPHER_CTX_key_length(ctx);
94     if (len <= 0)
95         goto err;
96     tkeylen = (size_t)len;
97
98     /* Generate random session key */
99     if (!enc || !ec->key) {
100         tkey = OPENSSL_malloc(tkeylen);
101         if (tkey == NULL) {
102             CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
103             goto err;
104         }
105         if (EVP_CIPHER_CTX_rand_key(ctx, tkey) <= 0)
106             goto err;
107     }
108
109     if (!ec->key) {
110         ec->key = tkey;
111         ec->keylen = tkeylen;
112         tkey = NULL;
113         if (enc)
114             keep_key = 1;
115         else
116             ERR_clear_error();
117
118     }
119
120     if (ec->keylen != tkeylen) {
121         /* If necessary set key length */
122         if (EVP_CIPHER_CTX_set_key_length(ctx, ec->keylen) <= 0) {
123             /*
124              * Only reveal failure if debugging so we don't leak information
125              * which may be useful in MMA.
126              */
127             if (enc || ec->debug) {
128                 CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
129                        CMS_R_INVALID_KEY_LENGTH);
130                 goto err;
131             } else {
132                 /* Use random key */
133                 OPENSSL_clear_free(ec->key, ec->keylen);
134                 ec->key = tkey;
135                 ec->keylen = tkeylen;
136                 tkey = NULL;
137                 ERR_clear_error();
138             }
139         }
140     }
141
142     if (EVP_CipherInit_ex(ctx, NULL, NULL, ec->key, piv, enc) <= 0) {
143         CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
144                CMS_R_CIPHER_INITIALISATION_ERROR);
145         goto err;
146     }
147     if (enc) {
148         calg->parameter = ASN1_TYPE_new();
149         if (calg->parameter == NULL) {
150             CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO, ERR_R_MALLOC_FAILURE);
151             goto err;
152         }
153         if (EVP_CIPHER_param_to_asn1(ctx, calg->parameter) <= 0) {
154             CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT_BIO,
155                    CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR);
156             goto err;
157         }
158         /* If parameter type not set omit parameter */
159         if (calg->parameter->type == V_ASN1_UNDEF) {
160             ASN1_TYPE_free(calg->parameter);
161             calg->parameter = NULL;
162         }
163     }
164     ok = 1;
165
166  err:
167     EVP_CIPHER_free(fetched_ciph);
168     if (!keep_key || !ok) {
169         OPENSSL_clear_free(ec->key, ec->keylen);
170         ec->key = NULL;
171     }
172     OPENSSL_clear_free(tkey, tkeylen);
173     if (ok)
174         return b;
175     BIO_free(b);
176     return NULL;
177 }
178
179 int cms_EncryptedContent_init(CMS_EncryptedContentInfo *ec,
180                               const EVP_CIPHER *cipher,
181                               const unsigned char *key, size_t keylen,
182                               const CMS_CTX *cms_ctx)
183 {
184     ec->cipher = cipher;
185     if (key) {
186         if ((ec->key = OPENSSL_malloc(keylen)) == NULL) {
187             CMSerr(CMS_F_CMS_ENCRYPTEDCONTENT_INIT, ERR_R_MALLOC_FAILURE);
188             return 0;
189         }
190         memcpy(ec->key, key, keylen);
191     }
192     ec->keylen = keylen;
193     if (cipher != NULL)
194         ec->contentType = OBJ_nid2obj(NID_pkcs7_data);
195     return 1;
196 }
197
198 int CMS_EncryptedData_set1_key(CMS_ContentInfo *cms, const EVP_CIPHER *ciph,
199                                const unsigned char *key, size_t keylen)
200 {
201     CMS_EncryptedContentInfo *ec;
202
203     if (!key || !keylen) {
204         CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NO_KEY);
205         return 0;
206     }
207     if (ciph) {
208         cms->d.encryptedData = M_ASN1_new_of(CMS_EncryptedData);
209         if (!cms->d.encryptedData) {
210             CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, ERR_R_MALLOC_FAILURE);
211             return 0;
212         }
213         cms->contentType = OBJ_nid2obj(NID_pkcs7_encrypted);
214         cms->d.encryptedData->version = 0;
215     } else if (OBJ_obj2nid(cms->contentType) != NID_pkcs7_encrypted) {
216         CMSerr(CMS_F_CMS_ENCRYPTEDDATA_SET1_KEY, CMS_R_NOT_ENCRYPTED_DATA);
217         return 0;
218     }
219     ec = cms->d.encryptedData->encryptedContentInfo;
220     return cms_EncryptedContent_init(ec, ciph, key, keylen, cms_get0_cmsctx(cms));
221 }
222
223 BIO *cms_EncryptedData_init_bio(const CMS_ContentInfo *cms)
224 {
225     CMS_EncryptedData *enc = cms->d.encryptedData;
226     if (enc->encryptedContentInfo->cipher && enc->unprotectedAttrs)
227         enc->version = 2;
228     return cms_EncryptedContent_init_bio(enc->encryptedContentInfo,
229                                          cms_get0_cmsctx(cms));
230 }