• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* $OpenBSD: ssh-rsa.c,v 1.67 2018/07/03 11:39:54 djm Exp $ */
2 /*
3  * Copyright (c) 2000, 2003 Markus Friedl <markus@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include "includes.h"
19 
20 #ifdef WITH_OPENSSL
21 
22 #include <sys/types.h>
23 
24 #include <openssl/evp.h>
25 #include <openssl/err.h>
26 
27 #include <stdarg.h>
28 #include <string.h>
29 
30 #include "sshbuf.h"
31 #include "compat.h"
32 #include "ssherr.h"
33 #define SSHKEY_INTERNAL
34 #include "sshkey.h"
35 #include "digest.h"
36 #include "log.h"
37 
38 #include "openbsd-compat/openssl-compat.h"
39 
40 static int openssh_RSA_verify(int, u_char *, size_t, u_char *, size_t, RSA *);
41 
42 static const char *
rsa_hash_alg_ident(int hash_alg)43 rsa_hash_alg_ident(int hash_alg)
44 {
45 	switch (hash_alg) {
46 	case SSH_DIGEST_SHA1:
47 		return "ssh-rsa";
48 	case SSH_DIGEST_SHA256:
49 		return "rsa-sha2-256";
50 	case SSH_DIGEST_SHA512:
51 		return "rsa-sha2-512";
52 	}
53 	return NULL;
54 }
55 
56 /*
57  * Returns the hash algorithm ID for a given algorithm identifier as used
58  * inside the signature blob,
59  */
60 static int
rsa_hash_id_from_ident(const char * ident)61 rsa_hash_id_from_ident(const char *ident)
62 {
63 	if (strcmp(ident, "ssh-rsa") == 0)
64 		return SSH_DIGEST_SHA1;
65 	if (strcmp(ident, "rsa-sha2-256") == 0)
66 		return SSH_DIGEST_SHA256;
67 	if (strcmp(ident, "rsa-sha2-512") == 0)
68 		return SSH_DIGEST_SHA512;
69 	return -1;
70 }
71 
72 /*
73  * Return the hash algorithm ID for the specified key name. This includes
74  * all the cases of rsa_hash_id_from_ident() but also the certificate key
75  * types.
76  */
77 static int
rsa_hash_id_from_keyname(const char * alg)78 rsa_hash_id_from_keyname(const char *alg)
79 {
80 	int r;
81 
82 	if ((r = rsa_hash_id_from_ident(alg)) != -1)
83 		return r;
84 	if (strcmp(alg, "ssh-rsa-cert-v01@openssh.com") == 0)
85 		return SSH_DIGEST_SHA1;
86 	if (strcmp(alg, "rsa-sha2-256-cert-v01@openssh.com") == 0)
87 		return SSH_DIGEST_SHA256;
88 	if (strcmp(alg, "rsa-sha2-512-cert-v01@openssh.com") == 0)
89 		return SSH_DIGEST_SHA512;
90 	return -1;
91 }
92 
93 static int
rsa_hash_alg_nid(int type)94 rsa_hash_alg_nid(int type)
95 {
96 	switch (type) {
97 	case SSH_DIGEST_SHA1:
98 		return NID_sha1;
99 	case SSH_DIGEST_SHA256:
100 		return NID_sha256;
101 	case SSH_DIGEST_SHA512:
102 		return NID_sha512;
103 	default:
104 		return -1;
105 	}
106 }
107 
108 int
ssh_rsa_complete_crt_parameters(struct sshkey * key,const BIGNUM * iqmp)109 ssh_rsa_complete_crt_parameters(struct sshkey *key, const BIGNUM *iqmp)
110 {
111 	const BIGNUM *rsa_p, *rsa_q, *rsa_d;
112 	BIGNUM *aux = NULL, *d_consttime = NULL;
113 	BIGNUM *rsa_dmq1 = NULL, *rsa_dmp1 = NULL, *rsa_iqmp = NULL;
114 	BN_CTX *ctx = NULL;
115 	int r;
116 
117 	if (key == NULL || key->rsa == NULL ||
118 	    sshkey_type_plain(key->type) != KEY_RSA)
119 		return SSH_ERR_INVALID_ARGUMENT;
120 
121 	RSA_get0_key(key->rsa, NULL, NULL, &rsa_d);
122 	RSA_get0_factors(key->rsa, &rsa_p, &rsa_q);
123 
124 	if ((ctx = BN_CTX_new()) == NULL)
125 		return SSH_ERR_ALLOC_FAIL;
126 	if ((aux = BN_new()) == NULL ||
127 	    (rsa_dmq1 = BN_new()) == NULL ||
128 	    (rsa_dmp1 = BN_new()) == NULL)
129 		return SSH_ERR_ALLOC_FAIL;
130 	if ((d_consttime = BN_dup(rsa_d)) == NULL ||
131 	    (rsa_iqmp = BN_dup(iqmp)) == NULL) {
132 		r = SSH_ERR_ALLOC_FAIL;
133 		goto out;
134 	}
135 #if !defined(OPENSSL_IS_BORINGSSL)
136 	BN_set_flags(aux, BN_FLG_CONSTTIME);
137 	BN_set_flags(d_consttime, BN_FLG_CONSTTIME);
138 #endif
139 
140 	if ((BN_sub(aux, rsa_q, BN_value_one()) == 0) ||
141 	    (BN_mod(rsa_dmq1, d_consttime, aux, ctx) == 0) ||
142 	    (BN_sub(aux, rsa_p, BN_value_one()) == 0) ||
143 	    (BN_mod(rsa_dmp1, d_consttime, aux, ctx) == 0)) {
144 		r = SSH_ERR_LIBCRYPTO_ERROR;
145 		goto out;
146 	}
147 	if (!RSA_set0_crt_params(key->rsa, rsa_dmp1, rsa_dmq1, rsa_iqmp)) {
148 		r = SSH_ERR_LIBCRYPTO_ERROR;
149 		goto out;
150 	}
151 	rsa_dmp1 = rsa_dmq1 = rsa_iqmp = NULL; /* transferred */
152 	/* success */
153 	r = 0;
154  out:
155 	BN_clear_free(aux);
156 	BN_clear_free(d_consttime);
157 	BN_clear_free(rsa_dmp1);
158 	BN_clear_free(rsa_dmq1);
159 	BN_clear_free(rsa_iqmp);
160 	BN_CTX_free(ctx);
161 	return r;
162 }
163 
164 /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
165 int
ssh_rsa_sign(const struct sshkey * key,u_char ** sigp,size_t * lenp,const u_char * data,size_t datalen,const char * alg_ident)166 ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
167     const u_char *data, size_t datalen, const char *alg_ident)
168 {
169 	const BIGNUM *rsa_n;
170 	u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL;
171 	size_t slen = 0;
172 	u_int dlen, len;
173 	int nid, hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
174 	struct sshbuf *b = NULL;
175 
176 	if (lenp != NULL)
177 		*lenp = 0;
178 	if (sigp != NULL)
179 		*sigp = NULL;
180 
181 	if (alg_ident == NULL || strlen(alg_ident) == 0)
182 		hash_alg = SSH_DIGEST_SHA1;
183 	else
184 		hash_alg = rsa_hash_id_from_keyname(alg_ident);
185 	if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
186 	    sshkey_type_plain(key->type) != KEY_RSA)
187 		return SSH_ERR_INVALID_ARGUMENT;
188 	RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);
189 	if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
190 		return SSH_ERR_KEY_LENGTH;
191 	slen = RSA_size(key->rsa);
192 	if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
193 		return SSH_ERR_INVALID_ARGUMENT;
194 
195 	/* hash the data */
196 	nid = rsa_hash_alg_nid(hash_alg);
197 	if ((dlen = ssh_digest_bytes(hash_alg)) == 0)
198 		return SSH_ERR_INTERNAL_ERROR;
199 	if ((ret = ssh_digest_memory(hash_alg, data, datalen,
200 	    digest, sizeof(digest))) != 0)
201 		goto out;
202 
203 	if ((sig = malloc(slen)) == NULL) {
204 		ret = SSH_ERR_ALLOC_FAIL;
205 		goto out;
206 	}
207 
208 	if (RSA_sign(nid, digest, dlen, sig, &len, key->rsa) != 1) {
209 		ret = SSH_ERR_LIBCRYPTO_ERROR;
210 		goto out;
211 	}
212 	if (len < slen) {
213 		size_t diff = slen - len;
214 		memmove(sig + diff, sig, len);
215 		explicit_bzero(sig, diff);
216 	} else if (len > slen) {
217 		ret = SSH_ERR_INTERNAL_ERROR;
218 		goto out;
219 	}
220 	/* encode signature */
221 	if ((b = sshbuf_new()) == NULL) {
222 		ret = SSH_ERR_ALLOC_FAIL;
223 		goto out;
224 	}
225 	if ((ret = sshbuf_put_cstring(b, rsa_hash_alg_ident(hash_alg))) != 0 ||
226 	    (ret = sshbuf_put_string(b, sig, slen)) != 0)
227 		goto out;
228 	len = sshbuf_len(b);
229 	if (sigp != NULL) {
230 		if ((*sigp = malloc(len)) == NULL) {
231 			ret = SSH_ERR_ALLOC_FAIL;
232 			goto out;
233 		}
234 		memcpy(*sigp, sshbuf_ptr(b), len);
235 	}
236 	if (lenp != NULL)
237 		*lenp = len;
238 	ret = 0;
239  out:
240 	explicit_bzero(digest, sizeof(digest));
241 	freezero(sig, slen);
242 	sshbuf_free(b);
243 	return ret;
244 }
245 
246 int
ssh_rsa_verify(const struct sshkey * key,const u_char * sig,size_t siglen,const u_char * data,size_t datalen,const char * alg)247 ssh_rsa_verify(const struct sshkey *key,
248     const u_char *sig, size_t siglen, const u_char *data, size_t datalen,
249     const char *alg)
250 {
251 	const BIGNUM *rsa_n;
252 	char *sigtype = NULL;
253 	int hash_alg, want_alg, ret = SSH_ERR_INTERNAL_ERROR;
254 	size_t len = 0, diff, modlen, dlen;
255 	struct sshbuf *b = NULL;
256 	u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
257 
258 	if (key == NULL || key->rsa == NULL ||
259 	    sshkey_type_plain(key->type) != KEY_RSA ||
260 	    sig == NULL || siglen == 0)
261 		return SSH_ERR_INVALID_ARGUMENT;
262 	RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);
263 	if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
264 		return SSH_ERR_KEY_LENGTH;
265 
266 	if ((b = sshbuf_from(sig, siglen)) == NULL)
267 		return SSH_ERR_ALLOC_FAIL;
268 	if (sshbuf_get_cstring(b, &sigtype, NULL) != 0) {
269 		ret = SSH_ERR_INVALID_FORMAT;
270 		goto out;
271 	}
272 	if ((hash_alg = rsa_hash_id_from_ident(sigtype)) == -1) {
273 		ret = SSH_ERR_KEY_TYPE_MISMATCH;
274 		goto out;
275 	}
276 	/*
277 	 * Allow ssh-rsa-cert-v01 certs to generate SHA2 signatures for
278 	 * legacy reasons, but otherwise the signature type should match.
279 	 */
280 	if (alg != NULL && strcmp(alg, "ssh-rsa-cert-v01@openssh.com") != 0) {
281 		if ((want_alg = rsa_hash_id_from_keyname(alg)) == -1) {
282 			ret = SSH_ERR_INVALID_ARGUMENT;
283 			goto out;
284 		}
285 		if (hash_alg != want_alg) {
286 			ret = SSH_ERR_SIGNATURE_INVALID;
287 			goto out;
288 		}
289 	}
290 	if (sshbuf_get_string(b, &sigblob, &len) != 0) {
291 		ret = SSH_ERR_INVALID_FORMAT;
292 		goto out;
293 	}
294 	if (sshbuf_len(b) != 0) {
295 		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
296 		goto out;
297 	}
298 	/* RSA_verify expects a signature of RSA_size */
299 	modlen = RSA_size(key->rsa);
300 	if (len > modlen) {
301 		ret = SSH_ERR_KEY_BITS_MISMATCH;
302 		goto out;
303 	} else if (len < modlen) {
304 		diff = modlen - len;
305 		osigblob = sigblob;
306 		if ((sigblob = realloc(sigblob, modlen)) == NULL) {
307 			sigblob = osigblob; /* put it back for clear/free */
308 			ret = SSH_ERR_ALLOC_FAIL;
309 			goto out;
310 		}
311 		memmove(sigblob + diff, sigblob, len);
312 		explicit_bzero(sigblob, diff);
313 		len = modlen;
314 	}
315 	if ((dlen = ssh_digest_bytes(hash_alg)) == 0) {
316 		ret = SSH_ERR_INTERNAL_ERROR;
317 		goto out;
318 	}
319 	if ((ret = ssh_digest_memory(hash_alg, data, datalen,
320 	    digest, sizeof(digest))) != 0)
321 		goto out;
322 
323 	ret = openssh_RSA_verify(hash_alg, digest, dlen, sigblob, len,
324 	    key->rsa);
325  out:
326 	freezero(sigblob, len);
327 	free(sigtype);
328 	sshbuf_free(b);
329 	explicit_bzero(digest, sizeof(digest));
330 	return ret;
331 }
332 
333 /*
334  * See:
335  * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
336  * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
337  */
338 
339 /*
340  * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
341  *	oiw(14) secsig(3) algorithms(2) 26 }
342  */
343 static const u_char id_sha1[] = {
344 	0x30, 0x21, /* type Sequence, length 0x21 (33) */
345 	0x30, 0x09, /* type Sequence, length 0x09 */
346 	0x06, 0x05, /* type OID, length 0x05 */
347 	0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
348 	0x05, 0x00, /* NULL */
349 	0x04, 0x14  /* Octet string, length 0x14 (20), followed by sha1 hash */
350 };
351 
352 /*
353  * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
354  * id-sha256 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
355  *      organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
356  *      id-sha256(1) }
357  */
358 static const u_char id_sha256[] = {
359 	0x30, 0x31, /* type Sequence, length 0x31 (49) */
360 	0x30, 0x0d, /* type Sequence, length 0x0d (13) */
361 	0x06, 0x09, /* type OID, length 0x09 */
362 	0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, /* id-sha256 */
363 	0x05, 0x00, /* NULL */
364 	0x04, 0x20  /* Octet string, length 0x20 (32), followed by sha256 hash */
365 };
366 
367 /*
368  * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
369  * id-sha512 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
370  *      organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
371  *      id-sha256(3) }
372  */
373 static const u_char id_sha512[] = {
374 	0x30, 0x51, /* type Sequence, length 0x51 (81) */
375 	0x30, 0x0d, /* type Sequence, length 0x0d (13) */
376 	0x06, 0x09, /* type OID, length 0x09 */
377 	0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, /* id-sha512 */
378 	0x05, 0x00, /* NULL */
379 	0x04, 0x40  /* Octet string, length 0x40 (64), followed by sha512 hash */
380 };
381 
382 static int
rsa_hash_alg_oid(int hash_alg,const u_char ** oidp,size_t * oidlenp)383 rsa_hash_alg_oid(int hash_alg, const u_char **oidp, size_t *oidlenp)
384 {
385 	switch (hash_alg) {
386 	case SSH_DIGEST_SHA1:
387 		*oidp = id_sha1;
388 		*oidlenp = sizeof(id_sha1);
389 		break;
390 	case SSH_DIGEST_SHA256:
391 		*oidp = id_sha256;
392 		*oidlenp = sizeof(id_sha256);
393 		break;
394 	case SSH_DIGEST_SHA512:
395 		*oidp = id_sha512;
396 		*oidlenp = sizeof(id_sha512);
397 		break;
398 	default:
399 		return SSH_ERR_INVALID_ARGUMENT;
400 	}
401 	return 0;
402 }
403 
404 static int
openssh_RSA_verify(int hash_alg,u_char * hash,size_t hashlen,u_char * sigbuf,size_t siglen,RSA * rsa)405 openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen,
406     u_char *sigbuf, size_t siglen, RSA *rsa)
407 {
408 	size_t rsasize = 0, oidlen = 0, hlen = 0;
409 	int ret, len, oidmatch, hashmatch;
410 	const u_char *oid = NULL;
411 	u_char *decrypted = NULL;
412 
413 	if ((ret = rsa_hash_alg_oid(hash_alg, &oid, &oidlen)) != 0)
414 		return ret;
415 	ret = SSH_ERR_INTERNAL_ERROR;
416 	hlen = ssh_digest_bytes(hash_alg);
417 	if (hashlen != hlen) {
418 		ret = SSH_ERR_INVALID_ARGUMENT;
419 		goto done;
420 	}
421 	rsasize = RSA_size(rsa);
422 	if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
423 	    siglen == 0 || siglen > rsasize) {
424 		ret = SSH_ERR_INVALID_ARGUMENT;
425 		goto done;
426 	}
427 	if ((decrypted = malloc(rsasize)) == NULL) {
428 		ret = SSH_ERR_ALLOC_FAIL;
429 		goto done;
430 	}
431 	if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
432 	    RSA_PKCS1_PADDING)) < 0) {
433 		ret = SSH_ERR_LIBCRYPTO_ERROR;
434 		goto done;
435 	}
436 	if (len < 0 || (size_t)len != hlen + oidlen) {
437 		ret = SSH_ERR_INVALID_FORMAT;
438 		goto done;
439 	}
440 	oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
441 	hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
442 	if (!oidmatch || !hashmatch) {
443 		ret = SSH_ERR_SIGNATURE_INVALID;
444 		goto done;
445 	}
446 	ret = 0;
447 done:
448 	freezero(decrypted, rsasize);
449 	return ret;
450 }
451 #endif /* WITH_OPENSSL */
452