Coverage Report

Created: 2025-12-31 06:58

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