]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rsa/rsa_sign.c
Partial revert of "Fix client verify mode to check SSL_VERIFY_PEER"
[thirdparty/openssl.git] / crypto / rsa / rsa_sign.c
CommitLineData
2039c421
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
2039c421
RS
4 * Licensed under the OpenSSL license (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
d02b48c6
RE
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
ec577822
BM
12#include <openssl/bn.h>
13#include <openssl/rsa.h>
14#include <openssl/objects.h>
15#include <openssl/x509.h>
a6eb1ce6 16#include "internal/x509_int.h"
777c47ac 17#include "rsa_locl.h"
d02b48c6 18
1c80019a 19/* Size of an SSL signature: MD5+SHA1 */
0f113f3e 20#define SSL_SIG_LENGTH 36
1c80019a 21
29c1f061 22int RSA_sign(int type, const unsigned char *m, unsigned int m_len,
0f113f3e
MC
23 unsigned char *sigret, unsigned int *siglen, RSA *rsa)
24{
25 X509_SIG sig;
26 ASN1_TYPE parameter;
27 int i, j, ret = 1;
28 unsigned char *p, *tmps = NULL;
29 const unsigned char *s = NULL;
30 X509_ALGOR algor;
31 ASN1_OCTET_STRING digest;
19c6d3ea 32 if (rsa->meth->rsa_sign) {
0f113f3e
MC
33 return rsa->meth->rsa_sign(type, m, m_len, sigret, siglen, rsa);
34 }
35 /* Special case: SSL signature, just check the length */
36 if (type == NID_md5_sha1) {
37 if (m_len != SSL_SIG_LENGTH) {
38 RSAerr(RSA_F_RSA_SIGN, RSA_R_INVALID_MESSAGE_LENGTH);
39 return (0);
40 }
41 i = SSL_SIG_LENGTH;
42 s = m;
43 } else {
44 sig.algor = &algor;
45 sig.algor->algorithm = OBJ_nid2obj(type);
46 if (sig.algor->algorithm == NULL) {
47 RSAerr(RSA_F_RSA_SIGN, RSA_R_UNKNOWN_ALGORITHM_TYPE);
48 return (0);
49 }
2e430277 50 if (OBJ_length(sig.algor->algorithm) == 0) {
0f113f3e
MC
51 RSAerr(RSA_F_RSA_SIGN,
52 RSA_R_THE_ASN1_OBJECT_IDENTIFIER_IS_NOT_KNOWN_FOR_THIS_MD);
53 return (0);
54 }
55 parameter.type = V_ASN1_NULL;
56 parameter.value.ptr = NULL;
57 sig.algor->parameter = &parameter;
d02b48c6 58
0f113f3e
MC
59 sig.digest = &digest;
60 sig.digest->data = (unsigned char *)m; /* TMP UGLY CAST */
61 sig.digest->length = m_len;
d02b48c6 62
0f113f3e
MC
63 i = i2d_X509_SIG(&sig, NULL);
64 }
65 j = RSA_size(rsa);
66 if (i > (j - RSA_PKCS1_PADDING_SIZE)) {
67 RSAerr(RSA_F_RSA_SIGN, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
68 return (0);
69 }
70 if (type != NID_md5_sha1) {
b196e7d9 71 tmps = OPENSSL_malloc((unsigned int)j + 1);
0f113f3e
MC
72 if (tmps == NULL) {
73 RSAerr(RSA_F_RSA_SIGN, ERR_R_MALLOC_FAILURE);
74 return (0);
75 }
76 p = tmps;
77 i2d_X509_SIG(&sig, &p);
78 s = tmps;
79 }
80 i = RSA_private_encrypt(i, s, sigret, rsa, RSA_PKCS1_PADDING);
81 if (i <= 0)
82 ret = 0;
83 else
84 *siglen = i;
d02b48c6 85
4b45c6e5
RS
86 if (type != NID_md5_sha1)
87 OPENSSL_clear_free(tmps, (unsigned int)j + 1);
0f113f3e
MC
88 return (ret);
89}
d02b48c6 90
1cfd255c
DSH
91/*
92 * Check DigestInfo structure does not contain extraneous data by reencoding
0f113f3e 93 * using DER and checking encoding against original.
1cfd255c 94 */
0f113f3e
MC
95static int rsa_check_digestinfo(X509_SIG *sig, const unsigned char *dinfo,
96 int dinfolen)
97{
98 unsigned char *der = NULL;
99 int derlen;
100 int ret = 0;
101 derlen = i2d_X509_SIG(sig, &der);
102 if (derlen <= 0)
103 return 0;
104 if (derlen == dinfolen && !memcmp(dinfo, der, derlen))
105 ret = 1;
4b45c6e5 106 OPENSSL_clear_free(der, derlen);
0f113f3e
MC
107 return ret;
108}
1cfd255c 109
777c47ac 110int int_rsa_verify(int dtype, const unsigned char *m,
0f113f3e
MC
111 unsigned int m_len,
112 unsigned char *rm, size_t *prm_len,
113 const unsigned char *sigbuf, size_t siglen, RSA *rsa)
114{
115 int i, ret = 0, sigtype;
116 unsigned char *s;
117 X509_SIG *sig = NULL;
d02b48c6 118
0f113f3e
MC
119 if (siglen != (unsigned int)RSA_size(rsa)) {
120 RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_WRONG_SIGNATURE_LENGTH);
121 return (0);
122 }
1c80019a 123
0f113f3e
MC
124 if ((dtype == NID_md5_sha1) && rm) {
125 i = RSA_public_decrypt((int)siglen,
126 sigbuf, rm, rsa, RSA_PKCS1_PADDING);
127 if (i <= 0)
128 return 0;
129 *prm_len = i;
130 return 1;
131 }
d02b48c6 132
b196e7d9 133 s = OPENSSL_malloc((unsigned int)siglen);
0f113f3e
MC
134 if (s == NULL) {
135 RSAerr(RSA_F_INT_RSA_VERIFY, ERR_R_MALLOC_FAILURE);
136 goto err;
137 }
138 if ((dtype == NID_md5_sha1) && (m_len != SSL_SIG_LENGTH)) {
139 RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_INVALID_MESSAGE_LENGTH);
140 goto err;
141 }
142 i = RSA_public_decrypt((int)siglen, sigbuf, s, rsa, RSA_PKCS1_PADDING);
d02b48c6 143
0f113f3e
MC
144 if (i <= 0)
145 goto err;
146 /*
147 * Oddball MDC2 case: signature can be OCTET STRING. check for correct
148 * tag and length octets.
149 */
150 if (dtype == NID_mdc2 && i == 18 && s[0] == 0x04 && s[1] == 0x10) {
151 if (rm) {
152 memcpy(rm, s + 2, 16);
153 *prm_len = 16;
154 ret = 1;
dffe5109 155 } else if (memcmp(m, s + 2, 16)) {
0f113f3e 156 RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
dffe5109 157 } else {
0f113f3e 158 ret = 1;
dffe5109
MC
159 }
160 } else if (dtype == NID_md5_sha1) {
161 /* Special case: SSL signature */
0f113f3e
MC
162 if ((i != SSL_SIG_LENGTH) || memcmp(s, m, SSL_SIG_LENGTH))
163 RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
164 else
165 ret = 1;
166 } else {
167 const unsigned char *p = s;
168 sig = d2i_X509_SIG(NULL, &p, (long)i);
b79aa05e 169
0f113f3e
MC
170 if (sig == NULL)
171 goto err;
b79aa05e 172
0f113f3e
MC
173 /* Excess data can be used to create forgeries */
174 if (p != s + i || !rsa_check_digestinfo(sig, s, i)) {
175 RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
176 goto err;
177 }
b79aa05e 178
0f113f3e
MC
179 /*
180 * Parameters to the signature algorithm can also be used to create
181 * forgeries
182 */
183 if (sig->algor->parameter
184 && ASN1_TYPE_get(sig->algor->parameter) != V_ASN1_NULL) {
185 RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
186 goto err;
187 }
d02b48c6 188
0f113f3e 189 sigtype = OBJ_obj2nid(sig->algor->algorithm);
dfeab068 190
0f113f3e 191 if (sigtype != dtype) {
3d0cf918
RS
192 RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_ALGORITHM_MISMATCH);
193 goto err;
0f113f3e
MC
194 }
195 if (rm) {
196 const EVP_MD *md;
197 md = EVP_get_digestbynid(dtype);
198 if (md && (EVP_MD_size(md) != sig->digest->length))
199 RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_INVALID_DIGEST_LENGTH);
200 else {
201 memcpy(rm, sig->digest->data, sig->digest->length);
202 *prm_len = sig->digest->length;
203 ret = 1;
204 }
205 } else if (((unsigned int)sig->digest->length != m_len) ||
206 (memcmp(m, sig->digest->data, m_len) != 0)) {
207 RSAerr(RSA_F_INT_RSA_VERIFY, RSA_R_BAD_SIGNATURE);
208 } else
209 ret = 1;
210 }
211 err:
222561fe 212 X509_SIG_free(sig);
4b45c6e5 213 OPENSSL_clear_free(s, (unsigned int)siglen);
0f113f3e
MC
214 return (ret);
215}
d02b48c6 216
b2a97be7 217int RSA_verify(int dtype, const unsigned char *m, unsigned int m_len,
0f113f3e
MC
218 const unsigned char *sigbuf, unsigned int siglen, RSA *rsa)
219{
b2a97be7 220
19c6d3ea 221 if (rsa->meth->rsa_verify) {
0f113f3e
MC
222 return rsa->meth->rsa_verify(dtype, m, m_len, sigbuf, siglen, rsa);
223 }
b2a97be7 224
0f113f3e
MC
225 return int_rsa_verify(dtype, m, m_len, NULL, NULL, sigbuf, siglen, rsa);
226}