Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

429 linhas
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 <limits.h>
  58. #include <openssl/err.h>
  59. #include <openssl/evp.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/x509.h>
  62. #include "internal.h"
  63. static int ssl_set_cert(CERT *c, X509 *x509);
  64. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey);
  65. static int is_key_type_supported(int key_type) {
  66. return key_type == EVP_PKEY_RSA || key_type == EVP_PKEY_EC;
  67. }
  68. int SSL_use_certificate(SSL *ssl, X509 *x) {
  69. if (x == NULL) {
  70. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  71. return 0;
  72. }
  73. return ssl_set_cert(ssl->cert, x);
  74. }
  75. int SSL_use_certificate_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
  76. if (der_len > LONG_MAX) {
  77. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  78. return 0;
  79. }
  80. const uint8_t *p = der;
  81. X509 *x509 = d2i_X509(NULL, &p, (long)der_len);
  82. if (x509 == NULL || p != der + der_len) {
  83. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  84. X509_free(x509);
  85. return 0;
  86. }
  87. int ret = SSL_use_certificate(ssl, x509);
  88. X509_free(x509);
  89. return ret;
  90. }
  91. int SSL_use_RSAPrivateKey(SSL *ssl, RSA *rsa) {
  92. EVP_PKEY *pkey;
  93. int ret;
  94. if (rsa == NULL) {
  95. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  96. return 0;
  97. }
  98. pkey = EVP_PKEY_new();
  99. if (pkey == NULL) {
  100. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  101. return 0;
  102. }
  103. RSA_up_ref(rsa);
  104. EVP_PKEY_assign_RSA(pkey, rsa);
  105. ret = ssl_set_pkey(ssl->cert, pkey);
  106. EVP_PKEY_free(pkey);
  107. return ret;
  108. }
  109. static int ssl_set_pkey(CERT *c, EVP_PKEY *pkey) {
  110. if (!is_key_type_supported(pkey->type)) {
  111. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  112. return 0;
  113. }
  114. if (c->x509 != NULL) {
  115. /* Sanity-check that the private key and the certificate match, unless the
  116. * key is opaque (in case of, say, a smartcard). */
  117. if (!EVP_PKEY_is_opaque(pkey) &&
  118. !X509_check_private_key(c->x509, pkey)) {
  119. X509_free(c->x509);
  120. c->x509 = NULL;
  121. return 0;
  122. }
  123. }
  124. EVP_PKEY_free(c->privatekey);
  125. c->privatekey = EVP_PKEY_up_ref(pkey);
  126. return 1;
  127. }
  128. int SSL_use_RSAPrivateKey_ASN1(SSL *ssl, const uint8_t *der, size_t der_len) {
  129. RSA *rsa = RSA_private_key_from_bytes(der, der_len);
  130. if (rsa == NULL) {
  131. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  132. return 0;
  133. }
  134. int ret = SSL_use_RSAPrivateKey(ssl, rsa);
  135. RSA_free(rsa);
  136. return ret;
  137. }
  138. int SSL_use_PrivateKey(SSL *ssl, EVP_PKEY *pkey) {
  139. int ret;
  140. if (pkey == NULL) {
  141. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  142. return 0;
  143. }
  144. ret = ssl_set_pkey(ssl->cert, pkey);
  145. return ret;
  146. }
  147. int SSL_use_PrivateKey_ASN1(int type, SSL *ssl, const uint8_t *der,
  148. size_t der_len) {
  149. if (der_len > LONG_MAX) {
  150. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  151. return 0;
  152. }
  153. const uint8_t *p = der;
  154. EVP_PKEY *pkey = d2i_PrivateKey(type, NULL, &p, (long)der_len);
  155. if (pkey == NULL || p != der + der_len) {
  156. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  157. EVP_PKEY_free(pkey);
  158. return 0;
  159. }
  160. int ret = SSL_use_PrivateKey(ssl, pkey);
  161. EVP_PKEY_free(pkey);
  162. return ret;
  163. }
  164. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) {
  165. if (x == NULL) {
  166. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  167. return 0;
  168. }
  169. return ssl_set_cert(ctx->cert, x);
  170. }
  171. static int ssl_set_cert(CERT *c, X509 *x) {
  172. EVP_PKEY *pkey = X509_get_pubkey(x);
  173. if (pkey == NULL) {
  174. OPENSSL_PUT_ERROR(SSL, SSL_R_X509_LIB);
  175. return 0;
  176. }
  177. if (!is_key_type_supported(pkey->type)) {
  178. OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CERTIFICATE_TYPE);
  179. EVP_PKEY_free(pkey);
  180. return 0;
  181. }
  182. if (c->privatekey != NULL) {
  183. /* Sanity-check that the private key and the certificate match, unless the
  184. * key is opaque (in case of, say, a smartcard). */
  185. if (!EVP_PKEY_is_opaque(c->privatekey) &&
  186. !X509_check_private_key(x, c->privatekey)) {
  187. /* don't fail for a cert/key mismatch, just free current private key
  188. * (when switching to a different cert & key, first this function should
  189. * be used, then ssl_set_pkey */
  190. EVP_PKEY_free(c->privatekey);
  191. c->privatekey = NULL;
  192. /* clear error queue */
  193. ERR_clear_error();
  194. }
  195. }
  196. EVP_PKEY_free(pkey);
  197. X509_free(c->x509);
  198. c->x509 = X509_up_ref(x);
  199. return 1;
  200. }
  201. int SSL_CTX_use_certificate_ASN1(SSL_CTX *ctx, size_t der_len,
  202. const uint8_t *der) {
  203. if (der_len > LONG_MAX) {
  204. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  205. return 0;
  206. }
  207. const uint8_t *p = der;
  208. X509 *x509 = d2i_X509(NULL, &p, (long)der_len);
  209. if (x509 == NULL || p != der + der_len) {
  210. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  211. X509_free(x509);
  212. return 0;
  213. }
  214. int ret = SSL_CTX_use_certificate(ctx, x509);
  215. X509_free(x509);
  216. return ret;
  217. }
  218. int SSL_CTX_use_RSAPrivateKey(SSL_CTX *ctx, RSA *rsa) {
  219. int ret;
  220. EVP_PKEY *pkey;
  221. if (rsa == NULL) {
  222. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  223. return 0;
  224. }
  225. pkey = EVP_PKEY_new();
  226. if (pkey == NULL) {
  227. OPENSSL_PUT_ERROR(SSL, ERR_R_EVP_LIB);
  228. return 0;
  229. }
  230. RSA_up_ref(rsa);
  231. EVP_PKEY_assign_RSA(pkey, rsa);
  232. ret = ssl_set_pkey(ctx->cert, pkey);
  233. EVP_PKEY_free(pkey);
  234. return ret;
  235. }
  236. int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX *ctx, const uint8_t *der,
  237. size_t der_len) {
  238. RSA *rsa = RSA_private_key_from_bytes(der, der_len);
  239. if (rsa == NULL) {
  240. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  241. return 0;
  242. }
  243. int ret = SSL_CTX_use_RSAPrivateKey(ctx, rsa);
  244. RSA_free(rsa);
  245. return ret;
  246. }
  247. int SSL_CTX_use_PrivateKey(SSL_CTX *ctx, EVP_PKEY *pkey) {
  248. if (pkey == NULL) {
  249. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  250. return 0;
  251. }
  252. return ssl_set_pkey(ctx->cert, pkey);
  253. }
  254. int SSL_CTX_use_PrivateKey_ASN1(int type, SSL_CTX *ctx, const uint8_t *der,
  255. size_t der_len) {
  256. if (der_len > LONG_MAX) {
  257. OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
  258. return 0;
  259. }
  260. const uint8_t *p = der;
  261. EVP_PKEY *pkey = d2i_PrivateKey(type, NULL, &p, (long)der_len);
  262. if (pkey == NULL || p != der + der_len) {
  263. OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB);
  264. EVP_PKEY_free(pkey);
  265. return 0;
  266. }
  267. int ret = SSL_CTX_use_PrivateKey(ctx, pkey);
  268. EVP_PKEY_free(pkey);
  269. return ret;
  270. }
  271. void SSL_set_private_key_method(SSL *ssl,
  272. const SSL_PRIVATE_KEY_METHOD *key_method) {
  273. ssl->cert->key_method = key_method;
  274. }
  275. void SSL_CTX_set_private_key_method(SSL_CTX *ctx,
  276. const SSL_PRIVATE_KEY_METHOD *key_method) {
  277. ctx->cert->key_method = key_method;
  278. }
  279. int SSL_set_private_key_digest_prefs(SSL *ssl, const int *digest_nids,
  280. size_t num_digests) {
  281. OPENSSL_free(ssl->cert->digest_nids);
  282. ssl->cert->num_digest_nids = 0;
  283. ssl->cert->digest_nids = BUF_memdup(digest_nids, num_digests*sizeof(int));
  284. if (ssl->cert->digest_nids == NULL) {
  285. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  286. return 0;
  287. }
  288. ssl->cert->num_digest_nids = num_digests;
  289. return 1;
  290. }
  291. int ssl_has_private_key(SSL *ssl) {
  292. return ssl->cert->privatekey != NULL || ssl->cert->key_method != NULL;
  293. }
  294. int ssl_private_key_type(SSL *ssl) {
  295. if (ssl->cert->key_method != NULL) {
  296. return ssl->cert->key_method->type(ssl);
  297. }
  298. return EVP_PKEY_id(ssl->cert->privatekey);
  299. }
  300. size_t ssl_private_key_max_signature_len(SSL *ssl) {
  301. if (ssl->cert->key_method != NULL) {
  302. return ssl->cert->key_method->max_signature_len(ssl);
  303. }
  304. return EVP_PKEY_size(ssl->cert->privatekey);
  305. }
  306. enum ssl_private_key_result_t ssl_private_key_sign(
  307. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out, const EVP_MD *md,
  308. const uint8_t *in, size_t in_len) {
  309. if (ssl->cert->key_method != NULL) {
  310. return ssl->cert->key_method->sign(ssl, out, out_len, max_out, md, in,
  311. in_len);
  312. }
  313. enum ssl_private_key_result_t ret = ssl_private_key_failure;
  314. EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new(ssl->cert->privatekey, NULL);
  315. if (ctx == NULL) {
  316. goto end;
  317. }
  318. size_t len = max_out;
  319. if (!EVP_PKEY_sign_init(ctx) ||
  320. !EVP_PKEY_CTX_set_signature_md(ctx, md) ||
  321. !EVP_PKEY_sign(ctx, out, &len, in, in_len)) {
  322. goto end;
  323. }
  324. *out_len = len;
  325. ret = ssl_private_key_success;
  326. end:
  327. EVP_PKEY_CTX_free(ctx);
  328. return ret;
  329. }
  330. enum ssl_private_key_result_t ssl_private_key_sign_complete(
  331. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) {
  332. /* Only custom keys may be asynchronous. */
  333. return ssl->cert->key_method->sign_complete(ssl, out, out_len, max_out);
  334. }
  335. enum ssl_private_key_result_t ssl_private_key_decrypt(
  336. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out,
  337. const uint8_t *in, size_t in_len) {
  338. if (ssl->cert->key_method != NULL) {
  339. return ssl->cert->key_method->decrypt(ssl, out, out_len, max_out, in,
  340. in_len);
  341. }
  342. RSA *rsa = EVP_PKEY_get0_RSA(ssl->cert->privatekey);
  343. if (rsa == NULL) {
  344. /* Decrypt operations are only supported for RSA keys. */
  345. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  346. return ssl_private_key_failure;
  347. }
  348. /* Decrypt with no padding. PKCS#1 padding will be removed as part
  349. * of the timing-sensitive code by the caller. */
  350. if (!RSA_decrypt(rsa, out_len, out, max_out, in, in_len, RSA_NO_PADDING)) {
  351. return ssl_private_key_failure;
  352. }
  353. return ssl_private_key_success;
  354. }
  355. enum ssl_private_key_result_t ssl_private_key_decrypt_complete(
  356. SSL *ssl, uint8_t *out, size_t *out_len, size_t max_out) {
  357. /* Only custom keys may be asynchronous. */
  358. return ssl->cert->key_method->decrypt_complete(ssl, out, out_len, max_out);
  359. }