xref: /freebsd/crypto/openssl/crypto/rsa/rsa_sign.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 1995-2025 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     case NID_##name:                            \
163         *len = sizeof(digestinfo_##name##_der); \
164         return digestinfo_##name##_der;
165 
ossl_rsa_digestinfo_encoding(int md_nid,size_t * len)166 const unsigned char *ossl_rsa_digestinfo_encoding(int md_nid, size_t *len)
167 {
168     switch (md_nid) {
169 #ifndef FIPS_MODULE
170 #ifndef OPENSSL_NO_MDC2
171         MD_CASE(mdc2)
172 #endif
173 #ifndef OPENSSL_NO_MD2
174         MD_CASE(md2)
175 #endif
176 #ifndef OPENSSL_NO_MD4
177         MD_CASE(md4)
178 #endif
179 #ifndef OPENSSL_NO_MD5
180         MD_CASE(md5)
181 #endif
182 #ifndef OPENSSL_NO_RMD160
183         MD_CASE(ripemd160)
184 #endif
185 #ifndef OPENSSL_NO_SM3
186         MD_CASE(sm3)
187 #endif
188 #endif /* FIPS_MODULE */
189         MD_CASE(sha1)
190         MD_CASE(sha224)
191         MD_CASE(sha256)
192         MD_CASE(sha384)
193         MD_CASE(sha512)
194         MD_CASE(sha512_224)
195         MD_CASE(sha512_256)
196         MD_CASE(sha3_224)
197         MD_CASE(sha3_256)
198         MD_CASE(sha3_384)
199         MD_CASE(sha3_512)
200     default:
201         return NULL;
202     }
203 }
204 
205 #define MD_NID_CASE(name, sz) \
206     case NID_##name:          \
207         return sz;
208 
digest_sz_from_nid(int nid)209 static int digest_sz_from_nid(int nid)
210 {
211     switch (nid) {
212 #ifndef FIPS_MODULE
213 #ifndef OPENSSL_NO_MDC2
214         MD_NID_CASE(mdc2, MDC2_DIGEST_LENGTH)
215 #endif
216 #ifndef OPENSSL_NO_MD2
217         MD_NID_CASE(md2, MD2_DIGEST_LENGTH)
218 #endif
219 #ifndef OPENSSL_NO_MD4
220         MD_NID_CASE(md4, MD4_DIGEST_LENGTH)
221 #endif
222 #ifndef OPENSSL_NO_MD5
223         MD_NID_CASE(md5, MD5_DIGEST_LENGTH)
224 #endif
225 #ifndef OPENSSL_NO_RMD160
226         MD_NID_CASE(ripemd160, RIPEMD160_DIGEST_LENGTH)
227 #endif
228 #endif /* FIPS_MODULE */
229         MD_NID_CASE(sha1, SHA_DIGEST_LENGTH)
230         MD_NID_CASE(sha224, SHA224_DIGEST_LENGTH)
231         MD_NID_CASE(sha256, SHA256_DIGEST_LENGTH)
232         MD_NID_CASE(sha384, SHA384_DIGEST_LENGTH)
233         MD_NID_CASE(sha512, SHA512_DIGEST_LENGTH)
234         MD_NID_CASE(sha512_224, SHA224_DIGEST_LENGTH)
235         MD_NID_CASE(sha512_256, SHA256_DIGEST_LENGTH)
236         MD_NID_CASE(sha3_224, SHA224_DIGEST_LENGTH)
237         MD_NID_CASE(sha3_256, SHA256_DIGEST_LENGTH)
238         MD_NID_CASE(sha3_384, SHA384_DIGEST_LENGTH)
239         MD_NID_CASE(sha3_512, SHA512_DIGEST_LENGTH)
240     default:
241         return 0;
242     }
243 }
244 
245 /* Size of an SSL signature: MD5+SHA1 */
246 #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  */
encode_pkcs1(unsigned char ** out,size_t * out_len,int type,const unsigned char * m,size_t m_len)257 static int encode_pkcs1(unsigned char **out, size_t *out_len, int type,
258     const unsigned char *m, size_t m_len)
259 {
260     size_t di_prefix_len, dig_info_len;
261     const unsigned char *di_prefix;
262     unsigned char *dig_info;
263 
264     if (type == NID_undef) {
265         ERR_raise(ERR_LIB_RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE);
266         return 0;
267     }
268     di_prefix = ossl_rsa_digestinfo_encoding(type, &di_prefix_len);
269     if (di_prefix == NULL) {
270         ERR_raise(ERR_LIB_RSA,
271             RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
272         return 0;
273     }
274     dig_info_len = di_prefix_len + m_len;
275     dig_info = OPENSSL_malloc(dig_info_len);
276     if (dig_info == NULL)
277         return 0;
278     memcpy(dig_info, di_prefix, di_prefix_len);
279     memcpy(dig_info + di_prefix_len, m, m_len);
280 
281     *out = dig_info;
282     *out_len = dig_info_len;
283     return 1;
284 }
285 
RSA_sign(int type,const unsigned char * m,unsigned int m_len,unsigned char * sigret,unsigned int * siglen,RSA * rsa)286 int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
287     unsigned char *sigret, unsigned int *siglen, RSA *rsa)
288 {
289     int encrypt_len, ret = 0;
290     size_t encoded_len = 0;
291     unsigned char *tmps = NULL;
292     const unsigned char *encoded = NULL;
293 
294 #ifndef FIPS_MODULE
295     if (rsa->meth->rsa_sign != NULL)
296         return rsa->meth->rsa_sign(type, m, m_len, sigret, siglen, rsa) > 0;
297 #endif /* FIPS_MODULE */
298 
299     /* Compute the encoded digest. */
300     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         if (m_len != SSL_SIG_LENGTH) {
307             ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH);
308             return 0;
309         }
310         encoded_len = SSL_SIG_LENGTH;
311         encoded = m;
312     } else {
313         if (!encode_pkcs1(&tmps, &encoded_len, type, m, m_len))
314             goto err;
315         encoded = tmps;
316     }
317 
318     if (encoded_len + RSA_PKCS1_PADDING_SIZE > (size_t)RSA_size(rsa)) {
319         ERR_raise(ERR_LIB_RSA, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
320         goto err;
321     }
322     encrypt_len = RSA_private_encrypt((int)encoded_len, encoded, sigret, rsa,
323         RSA_PKCS1_PADDING);
324     if (encrypt_len <= 0)
325         goto err;
326 
327     *siglen = encrypt_len;
328     ret = 1;
329 
330 err:
331     OPENSSL_clear_free(tmps, encoded_len);
332     return ret;
333 }
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  */
ossl_rsa_verify(int type,const unsigned char * m,unsigned int m_len,unsigned char * rm,size_t * prm_len,const unsigned char * sigbuf,size_t siglen,RSA * rsa)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 {
348     int len, ret = 0;
349     size_t decrypt_len, encoded_len = 0;
350     unsigned char *decrypt_buf = NULL, *encoded = NULL;
351 
352     if (siglen != (size_t)RSA_size(rsa)) {
353         ERR_raise(ERR_LIB_RSA, RSA_R_WRONG_SIGNATURE_LENGTH);
354         return 0;
355     }
356 
357     /* Recover the encoded digest. */
358     decrypt_buf = OPENSSL_malloc(siglen);
359     if (decrypt_buf == NULL)
360         goto err;
361 
362     len = RSA_public_decrypt((int)siglen, sigbuf, decrypt_buf, rsa,
363         RSA_PKCS1_PADDING);
364     if (len <= 0)
365         goto err;
366     decrypt_len = len;
367 
368 #ifndef FIPS_MODULE
369     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         if (decrypt_len != SSL_SIG_LENGTH) {
376             ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
377             goto err;
378         }
379 
380         if (rm != NULL) {
381             memcpy(rm, decrypt_buf, SSL_SIG_LENGTH);
382             *prm_len = SSL_SIG_LENGTH;
383         } else {
384             if (m_len != SSL_SIG_LENGTH) {
385                 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH);
386                 goto err;
387             }
388 
389             if (memcmp(decrypt_buf, m, SSL_SIG_LENGTH) != 0) {
390                 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
391                 goto err;
392             }
393         }
394     } else if (type == NID_mdc2 && decrypt_len == 2 + 16
395         && 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         if (rm != NULL) {
401             memcpy(rm, decrypt_buf + 2, 16);
402             *prm_len = 16;
403         } else {
404             if (m_len != 16) {
405                 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_MESSAGE_LENGTH);
406                 goto err;
407             }
408 
409             if (memcmp(m, decrypt_buf + 2, 16) != 0) {
410                 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
411                 goto err;
412             }
413         }
414     } else
415 #endif /* FIPS_MODULE */
416     {
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         if (rm != NULL) {
423             len = digest_sz_from_nid(type);
424 
425             if (len <= 0)
426                 goto err;
427             m_len = (unsigned int)len;
428             if (m_len > decrypt_len) {
429                 ERR_raise(ERR_LIB_RSA, RSA_R_INVALID_DIGEST_LENGTH);
430                 goto err;
431             }
432             m = decrypt_buf + decrypt_len - m_len;
433         }
434 
435         /* Construct the encoded digest and ensure it matches. */
436         if (!encode_pkcs1(&encoded, &encoded_len, type, m, m_len))
437             goto err;
438 
439         if (encoded_len != decrypt_len
440             || memcmp(encoded, decrypt_buf, encoded_len) != 0) {
441             ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
442             goto err;
443         }
444 
445         /* Output the recovered digest. */
446         if (rm != NULL) {
447             memcpy(rm, m, m_len);
448             *prm_len = m_len;
449         }
450     }
451 
452     ret = 1;
453 
454 err:
455     OPENSSL_clear_free(encoded, encoded_len);
456     OPENSSL_clear_free(decrypt_buf, siglen);
457     return ret;
458 }
459 
RSA_verify(int type,const unsigned char * m,unsigned int m_len,const unsigned char * sigbuf,unsigned int siglen,RSA * rsa)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 {
463 
464     if (rsa->meth->rsa_verify != NULL)
465         return rsa->meth->rsa_verify(type, m, m_len, sigbuf, siglen, rsa);
466 
467     return ossl_rsa_verify(type, m, m_len, NULL, NULL, sigbuf, siglen, rsa);
468 }
469