Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/cms/cms_rsa.c

raw
1 /*
2 * Copyright 2006-2026 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 <assert.h>
11 #include <openssl/cms.h>
12 #include <openssl/err.h>
13 #include <openssl/core_names.h>
14 #include "crypto/asn1.h"
15 #include "crypto/rsa.h"
16 #include "crypto/evp.h"
17 #include "cms_local.h"
18
19 static RSA_OAEP_PARAMS *rsa_oaep_decode(const X509_ALGOR *alg)
20 {
21 RSA_OAEP_PARAMS *oaep;
22
23 oaep = ASN1_TYPE_unpack_sequence(ASN1_ITEM_rptr(RSA_OAEP_PARAMS),
24 alg->parameter);
25
26 if (oaep == NULL)
27 return NULL;
28
29 if (oaep->maskGenFunc != NULL) {
30 oaep->maskHash = ossl_x509_algor_mgf1_decode(oaep->maskGenFunc);
31 if (oaep->maskHash == NULL) {
32 RSA_OAEP_PARAMS_free(oaep);
33 return NULL;
34 }
35 }
36 return oaep;
37 }
38
39 static int rsa_cms_decrypt(CMS_RecipientInfo *ri)
40 {
41 EVP_PKEY_CTX *pkctx;
42 X509_ALGOR *cmsalg;
43 int nid;
44 int rv = -1;
45 const unsigned char *label = NULL;
46 int labellen = 0;
47 const EVP_MD *mgf1md = NULL, *md = NULL;
48 RSA_OAEP_PARAMS *oaep;
49 const ASN1_OBJECT *aoid;
50 const void *parameter = NULL;
51 int ptype = 0;
52
53 pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
54 if (pkctx == NULL)
55 return 0;
56 if (!CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &cmsalg))
57 return -1;
58 nid = OBJ_obj2nid(cmsalg->algorithm);
59 if (nid == NID_rsaEncryption)
60 return 1;
61 if (nid != NID_rsaesOaep) {
62 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_ENCRYPTION_TYPE);
63 return -1;
64 }
65 /* Decode OAEP parameters */
66 oaep = rsa_oaep_decode(cmsalg);
67
68 if (oaep == NULL) {
69 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_OAEP_PARAMETERS);
70 goto err;
71 }
72
73 mgf1md = ossl_x509_algor_get_md(oaep->maskHash);
74 if (mgf1md == NULL)
75 goto err;
76 md = ossl_x509_algor_get_md(oaep->hashFunc);
77 if (md == NULL)
78 goto err;
79
80 if (oaep->pSourceFunc != NULL) {
81 X509_ALGOR_get0(&aoid, &ptype, &parameter, oaep->pSourceFunc);
82
83 if (OBJ_obj2nid(aoid) != NID_pSpecified) {
84 ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_LABEL_SOURCE);
85 goto err;
86 }
87 if (ptype != V_ASN1_OCTET_STRING) {
88 ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_LABEL);
89 goto err;
90 }
91
92 label = ASN1_STRING_get0_data(parameter);
93 labellen = ASN1_STRING_length(parameter);
94 }
95
96 if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0)
97 goto err;
98 if (EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, md) <= 0)
99 goto err;
100 if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0)
101 goto err;
102 if (label != NULL) {
103 unsigned char *dup_label = OPENSSL_memdup(label, labellen);
104
105 if (dup_label == NULL)
106 goto err;
107
108 if (EVP_PKEY_CTX_set0_rsa_oaep_label(pkctx, dup_label, labellen) <= 0) {
109 OPENSSL_free(dup_label);
110 goto err;
111 }
112 }
113 /* Carry on */
114 rv = 1;
115
116 err:
117 RSA_OAEP_PARAMS_free(oaep);
118 return rv;
119 }
120
121 static int rsa_cms_encrypt(CMS_RecipientInfo *ri)
122 {
123 const EVP_MD *md, *mgf1md;
124 RSA_OAEP_PARAMS *oaep = NULL;
125 ASN1_STRING *os = NULL;
126 ASN1_OCTET_STRING *los = NULL;
127 X509_ALGOR *alg;
128 EVP_PKEY_CTX *pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri);
129 int pad_mode = RSA_PKCS1_PADDING, rv = 0, labellen;
130 unsigned char *label;
131
132 if (CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &alg) <= 0)
133 return 0;
134 if (pkctx != NULL) {
135 if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
136 return 0;
137 }
138 if (pad_mode == RSA_PKCS1_PADDING)
139 return X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption),
140 V_ASN1_NULL, NULL);
141
142 /* Not supported */
143 if (pad_mode != RSA_PKCS1_OAEP_PADDING)
144 return 0;
145 if (EVP_PKEY_CTX_get_rsa_oaep_md(pkctx, &md) <= 0)
146 goto err;
147 if (EVP_PKEY_CTX_get_rsa_mgf1_md(pkctx, &mgf1md) <= 0)
148 goto err;
149 labellen = EVP_PKEY_CTX_get0_rsa_oaep_label(pkctx, &label);
150 if (labellen < 0)
151 goto err;
152 oaep = RSA_OAEP_PARAMS_new();
153 if (oaep == NULL)
154 goto err;
155 if (!ossl_x509_algor_new_from_md(&oaep->hashFunc, md))
156 goto err;
157 if (!ossl_x509_algor_md_to_mgf1(&oaep->maskGenFunc, mgf1md))
158 goto err;
159 if (labellen > 0) {
160 los = ASN1_OCTET_STRING_new();
161
162 if (los == NULL)
163 goto err;
164 if (!ASN1_OCTET_STRING_set(los, label, labellen))
165 goto err;
166
167 oaep->pSourceFunc = ossl_X509_ALGOR_from_nid(NID_pSpecified,
168 V_ASN1_OCTET_STRING, los);
169 if (oaep->pSourceFunc == NULL)
170 goto err;
171
172 los = NULL;
173 }
174 /* create string with oaep parameter encoding. */
175 if (!ASN1_item_pack(oaep, ASN1_ITEM_rptr(RSA_OAEP_PARAMS), &os))
176 goto err;
177 if (!X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaesOaep), V_ASN1_SEQUENCE, os))
178 goto err;
179 os = NULL;
180 rv = 1;
181 err:
182 RSA_OAEP_PARAMS_free(oaep);
183 ASN1_STRING_free(os);
184 ASN1_OCTET_STRING_free(los);
185 return rv;
186 }
187
188 int ossl_cms_rsa_envelope(CMS_RecipientInfo *ri, int decrypt)
189 {
190 assert(decrypt == 0 || decrypt == 1);
191
192 if (decrypt == 1)
193 return rsa_cms_decrypt(ri);
194
195 if (decrypt == 0)
196 return rsa_cms_encrypt(ri);
197
198 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
199 return 0;
200 }
201
202 static int rsa_cms_sign(CMS_SignerInfo *si)
203 {
204 int pad_mode = RSA_PKCS1_PADDING;
205 X509_ALGOR *alg;
206 EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
207 unsigned char aid[128];
208 const unsigned char *pp = aid;
209 size_t aid_len = 0;
210 OSSL_PARAM params[2];
211
212 CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
213 if (pkctx != NULL) {
214 if (EVP_PKEY_CTX_get_rsa_padding(pkctx, &pad_mode) <= 0)
215 return 0;
216 }
217 if (pad_mode == RSA_PKCS1_PADDING)
218 return X509_ALGOR_set0(alg, OBJ_nid2obj(NID_rsaEncryption),
219 V_ASN1_NULL, NULL);
220
221 /* We don't support it */
222 if (pad_mode != RSA_PKCS1_PSS_PADDING)
223 return 0;
224
225 if (evp_pkey_ctx_is_legacy(pkctx)) {
226 /* No provider -> we cannot query it for algorithm ID. */
227 ASN1_STRING *os = NULL;
228
229 os = ossl_rsa_ctx_to_pss_string(pkctx);
230 if (os == NULL)
231 return 0;
232 if (X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_PKEY_RSA_PSS), V_ASN1_SEQUENCE, os))
233 return 1;
234 ASN1_STRING_free(os);
235 return 0;
236 }
237
238 params[0] = OSSL_PARAM_construct_octet_string(
239 OSSL_SIGNATURE_PARAM_ALGORITHM_ID, aid, sizeof(aid));
240 params[1] = OSSL_PARAM_construct_end();
241
242 if (EVP_PKEY_CTX_get_params(pkctx, params) <= 0)
243 return 0;
244 if ((aid_len = params[0].return_size) == 0)
245 return 0;
246 if (d2i_X509_ALGOR(&alg, &pp, (long)aid_len) == NULL)
247 return 0;
248 return 1;
249 }
250
251 static int rsa_cms_verify(CMS_SignerInfo *si)
252 {
253 int nid, nid2;
254 X509_ALGOR *alg;
255 EVP_PKEY_CTX *pkctx = CMS_SignerInfo_get0_pkey_ctx(si);
256 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pkctx);
257
258 CMS_SignerInfo_get0_algs(si, NULL, NULL, NULL, &alg);
259 nid = OBJ_obj2nid(alg->algorithm);
260 if (nid == EVP_PKEY_RSA_PSS)
261 return ossl_rsa_pss_to_ctx(NULL, pkctx, alg, NULL) > 0;
262 /* Only PSS allowed for PSS keys */
263 if (EVP_PKEY_is_a(pkey, "RSA-PSS")) {
264 ERR_raise(ERR_LIB_RSA, RSA_R_ILLEGAL_OR_UNSUPPORTED_PADDING_MODE);
265 return 0;
266 }
267 if (nid == NID_rsaEncryption)
268 return 1;
269 /* Workaround for some implementation that use a signature OID */
270 if (OBJ_find_sigid_algs(nid, NULL, &nid2)) {
271 if (nid2 == NID_rsaEncryption)
272 return 1;
273 }
274 return 0;
275 }
276
277 int ossl_cms_rsa_sign(CMS_SignerInfo *si, int verify)
278 {
279 assert(verify == 0 || verify == 1);
280
281 if (verify == 1)
282 return rsa_cms_verify(si);
283
284 if (verify == 0)
285 return rsa_cms_sign(si);
286
287 ERR_raise(ERR_LIB_CMS, CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE);
288 return 0;
289 }