Coverage Report

Created: 2025-07-23 06:49

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