ssh-rsa.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #include "includes.h"
  18. #ifdef WITH_OPENSSL
  19. #include <sys/types.h>
  20. #include <openssl/evp.h>
  21. #include <openssl/err.h>
  22. #include <stdarg.h>
  23. #include <string.h>
  24. #include "sshbuf.h"
  25. #include "compat.h"
  26. #include "ssherr.h"
  27. #define SSHKEY_INTERNAL
  28. #include "sshkey.h"
  29. #include "digest.h"
  30. #include "log.h"
  31. #include "openbsd-compat/openssl-compat.h"
  32. static int openssh_RSA_verify(int, const u_char *, size_t, u_char *, size_t, EVP_PKEY *);
  33. static const char *
  34. rsa_hash_alg_ident(int hash_alg)
  35. {
  36. switch (hash_alg) {
  37. case SSH_DIGEST_SHA1:
  38. return "ssh-rsa";
  39. case SSH_DIGEST_SHA256:
  40. return "rsa-sha2-256";
  41. case SSH_DIGEST_SHA512:
  42. return "rsa-sha2-512";
  43. }
  44. return NULL;
  45. }
  46. /*
  47. * Returns the hash algorithm ID for a given algorithm identifier as used
  48. * inside the signature blob,
  49. */
  50. static int
  51. rsa_hash_id_from_ident(const char *ident)
  52. {
  53. if (strcmp(ident, "ssh-rsa") == 0)
  54. return SSH_DIGEST_SHA1;
  55. if (strcmp(ident, "rsa-sha2-256") == 0)
  56. return SSH_DIGEST_SHA256;
  57. if (strcmp(ident, "rsa-sha2-512") == 0)
  58. return SSH_DIGEST_SHA512;
  59. return -1;
  60. }
  61. /*
  62. * Return the hash algorithm ID for the specified key name. This includes
  63. * all the cases of rsa_hash_id_from_ident() but also the certificate key
  64. * types.
  65. */
  66. static int
  67. rsa_hash_id_from_keyname(const char *alg)
  68. {
  69. int r;
  70. if ((r = rsa_hash_id_from_ident(alg)) != -1)
  71. return r;
  72. if (strcmp(alg, "ssh-rsa-cert-v01@openssh.com") == 0)
  73. return SSH_DIGEST_SHA1;
  74. if (strcmp(alg, "rsa-sha2-256-cert-v01@openssh.com") == 0)
  75. return SSH_DIGEST_SHA256;
  76. if (strcmp(alg, "rsa-sha2-512-cert-v01@openssh.com") == 0)
  77. return SSH_DIGEST_SHA512;
  78. return -1;
  79. }
  80. int
  81. ssh_rsa_complete_crt_parameters(struct sshkey *key, const BIGNUM *iqmp)
  82. {
  83. const BIGNUM *rsa_p, *rsa_q, *rsa_d;
  84. BIGNUM *aux = NULL, *d_consttime = NULL;
  85. BIGNUM *rsa_dmq1 = NULL, *rsa_dmp1 = NULL, *rsa_iqmp = NULL;
  86. BN_CTX *ctx = NULL;
  87. int r;
  88. if (key == NULL || key->rsa == NULL ||
  89. sshkey_type_plain(key->type) != KEY_RSA)
  90. return SSH_ERR_INVALID_ARGUMENT;
  91. RSA_get0_key(key->rsa, NULL, NULL, &rsa_d);
  92. RSA_get0_factors(key->rsa, &rsa_p, &rsa_q);
  93. if ((ctx = BN_CTX_new()) == NULL)
  94. return SSH_ERR_ALLOC_FAIL;
  95. if ((aux = BN_new()) == NULL ||
  96. (rsa_dmq1 = BN_new()) == NULL ||
  97. (rsa_dmp1 = BN_new()) == NULL)
  98. return SSH_ERR_ALLOC_FAIL;
  99. if ((d_consttime = BN_dup(rsa_d)) == NULL ||
  100. (rsa_iqmp = BN_dup(iqmp)) == NULL) {
  101. r = SSH_ERR_ALLOC_FAIL;
  102. goto out;
  103. }
  104. BN_set_flags(aux, BN_FLG_CONSTTIME);
  105. BN_set_flags(d_consttime, BN_FLG_CONSTTIME);
  106. if ((BN_sub(aux, rsa_q, BN_value_one()) == 0) ||
  107. (BN_mod(rsa_dmq1, d_consttime, aux, ctx) == 0) ||
  108. (BN_sub(aux, rsa_p, BN_value_one()) == 0) ||
  109. (BN_mod(rsa_dmp1, d_consttime, aux, ctx) == 0)) {
  110. debug("((BN_sub(aux, rsa_q, BN_value_one()) == 0) || (BN_mod(rsa_dmq1, d_consttime, aux, ctx) == 0) || (BN_sub(aux, rsa_p, BN_value_one()) == 0) || (BN_mod(rsa_dmp1, d_consttime, aux, ctx) == 0))");
  111. r = SSH_ERR_LIBCRYPTO_ERROR;
  112. goto out;
  113. }
  114. if (!RSA_set0_crt_params(key->rsa, rsa_dmp1, rsa_dmq1, rsa_iqmp)) {
  115. debug("(!RSA_set0_crt_params(key->rsa, rsa_dmp1, rsa_dmq1, rsa_iqmp))");
  116. r = SSH_ERR_LIBCRYPTO_ERROR;
  117. goto out;
  118. }
  119. rsa_dmp1 = rsa_dmq1 = rsa_iqmp = NULL; /* transferred */
  120. /* success */
  121. r = 0;
  122. out:
  123. BN_clear_free(aux);
  124. BN_clear_free(d_consttime);
  125. BN_clear_free(rsa_dmp1);
  126. BN_clear_free(rsa_dmq1);
  127. BN_clear_free(rsa_iqmp);
  128. BN_CTX_free(ctx);
  129. return r;
  130. }
  131. /* RSASSA-PKCS1-v1_5 (PKCS #1 v2.0 signature) with SHA1 */
  132. int
  133. ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
  134. const u_char *data, size_t datalen, const char *alg_ident)
  135. {
  136. EVP_PKEY *pkey = NULL;
  137. u_char *sig = NULL;
  138. int len, slen = 0;
  139. int hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
  140. struct sshbuf *b = NULL;
  141. if (lenp != NULL)
  142. *lenp = 0;
  143. if (sigp != NULL)
  144. *sigp = NULL;
  145. if (alg_ident == NULL || strlen(alg_ident) == 0)
  146. hash_alg = SSH_DIGEST_SHA1;
  147. else
  148. hash_alg = rsa_hash_id_from_keyname(alg_ident);
  149. if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
  150. sshkey_type_plain(key->type) != KEY_RSA)
  151. return SSH_ERR_INVALID_ARGUMENT;
  152. slen = RSA_size(key->rsa);
  153. if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
  154. return SSH_ERR_KEY_LENGTH;
  155. if ((pkey = EVP_PKEY_new()) == NULL ||
  156. EVP_PKEY_set1_RSA(pkey, key->rsa) != 1)
  157. return SSH_ERR_ALLOC_FAIL;
  158. ret = sshkey_calculate_signature(pkey, hash_alg, &sig, &len, data,
  159. datalen);
  160. EVP_PKEY_free(pkey);
  161. if (ret < 0) {
  162. goto out;
  163. }
  164. if (len < slen) {
  165. size_t diff = slen - len;
  166. memmove(sig + diff, sig, len);
  167. explicit_bzero(sig, diff);
  168. } else if (len > slen) {
  169. ret = SSH_ERR_INTERNAL_ERROR;
  170. goto out;
  171. }
  172. /* encode signature */
  173. if ((b = sshbuf_new()) == NULL) {
  174. ret = SSH_ERR_ALLOC_FAIL;
  175. goto out;
  176. }
  177. if ((ret = sshbuf_put_cstring(b, rsa_hash_alg_ident(hash_alg))) != 0 ||
  178. (ret = sshbuf_put_string(b, sig, slen)) != 0)
  179. goto out;
  180. len = sshbuf_len(b);
  181. if (sigp != NULL) {
  182. if ((*sigp = malloc(len)) == NULL) {
  183. ret = SSH_ERR_ALLOC_FAIL;
  184. goto out;
  185. }
  186. memcpy(*sigp, sshbuf_ptr(b), len);
  187. }
  188. if (lenp != NULL)
  189. *lenp = len;
  190. ret = 0;
  191. out:
  192. freezero(sig, slen);
  193. sshbuf_free(b);
  194. return ret;
  195. }
  196. int
  197. ssh_rsa_verify(const struct sshkey *key,
  198. const u_char *sig, size_t siglen, const u_char *data, size_t datalen,
  199. const char *alg)
  200. {
  201. EVP_PKEY *pkey = NULL;
  202. char *sigtype = NULL;
  203. int hash_alg, want_alg, ret = SSH_ERR_INTERNAL_ERROR;
  204. size_t len = 0, diff, modlen;
  205. struct sshbuf *b = NULL;
  206. u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
  207. if (key == NULL || key->rsa == NULL ||
  208. sshkey_type_plain(key->type) != KEY_RSA ||
  209. sig == NULL || siglen == 0)
  210. return SSH_ERR_INVALID_ARGUMENT;
  211. if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
  212. return SSH_ERR_KEY_LENGTH;
  213. if ((b = sshbuf_from(sig, siglen)) == NULL)
  214. return SSH_ERR_ALLOC_FAIL;
  215. if (sshbuf_get_cstring(b, &sigtype, NULL) != 0) {
  216. ret = SSH_ERR_INVALID_FORMAT;
  217. goto out;
  218. }
  219. if ((hash_alg = rsa_hash_id_from_ident(sigtype)) == -1) {
  220. ret = SSH_ERR_KEY_TYPE_MISMATCH;
  221. goto out;
  222. }
  223. /*
  224. * Allow ssh-rsa-cert-v01 certs to generate SHA2 signatures for
  225. * legacy reasons, but otherwise the signature type should match.
  226. */
  227. if (alg != NULL && strcmp(alg, "ssh-rsa-cert-v01@openssh.com") != 0) {
  228. if ((want_alg = rsa_hash_id_from_keyname(alg)) == -1) {
  229. ret = SSH_ERR_INVALID_ARGUMENT;
  230. goto out;
  231. }
  232. if (hash_alg != want_alg) {
  233. ret = SSH_ERR_SIGNATURE_INVALID;
  234. goto out;
  235. }
  236. }
  237. if (sshbuf_get_string(b, &sigblob, &len) != 0) {
  238. ret = SSH_ERR_INVALID_FORMAT;
  239. goto out;
  240. }
  241. if (sshbuf_len(b) != 0) {
  242. ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
  243. goto out;
  244. }
  245. /* RSA_verify expects a signature of RSA_size */
  246. modlen = RSA_size(key->rsa);
  247. if (len > modlen) {
  248. ret = SSH_ERR_KEY_BITS_MISMATCH;
  249. goto out;
  250. } else if (len < modlen) {
  251. diff = modlen - len;
  252. osigblob = sigblob;
  253. if ((sigblob = realloc(sigblob, modlen)) == NULL) {
  254. sigblob = osigblob; /* put it back for clear/free */
  255. ret = SSH_ERR_ALLOC_FAIL;
  256. goto out;
  257. }
  258. memmove(sigblob + diff, sigblob, len);
  259. explicit_bzero(sigblob, diff);
  260. len = modlen;
  261. }
  262. if ((pkey = EVP_PKEY_new()) == NULL ||
  263. EVP_PKEY_set1_RSA(pkey, key->rsa) != 1) {
  264. ret = SSH_ERR_ALLOC_FAIL;
  265. goto out;
  266. }
  267. ret = openssh_RSA_verify(hash_alg, data, datalen, sigblob, len, pkey);
  268. EVP_PKEY_free(pkey);
  269. out:
  270. freezero(sigblob, len);
  271. free(sigtype);
  272. sshbuf_free(b);
  273. explicit_bzero(digest, sizeof(digest));
  274. return ret;
  275. }
  276. static int
  277. openssh_RSA_verify(int hash_alg, const u_char *data, size_t datalen,
  278. u_char *sigbuf, size_t siglen, EVP_PKEY *pkey)
  279. {
  280. size_t rsasize = 0;
  281. const RSA *rsa;
  282. int ret;
  283. rsa = EVP_PKEY_get0_RSA(pkey);
  284. rsasize = RSA_size(rsa);
  285. if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
  286. siglen == 0 || siglen > rsasize) {
  287. ret = SSH_ERR_INVALID_ARGUMENT;
  288. goto done;
  289. }
  290. ret = sshkey_verify_signature(pkey, hash_alg, data, datalen,
  291. sigbuf, siglen);
  292. done:
  293. return ret;
  294. }
  295. #endif /* WITH_OPENSSL */