You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

409 lines
12 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/ssl.h>
  57. #include <openssl/err.h>
  58. #include <openssl/evp.h>
  59. #include <openssl/mem.h>
  60. #include <openssl/x509.h>
  61. #include "internal.h"
  62. static int ssl_set_cert(CERT *c, X509 *x509);
  63. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
  64. static int is_key_type_supported(int key_type) {
  65. return key_type == EVP_PKEY_RSA || key_type == EVP_PKEY_EC;
  66. }
  67. int SSL_use_certificate(SSL *ssl, X509 *x) {
  68. if (x == NULL) {
  69. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  70. return 0;
  71. }
  72. return ssl_set_cert(ssl->cert, x);
  73. }
  74. int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *d, int len) {
  75. X509 *x;
  76. int ret;
  77. x = d2i_X509(NULL, &d, (long)len);
  78. if (x == NULL) {
  79. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  80. return 0;
  81. }
  82. ret = SSL_use_certificate(ssl, x);
  83. X509_free(x);
  84. return ret;
  85. }
  86. int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) {
  87. EVP_PKEY *pkey;
  88. int ret;
  89. if (rsa == NULL) {
  90. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  91. return 0;
  92. }
  93. pkey = EVP_PKEY_new();
  94. if (pkey == NULL) {
  95. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  96. return 0;
  97. }
  98. RSA_up_ref(rsa);
  99. EVP_PKEY_assign_RSA(pkey, rsa);
  100. ret = ssl_set_pkey(ssl->cert, pkey);
  101. EVP_PKEY_free(pkey);
  102. return ret;
  103. }
  104. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey) {
  105. if (!is_key_type_supported(pkey->type)) {
  106. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  107. return 0;
  108. }
  109. if (c->x509 != NULL) {
  110. /* Sanity-check that the private key and the certificate match, unless the
  111. * key is opaque (in case of, say, a smartcard). */
  112. if (!EVP_PKEY_is_opaque(pkey) &&
  113. !X509_check_private_key(c->x509, pkey)) {
  114. X509_free(c->x509);
  115. c->x509 = NULL;
  116. return 0;
  117. }
  118. }
  119. EVP_PKEY_free(c->privatekey);
  120. c->privatekey = EVP_PKEY_up_ref(pkey);
  121. return 1;
  122. }
  123. int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
  124. RSA *rsa = RSA_private_key_from_bytes(der, der_len);
  125. if (rsa == NULL) {
  126. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  127. return 0;
  128. }
  129. int ret = SSL_use_RSAPrivateKey(ssl, rsa);
  130. RSA_free(rsa);
  131. return ret;
  132. }
  133. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) {
  134. int ret;
  135. if (pkey == NULL) {
  136. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  137. return 0;
  138. }
  139. ret = ssl_set_pkey(ssl->cert, pkey);
  140. return ret;
  141. }
  142. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *d, long len) {
  143. int ret;
  144. const uint8_t *p;
  145. EVP_PKEY *pkey;
  146. p = d;
  147. pkey = d2i_PrivateKey(type, NULL, &p, (long)len);
  148. if (pkey == NULL) {
  149. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  150. return 0;
  151. }
  152. ret = SSL_use_PrivateKey(ssl, pkey);
  153. EVP_PKEY_free(pkey);
  154. return ret;
  155. }
  156. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) {
  157. if (x == NULL) {
  158. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  159. return 0;
  160. }
  161. return ssl_set_cert(ctx->cert, x);
  162. }
  163. static int ssl_set_cert(CERT *c, X509 *x) {
  164. EVP_PKEY *pkey = X509_get_pubkey(x);
  165. if (pkey == NULL) {
  166. OPENSSL_PUT_ERROR(SSL, SSL_R_X509_LIB);
  167. return 0;
  168. }
  169. if (!is_key_type_supported(pkey->type)) {
  170. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  171. EVP_PKEY_free(pkey);
  172. return 0;
  173. }
  174. if (c->privatekey != NULL) {
  175. /* Sanity-check that the private key and the certificate match, unless the
  176. * key is opaque (in case of, say, a smartcard). */
  177. if (!EVP_PKEY_is_opaque(c->privatekey) &&
  178. !X509_check_private_key(x, c->privatekey)) {
  179. /* don't fail for a cert/key mismatch, just free current private key
  180. * (when switching to a different cert & key, first this function should
  181. * be used, then ssl_set_pkey */
  182. EVP_PKEY_free(c->privatekey);
  183. c->privatekey = NULL;
  184. /* clear error queue */
  185. ERR_clear_error();
  186. }
  187. }
  188. EVP_PKEY_free(pkey);
  189. X509_free(c->x509);
  190. c->x509 = X509_up_ref(x);
  191. return 1;
  192. }
  193. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, int len, const uint8_t *d) {
  194. X509 *x;
  195. int ret;
  196. x = d2i_X509(NULL, &d, (long)len);
  197. if (x == NULL) {
  198. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  199. return 0;
  200. }
  201. ret = SSL_CTX_use_certificate(ctx, x);
  202. X509_free(x);
  203. return ret;
  204. }
  205. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) {
  206. int ret;
  207. EVP_PKEY *pkey;
  208. if (rsa == NULL) {
  209. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  210. return 0;
  211. }
  212. pkey = EVP_PKEY_new();
  213. if (pkey == NULL) {
  214. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  215. return 0;
  216. }
  217. RSA_up_ref(rsa);
  218. EVP_PKEY_assign_RSA(pkey, rsa);
  219. ret = ssl_set_pkey(ctx->cert, pkey);
  220. EVP_PKEY_free(pkey);
  221. return ret;
  222. }
  223. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der,
  224. size_t der_len) {
  225. RSA *rsa = RSA_private_key_from_bytes(der, der_len);
  226. if (rsa == NULL) {
  227. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  228. return 0;
  229. }
  230. int ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  231. RSA_free(rsa);
  232. return ret;
  233. }
  234. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) {
  235. if (pkey == NULL) {
  236. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  237. return 0;
  238. }
  239. return ssl_set_pkey(ctx->cert, pkey);
  240. }
  241. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *d,
  242. long len) {
  243. int ret;
  244. const uint8_t *p;
  245. EVP_PKEY *pkey;
  246. p = d;
  247. pkey = d2i_PrivateKey(type, NULL, &p, (long)len);
  248. if (pkey == NULL) {
  249. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  250. return 0;
  251. }
  252. ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  253. EVP_PKEY_free(pkey);
  254. return ret;
  255. }
  256. void SSL_set_private_key_method(SSL *ssl,
  257. const SSL_PRIVATE_KEY_METHOD *key_method) {
  258. ssl->cert->key_method = key_method;
  259. }
  260. int SSL_set_private_key_digest_prefs(SSL *ssl, const int *digest_nids,
  261. size_t num_digests) {
  262. OPENSSL_free(ssl->cert->digest_nids);
  263. ssl->cert->num_digest_nids = 0;
  264. ssl->cert->digest_nids = BUF_memdup(digest_nids, num_digests*sizeof(int));
  265. if (ssl->cert->digest_nids == NULL) {
  266. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  267. return 0;
  268. }
  269. ssl->cert->num_digest_nids = num_digests;
  270. return 1;
  271. }
  272. int ssl_has_private_key(SSL *ssl) {
  273. return ssl->cert->privatekey != NULL || ssl->cert->key_method != NULL;
  274. }
  275. int ssl_private_key_type(SSL *ssl) {
  276. if (ssl->cert->key_method != NULL) {
  277. return ssl->cert->key_method->type(ssl);
  278. }
  279. return EVP_PKEY_id(ssl->cert->privatekey);
  280. }
  281. size_t ssl_private_key_max_signature_len(SSL *ssl) {
  282. if (ssl->cert->key_method != NULL) {
  283. return ssl->cert->key_method->max_signature_len(ssl);
  284. }
  285. return EVP_PKEY_size(ssl->cert->privatekey);
  286. }
  287. enum ssl_private_key_result_t ssl_private_key_sign(
  288. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, const EVP_MD *md,
  289. const uint8_t *in, size_t in_len) {
  290. if (ssl->cert->key_method != NULL) {
  291. return ssl->cert->key_method->sign(ssl, out, out_len, max_out, md, in,
  292. in_len);
  293. }
  294. enum ssl_private_key_result_t ret = ssl_private_key_failure;
  295. EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
  296. if (ctx == NULL) {
  297. goto end;
  298. }
  299. size_t len = max_out;
  300. if (!EVP_PKEY_sign_init(ctx) ||
  301. !EVP_PKEY_CTX_set_signature_md(ctx, md) ||
  302. !EVP_PKEY_sign(ctx, out, &len, in, in_len)) {
  303. goto end;
  304. }
  305. *out_len = len;
  306. ret = ssl_private_key_success;
  307. end:
  308. EVP_PKEY_CTX_free(ctx);
  309. return ret;
  310. }
  311. enum ssl_private_key_result_t ssl_private_key_sign_complete(
  312. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) {
  313. /* Only custom keys may be asynchronous. */
  314. return ssl->cert->key_method->sign_complete(ssl, out, out_len, max_out);
  315. }
  316. enum ssl_private_key_result_t ssl_private_key_decrypt(
  317. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  318. const uint8_t *in, size_t in_len) {
  319. if (ssl->cert->key_method != NULL) {
  320. return ssl->cert->key_method->decrypt(ssl, out, out_len, max_out, in,
  321. in_len);
  322. }
  323. if (ssl_private_key_type(ssl) != EVP_PKEY_RSA) {
  324. /* Decrypt operations are only supported for RSA keys. */
  325. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  326. return ssl_private_key_failure;
  327. }
  328. enum ssl_private_key_result_t ret = ssl_private_key_failure;
  329. RSA *rsa = ssl->cert->privatekey->pkey.rsa;
  330. /* Decrypt with no padding. PKCS#1 padding will be removed as part
  331. * of the timing-sensitive code by the caller. */
  332. if (RSA_decrypt(rsa, out_len, out, max_out, in, in_len, RSA_NO_PADDING)) {
  333. ret = ssl_private_key_success;
  334. }
  335. return ret;
  336. }
  337. enum ssl_private_key_result_t ssl_private_key_decrypt_complete(
  338. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) {
  339. /* Only custom keys may be asynchronous. */
  340. return ssl->cert->key_method->decrypt_complete(ssl, out, out_len, max_out);
  341. }