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