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