Adam Langley | d1515a3 | 2017-01-23 16:46:16 -0800 | [diff] [blame] | 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 | |
| 141 | #include <openssl/ssl.h> |
| 142 | |
| 143 | #include <openssl/stack.h> |
| 144 | #include <openssl/x509.h> |
| 145 | #include <openssl/x509_vfy.h> |
| 146 | |
| 147 | #include "internal.h" |
| 148 | |
| 149 | |
| 150 | X509 *SSL_get_peer_certificate(const SSL *ssl) { |
| 151 | if (ssl == NULL) { |
| 152 | return NULL; |
| 153 | } |
| 154 | SSL_SESSION *session = SSL_get_session(ssl); |
| 155 | if (session == NULL || session->x509_peer == NULL) { |
| 156 | return NULL; |
| 157 | } |
| 158 | X509_up_ref(session->x509_peer); |
| 159 | return session->x509_peer; |
| 160 | } |
| 161 | |
| 162 | STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) { |
| 163 | if (ssl == NULL) { |
| 164 | return NULL; |
| 165 | } |
| 166 | SSL_SESSION *session = SSL_get_session(ssl); |
| 167 | if (session == NULL || |
| 168 | session->x509_chain == NULL) { |
| 169 | return NULL; |
| 170 | } |
| 171 | |
| 172 | if (!ssl->server) { |
| 173 | return session->x509_chain; |
| 174 | } |
| 175 | |
| 176 | /* OpenSSL historically didn't include the leaf certificate in the returned |
| 177 | * certificate chain, but only for servers. */ |
| 178 | if (session->x509_chain_without_leaf == NULL) { |
| 179 | session->x509_chain_without_leaf = sk_X509_new_null(); |
| 180 | if (session->x509_chain_without_leaf == NULL) { |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | for (size_t i = 1; i < sk_X509_num(session->x509_chain); i++) { |
| 185 | X509 *cert = sk_X509_value(session->x509_chain, i); |
| 186 | if (!sk_X509_push(session->x509_chain_without_leaf, cert)) { |
| 187 | sk_X509_pop_free(session->x509_chain_without_leaf, X509_free); |
| 188 | session->x509_chain_without_leaf = NULL; |
| 189 | return NULL; |
| 190 | } |
| 191 | X509_up_ref(cert); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return session->x509_chain_without_leaf; |
| 196 | } |
| 197 | |
| 198 | STACK_OF(X509) *SSL_get_peer_full_cert_chain(const SSL *ssl) { |
| 199 | SSL_SESSION *session = SSL_get_session(ssl); |
| 200 | if (session == NULL) { |
| 201 | return NULL; |
| 202 | } |
| 203 | |
| 204 | return session->x509_chain; |
| 205 | } |
| 206 | |
| 207 | int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) { |
| 208 | return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose); |
| 209 | } |
| 210 | |
| 211 | int SSL_set_purpose(SSL *ssl, int purpose) { |
| 212 | return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose); |
| 213 | } |
| 214 | |
| 215 | int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) { |
| 216 | return X509_VERIFY_PARAM_set_trust(ctx->param, trust); |
| 217 | } |
| 218 | |
| 219 | int SSL_set_trust(SSL *ssl, int trust) { |
| 220 | return X509_VERIFY_PARAM_set_trust(ssl->param, trust); |
| 221 | } |
| 222 | |
| 223 | int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) { |
| 224 | return X509_VERIFY_PARAM_set1(ctx->param, param); |
| 225 | } |
| 226 | |
| 227 | int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) { |
| 228 | return X509_VERIFY_PARAM_set1(ssl->param, param); |
| 229 | } |
| 230 | |
| 231 | X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; } |
| 232 | |
| 233 | X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; } |
| 234 | |
| 235 | int SSL_get_verify_depth(const SSL *ssl) { |
| 236 | return X509_VERIFY_PARAM_get_depth(ssl->param); |
| 237 | } |
| 238 | |
| 239 | int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) { |
| 240 | return ssl->verify_callback; |
| 241 | } |
| 242 | |
| 243 | int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; } |
| 244 | |
| 245 | int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) { |
| 246 | return X509_VERIFY_PARAM_get_depth(ctx->param); |
| 247 | } |
| 248 | |
| 249 | int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))( |
| 250 | int ok, X509_STORE_CTX *store_ctx) { |
| 251 | return ctx->default_verify_callback; |
| 252 | } |
| 253 | |
| 254 | void SSL_set_verify(SSL *ssl, int mode, |
| 255 | int (*callback)(int ok, X509_STORE_CTX *store_ctx)) { |
| 256 | ssl->verify_mode = mode; |
| 257 | if (callback != NULL) { |
| 258 | ssl->verify_callback = callback; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void SSL_set_verify_depth(SSL *ssl, int depth) { |
| 263 | X509_VERIFY_PARAM_set_depth(ssl->param, depth); |
| 264 | } |
| 265 | |
| 266 | void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx, |
| 267 | int (*cb)(X509_STORE_CTX *store_ctx, |
| 268 | void *arg), |
| 269 | void *arg) { |
| 270 | ctx->app_verify_callback = cb; |
| 271 | ctx->app_verify_arg = arg; |
| 272 | } |
| 273 | |
| 274 | void SSL_CTX_set_verify(SSL_CTX *ctx, int mode, |
| 275 | int (*cb)(int, X509_STORE_CTX *)) { |
| 276 | ctx->verify_mode = mode; |
| 277 | ctx->default_verify_callback = cb; |
| 278 | } |
| 279 | |
| 280 | void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) { |
| 281 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); |
| 282 | } |
| 283 | |
| 284 | X509 *SSL_get_certificate(const SSL *ssl) { |
| 285 | if (ssl->cert != NULL) { |
| 286 | return ssl->cert->x509_leaf; |
| 287 | } |
| 288 | |
| 289 | return NULL; |
| 290 | } |
| 291 | |
| 292 | X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) { |
| 293 | if (ctx->cert != NULL) { |
| 294 | return ctx->cert->x509_leaf; |
| 295 | } |
| 296 | |
| 297 | return NULL; |
| 298 | } |
| 299 | |
| 300 | int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) { |
| 301 | return X509_STORE_set_default_paths(ctx->cert_store); |
| 302 | } |
| 303 | |
| 304 | int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file, |
| 305 | const char *ca_dir) { |
| 306 | return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir); |
| 307 | } |
| 308 | |
| 309 | void SSL_set_verify_result(SSL *ssl, long result) { |
| 310 | if (result != X509_V_OK) { |
| 311 | abort(); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | long SSL_get_verify_result(const SSL *ssl) { |
| 316 | SSL_SESSION *session = SSL_get_session(ssl); |
| 317 | if (session == NULL) { |
| 318 | return X509_V_ERR_INVALID_CALL; |
| 319 | } |
| 320 | return session->verify_result; |
| 321 | } |
| 322 | |
| 323 | X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) { |
| 324 | return ctx->cert_store; |
| 325 | } |
| 326 | |
| 327 | void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) { |
| 328 | X509_STORE_free(ctx->cert_store); |
| 329 | ctx->cert_store = store; |
| 330 | } |