Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/rsa/rsa_sign.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2024 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
/*
11
 * RSA low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdio.h>
17
#include "internal/cryptlib.h"
18
#include <openssl/bn.h>
19
#include <openssl/rsa.h>
20
#include <openssl/objects.h>
21
#ifndef FIPS_MODULE
22
#ifndef OPENSSL_NO_MD2
23
#include <openssl/md2.h> /* uses MD2_DIGEST_LENGTH */
24
#endif
25
#ifndef OPENSSL_NO_MD4
26
#include <openssl/md4.h> /* uses MD4_DIGEST_LENGTH */
27
#endif
28
#ifndef OPENSSL_NO_MD5
29
#include <openssl/md5.h> /* uses MD5_DIGEST_LENGTH */
30
#endif
31
#ifndef OPENSSL_NO_MDC2
32
#include <openssl/mdc2.h> /* uses MDC2_DIGEST_LENGTH */
33
#endif
34
#ifndef OPENSSL_NO_RMD160
35
#include <openssl/ripemd.h> /* uses RIPEMD160_DIGEST_LENGTH */
36
#endif
37
#ifndef OPENSSL_NO_SM3
38
#include "internal/sm3.h" /* uses SM3_DIGEST_LENGTH */
39
#endif
40
#endif
41
#include <openssl/sha.h> /* uses SHA???_DIGEST_LENGTH */
42
#include "crypto/rsa.h"
43
#include "rsa_local.h"
44
45
/*
46
 * The general purpose ASN1 code is not available inside the FIPS provider.
47
 * To remove the dependency RSASSA-PKCS1-v1_5 DigestInfo encodings can be
48
 * treated as a special case by pregenerating the required ASN1 encoding.
49
 * This encoding will also be shared by the default provider.
50
 *
51
 * The EMSA-PKCS1-v1_5 encoding method includes an ASN.1 value of type
52
 * DigestInfo, where the type DigestInfo has the syntax
53
 *
54
 *     DigestInfo ::= SEQUENCE {
55
 *         digestAlgorithm DigestAlgorithm,
56
 *         digest OCTET STRING
57
 *     }
58
 *
59
 *     DigestAlgorithm ::= AlgorithmIdentifier {
60
 *         {PKCS1-v1-5DigestAlgorithms}
61
 *     }
62
 *
63
 * The AlgorithmIdentifier is a sequence containing the digest OID and
64
 * parameters (a value of type NULL).
65
 *
66
 * The ENCODE_DIGESTINFO_SHA() and ENCODE_DIGESTINFO_MD() macros define an
67
 * initialized array containing the DER encoded DigestInfo for the specified
68
 * SHA or MD digest. The content of the OCTET STRING is not included.
69
 * |name| is the digest name.
70
 * |n| is last byte in the encoded OID for the digest.
71
 * |sz| is the digest length in bytes. It must not be greater than 110.
72
 */
73
74
#define ASN1_SEQUENCE 0x30
75
#define ASN1_OCTET_STRING 0x04
76
#define ASN1_NULL 0x05
77
#define ASN1_OID 0x06
78
79
/* SHA OIDs are of the form: (2 16 840 1 101 3 4 2 |n|) */
80
#define ENCODE_DIGESTINFO_SHA(name, n, sz)                           \
81
    static const unsigned char digestinfo_##name##_der[] = {         \
82
        ASN1_SEQUENCE, 0x11 + sz,                                    \
83
        ASN1_SEQUENCE, 0x0d,                                         \
84
        ASN1_OID, 0x09, 2 * 40 + 16, 0x86, 0x48, 1, 101, 3, 4, 2, n, \
85
        ASN1_NULL, 0x00,                                             \
86
        ASN1_OCTET_STRING, sz                                        \
87
    };
88
89
/* MD2, MD4 and MD5 OIDs are of the form: (1 2 840 113549 2 |n|) */
90
#define ENCODE_DIGESTINFO_MD(name, n, sz)                               \
91
    static const unsigned char digestinfo_##name##_der[] = {            \
92
        ASN1_SEQUENCE, 0x10 + sz,                                       \
93
        ASN1_SEQUENCE, 0x0c,                                            \
94
        ASN1_OID, 0x08, 1 * 40 + 2, 0x86, 0x48, 0x86, 0xf7, 0x0d, 2, n, \
95
        ASN1_NULL, 0x00,                                                \
96
        ASN1_OCTET_STRING, sz                                           \
