No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

ssl_x509.c 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  114. */
  115. /* ====================================================================
  116. * Copyright 2005 Nokia. All rights reserved.
  117. *
  118. * The portions of the attached software ("Contribution") is developed by
  119. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  120. * license.
  121. *
  122. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  123. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  124. * support (see RFC 4279) to OpenSSL.
  125. *
  126. * No patent licenses or other rights except those expressly stated in
  127. * the OpenSSL open source license shall be deemed granted or received
  128. * expressly, by implication, estoppel, or otherwise.
  129. *
  130. * No assurances are provided by Nokia that the Contribution does not
  131. * infringe the patent or other intellectual property rights of any third
  132. * party or that the license provides you with all the necessary rights
  133. * to make use of the Contribution.
  134. *
  135. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  136. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  137. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  138. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  139. * OTHERWISE. */
  140. #include <openssl/ssl.h>
  141. #include <openssl/stack.h>
  142. #include <openssl/x509.h>
  143. #include <openssl/x509_vfy.h>
  144. #include "internal.h"
  145. X509 *SSL_get_peer_certificate(const SSL *ssl) {
  146. if (ssl == NULL) {
  147. return NULL;
  148. }
  149. SSL_SESSION *session = SSL_get_session(ssl);
  150. if (session == NULL || session->x509_peer == NULL) {
  151. return NULL;
  152. }
  153. X509_up_ref(session->x509_peer);
  154. return session->x509_peer;
  155. }
  156. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
  157. if (ssl == NULL) {
  158. return NULL;
  159. }
  160. SSL_SESSION *session = SSL_get_session(ssl);
  161. if (session == NULL ||
  162. session->x509_chain == NULL) {
  163. return NULL;
  164. }
  165. if (!ssl->server) {
  166. return session->x509_chain;
  167. }
  168. /* OpenSSL historically didn't include the leaf certificate in the returned
  169. * certificate chain, but only for servers. */
  170. if (session->x509_chain_without_leaf == NULL) {
  171. session->x509_chain_without_leaf = sk_X509_new_null();
  172. if (session->x509_chain_without_leaf == NULL) {
  173. return NULL;
  174. }
  175. for (size_t i = 1; i < sk_X509_num(session->x509_chain); i++) {
  176. X509 *cert = sk_X509_value(session->x509_chain, i);
  177. if (!sk_X509_push(session->x509_chain_without_leaf, cert)) {
  178. sk_X509_pop_free(session->x509_chain_without_leaf, X509_free);
  179. session->x509_chain_without_leaf = NULL;
  180. return NULL;
  181. }
  182. X509_up_ref(cert);
  183. }
  184. }
  185. return session->x509_chain_without_leaf;
  186. }
  187. STACK_OF(X509) *SSL_get_peer_full_cert_chain(const SSL *ssl) {
  188. SSL_SESSION *session = SSL_get_session(ssl);
  189. if (session == NULL) {
  190. return NULL;
  191. }
  192. return session->x509_chain;
  193. }
  194. int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
  195. return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
  196. }
  197. int SSL_set_purpose(SSL *ssl, int purpose) {
  198. return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose);
  199. }
  200. int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
  201. return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
  202. }
  203. int SSL_set_trust(SSL *ssl, int trust) {
  204. return X509_VERIFY_PARAM_set_trust(ssl->param, trust);
  205. }
  206. int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
  207. return X509_VERIFY_PARAM_set1(ctx->param, param);
  208. }
  209. int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
  210. return X509_VERIFY_PARAM_set1(ssl->param, param);
  211. }
  212. X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
  213. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
  214. int SSL_get_verify_depth(const SSL *ssl) {
  215. return X509_VERIFY_PARAM_get_depth(ssl->param);
  216. }
  217. int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
  218. return ssl->verify_callback;
  219. }
  220. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
  221. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
  222. return X509_VERIFY_PARAM_get_depth(ctx->param);
  223. }
  224. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
  225. int ok, X509_STORE_CTX *store_ctx) {
  226. return ctx->default_verify_callback;
  227. }
  228. void SSL_set_verify(SSL *ssl, int mode,
  229. int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
  230. ssl->verify_mode = mode;
  231. if (callback != NULL) {
  232. ssl->verify_callback = callback;
  233. }
  234. }
  235. void SSL_set_verify_depth(SSL *ssl, int depth) {
  236. X509_VERIFY_PARAM_set_depth(ssl->param, depth);
  237. }
  238. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  239. int (*cb)(X509_STORE_CTX *store_ctx,
  240. void *arg),
  241. void *arg) {
  242. ctx->app_verify_callback = cb;
  243. ctx->app_verify_arg = arg;
  244. }
  245. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  246. int (*cb)(int, X509_STORE_CTX *)) {
  247. ctx->verify_mode = mode;
  248. ctx->default_verify_callback = cb;
  249. }
  250. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
  251. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  252. }
  253. X509 *SSL_get_certificate(const SSL *ssl) {
  254. if (ssl->cert != NULL) {
  255. return ssl->cert->x509_leaf;
  256. }
  257. return NULL;
  258. }
  259. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
  260. if (ctx->cert != NULL) {
  261. return ctx->cert->x509_leaf;
  262. }
  263. return NULL;
  264. }
  265. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
  266. return X509_STORE_set_default_paths(ctx->cert_store);
  267. }
  268. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
  269. const char *ca_dir) {
  270. return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
  271. }
  272. void SSL_set_verify_result(SSL *ssl, long result) {
  273. if (result != X509_V_OK) {
  274. abort();
  275. }
  276. }
  277. long SSL_get_verify_result(const SSL *ssl) {
  278. SSL_SESSION *session = SSL_get_session(ssl);
  279. if (session == NULL) {
  280. return X509_V_ERR_INVALID_CALL;
  281. }
  282. return session->verify_result;
  283. }
  284. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
  285. return ctx->cert_store;
  286. }
  287. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  288. X509_STORE_free(ctx->cert_store);
  289. ctx->cert_store = store;
  290. }