David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project |
| 3 | * 2004. |
| 4 | */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 5 | /* ==================================================================== |
| 6 | * Copyright (c) 2004 The OpenSSL Project. All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 13 | * notice, this list of conditions and the following disclaimer. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 14 | * |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in |
| 17 | * the documentation and/or other materials provided with the |
| 18 | * distribution. |
| 19 | * |
| 20 | * 3. All advertising materials mentioning features or use of this |
| 21 | * software must display the following acknowledgment: |
| 22 | * "This product includes software developed by the OpenSSL Project |
| 23 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 24 | * |
| 25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 26 | * endorse or promote products derived from this software without |
| 27 | * prior written permission. For written permission, please contact |
| 28 | * licensing@OpenSSL.org. |
| 29 | * |
| 30 | * 5. Products derived from this software may not be called "OpenSSL" |
| 31 | * nor may "OpenSSL" appear in their names without prior written |
| 32 | * permission of the OpenSSL Project. |
| 33 | * |
| 34 | * 6. Redistributions of any form whatsoever must retain the following |
| 35 | * acknowledgment: |
| 36 | * "This product includes software developed by the OpenSSL Project |
| 37 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 38 | * |
| 39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 50 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 51 | * ==================================================================== |
| 52 | * |
| 53 | * This product includes cryptographic software written by Eric Young |
| 54 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 55 | * Hudson (tjh@cryptsoft.com). */ |
| 56 | |
| 57 | #include <string.h> |
| 58 | |
| 59 | #include <openssl/buf.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 60 | #include <openssl/mem.h> |
| 61 | #include <openssl/obj.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 62 | #include <openssl/stack.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 63 | #include <openssl/x509.h> |
| 64 | #include <openssl/x509v3.h> |
| 65 | |
| 66 | #include "vpm_int.h" |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 67 | #include "../internal.h" |
| 68 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 69 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 70 | /* X509_VERIFY_PARAM functions */ |
| 71 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 72 | #define SET_HOST 0 |
| 73 | #define ADD_HOST 1 |
| 74 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 75 | static char *str_copy(char *s) |
| 76 | { |
| 77 | return OPENSSL_strdup(s); |
| 78 | } |
| 79 | |
| 80 | static void str_free(char *s) |
| 81 | { |
| 82 | OPENSSL_free(s); |
| 83 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 84 | |
| 85 | #define string_stack_free(sk) sk_OPENSSL_STRING_pop_free(sk, str_free) |
| 86 | |
| 87 | static int int_x509_param_set_hosts(X509_VERIFY_PARAM_ID *id, int mode, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 88 | const char *name, size_t namelen) |
| 89 | { |
| 90 | char *copy; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 91 | |
Robert Sloan | ab8b888 | 2018-03-26 11:39:51 -0700 | [diff] [blame^] | 92 | // This is an OpenSSL quirk that BoringSSL typically doesn't support. |
| 93 | // However, we didn't make this a fatal error at the time, which was a |
| 94 | // mistake. Because of that, given the risk that someone could assume the |
| 95 | // OpenSSL semantics from BoringSSL, it's supported in this case. |
| 96 | if (name != NULL && namelen == 0) { |
| 97 | namelen = strlen(name); |
| 98 | } |
| 99 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 100 | /* |
| 101 | * Refuse names with embedded NUL bytes. |
| 102 | * XXX: Do we need to push an error onto the error stack? |
| 103 | */ |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 104 | if (name && OPENSSL_memchr(name, '\0', namelen)) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 105 | return 0; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 106 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 107 | if (mode == SET_HOST && id->hosts) { |
| 108 | string_stack_free(id->hosts); |
| 109 | id->hosts = NULL; |
| 110 | } |
| 111 | if (name == NULL || namelen == 0) |
| 112 | return 1; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 113 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 114 | copy = BUF_strndup(name, namelen); |
| 115 | if (copy == NULL) |
| 116 | return 0; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 117 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 118 | if (id->hosts == NULL && |
| 119 | (id->hosts = sk_OPENSSL_STRING_new_null()) == NULL) { |
| 120 | OPENSSL_free(copy); |
| 121 | return 0; |
| 122 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 123 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 124 | if (!sk_OPENSSL_STRING_push(id->hosts, copy)) { |
| 125 | OPENSSL_free(copy); |
| 126 | if (sk_OPENSSL_STRING_num(id->hosts) == 0) { |
| 127 | sk_OPENSSL_STRING_free(id->hosts); |
| 128 | id->hosts = NULL; |
| 129 | } |
| 130 | return 0; |
| 131 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 132 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 133 | return 1; |
| 134 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 135 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 136 | static void x509_verify_param_zero(X509_VERIFY_PARAM *param) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 137 | { |
| 138 | X509_VERIFY_PARAM_ID *paramid; |
| 139 | if (!param) |
| 140 | return; |
| 141 | param->name = NULL; |
| 142 | param->purpose = 0; |
| 143 | param->trust = 0; |
| 144 | /* |
| 145 | * param->inh_flags = X509_VP_FLAG_DEFAULT; |
| 146 | */ |
| 147 | param->inh_flags = 0; |
| 148 | param->flags = 0; |
| 149 | param->depth = -1; |
| 150 | if (param->policies) { |
| 151 | sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free); |
| 152 | param->policies = NULL; |
| 153 | } |
| 154 | paramid = param->id; |
| 155 | if (paramid->hosts) { |
| 156 | string_stack_free(paramid->hosts); |
| 157 | paramid->hosts = NULL; |
| 158 | } |
| 159 | if (paramid->peername) { |
| 160 | OPENSSL_free(paramid->peername); |
| 161 | paramid->peername = NULL; |
| 162 | } |
| 163 | if (paramid->email) { |
| 164 | OPENSSL_free(paramid->email); |
| 165 | paramid->email = NULL; |
| 166 | paramid->emaillen = 0; |
| 167 | } |
| 168 | if (paramid->ip) { |
| 169 | OPENSSL_free(paramid->ip); |
| 170 | paramid->ip = NULL; |
| 171 | paramid->iplen = 0; |
| 172 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 173 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 174 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 175 | |
| 176 | X509_VERIFY_PARAM *X509_VERIFY_PARAM_new(void) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 177 | { |
| 178 | X509_VERIFY_PARAM *param; |
| 179 | X509_VERIFY_PARAM_ID *paramid; |
| 180 | param = OPENSSL_malloc(sizeof(X509_VERIFY_PARAM)); |
| 181 | if (!param) |
| 182 | return NULL; |
| 183 | paramid = OPENSSL_malloc(sizeof(X509_VERIFY_PARAM_ID)); |
| 184 | if (!paramid) { |
| 185 | OPENSSL_free(param); |
| 186 | return NULL; |
| 187 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 188 | OPENSSL_memset(param, 0, sizeof(X509_VERIFY_PARAM)); |
| 189 | OPENSSL_memset(paramid, 0, sizeof(X509_VERIFY_PARAM_ID)); |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 190 | param->id = paramid; |
| 191 | x509_verify_param_zero(param); |
| 192 | return param; |
| 193 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 194 | |
| 195 | void X509_VERIFY_PARAM_free(X509_VERIFY_PARAM *param) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 196 | { |
| 197 | if (param == NULL) |
| 198 | return; |
| 199 | x509_verify_param_zero(param); |
| 200 | OPENSSL_free(param->id); |
| 201 | OPENSSL_free(param); |
| 202 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 203 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 204 | /*- |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 205 | * This function determines how parameters are "inherited" from one structure |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 206 | * to another. There are several different ways this can happen. |
| 207 | * |
| 208 | * 1. If a child structure needs to have its values initialized from a parent |
| 209 | * they are simply copied across. For example SSL_CTX copied to SSL. |
| 210 | * 2. If the structure should take on values only if they are currently unset. |
| 211 | * For example the values in an SSL structure will take appropriate value |
| 212 | * for SSL servers or clients but only if the application has not set new |
| 213 | * ones. |
| 214 | * |
| 215 | * The "inh_flags" field determines how this function behaves. |
| 216 | * |
| 217 | * Normally any values which are set in the default are not copied from the |
| 218 | * destination and verify flags are ORed together. |
| 219 | * |
| 220 | * If X509_VP_FLAG_DEFAULT is set then anything set in the source is copied |
| 221 | * to the destination. Effectively the values in "to" become default values |
| 222 | * which will be used only if nothing new is set in "from". |
| 223 | * |
| 224 | * If X509_VP_FLAG_OVERWRITE is set then all value are copied across whether |
| 225 | * they are set or not. Flags is still Ored though. |
| 226 | * |
| 227 | * If X509_VP_FLAG_RESET_FLAGS is set then the flags value is copied instead |
| 228 | * of ORed. |
| 229 | * |
| 230 | * If X509_VP_FLAG_LOCKED is set then no values are copied. |
| 231 | * |
| 232 | * If X509_VP_FLAG_ONCE is set then the current inh_flags setting is zeroed |
| 233 | * after the next call. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 234 | */ |
| 235 | |
| 236 | /* Macro to test if a field should be copied from src to dest */ |
| 237 | |
| 238 | #define test_x509_verify_param_copy(field, def) \ |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 239 | (to_overwrite || \ |
| 240 | ((src->field != (def)) && (to_default || (dest->field == (def))))) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 241 | |
| 242 | /* As above but for ID fields */ |
| 243 | |
| 244 | #define test_x509_verify_param_copy_id(idf, def) \ |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 245 | test_x509_verify_param_copy(id->idf, def) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 246 | |
| 247 | /* Macro to test and copy a field if necessary */ |
| 248 | |
| 249 | #define x509_verify_param_copy(field, def) \ |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 250 | if (test_x509_verify_param_copy(field, def)) \ |
| 251 | dest->field = src->field |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 252 | |
| 253 | int X509_VERIFY_PARAM_inherit(X509_VERIFY_PARAM *dest, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 254 | const X509_VERIFY_PARAM *src) |
| 255 | { |
| 256 | unsigned long inh_flags; |
| 257 | int to_default, to_overwrite; |
| 258 | X509_VERIFY_PARAM_ID *id; |
| 259 | if (!src) |
| 260 | return 1; |
| 261 | id = src->id; |
| 262 | inh_flags = dest->inh_flags | src->inh_flags; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 263 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 264 | if (inh_flags & X509_VP_FLAG_ONCE) |
| 265 | dest->inh_flags = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 266 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 267 | if (inh_flags & X509_VP_FLAG_LOCKED) |
| 268 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 269 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 270 | if (inh_flags & X509_VP_FLAG_DEFAULT) |
| 271 | to_default = 1; |
| 272 | else |
| 273 | to_default = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 274 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 275 | if (inh_flags & X509_VP_FLAG_OVERWRITE) |
| 276 | to_overwrite = 1; |
| 277 | else |
| 278 | to_overwrite = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 279 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 280 | x509_verify_param_copy(purpose, 0); |
| 281 | x509_verify_param_copy(trust, 0); |
| 282 | x509_verify_param_copy(depth, -1); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 283 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 284 | /* If overwrite or check time not set, copy across */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 285 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 286 | if (to_overwrite || !(dest->flags & X509_V_FLAG_USE_CHECK_TIME)) { |
| 287 | dest->check_time = src->check_time; |
| 288 | dest->flags &= ~X509_V_FLAG_USE_CHECK_TIME; |
| 289 | /* Don't need to copy flag: that is done below */ |
| 290 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 291 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 292 | if (inh_flags & X509_VP_FLAG_RESET_FLAGS) |
| 293 | dest->flags = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 294 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 295 | dest->flags |= src->flags; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 296 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 297 | if (test_x509_verify_param_copy(policies, NULL)) { |
| 298 | if (!X509_VERIFY_PARAM_set1_policies(dest, src->policies)) |
| 299 | return 0; |
| 300 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 301 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 302 | /* Copy the host flags if and only if we're copying the host list */ |
| 303 | if (test_x509_verify_param_copy_id(hosts, NULL)) { |
| 304 | if (dest->id->hosts) { |
| 305 | string_stack_free(dest->id->hosts); |
| 306 | dest->id->hosts = NULL; |
| 307 | } |
| 308 | if (id->hosts) { |
| 309 | dest->id->hosts = |
| 310 | sk_OPENSSL_STRING_deep_copy(id->hosts, str_copy, str_free); |
| 311 | if (dest->id->hosts == NULL) |
| 312 | return 0; |
| 313 | dest->id->hostflags = id->hostflags; |
| 314 | } |
| 315 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 316 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 317 | if (test_x509_verify_param_copy_id(email, NULL)) { |
| 318 | if (!X509_VERIFY_PARAM_set1_email(dest, id->email, id->emaillen)) |
| 319 | return 0; |
| 320 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 321 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 322 | if (test_x509_verify_param_copy_id(ip, NULL)) { |
| 323 | if (!X509_VERIFY_PARAM_set1_ip(dest, id->ip, id->iplen)) |
| 324 | return 0; |
| 325 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 326 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 327 | return 1; |
| 328 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 329 | |
| 330 | int X509_VERIFY_PARAM_set1(X509_VERIFY_PARAM *to, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 331 | const X509_VERIFY_PARAM *from) |
| 332 | { |
| 333 | unsigned long save_flags = to->inh_flags; |
| 334 | int ret; |
| 335 | to->inh_flags |= X509_VP_FLAG_DEFAULT; |
| 336 | ret = X509_VERIFY_PARAM_inherit(to, from); |
| 337 | to->inh_flags = save_flags; |
| 338 | return ret; |
| 339 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 340 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 341 | static int int_x509_param_set1(char **pdest, size_t *pdestlen, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 342 | const char *src, size_t srclen) |
| 343 | { |
| 344 | void *tmp; |
| 345 | if (src) { |
| 346 | if (srclen == 0) { |
| 347 | tmp = BUF_strdup(src); |
| 348 | srclen = strlen(src); |
| 349 | } else |
| 350 | tmp = BUF_memdup(src, srclen); |
| 351 | if (!tmp) |
| 352 | return 0; |
| 353 | } else { |
| 354 | tmp = NULL; |
| 355 | srclen = 0; |
| 356 | } |
| 357 | if (*pdest) |
| 358 | OPENSSL_free(*pdest); |
| 359 | *pdest = tmp; |
| 360 | if (pdestlen) |
| 361 | *pdestlen = srclen; |
| 362 | return 1; |
| 363 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 364 | |
| 365 | int X509_VERIFY_PARAM_set1_name(X509_VERIFY_PARAM *param, const char *name) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 366 | { |
| 367 | if (param->name) |
| 368 | OPENSSL_free(param->name); |
| 369 | param->name = BUF_strdup(name); |
| 370 | if (param->name) |
| 371 | return 1; |
| 372 | return 0; |
| 373 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 374 | |
| 375 | int X509_VERIFY_PARAM_set_flags(X509_VERIFY_PARAM *param, unsigned long flags) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 376 | { |
| 377 | param->flags |= flags; |
| 378 | if (flags & X509_V_FLAG_POLICY_MASK) |
| 379 | param->flags |= X509_V_FLAG_POLICY_CHECK; |
| 380 | return 1; |
| 381 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 382 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 383 | int X509_VERIFY_PARAM_clear_flags(X509_VERIFY_PARAM *param, |
| 384 | unsigned long flags) |
| 385 | { |
| 386 | param->flags &= ~flags; |
| 387 | return 1; |
| 388 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 389 | |
| 390 | unsigned long X509_VERIFY_PARAM_get_flags(X509_VERIFY_PARAM *param) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 391 | { |
| 392 | return param->flags; |
| 393 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 394 | |
| 395 | int X509_VERIFY_PARAM_set_purpose(X509_VERIFY_PARAM *param, int purpose) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 396 | { |
| 397 | return X509_PURPOSE_set(¶m->purpose, purpose); |
| 398 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 399 | |
| 400 | int X509_VERIFY_PARAM_set_trust(X509_VERIFY_PARAM *param, int trust) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 401 | { |
| 402 | return X509_TRUST_set(¶m->trust, trust); |
| 403 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 404 | |
| 405 | void X509_VERIFY_PARAM_set_depth(X509_VERIFY_PARAM *param, int depth) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 406 | { |
| 407 | param->depth = depth; |
| 408 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 409 | |
| 410 | void X509_VERIFY_PARAM_set_time(X509_VERIFY_PARAM *param, time_t t) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 411 | { |
| 412 | param->check_time = t; |
| 413 | param->flags |= X509_V_FLAG_USE_CHECK_TIME; |
| 414 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 415 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 416 | int X509_VERIFY_PARAM_add0_policy(X509_VERIFY_PARAM *param, |
| 417 | ASN1_OBJECT *policy) |
| 418 | { |
| 419 | if (!param->policies) { |
| 420 | param->policies = sk_ASN1_OBJECT_new_null(); |
| 421 | if (!param->policies) |
| 422 | return 0; |
| 423 | } |
| 424 | if (!sk_ASN1_OBJECT_push(param->policies, policy)) |
| 425 | return 0; |
| 426 | return 1; |
| 427 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 428 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 429 | int X509_VERIFY_PARAM_set1_policies(X509_VERIFY_PARAM *param, |
| 430 | STACK_OF(ASN1_OBJECT) *policies) |
| 431 | { |
| 432 | size_t i; |
| 433 | ASN1_OBJECT *oid, *doid; |
| 434 | if (!param) |
| 435 | return 0; |
| 436 | if (param->policies) |
| 437 | sk_ASN1_OBJECT_pop_free(param->policies, ASN1_OBJECT_free); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 438 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 439 | if (!policies) { |
| 440 | param->policies = NULL; |
| 441 | return 1; |
| 442 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 443 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 444 | param->policies = sk_ASN1_OBJECT_new_null(); |
| 445 | if (!param->policies) |
| 446 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 447 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 448 | for (i = 0; i < sk_ASN1_OBJECT_num(policies); i++) { |
| 449 | oid = sk_ASN1_OBJECT_value(policies, i); |
| 450 | doid = OBJ_dup(oid); |
| 451 | if (!doid) |
| 452 | return 0; |
| 453 | if (!sk_ASN1_OBJECT_push(param->policies, doid)) { |
| 454 | ASN1_OBJECT_free(doid); |
| 455 | return 0; |
| 456 | } |
| 457 | } |
| 458 | param->flags |= X509_V_FLAG_POLICY_CHECK; |
| 459 | return 1; |
| 460 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 461 | |
| 462 | int X509_VERIFY_PARAM_set1_host(X509_VERIFY_PARAM *param, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 463 | const char *name, size_t namelen) |
| 464 | { |
| 465 | return int_x509_param_set_hosts(param->id, SET_HOST, name, namelen); |
| 466 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 467 | |
| 468 | int X509_VERIFY_PARAM_add1_host(X509_VERIFY_PARAM *param, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 469 | const char *name, size_t namelen) |
| 470 | { |
| 471 | return int_x509_param_set_hosts(param->id, ADD_HOST, name, namelen); |
| 472 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 473 | |
| 474 | void X509_VERIFY_PARAM_set_hostflags(X509_VERIFY_PARAM *param, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 475 | unsigned int flags) |
| 476 | { |
| 477 | param->id->hostflags = flags; |
| 478 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 479 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 480 | char *X509_VERIFY_PARAM_get0_peername(X509_VERIFY_PARAM *param) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 481 | { |
| 482 | return param->id->peername; |
| 483 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 484 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 485 | int X509_VERIFY_PARAM_set1_email(X509_VERIFY_PARAM *param, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 486 | const char *email, size_t emaillen) |
| 487 | { |
| 488 | return int_x509_param_set1(¶m->id->email, ¶m->id->emaillen, |
| 489 | email, emaillen); |
| 490 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 491 | |
| 492 | int X509_VERIFY_PARAM_set1_ip(X509_VERIFY_PARAM *param, |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 493 | const unsigned char *ip, size_t iplen) |
| 494 | { |
| 495 | if (iplen != 0 && iplen != 4 && iplen != 16) |
| 496 | return 0; |
| 497 | return int_x509_param_set1((char **)¶m->id->ip, ¶m->id->iplen, |
| 498 | (char *)ip, iplen); |
| 499 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 500 | |
| 501 | int X509_VERIFY_PARAM_set1_ip_asc(X509_VERIFY_PARAM *param, const char *ipasc) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 502 | { |
| 503 | unsigned char ipout[16]; |
| 504 | size_t iplen; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 505 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 506 | iplen = (size_t)a2i_ipadd(ipout, ipasc); |
| 507 | if (iplen == 0) |
| 508 | return 0; |
| 509 | return X509_VERIFY_PARAM_set1_ip(param, ipout, iplen); |
| 510 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 511 | |
| 512 | int X509_VERIFY_PARAM_get_depth(const X509_VERIFY_PARAM *param) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 513 | { |
| 514 | return param->depth; |
| 515 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 516 | |
| 517 | const char *X509_VERIFY_PARAM_get0_name(const X509_VERIFY_PARAM *param) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 518 | { |
| 519 | return param->name; |
| 520 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 521 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 522 | static const X509_VERIFY_PARAM_ID _empty_id = |
| 523 | { NULL, 0U, NULL, NULL, 0, NULL, 0 }; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 524 | |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 525 | #define vpm_empty_id ((X509_VERIFY_PARAM_ID *)&_empty_id) |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 526 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 527 | /* |
| 528 | * Default verify parameters: these are used for various applications and can |
| 529 | * be overridden by the user specified table. NB: the 'name' field *must* be |
| 530 | * in alphabetical order because it will be searched using OBJ_search. |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 531 | */ |
| 532 | |
| 533 | static const X509_VERIFY_PARAM default_table[] = { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 534 | { |
| 535 | (char *)"default", /* X509 default parameters */ |
| 536 | 0, /* Check time */ |
| 537 | 0, /* internal flags */ |
| 538 | 0, /* flags */ |
| 539 | 0, /* purpose */ |
| 540 | 0, /* trust */ |
| 541 | 100, /* depth */ |
| 542 | NULL, /* policies */ |
| 543 | vpm_empty_id}, |
| 544 | { |
| 545 | (char *)"pkcs7", /* S/MIME sign parameters */ |
| 546 | 0, /* Check time */ |
| 547 | 0, /* internal flags */ |
| 548 | 0, /* flags */ |
| 549 | X509_PURPOSE_SMIME_SIGN, /* purpose */ |
| 550 | X509_TRUST_EMAIL, /* trust */ |
| 551 | -1, /* depth */ |
| 552 | NULL, /* policies */ |
| 553 | vpm_empty_id}, |
| 554 | { |
| 555 | (char *)"smime_sign", /* S/MIME sign parameters */ |
| 556 | 0, /* Check time */ |
| 557 | 0, /* internal flags */ |
| 558 | 0, /* flags */ |
| 559 | X509_PURPOSE_SMIME_SIGN, /* purpose */ |
| 560 | X509_TRUST_EMAIL, /* trust */ |
| 561 | -1, /* depth */ |
| 562 | NULL, /* policies */ |
| 563 | vpm_empty_id}, |
| 564 | { |
| 565 | (char *)"ssl_client", /* SSL/TLS client parameters */ |
| 566 | 0, /* Check time */ |
| 567 | 0, /* internal flags */ |
| 568 | 0, /* flags */ |
| 569 | X509_PURPOSE_SSL_CLIENT, /* purpose */ |
| 570 | X509_TRUST_SSL_CLIENT, /* trust */ |
| 571 | -1, /* depth */ |
| 572 | NULL, /* policies */ |
| 573 | vpm_empty_id}, |
| 574 | { |
| 575 | (char *)"ssl_server", /* SSL/TLS server parameters */ |
| 576 | 0, /* Check time */ |
| 577 | 0, /* internal flags */ |
| 578 | 0, /* flags */ |
| 579 | X509_PURPOSE_SSL_SERVER, /* purpose */ |
| 580 | X509_TRUST_SSL_SERVER, /* trust */ |
| 581 | -1, /* depth */ |
| 582 | NULL, /* policies */ |
| 583 | vpm_empty_id} |
| 584 | }; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 585 | |
| 586 | static STACK_OF(X509_VERIFY_PARAM) *param_table = NULL; |
| 587 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 588 | static int param_cmp(const X509_VERIFY_PARAM **a, const X509_VERIFY_PARAM **b) |
| 589 | { |
| 590 | return strcmp((*a)->name, (*b)->name); |
| 591 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 592 | |
| 593 | int X509_VERIFY_PARAM_add0_table(X509_VERIFY_PARAM *param) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 594 | { |
| 595 | X509_VERIFY_PARAM *ptmp; |
| 596 | if (!param_table) { |
| 597 | param_table = sk_X509_VERIFY_PARAM_new(param_cmp); |
| 598 | if (!param_table) |
| 599 | return 0; |
| 600 | } else { |
| 601 | size_t idx; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 602 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 603 | if (sk_X509_VERIFY_PARAM_find(param_table, &idx, param)) { |
| 604 | ptmp = sk_X509_VERIFY_PARAM_value(param_table, idx); |
| 605 | X509_VERIFY_PARAM_free(ptmp); |
| 606 | (void)sk_X509_VERIFY_PARAM_delete(param_table, idx); |
| 607 | } |
| 608 | } |
| 609 | if (!sk_X509_VERIFY_PARAM_push(param_table, param)) |
| 610 | return 0; |
| 611 | return 1; |
| 612 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 613 | |
| 614 | int X509_VERIFY_PARAM_get_count(void) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 615 | { |
| 616 | int num = sizeof(default_table) / sizeof(X509_VERIFY_PARAM); |
| 617 | if (param_table) |
| 618 | num += sk_X509_VERIFY_PARAM_num(param_table); |
| 619 | return num; |
| 620 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 621 | |
| 622 | const X509_VERIFY_PARAM *X509_VERIFY_PARAM_get0(int id) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 623 | { |
| 624 | int num = sizeof(default_table) / sizeof(X509_VERIFY_PARAM); |
| 625 | if (id < num) |
| 626 | return default_table + id; |
| 627 | return sk_X509_VERIFY_PARAM_value(param_table, id - num); |
| 628 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 629 | |
| 630 | const X509_VERIFY_PARAM *X509_VERIFY_PARAM_lookup(const char *name) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 631 | { |
| 632 | X509_VERIFY_PARAM pm; |
| 633 | unsigned i, limit; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 634 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 635 | pm.name = (char *)name; |
| 636 | if (param_table) { |
| 637 | size_t idx; |
| 638 | if (sk_X509_VERIFY_PARAM_find(param_table, &idx, &pm)) |
| 639 | return sk_X509_VERIFY_PARAM_value(param_table, idx); |
| 640 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 641 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 642 | limit = sizeof(default_table) / sizeof(X509_VERIFY_PARAM); |
| 643 | for (i = 0; i < limit; i++) { |
| 644 | if (strcmp(default_table[i].name, name) == 0) { |
| 645 | return &default_table[i]; |
| 646 | } |
| 647 | } |
| 648 | return NULL; |
| 649 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 650 | |
| 651 | void X509_VERIFY_PARAM_table_cleanup(void) |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 652 | { |
| 653 | if (param_table) |
| 654 | sk_X509_VERIFY_PARAM_pop_free(param_table, X509_VERIFY_PARAM_free); |
| 655 | param_table = NULL; |
| 656 | } |