Coverage Report

Created: 2026-09-12 06:55

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