97
    };
98
99
#ifndef FIPS_MODULE
100
#ifndef OPENSSL_NO_MD2
101
ENCODE_DIGESTINFO_MD(md2, 0x02, MD2_DIGEST_LENGTH)
102
#endif
103
#ifndef OPENSSL_NO_MD4
104
ENCODE_DIGESTINFO_MD(md4, 0x03, MD4_DIGEST_LENGTH)
105
#endif
106
#ifndef OPENSSL_NO_MD5
107
ENCODE_DIGESTINFO_MD(md5, 0x05, MD5_DIGEST_LENGTH)
108
#endif
109
#ifndef OPENSSL_NO_MDC2
110
/* MDC-2 (2 5 8 3 101) */
111
static const unsigned char digestinfo_mdc2_der[] = {
112
    ASN1_SEQUENCE, 0x0c + MDC2_DIGEST_LENGTH,
113
    ASN1_SEQUENCE, 0x08,
114
    ASN1_OID, 0x04, 2 * 40 + 5, 8, 3, 101,
115
    ASN1_NULL, 0x00,
116
    ASN1_OCTET_STRING, MDC2_DIGEST_LENGTH
117
};
118
#endif
119
#ifndef OPENSSL_NO_RMD160
120
/* RIPEMD160 (1 3 36 3 2 1) */
121
static const unsigned char digestinfo_ripemd160_der[] = {
122
    ASN1_SEQUENCE, 0x0d + RIPEMD160_DIGEST_LENGTH,
123
    ASN1_SEQUENCE, 0x09,
124
    ASN1_OID, 0x05, 1 * 40 + 3, 36, 3, 2, 1,
125
    ASN1_NULL, 0x00,
126
    ASN1_OCTET_STRING, RIPEMD160_DIGEST_LENGTH
127
};
128
#endif
129
#ifndef OPENSSL_NO_SM3
130
/* SM3 (1 2 156 10197 1 401) */
131
static const unsigned char digestinfo_sm3_der[] = {
132
    ASN1_SEQUENCE, 0x10 + SM3_DIGEST_LENGTH,
133
    ASN1_SEQUENCE, 0x0c,
134
    ASN1_OID, 0x08, 1 * 40 + 2, 0x81, 0x1c, 0xcf, 0x55, 1, 0x83, 0x78,
135
    ASN1_NULL, 0x00,
136
    ASN1_OCTET_STRING, SM3_DIGEST_LENGTH
137
};
138
#endif
139
#endif /* FIPS_MODULE */
140
141
/* SHA-1 (1 3 14 3 2 26) */
142
static const unsigned char digestinfo_sha1_der[] = {
143
    ASN1_SEQUENCE, 0x0d + SHA_DIGEST_LENGTH,
144
    ASN1_SEQUENCE, 0x09,
145
    ASN1_OID, 0x05, 1 * 40 + 3, 14, 3, 2, 26,
146
    ASN1_NULL, 0x00,
147
    ASN1_OCTET_STRING, SHA_DIGEST_LENGTH
148
};
149
150
ENCODE_DIGESTINFO_SHA(sha256, 0x01, SHA256_DIGEST_LENGTH)
151
ENCODE_DIGESTINFO_SHA(sha384, 0x02, SHA384_DIGEST_LENGTH)
152
ENCODE_DIGESTINFO_SHA(sha512, 0x03, SHA512_DIGEST_LENGTH)
153
ENCODE_DIGESTINFO_SHA(sha224, 0x04, SHA224_DIGEST_LENGTH)
154
ENCODE_DIGESTINFO_SHA(sha512_224, 0x05, SHA224_DIGEST_LENGTH)
155
ENCODE_DIGESTINFO_SHA(sha512_256, 0x06, SHA256_DIGEST_LENGTH)
156
ENCODE_DIGESTINFO_SHA(sha3_224, 0x07, SHA224_DIGEST_LENGTH)
157
ENCODE_DIGESTINFO_SHA(sha3_256, 0x08, SHA256_DIGEST_LENGTH)
158
ENCODE_DIGESTINFO_SHA(sha3_384, 0x09, SHA384_DIGEST_LENGTH)
159
ENCODE_DIGESTINFO_SHA(sha3_512, 0x0a, SHA512_DIGEST_LENGTH)
160
161
#define MD_CASE(name)                           \
162
0
    case NID_##name:                            \
