Coverage Report

Created: 2025-06-13 06:58

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