163
0
        *len = sizeof(digestinfo_##name##_der); \
164
0
        return digestinfo_##name##_der;
165
166
const unsigned char *ossl_rsa_digestinfo_encoding(int md_nid, size_t *len)
167
0
{
168
0
    switch (md_nid) {
169
0
#ifndef FIPS_MODULE
170
0
#ifndef OPENSSL_NO_MDC2
171
0
        MD_CASE(mdc2)
172
0
#endif
173
#ifndef OPENSSL_NO_MD2
174
        MD_CASE(md2)
175
#endif
176
0
#ifndef OPENSSL_NO_MD4
177
0
        MD_CASE(md4)
178
0
#endif
179
0
#ifndef OPENSSL_NO_MD5
180
0
        MD_CASE(md5)
181
0
#endif
182
0
#ifndef OPENSSL_NO_RMD160
183
0
        MD_CASE(ripemd160)
184
0
#endif
185
0
#ifndef OPENSSL_NO_SM3
186
0
        MD_CASE(sm3)
187
0
#endif
188
0
#endif /* FIPS_MODULE */
189
0
        MD_CASE(sha1)
190
0
        MD_CASE(sha224)
191
0
        MD_CASE(sha256)
192
0
        MD_CASE(sha384)
193
0
        MD_CASE(sha512)
194
0
        MD_CASE(sha512_224)
195
0
        MD_CASE(sha512_256)
196
0
        MD_CASE(sha3_224)
197
0
        MD_CASE(sha3_256)
198
0
        MD_CASE(sha3_384)
199
0
        MD_CASE(sha3_512)
200
0
    default:
201
0
        return NULL;
202
0
    }
203
0
}
204
205
#define MD_NID_CASE(name, sz) \
206
0
    case NID_##name:          \
207
0
        return sz;
208
209
static int digest_sz_from_nid(int nid)
210
0
{
211
0
    switch (nid) {
212
0
#ifndef FIPS_MODULE
213
0
#ifndef OPENSSL_NO_MDC2
214
0
        MD_NID_CASE(mdc2, MDC2_DIGEST_LENGTH)
215
0
#endif
216
#ifndef OPENSSL_NO_MD2
217
        MD_NID_CASE(md2, MD2_DIGEST_LENGTH)
218
#endif
219
0
#ifndef OPENSSL_NO_MD4
220
0
        MD_NID_CASE(md4, MD4_DIGEST_LENGTH)
221
0
#endif
222
0
#ifndef OPENSSL_NO_MD5
223
0
        MD_NID_CASE(md5, MD5_DIGEST_LENGTH)
224
0
#endif
225
0
#ifndef OPENSSL_NO_RMD160
226
0
        MD_NID_CASE(ripemd160, RIPEMD160_DIGEST_LENGTH)
227
0
#endif
228
0
#endif /* FIPS_MODULE */
229
0
        MD_NID_CASE(sha1, SHA_DIGEST_LENGTH)
230
0
        MD_NID_CASE(sha224, SHA224_DIGEST_LENGTH)
231
0
        MD_NID_CASE(sha256, SHA256_DIGEST_LENGTH)
232
0
        MD_NID_CASE(sha384, SHA384_DIGEST_LENGTH)
233
0
        MD_NID_CASE(sha512, SHA512_DIGEST_LENGTH)
234
0
        MD_NID_CASE(sha512_224, SHA224_DIGEST_LENGTH)
235
0
        MD_NID_CASE(sha512_256, SHA256_DIGEST_LENGTH)
236
0
        MD_NID_CASE(sha3_224, SHA224_DIGEST_LENGTH)
237
0
        MD_NID_CASE(sha3_256, SHA256_DIGEST_LENGTH)
238
0
        MD_NID_CASE(sha3_384, SHA384_DIGEST_LENGTH)
239
0
        MD_NID_CASE(sha3_512, SHA512_DIGEST_LENGTH)
240
0
    default:
241
0
        return 0;
242
0
    }
243
0
}
244
245
/* Size of an SSL signature: MD5+SHA1 */
246
0
#define SSL_SIG_LENGTH 36
247
248
/*
249
 * Encodes a DigestInfo prefix of hash |type| and digest |m|, as
250
 * described in EMSA-PKCS1-v1_5-ENCODE, RFC 3447 section 9.2 step 2. This
251
 * encodes the DigestInfo (T and tLen) but does not add the padding.
252
 *
253
 * On success, it returns one and sets |*out| to a newly allocated buffer
254
 * containing the result and |*out_len| to its length. The caller must free
255
 * |*out| with OPENSSL_free(). Otherwise, it returns zero.
256
 */
257
static int encode_pkcs1(unsigned char **out, size_t *out_len, int type,
258
    const unsigned char *m, size_t m_len)
259
0
{
260
0
    size_t di_prefix_len, dig_info_len;
261
0
    const unsigned char *di_prefix;
262
0
    unsigned char *dig_info;
263
264
0
    if (type == NID_undef) {
265
0
        ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE);
266
0
        return 0;
267
0
    }
268
0
    di_prefix = ossl_rsa_digestinfo_encoding(type, &di_prefix_len);
269
0
    if (di_prefix == NULL) {
270
0
        ERR_raise(ERR_LIB_RSA,
271
0
            RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
272
0
        return 0;
273
0
    }
274
0
    dig_info_len = di_prefix_len + m_len;
275
0
    dig_info = OPENSSL_malloc(dig_info_len);
276
0
    if (dig_info == NULL)
277
0
        return 0;
278
0
    memcpy(dig_info, di_prefix, di_prefix_len);
279
0
    memcpy(dig_info + di_prefix_len, m, m_len);
280
281
0
    *out = dig_info;
282
0
    *out_len = dig_info_len;
283
0
    return 1;
284
0
}
285
286
int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
287
    unsigned char *sigret, unsigned int *siglen, RSA *rsa)
288
0
{
289
0
    int encrypt_len, ret = 0;
290
0
    size_t encoded_len = 0;
291
0
    unsigned char *tmps = NULL;
292
0
    const unsigned char *encoded = NULL;
293
294
0
#ifndef FIPS_MODULE
295
0
    if (rsa->meth->rsa_sign != NULL)
296
0
        return rsa->meth->rsa_sign(type, m, m_len, sigret, siglen, rsa) > 0;
297
0
#endif /* FIPS_MODULE */
298
299
    /* Compute the encoded digest. */
300
0
    if (type == NID_md5_sha1) {
301
        /*
302
         * NID_md5_sha1 corresponds to the MD5/SHA1 combination in TLS 1.1 and
303
         * earlier. It has no DigestInfo wrapper but otherwise is
304
         * RSASSA-PKCS1-v1_5.
305
         */
306
0
        if (m_len != SSL_SIG_LENGTH) {
307
0
            ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH);
308
0
            return 0;
309
0
        }
310
0
        encoded_len = SSL_SIG_LENGTH;
311
0
        encoded = m;
312
0
    } else {
313
0
        if (!encode_pkcs1(&tmps, &encoded_len, type, m, m_len))
314
0
            goto err;
315
0
        encoded = tmps;
316
0
    }
317
318
0
    if (encoded_len + RSA_PKCS1_PADDING_SIZE > (size_t)RSA_size(rsa)) {
319
0
        ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
320
0
        goto err;
321
0
    }
322
0
    encrypt_len = RSA_private_encrypt((int)encoded_len, encoded, sigret, rsa,
323
0
        RSA_PKCS1_PADDING);
324
0
    if (encrypt_len <= 0)
325
0
        goto err;
326
327
0
    *siglen = encrypt_len;
328
0
    ret = 1;
329
330
0
err:
331
0
    OPENSSL_clear_free(tmps, encoded_len);
332
0
    return ret;
333
0
}
334
335
/*
336
 * Verify an RSA signature in |sigbuf| using |rsa|.
337
 * |type| is the NID of the digest algorithm to use.
338
 * If |rm| is NULL, it verifies the signature for digest |m|, otherwise
339
 * it recovers the digest from the signature, writing the digest to |rm| and
340
 * the length to |*prm_len|.
341
 *
342
 * It returns one on successful verification or zero otherwise.
343
 */
344
int ossl_rsa_verify(int type, const unsigned char *m, unsigned int m_len,
345
    unsigned char *rm, size_t *prm_len,
346
    const unsigned char *sigbuf, size_t siglen, RSA *rsa)
347
0
{
348
0
    int len, ret = 0;
349
0
    size_t decrypt_len, encoded_len = 0;
350
0
    unsigned char *decrypt_buf = NULL, *encoded = NULL;
351
352
0
    if (siglen != (size_t)RSA_size(rsa)) {
353
0
        ERR_raise(ERR_LIB_RSA, RSA_R_WRONG_SIGNATURE_LENGTH);
354
0
        return 0;
355
0
    }
356
357
    /* Recover the encoded digest. */
358
0
    decrypt_buf = OPENSSL_malloc(siglen);
359
0
    if (decrypt_buf == NULL)
360
0
        goto err;
361
362
0
    len = RSA_public_decrypt((int)siglen, sigbuf, decrypt_buf, rsa,
363
0
        RSA_PKCS1_PADDING);
364
0
    if (len <= 0)
365
0
        goto err;
366
0
    decrypt_len = len;
367
368
0
#ifndef FIPS_MODULE
369
0
    if (type == NID_md5_sha1) {
370
        /*
371
         * NID_md5_sha1 corresponds to the MD5/SHA1 combination in TLS 1.1 and
372
         * earlier. It has no DigestInfo wrapper but otherwise is
373
         * RSASSA-PKCS1-v1_5.
374
         */
375
0
        if (decrypt_len != SSL_SIG_LENGTH) {
376
0
            ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
377
0
            goto err;
378
0
        }
379
380
0
        if (rm != NULL) {
381
0
            memcpy(rm, decrypt_buf, SSL_SIG_LENGTH);
382
0
            *prm_len = SSL_SIG_LENGTH;
383
0
        } else {
384
0
            if (m_len != SSL_SIG_LENGTH) {
385
0
                ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH);
386
0
                goto err;
387
0
            }
388
389
0
            if (memcmp(decrypt_buf, m, SSL_SIG_LENGTH) != 0) {
390
0
                ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
391
0
                goto err;
392
0
            }
393
0
        }
394
0
    } else if (type == NID_mdc2 && decrypt_len == 2 + 16
395
0
        && decrypt_buf[0] == 0x04 && decrypt_buf[1] == 0x10) {
396
        /*
397
         * Oddball MDC2 case: signature can be OCTET STRING. check for correct
398
         * tag and length octets.
399
         */
400
0
        if (rm != NULL) {
401
0
            memcpy(rm, decrypt_buf + 2, 16);
402
0
            *prm_len = 16;
403
0
        } else {
404
0
            if (m_len != 16) {
405
0
                ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH);
406
0
                goto err;
407
0
            }
408
409
0
            if (memcmp(m, decrypt_buf + 2, 16) != 0) {
410
0
                ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
411
0
                goto err;
412
0
            }
413
0
        }
414
0
    } else
415
0
#endif /* FIPS_MODULE */
416
0
    {
417
        /*
418
         * If recovering the digest, extract a digest-sized output from the end
419
         * of |decrypt_buf| for |encode_pkcs1|, then compare the decryption
420
         * output as in a standard verification.
421
         */
422
0
        if (rm != NULL) {
423
0
            len = digest_sz_from_nid(type);
424
425
0
            if (len <= 0)
426
0
                goto err;
427
0
            m_len = (unsigned int)len;
428
0
            if (m_len > decrypt_len) {
429
0
                ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
430
0
                goto err;
431
0
            }
432
0
            m = decrypt_buf + decrypt_len - m_len;
433
0
        }
434
435
        /* Construct the encoded digest and ensure it matches. */
436
0
        if (!encode_pkcs1(&encoded, &encoded_len, type, m, m_len))
437
0
            goto err;
438
439
0
        if (encoded_len != decrypt_len
440
0
            || memcmp(encoded, decrypt_buf, encoded_len) != 0) {
441
0
            ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
442
0
            goto err;
443
0
        }
444
445
        /* Output the recovered digest. */
446
0
        if (rm != NULL) {
447
0
            memcpy(rm, m, m_len);
448
0
            *prm_len = m_len;
449
0
        }
450
0
    }
451
452
0
    ret = 1;
453
454
0
err:
455
0
    OPENSSL_clear_free(encoded, encoded_len);
456
0
    OPENSSL_clear_free(decrypt_buf, siglen);
457
0
    return ret;
458
0
}
459
460
int RSA_verify(int type, const unsigned char *m, unsigned int m_len,
461
    const unsigned char *sigbuf, unsigned int siglen, RSA *rsa)
462
0
{
463
464
0
    if (rsa->meth->rsa_verify != NULL)
465
0
        return rsa->meth->rsa_verify(type, m, m_len, sigbuf, siglen, rsa);
466
467
0
    return ossl_rsa_verify(type, m, m_len, NULL, NULL, sigbuf, siglen, rsa);
468
0
}