Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* v3_utl.c */ |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 4 | * project. |
| 5 | */ |
| 6 | /* ==================================================================== |
| 7 | * Copyright (c) 1999-2003 The OpenSSL Project. All rights reserved. |
| 8 | * |
| 9 | * Redistribution and use in source and binary forms, with or without |
| 10 | * modification, are permitted provided that the following conditions |
| 11 | * are met: |
| 12 | * |
| 13 | * 1. Redistributions of source code must retain the above copyright |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 14 | * notice, this list of conditions and the following disclaimer. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 15 | * |
| 16 | * 2. Redistributions in binary form must reproduce the above copyright |
| 17 | * notice, this list of conditions and the following disclaimer in |
| 18 | * the documentation and/or other materials provided with the |
| 19 | * distribution. |
| 20 | * |
| 21 | * 3. All advertising materials mentioning features or use of this |
| 22 | * software must display the following acknowledgment: |
| 23 | * "This product includes software developed by the OpenSSL Project |
| 24 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 25 | * |
| 26 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 27 | * endorse or promote products derived from this software without |
| 28 | * prior written permission. For written permission, please contact |
| 29 | * licensing@OpenSSL.org. |
| 30 | * |
| 31 | * 5. Products derived from this software may not be called "OpenSSL" |
| 32 | * nor may "OpenSSL" appear in their names without prior written |
| 33 | * permission of the OpenSSL Project. |
| 34 | * |
| 35 | * 6. Redistributions of any form whatsoever must retain the following |
| 36 | * acknowledgment: |
| 37 | * "This product includes software developed by the OpenSSL Project |
| 38 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 41 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 43 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 44 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 45 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 46 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 47 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 49 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 50 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 51 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 52 | * ==================================================================== |
| 53 | * |
| 54 | * This product includes cryptographic software written by Eric Young |
| 55 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 56 | * Hudson (tjh@cryptsoft.com). |
| 57 | * |
| 58 | */ |
| 59 | /* X509 v3 extension utilities */ |
| 60 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 61 | #include <ctype.h> |
Adam Langley | dc160f8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 62 | #include <stdio.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 63 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 64 | |
| 65 | #include <openssl/bn.h> |
| 66 | #include <openssl/buf.h> |
| 67 | #include <openssl/conf.h> |
| 68 | #include <openssl/err.h> |
| 69 | #include <openssl/mem.h> |
| 70 | #include <openssl/obj.h> |
| 71 | #include <openssl/x509v3.h> |
| 72 | |
Matt Braithwaite | c4ef5ff | 2015-05-21 17:56:38 -0700 | [diff] [blame] | 73 | #include "../conf/internal.h" |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 74 | #include "../internal.h" |
| 75 | |
Matt Braithwaite | c4ef5ff | 2015-05-21 17:56:38 -0700 | [diff] [blame] | 76 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 77 | static char *strip_spaces(char *name); |
| 78 | static int sk_strcmp(const OPENSSL_STRING *a, const OPENSSL_STRING *b); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 79 | static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name, |
| 80 | GENERAL_NAMES *gens); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 81 | static void str_free(OPENSSL_STRING str); |
| 82 | static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email); |
| 83 | |
| 84 | static int ipv4_from_asc(unsigned char *v4, const char *in); |
| 85 | static int ipv6_from_asc(unsigned char *v6, const char *in); |
| 86 | static int ipv6_cb(const char *elem, int len, void *usr); |
| 87 | static int ipv6_hex(unsigned char *out, const char *in, int inlen); |
| 88 | |
| 89 | /* Add a CONF_VALUE name value pair to stack */ |
| 90 | |
| 91 | int X509V3_add_value(const char *name, const char *value, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 92 | STACK_OF(CONF_VALUE) **extlist) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 93 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 94 | CONF_VALUE *vtmp = NULL; |
| 95 | char *tname = NULL, *tvalue = NULL; |
| 96 | if (name && !(tname = BUF_strdup(name))) |
| 97 | goto err; |
| 98 | if (value && !(tvalue = BUF_strdup(value))) |
| 99 | goto err; |
| 100 | if (!(vtmp = CONF_VALUE_new())) |
| 101 | goto err; |
| 102 | if (!*extlist && !(*extlist = sk_CONF_VALUE_new_null())) |
| 103 | goto err; |
| 104 | vtmp->section = NULL; |
| 105 | vtmp->name = tname; |
| 106 | vtmp->value = tvalue; |
| 107 | if (!sk_CONF_VALUE_push(*extlist, vtmp)) |
| 108 | goto err; |
| 109 | return 1; |
| 110 | err: |
| 111 | OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE); |
| 112 | if (vtmp) |
| 113 | OPENSSL_free(vtmp); |
| 114 | if (tname) |
| 115 | OPENSSL_free(tname); |
| 116 | if (tvalue) |
| 117 | OPENSSL_free(tvalue); |
| 118 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | int X509V3_add_value_uchar(const char *name, const unsigned char *value, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 122 | STACK_OF(CONF_VALUE) **extlist) |
| 123 | { |
| 124 | return X509V3_add_value(name, (const char *)value, extlist); |
| 125 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 126 | |
| 127 | /* Free function for STACK_OF(CONF_VALUE) */ |
| 128 | |
| 129 | void X509V3_conf_free(CONF_VALUE *conf) |
| 130 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 131 | if (!conf) |
| 132 | return; |
| 133 | if (conf->name) |
| 134 | OPENSSL_free(conf->name); |
| 135 | if (conf->value) |
| 136 | OPENSSL_free(conf->value); |
| 137 | if (conf->section) |
| 138 | OPENSSL_free(conf->section); |
| 139 | OPENSSL_free(conf); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | int X509V3_add_value_bool(const char *name, int asn1_bool, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 143 | STACK_OF(CONF_VALUE) **extlist) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 144 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 145 | if (asn1_bool) |
| 146 | return X509V3_add_value(name, "TRUE", extlist); |
| 147 | return X509V3_add_value(name, "FALSE", extlist); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | int X509V3_add_value_bool_nf(char *name, int asn1_bool, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 151 | STACK_OF(CONF_VALUE) **extlist) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 152 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 153 | if (asn1_bool) |
| 154 | return X509V3_add_value(name, "TRUE", extlist); |
| 155 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 158 | char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a) |
| 159 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 160 | BIGNUM *bntmp = NULL; |
| 161 | char *strtmp = NULL; |
| 162 | if (!a) |
| 163 | return NULL; |
| 164 | if (!(bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) || |
| 165 | !(strtmp = BN_bn2dec(bntmp))) |
| 166 | OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE); |
| 167 | BN_free(bntmp); |
| 168 | return strtmp; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a) |
| 172 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 173 | BIGNUM *bntmp = NULL; |
| 174 | char *strtmp = NULL; |
| 175 | if (!a) |
| 176 | return NULL; |
| 177 | if (!(bntmp = ASN1_INTEGER_to_BN(a, NULL)) || |
| 178 | !(strtmp = BN_bn2dec(bntmp))) |
| 179 | OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE); |
| 180 | BN_free(bntmp); |
| 181 | return strtmp; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value) |
| 185 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 186 | BIGNUM *bn = NULL; |
| 187 | ASN1_INTEGER *aint; |
| 188 | int isneg, ishex; |
| 189 | int ret; |
| 190 | if (!value) { |
| 191 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE); |
| 192 | return 0; |
| 193 | } |
| 194 | bn = BN_new(); |
| 195 | if (value[0] == '-') { |
| 196 | value++; |
| 197 | isneg = 1; |
| 198 | } else |
| 199 | isneg = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 200 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 201 | if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) { |
| 202 | value += 2; |
| 203 | ishex = 1; |
| 204 | } else |
| 205 | ishex = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 206 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 207 | if (ishex) |
| 208 | ret = BN_hex2bn(&bn, value); |
| 209 | else |
| 210 | ret = BN_dec2bn(&bn, value); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 211 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 212 | if (!ret || value[ret]) { |
| 213 | BN_free(bn); |
| 214 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_BN_DEC2BN_ERROR); |
| 215 | return 0; |
| 216 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 218 | if (isneg && BN_is_zero(bn)) |
| 219 | isneg = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 220 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 221 | aint = BN_to_ASN1_INTEGER(bn, NULL); |
| 222 | BN_free(bn); |
| 223 | if (!aint) { |
| 224 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_BN_TO_ASN1_INTEGER_ERROR); |
| 225 | return 0; |
| 226 | } |
| 227 | if (isneg) |
| 228 | aint->type |= V_ASN1_NEG; |
| 229 | return aint; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 233 | STACK_OF(CONF_VALUE) **extlist) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 234 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 235 | char *strtmp; |
| 236 | int ret; |
| 237 | if (!aint) |
| 238 | return 1; |
| 239 | if (!(strtmp = i2s_ASN1_INTEGER(NULL, aint))) |
| 240 | return 0; |
| 241 | ret = X509V3_add_value(name, strtmp, extlist); |
| 242 | OPENSSL_free(strtmp); |
| 243 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool) |
| 247 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 248 | char *btmp; |
| 249 | if (!(btmp = value->value)) |
| 250 | goto err; |
| 251 | if (!strcmp(btmp, "TRUE") || !strcmp(btmp, "true") |
| 252 | || !strcmp(btmp, "Y") || !strcmp(btmp, "y") |
| 253 | || !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) { |
| 254 | *asn1_bool = 0xff; |
| 255 | return 1; |
| 256 | } else if (!strcmp(btmp, "FALSE") || !strcmp(btmp, "false") |
| 257 | || !strcmp(btmp, "N") || !strcmp(btmp, "n") |
| 258 | || !strcmp(btmp, "NO") || !strcmp(btmp, "no")) { |
| 259 | *asn1_bool = 0; |
| 260 | return 1; |
| 261 | } |
| 262 | err: |
| 263 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_BOOLEAN_STRING); |
| 264 | X509V3_conf_err(value); |
| 265 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint) |
| 269 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 270 | ASN1_INTEGER *itmp; |
| 271 | if (!(itmp = s2i_ASN1_INTEGER(NULL, value->value))) { |
| 272 | X509V3_conf_err(value); |
| 273 | return 0; |
| 274 | } |
| 275 | *aint = itmp; |
| 276 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 279 | #define HDR_NAME 1 |
| 280 | #define HDR_VALUE 2 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 281 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 282 | /* |
| 283 | * #define DEBUG |
| 284 | */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 285 | |
| 286 | STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line) |
| 287 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 288 | char *p, *q, c; |
| 289 | char *ntmp, *vtmp; |
| 290 | STACK_OF(CONF_VALUE) *values = NULL; |
| 291 | char *linebuf; |
| 292 | int state; |
| 293 | /* We are going to modify the line so copy it first */ |
| 294 | linebuf = BUF_strdup(line); |
| 295 | if (linebuf == NULL) { |
| 296 | OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE); |
| 297 | goto err; |
| 298 | } |
| 299 | state = HDR_NAME; |
| 300 | ntmp = NULL; |
| 301 | /* Go through all characters */ |
| 302 | for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n'); |
| 303 | p++) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 304 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 305 | switch (state) { |
| 306 | case HDR_NAME: |
| 307 | if (c == ':') { |
| 308 | state = HDR_VALUE; |
| 309 | *p = 0; |
| 310 | ntmp = strip_spaces(q); |
| 311 | if (!ntmp) { |
| 312 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME); |
| 313 | goto err; |
| 314 | } |
| 315 | q = p + 1; |
| 316 | } else if (c == ',') { |
| 317 | *p = 0; |
| 318 | ntmp = strip_spaces(q); |
| 319 | q = p + 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 320 | #if 0 |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 321 | printf("%s\n", ntmp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 322 | #endif |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 323 | if (!ntmp) { |
| 324 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME); |
| 325 | goto err; |
| 326 | } |
| 327 | X509V3_add_value(ntmp, NULL, &values); |
| 328 | } |
| 329 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 330 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 331 | case HDR_VALUE: |
| 332 | if (c == ',') { |
| 333 | state = HDR_NAME; |
| 334 | *p = 0; |
| 335 | vtmp = strip_spaces(q); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 336 | #if 0 |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 337 | printf("%s\n", ntmp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 338 | #endif |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 339 | if (!vtmp) { |
| 340 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE); |
| 341 | goto err; |
| 342 | } |
| 343 | X509V3_add_value(ntmp, vtmp, &values); |
| 344 | ntmp = NULL; |
| 345 | q = p + 1; |
| 346 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 347 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 348 | } |
| 349 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 350 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 351 | if (state == HDR_VALUE) { |
| 352 | vtmp = strip_spaces(q); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 353 | #if 0 |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 354 | printf("%s=%s\n", ntmp, vtmp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 355 | #endif |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 356 | if (!vtmp) { |
| 357 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE); |
| 358 | goto err; |
| 359 | } |
| 360 | X509V3_add_value(ntmp, vtmp, &values); |
| 361 | } else { |
| 362 | ntmp = strip_spaces(q); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 363 | #if 0 |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 364 | printf("%s\n", ntmp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 365 | #endif |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 366 | if (!ntmp) { |
| 367 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME); |
| 368 | goto err; |
| 369 | } |
| 370 | X509V3_add_value(ntmp, NULL, &values); |
| 371 | } |
| 372 | OPENSSL_free(linebuf); |
| 373 | return values; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 374 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 375 | err: |
| 376 | OPENSSL_free(linebuf); |
| 377 | sk_CONF_VALUE_pop_free(values, X509V3_conf_free); |
| 378 | return NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 379 | |
| 380 | } |
| 381 | |
| 382 | /* Delete leading and trailing spaces from a string */ |
| 383 | static char *strip_spaces(char *name) |
| 384 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 385 | char *p, *q; |
| 386 | /* Skip over leading spaces */ |
| 387 | p = name; |
| 388 | while (*p && isspace((unsigned char)*p)) |
| 389 | p++; |
| 390 | if (!*p) |
| 391 | return NULL; |
| 392 | q = p + strlen(p) - 1; |
| 393 | while ((q != p) && isspace((unsigned char)*q)) |
| 394 | q--; |
| 395 | if (p != q) |
| 396 | q[1] = 0; |
| 397 | if (!*p) |
| 398 | return NULL; |
| 399 | return p; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* hex string utilities */ |
| 403 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 404 | /* |
| 405 | * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its |
| 406 | * hex representation @@@ (Contents of buffer are always kept in ASCII, also |
| 407 | * on EBCDIC machines) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 408 | */ |
| 409 | |
| 410 | char *hex_to_string(const unsigned char *buffer, long len) |
| 411 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 412 | char *tmp, *q; |
| 413 | const unsigned char *p; |
| 414 | int i; |
| 415 | static const char hexdig[] = "0123456789ABCDEF"; |
| 416 | if (!buffer || !len) |
| 417 | return NULL; |
| 418 | if (!(tmp = OPENSSL_malloc(len * 3 + 1))) { |
| 419 | OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE); |
| 420 | return NULL; |
| 421 | } |
| 422 | q = tmp; |
| 423 | for (i = 0, p = buffer; i < len; i++, p++) { |
| 424 | *q++ = hexdig[(*p >> 4) & 0xf]; |
| 425 | *q++ = hexdig[*p & 0xf]; |
| 426 | *q++ = ':'; |
| 427 | } |
| 428 | q[-1] = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 429 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 430 | return tmp; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 433 | /* |
| 434 | * Give a string of hex digits convert to a buffer |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 435 | */ |
| 436 | |
| 437 | unsigned char *string_to_hex(const char *str, long *len) |
| 438 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 439 | unsigned char *hexbuf, *q; |
| 440 | unsigned char ch, cl, *p; |
| 441 | if (!str) { |
| 442 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_ARGUMENT); |
| 443 | return NULL; |
| 444 | } |
| 445 | if (!(hexbuf = OPENSSL_malloc(strlen(str) >> 1))) |
| 446 | goto err; |
| 447 | for (p = (unsigned char *)str, q = hexbuf; *p;) { |
| 448 | ch = *p++; |
| 449 | if (ch == ':') |
| 450 | continue; |
| 451 | cl = *p++; |
| 452 | if (!cl) { |
| 453 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_ODD_NUMBER_OF_DIGITS); |
| 454 | OPENSSL_free(hexbuf); |
| 455 | return NULL; |
| 456 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 457 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 458 | if ((ch >= '0') && (ch <= '9')) |
| 459 | ch -= '0'; |
| 460 | else if ((ch >= 'a') && (ch <= 'f')) |
| 461 | ch -= 'a' - 10; |
Adam Langley | ccf8057 | 2017-07-25 14:49:30 -0700 | [diff] [blame] | 462 | else if ((ch >= 'A') && (ch <= 'F')) |
| 463 | ch -= 'A' - 10; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 464 | else |
| 465 | goto badhex; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 466 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 467 | if ((cl >= '0') && (cl <= '9')) |
| 468 | cl -= '0'; |
| 469 | else if ((cl >= 'a') && (cl <= 'f')) |
| 470 | cl -= 'a' - 10; |
Adam Langley | ccf8057 | 2017-07-25 14:49:30 -0700 | [diff] [blame] | 471 | else if ((cl >= 'A') && (cl <= 'F')) |
| 472 | cl -= 'A' - 10; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 473 | else |
| 474 | goto badhex; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 475 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 476 | *q++ = (ch << 4) | cl; |
| 477 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 478 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 479 | if (len) |
| 480 | *len = q - hexbuf; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 481 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 482 | return hexbuf; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 483 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 484 | err: |
| 485 | if (hexbuf) |
| 486 | OPENSSL_free(hexbuf); |
| 487 | OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE); |
| 488 | return NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 489 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 490 | badhex: |
| 491 | OPENSSL_free(hexbuf); |
| 492 | OPENSSL_PUT_ERROR(X509V3, X509V3_R_ILLEGAL_HEX_DIGIT); |
| 493 | return NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 494 | |
| 495 | } |
| 496 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 497 | /* |
| 498 | * V2I name comparison function: returns zero if 'name' matches cmp or cmp.* |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 499 | */ |
| 500 | |
| 501 | int name_cmp(const char *name, const char *cmp) |
| 502 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 503 | int len, ret; |
| 504 | char c; |
| 505 | len = strlen(cmp); |
| 506 | if ((ret = strncmp(name, cmp, len))) |
| 507 | return ret; |
| 508 | c = name[len]; |
| 509 | if (!c || (c == '.')) |
| 510 | return 0; |
| 511 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | static int sk_strcmp(const OPENSSL_STRING *a, const OPENSSL_STRING *b) |
| 515 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 516 | return strcmp(*a, *b); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x) |
| 520 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 521 | GENERAL_NAMES *gens; |
| 522 | STACK_OF(OPENSSL_STRING) *ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 523 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 524 | gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 525 | ret = get_email(X509_get_subject_name(x), gens); |
| 526 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
| 527 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x) |
| 531 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 532 | AUTHORITY_INFO_ACCESS *info; |
| 533 | STACK_OF(OPENSSL_STRING) *ret = NULL; |
| 534 | size_t i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 535 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 536 | info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL); |
| 537 | if (!info) |
| 538 | return NULL; |
| 539 | for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) { |
| 540 | ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i); |
| 541 | if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) { |
| 542 | if (ad->location->type == GEN_URI) { |
| 543 | if (!append_ia5 |
| 544 | (&ret, ad->location->d.uniformResourceIdentifier)) |
| 545 | break; |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | AUTHORITY_INFO_ACCESS_free(info); |
| 550 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x) |
| 554 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 555 | GENERAL_NAMES *gens; |
| 556 | STACK_OF(X509_EXTENSION) *exts; |
| 557 | STACK_OF(OPENSSL_STRING) *ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 558 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 559 | exts = X509_REQ_get_extensions(x); |
| 560 | gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL); |
| 561 | ret = get_email(X509_REQ_get_subject_name(x), gens); |
| 562 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
| 563 | sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free); |
| 564 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 567 | static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name, |
| 568 | GENERAL_NAMES *gens) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 569 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 570 | STACK_OF(OPENSSL_STRING) *ret = NULL; |
| 571 | X509_NAME_ENTRY *ne; |
| 572 | ASN1_IA5STRING *email; |
| 573 | GENERAL_NAME *gen; |
| 574 | int i; |
| 575 | size_t j; |
| 576 | /* Now add any email address(es) to STACK */ |
| 577 | i = -1; |
| 578 | /* First supplied X509_NAME */ |
| 579 | while ((i = X509_NAME_get_index_by_NID(name, |
| 580 | NID_pkcs9_emailAddress, i)) >= 0) { |
| 581 | ne = X509_NAME_get_entry(name, i); |
| 582 | email = X509_NAME_ENTRY_get_data(ne); |
| 583 | if (!append_ia5(&ret, email)) |
| 584 | return NULL; |
| 585 | } |
| 586 | for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) { |
| 587 | gen = sk_GENERAL_NAME_value(gens, j); |
| 588 | if (gen->type != GEN_EMAIL) |
| 589 | continue; |
| 590 | if (!append_ia5(&ret, gen->d.ia5)) |
| 591 | return NULL; |
| 592 | } |
| 593 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | static void str_free(OPENSSL_STRING str) |
| 597 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 598 | OPENSSL_free(str); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email) |
| 602 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 603 | char *emtmp; |
| 604 | /* First some sanity checks */ |
| 605 | if (email->type != V_ASN1_IA5STRING) |
| 606 | return 1; |
| 607 | if (!email->data || !email->length) |
| 608 | return 1; |
| 609 | if (!*sk) |
| 610 | *sk = sk_OPENSSL_STRING_new(sk_strcmp); |
| 611 | if (!*sk) |
| 612 | return 0; |
| 613 | /* Don't add duplicates */ |
| 614 | if (sk_OPENSSL_STRING_find(*sk, NULL, (char *)email->data)) |
| 615 | return 1; |
| 616 | emtmp = BUF_strdup((char *)email->data); |
| 617 | if (!emtmp || !sk_OPENSSL_STRING_push(*sk, emtmp)) { |
| 618 | X509_email_free(*sk); |
| 619 | *sk = NULL; |
| 620 | return 0; |
| 621 | } |
| 622 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | void X509_email_free(STACK_OF(OPENSSL_STRING) *sk) |
| 626 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 627 | sk_OPENSSL_STRING_pop_free(sk, str_free); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 628 | } |
| 629 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 630 | typedef int (*equal_fn) (const unsigned char *pattern, size_t pattern_len, |
| 631 | const unsigned char *subject, size_t subject_len, |
| 632 | unsigned int flags); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 633 | |
Adam Langley | 8241005 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 634 | /* Skip pattern prefix to match "wildcard" subject */ |
| 635 | static void skip_prefix(const unsigned char **p, size_t *plen, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 636 | const unsigned char *subject, size_t subject_len, |
| 637 | unsigned int flags) |
| 638 | { |
| 639 | const unsigned char *pattern = *p; |
| 640 | size_t pattern_len = *plen; |
Adam Langley | 8241005 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 641 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 642 | /* |
| 643 | * If subject starts with a leading '.' followed by more octets, and |
| 644 | * pattern is longer, compare just an equal-length suffix with the |
| 645 | * full subject (starting at the '.'), provided the prefix contains |
| 646 | * no NULs. |
| 647 | */ |
| 648 | if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0) |
| 649 | return; |
Adam Langley | 8241005 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 650 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 651 | while (pattern_len > subject_len && *pattern) { |
| 652 | if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) && |
| 653 | *pattern == '.') |
| 654 | break; |
| 655 | ++pattern; |
| 656 | --pattern_len; |
| 657 | } |
Adam Langley | 8241005 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 658 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 659 | /* Skip if entire prefix acceptable */ |
| 660 | if (pattern_len == subject_len) { |
| 661 | *p = pattern; |
| 662 | *plen = pattern_len; |
| 663 | } |
| 664 | } |
Adam Langley | 8241005 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 665 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 666 | /* Compare while ASCII ignoring case. */ |
| 667 | static int equal_nocase(const unsigned char *pattern, size_t pattern_len, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 668 | const unsigned char *subject, size_t subject_len, |
| 669 | unsigned int flags) |
| 670 | { |
| 671 | skip_prefix(&pattern, &pattern_len, subject, subject_len, flags); |
| 672 | if (pattern_len != subject_len) |
| 673 | return 0; |
| 674 | while (pattern_len) { |
| 675 | unsigned char l = *pattern; |
| 676 | unsigned char r = *subject; |
| 677 | /* The pattern must not contain NUL characters. */ |
| 678 | if (l == 0) |
| 679 | return 0; |
| 680 | if (l != r) { |
| 681 | if ('A' <= l && l <= 'Z') |
| 682 | l = (l - 'A') + 'a'; |
| 683 | if ('A' <= r && r <= 'Z') |
| 684 | r = (r - 'A') + 'a'; |
| 685 | if (l != r) |
| 686 | return 0; |
| 687 | } |
| 688 | ++pattern; |
| 689 | ++subject; |
| 690 | --pattern_len; |
| 691 | } |
| 692 | return 1; |
| 693 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 694 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 695 | /* Compare using OPENSSL_memcmp. */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 696 | static int equal_case(const unsigned char *pattern, size_t pattern_len, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 697 | const unsigned char *subject, size_t subject_len, |
| 698 | unsigned int flags) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 699 | { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 700 | skip_prefix(&pattern, &pattern_len, subject, subject_len, flags); |
| 701 | if (pattern_len != subject_len) |
| 702 | return 0; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 703 | return !OPENSSL_memcmp(pattern, subject, pattern_len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 704 | } |
| 705 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 706 | /* |
| 707 | * RFC 5280, section 7.5, requires that only the domain is compared in a |
| 708 | * case-insensitive manner. |
| 709 | */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 710 | static int equal_email(const unsigned char *a, size_t a_len, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 711 | const unsigned char *b, size_t b_len, |
| 712 | unsigned int unused_flags) |
| 713 | { |
| 714 | size_t i = a_len; |
| 715 | if (a_len != b_len) |
| 716 | return 0; |
| 717 | /* |
| 718 | * We search backwards for the '@' character, so that we do not have to |
| 719 | * deal with quoted local-parts. The domain part is compared in a |
| 720 | * case-insensitive manner. |
| 721 | */ |
| 722 | while (i > 0) { |
| 723 | --i; |
| 724 | if (a[i] == '@' || b[i] == '@') { |
| 725 | if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0)) |
| 726 | return 0; |
| 727 | break; |
| 728 | } |
| 729 | } |
| 730 | if (i == 0) |
| 731 | i = a_len; |
| 732 | return equal_case(a, i, b, i, 0); |
| 733 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 734 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 735 | /* |
| 736 | * Compare the prefix and suffix with the subject, and check that the |
| 737 | * characters in-between are valid. |
| 738 | */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 739 | static int wildcard_match(const unsigned char *prefix, size_t prefix_len, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 740 | const unsigned char *suffix, size_t suffix_len, |
| 741 | const unsigned char *subject, size_t subject_len, |
| 742 | unsigned int flags) |
| 743 | { |
| 744 | const unsigned char *wildcard_start; |
| 745 | const unsigned char *wildcard_end; |
| 746 | const unsigned char *p; |
| 747 | int allow_multi = 0; |
| 748 | int allow_idna = 0; |
Adam Langley | dc160f8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 749 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 750 | if (subject_len < prefix_len + suffix_len) |
| 751 | return 0; |
| 752 | if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags)) |
| 753 | return 0; |
| 754 | wildcard_start = subject + prefix_len; |
| 755 | wildcard_end = subject + (subject_len - suffix_len); |
| 756 | if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags)) |
| 757 | return 0; |
| 758 | /* |
| 759 | * If the wildcard makes up the entire first label, it must match at |
| 760 | * least one character. |
| 761 | */ |
| 762 | if (prefix_len == 0 && *suffix == '.') { |
| 763 | if (wildcard_start == wildcard_end) |
| 764 | return 0; |
| 765 | allow_idna = 1; |
| 766 | if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS) |
| 767 | allow_multi = 1; |
| 768 | } |
| 769 | /* IDNA labels cannot match partial wildcards */ |
| 770 | if (!allow_idna && |
| 771 | subject_len >= 4 |
| 772 | && OPENSSL_strncasecmp((char *)subject, "xn--", 4) == 0) |
| 773 | return 0; |
| 774 | /* The wildcard may match a literal '*' */ |
| 775 | if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*') |
| 776 | return 1; |
| 777 | /* |
| 778 | * Check that the part matched by the wildcard contains only |
| 779 | * permitted characters and only matches a single label unless |
| 780 | * allow_multi is set. |
| 781 | */ |
| 782 | for (p = wildcard_start; p != wildcard_end; ++p) |
| 783 | if (!(('0' <= *p && *p <= '9') || |
| 784 | ('A' <= *p && *p <= 'Z') || |
| 785 | ('a' <= *p && *p <= 'z') || |
| 786 | *p == '-' || (allow_multi && *p == '.'))) |
| 787 | return 0; |
| 788 | return 1; |
| 789 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 790 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 791 | #define LABEL_START (1 << 0) |
| 792 | #define LABEL_END (1 << 1) |
| 793 | #define LABEL_HYPHEN (1 << 2) |
| 794 | #define LABEL_IDNA (1 << 3) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 795 | |
Adam Langley | dc160f8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 796 | static const unsigned char *valid_star(const unsigned char *p, size_t len, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 797 | unsigned int flags) |
| 798 | { |
| 799 | const unsigned char *star = 0; |
| 800 | size_t i; |
| 801 | int state = LABEL_START; |
| 802 | int dots = 0; |
| 803 | for (i = 0; i < len; ++i) { |
| 804 | /* |
| 805 | * Locate first and only legal wildcard, either at the start |
| 806 | * or end of a non-IDNA first and not final label. |
| 807 | */ |
| 808 | if (p[i] == '*') { |
| 809 | int atstart = (state & LABEL_START); |
| 810 | int atend = (i == len - 1 || p[i + 1] == '.'); |
| 811 | /* |
| 812 | * At most one wildcard per pattern. |
| 813 | * No wildcards in IDNA labels. |
| 814 | * No wildcards after the first label. |
| 815 | */ |
| 816 | if (star != NULL || (state & LABEL_IDNA) != 0 || dots) |
| 817 | return NULL; |
| 818 | /* Only full-label '*.example.com' wildcards? */ |
| 819 | if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS) |
| 820 | && (!atstart || !atend)) |
| 821 | return NULL; |
| 822 | /* No 'foo*bar' wildcards */ |
| 823 | if (!atstart && !atend) |
| 824 | return NULL; |
| 825 | star = &p[i]; |
| 826 | state &= ~LABEL_START; |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 827 | } else if (('a' <= p[i] && p[i] <= 'z') |
| 828 | || ('A' <= p[i] && p[i] <= 'Z') |
| 829 | || ('0' <= p[i] && p[i] <= '9')) { |
Alessandro Ghedini | 5ae4165 | 2017-02-22 22:04:10 +0000 | [diff] [blame] | 830 | if ((state & LABEL_START) != 0 |
| 831 | && len - i >= 4 |
| 832 | && OPENSSL_strncasecmp((char *)&p[i], "xn--", 4) == 0) |
| 833 | state |= LABEL_IDNA; |
| 834 | state &= ~(LABEL_HYPHEN | LABEL_START); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 835 | } else if (p[i] == '.') { |
Alessandro Ghedini | 5ae4165 | 2017-02-22 22:04:10 +0000 | [diff] [blame] | 836 | if ((state & (LABEL_HYPHEN | LABEL_START)) != 0) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 837 | return NULL; |
| 838 | state = LABEL_START; |
| 839 | ++dots; |
| 840 | } else if (p[i] == '-') { |
Steven Valdez | e412bbd | 2016-02-23 15:37:41 -0500 | [diff] [blame] | 841 | /* no domain/subdomain starts with '-' */ |
| 842 | if ((state & LABEL_START) != 0) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 843 | return NULL; |
| 844 | state |= LABEL_HYPHEN; |
| 845 | } else |
| 846 | return NULL; |
| 847 | } |
Adam Langley | dc160f8 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 848 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 849 | /* |
| 850 | * The final label must not end in a hyphen or ".", and |
| 851 | * there must be at least two dots after the star. |
| 852 | */ |
| 853 | if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2) |
| 854 | return NULL; |
| 855 | return star; |
| 856 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 857 | |
| 858 | /* Compare using wildcards. */ |
| 859 | static int equal_wildcard(const unsigned char *pattern, size_t pattern_len, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 860 | const unsigned char *subject, size_t subject_len, |
| 861 | unsigned int flags) |
| 862 | { |
| 863 | const unsigned char *star = NULL; |
Adam Langley | 8241005 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 864 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 865 | /* |
| 866 | * Subject names starting with '.' can only match a wildcard pattern |
| 867 | * via a subject sub-domain pattern suffix match. |
| 868 | */ |
| 869 | if (!(subject_len > 1 && subject[0] == '.')) |
| 870 | star = valid_star(pattern, pattern_len, flags); |
| 871 | if (star == NULL) |
| 872 | return equal_nocase(pattern, pattern_len, |
| 873 | subject, subject_len, flags); |
| 874 | return wildcard_match(pattern, star - pattern, |
| 875 | star + 1, (pattern + pattern_len) - star - 1, |
| 876 | subject, subject_len, flags); |
| 877 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 878 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 879 | /* |
| 880 | * Compare an ASN1_STRING to a supplied string. If they match return 1. If |
| 881 | * cmp_type > 0 only compare if string matches the type, otherwise convert it |
| 882 | * to UTF8. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 883 | */ |
| 884 | |
| 885 | static int do_check_string(ASN1_STRING *a, int cmp_type, equal_fn equal, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 886 | unsigned int flags, const char *b, size_t blen, |
| 887 | char **peername) |
| 888 | { |
| 889 | int rv = 0; |
Adam Langley | 6f8c366 | 2015-02-11 15:30:43 -0800 | [diff] [blame] | 890 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 891 | if (!a->data || !a->length) |
| 892 | return 0; |
| 893 | if (cmp_type > 0) { |
| 894 | if (cmp_type != a->type) |
| 895 | return 0; |
| 896 | if (cmp_type == V_ASN1_IA5STRING) |
| 897 | rv = equal(a->data, a->length, (unsigned char *)b, blen, flags); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 898 | else if (a->length == (int)blen && !OPENSSL_memcmp(a->data, b, blen)) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 899 | rv = 1; |
| 900 | if (rv > 0 && peername) |
| 901 | *peername = BUF_strndup((char *)a->data, a->length); |
| 902 | } else { |
| 903 | int astrlen; |
| 904 | unsigned char *astr; |
| 905 | astrlen = ASN1_STRING_to_UTF8(&astr, a); |
| 906 | if (astrlen < 0) |
| 907 | return -1; |
| 908 | rv = equal(astr, astrlen, (unsigned char *)b, blen, flags); |
| 909 | if (rv > 0 && peername) |
| 910 | *peername = BUF_strndup((char *)astr, astrlen); |
| 911 | OPENSSL_free(astr); |
| 912 | } |
| 913 | return rv; |
| 914 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 915 | |
Adam Langley | 6899b19 | 2015-02-11 15:34:11 -0800 | [diff] [blame] | 916 | static int do_x509_check(X509 *x, const char *chk, size_t chklen, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 917 | unsigned int flags, int check_type, char **peername) |
| 918 | { |
| 919 | GENERAL_NAMES *gens = NULL; |
| 920 | X509_NAME *name = NULL; |
| 921 | size_t i; |
| 922 | int j; |
| 923 | int cnid = NID_undef; |
| 924 | int alt_type; |
| 925 | int san_present = 0; |
| 926 | int rv = 0; |
| 927 | equal_fn equal; |
Adam Langley | 464f3a1 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 928 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 929 | /* See below, this flag is internal-only */ |
| 930 | flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS; |
| 931 | if (check_type == GEN_EMAIL) { |
| 932 | cnid = NID_pkcs9_emailAddress; |
| 933 | alt_type = V_ASN1_IA5STRING; |
| 934 | equal = equal_email; |
| 935 | } else if (check_type == GEN_DNS) { |
| 936 | cnid = NID_commonName; |
| 937 | /* Implicit client-side DNS sub-domain pattern */ |
| 938 | if (chklen > 1 && chk[0] == '.') |
| 939 | flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS; |
| 940 | alt_type = V_ASN1_IA5STRING; |
| 941 | if (flags & X509_CHECK_FLAG_NO_WILDCARDS) |
| 942 | equal = equal_nocase; |
| 943 | else |
| 944 | equal = equal_wildcard; |
| 945 | } else { |
| 946 | alt_type = V_ASN1_OCTET_STRING; |
| 947 | equal = equal_case; |
| 948 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 949 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 950 | gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 951 | if (gens) { |
| 952 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
| 953 | GENERAL_NAME *gen; |
| 954 | ASN1_STRING *cstr; |
| 955 | gen = sk_GENERAL_NAME_value(gens, i); |
| 956 | if (gen->type != check_type) |
| 957 | continue; |
| 958 | san_present = 1; |
| 959 | if (check_type == GEN_EMAIL) |
| 960 | cstr = gen->d.rfc822Name; |
| 961 | else if (check_type == GEN_DNS) |
| 962 | cstr = gen->d.dNSName; |
| 963 | else |
| 964 | cstr = gen->d.iPAddress; |
| 965 | /* Positive on success, negative on error! */ |
| 966 | if ((rv = do_check_string(cstr, alt_type, equal, flags, |
| 967 | chk, chklen, peername)) != 0) |
| 968 | break; |
| 969 | } |
| 970 | GENERAL_NAMES_free(gens); |
| 971 | if (rv != 0) |
| 972 | return rv; |
| 973 | if (cnid == NID_undef |
| 974 | || (san_present |
| 975 | && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT))) |
| 976 | return 0; |
| 977 | } |
David Benjamin | b50c91b | 2015-09-19 13:50:27 -0400 | [diff] [blame] | 978 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 979 | /* We're done if CN-ID is not pertinent */ |
| 980 | if (cnid == NID_undef) |
| 981 | return 0; |
David Benjamin | b50c91b | 2015-09-19 13:50:27 -0400 | [diff] [blame] | 982 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 983 | j = -1; |
| 984 | name = X509_get_subject_name(x); |
| 985 | while ((j = X509_NAME_get_index_by_NID(name, cnid, j)) >= 0) { |
| 986 | X509_NAME_ENTRY *ne; |
| 987 | ASN1_STRING *str; |
| 988 | ne = X509_NAME_get_entry(name, j); |
| 989 | str = X509_NAME_ENTRY_get_data(ne); |
| 990 | /* Positive on success, negative on error! */ |
| 991 | if ((rv = do_check_string(str, -1, equal, flags, |
| 992 | chk, chklen, peername)) != 0) |
| 993 | return rv; |
| 994 | } |
| 995 | return 0; |
| 996 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 997 | |
Adam Langley | 6899b19 | 2015-02-11 15:34:11 -0800 | [diff] [blame] | 998 | int X509_check_host(X509 *x, const char *chk, size_t chklen, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 999 | unsigned int flags, char **peername) |
| 1000 | { |
| 1001 | if (chk == NULL) |
| 1002 | return -2; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1003 | if (OPENSSL_memchr(chk, '\0', chklen)) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1004 | return -2; |
| 1005 | return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername); |
| 1006 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1007 | |
Adam Langley | 6899b19 | 2015-02-11 15:34:11 -0800 | [diff] [blame] | 1008 | int X509_check_email(X509 *x, const char *chk, size_t chklen, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1009 | unsigned int flags) |
| 1010 | { |
| 1011 | if (chk == NULL) |
| 1012 | return -2; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1013 | if (OPENSSL_memchr(chk, '\0', chklen)) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1014 | return -2; |
| 1015 | return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL); |
| 1016 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1017 | |
| 1018 | int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen, |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1019 | unsigned int flags) |
| 1020 | { |
| 1021 | if (chk == NULL) |
| 1022 | return -2; |
| 1023 | return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL); |
| 1024 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1025 | |
| 1026 | int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1027 | { |
| 1028 | unsigned char ipout[16]; |
| 1029 | size_t iplen; |
Adam Langley | 6899b19 | 2015-02-11 15:34:11 -0800 | [diff] [blame] | 1030 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1031 | if (ipasc == NULL) |
| 1032 | return -2; |
| 1033 | iplen = (size_t)a2i_ipadd(ipout, ipasc); |
| 1034 | if (iplen == 0) |
| 1035 | return -2; |
| 1036 | return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL); |
| 1037 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1038 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1039 | /* |
| 1040 | * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible |
| 1041 | * with RFC3280. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1042 | */ |
| 1043 | |
| 1044 | ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1045 | { |
| 1046 | unsigned char ipout[16]; |
| 1047 | ASN1_OCTET_STRING *ret; |
| 1048 | int iplen; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1049 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1050 | /* If string contains a ':' assume IPv6 */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1051 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1052 | iplen = a2i_ipadd(ipout, ipasc); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1053 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1054 | if (!iplen) |
| 1055 | return NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1056 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1057 | ret = ASN1_OCTET_STRING_new(); |
| 1058 | if (!ret) |
| 1059 | return NULL; |
| 1060 | if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) { |
| 1061 | ASN1_OCTET_STRING_free(ret); |
| 1062 | return NULL; |
| 1063 | } |
| 1064 | return ret; |
| 1065 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1066 | |
| 1067 | ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1068 | { |
| 1069 | ASN1_OCTET_STRING *ret = NULL; |
| 1070 | unsigned char ipout[32]; |
| 1071 | char *iptmp = NULL, *p; |
| 1072 | int iplen1, iplen2; |
| 1073 | p = strchr(ipasc, '/'); |
| 1074 | if (!p) |
| 1075 | return NULL; |
| 1076 | iptmp = BUF_strdup(ipasc); |
| 1077 | if (!iptmp) |
| 1078 | return NULL; |
| 1079 | p = iptmp + (p - ipasc); |
| 1080 | *p++ = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1081 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1082 | iplen1 = a2i_ipadd(ipout, iptmp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1083 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1084 | if (!iplen1) |
| 1085 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1086 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1087 | iplen2 = a2i_ipadd(ipout + iplen1, p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1088 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1089 | OPENSSL_free(iptmp); |
| 1090 | iptmp = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1091 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1092 | if (!iplen2 || (iplen1 != iplen2)) |
| 1093 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1094 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1095 | ret = ASN1_OCTET_STRING_new(); |
| 1096 | if (!ret) |
| 1097 | goto err; |
| 1098 | if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2)) |
| 1099 | goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1100 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1101 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1102 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1103 | err: |
| 1104 | if (iptmp) |
| 1105 | OPENSSL_free(iptmp); |
| 1106 | if (ret) |
| 1107 | ASN1_OCTET_STRING_free(ret); |
| 1108 | return NULL; |
| 1109 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1110 | |
| 1111 | int a2i_ipadd(unsigned char *ipout, const char *ipasc) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1112 | { |
| 1113 | /* If string contains a ':' assume IPv6 */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1114 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1115 | if (strchr(ipasc, ':')) { |
| 1116 | if (!ipv6_from_asc(ipout, ipasc)) |
| 1117 | return 0; |
| 1118 | return 16; |
| 1119 | } else { |
| 1120 | if (!ipv4_from_asc(ipout, ipasc)) |
| 1121 | return 0; |
| 1122 | return 4; |
| 1123 | } |
| 1124 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1125 | |
| 1126 | static int ipv4_from_asc(unsigned char *v4, const char *in) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1127 | { |
| 1128 | int a0, a1, a2, a3; |
| 1129 | if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4) |
| 1130 | return 0; |
| 1131 | if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255) |
| 1132 | || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255)) |
| 1133 | return 0; |
| 1134 | v4[0] = a0; |
| 1135 | v4[1] = a1; |
| 1136 | v4[2] = a2; |
| 1137 | v4[3] = a3; |
| 1138 | return 1; |
| 1139 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1140 | |
| 1141 | typedef struct { |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1142 | /* Temporary store for IPV6 output */ |
| 1143 | unsigned char tmp[16]; |
| 1144 | /* Total number of bytes in tmp */ |
| 1145 | int total; |
| 1146 | /* The position of a zero (corresponding to '::') */ |
| 1147 | int zero_pos; |
| 1148 | /* Number of zeroes */ |
| 1149 | int zero_cnt; |
| 1150 | } IPV6_STAT; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1151 | |
| 1152 | static int ipv6_from_asc(unsigned char *v6, const char *in) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1153 | { |
| 1154 | IPV6_STAT v6stat; |
| 1155 | v6stat.total = 0; |
| 1156 | v6stat.zero_pos = -1; |
| 1157 | v6stat.zero_cnt = 0; |
| 1158 | /* |
| 1159 | * Treat the IPv6 representation as a list of values separated by ':'. |
| 1160 | * The presence of a '::' will parse as one, two or three zero length |
| 1161 | * elements. |
| 1162 | */ |
| 1163 | if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat)) |
| 1164 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1165 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1166 | /* Now for some sanity checks */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1167 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1168 | if (v6stat.zero_pos == -1) { |
| 1169 | /* If no '::' must have exactly 16 bytes */ |
| 1170 | if (v6stat.total != 16) |
| 1171 | return 0; |
| 1172 | } else { |
| 1173 | /* If '::' must have less than 16 bytes */ |
| 1174 | if (v6stat.total == 16) |
| 1175 | return 0; |
| 1176 | /* More than three zeroes is an error */ |
| 1177 | if (v6stat.zero_cnt > 3) |
| 1178 | return 0; |
| 1179 | /* Can only have three zeroes if nothing else present */ |
| 1180 | else if (v6stat.zero_cnt == 3) { |
| 1181 | if (v6stat.total > 0) |
| 1182 | return 0; |
| 1183 | } |
| 1184 | /* Can only have two zeroes if at start or end */ |
| 1185 | else if (v6stat.zero_cnt == 2) { |
| 1186 | if ((v6stat.zero_pos != 0) |
| 1187 | && (v6stat.zero_pos != v6stat.total)) |
| 1188 | return 0; |
| 1189 | } else |
| 1190 | /* Can only have one zero if *not* start or end */ |
| 1191 | { |
| 1192 | if ((v6stat.zero_pos == 0) |
| 1193 | || (v6stat.zero_pos == v6stat.total)) |
| 1194 | return 0; |
| 1195 | } |
| 1196 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1197 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1198 | /* Format result */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1199 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1200 | if (v6stat.zero_pos >= 0) { |
| 1201 | /* Copy initial part */ |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1202 | OPENSSL_memcpy(v6, v6stat.tmp, v6stat.zero_pos); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1203 | /* Zero middle */ |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1204 | OPENSSL_memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1205 | /* Copy final part */ |
| 1206 | if (v6stat.total != v6stat.zero_pos) |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1207 | OPENSSL_memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total, |
| 1208 | v6stat.tmp + v6stat.zero_pos, |
| 1209 | v6stat.total - v6stat.zero_pos); |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1210 | } else |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1211 | OPENSSL_memcpy(v6, v6stat.tmp, 16); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1212 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1213 | return 1; |
| 1214 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1215 | |
| 1216 | static int ipv6_cb(const char *elem, int len, void *usr) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1217 | { |
| 1218 | IPV6_STAT *s = usr; |
| 1219 | /* Error if 16 bytes written */ |
| 1220 | if (s->total == 16) |
| 1221 | return 0; |
| 1222 | if (len == 0) { |
| 1223 | /* Zero length element, corresponds to '::' */ |
| 1224 | if (s->zero_pos == -1) |
| 1225 | s->zero_pos = s->total; |
| 1226 | /* If we've already got a :: its an error */ |
| 1227 | else if (s->zero_pos != s->total) |
| 1228 | return 0; |
| 1229 | s->zero_cnt++; |
| 1230 | } else { |
| 1231 | /* If more than 4 characters could be final a.b.c.d form */ |
| 1232 | if (len > 4) { |
| 1233 | /* Need at least 4 bytes left */ |
| 1234 | if (s->total > 12) |
| 1235 | return 0; |
| 1236 | /* Must be end of string */ |
| 1237 | if (elem[len]) |
| 1238 | return 0; |
| 1239 | if (!ipv4_from_asc(s->tmp + s->total, elem)) |
| 1240 | return 0; |
| 1241 | s->total += 4; |
| 1242 | } else { |
| 1243 | if (!ipv6_hex(s->tmp + s->total, elem, len)) |
| 1244 | return 0; |
| 1245 | s->total += 2; |
| 1246 | } |
| 1247 | } |
| 1248 | return 1; |
| 1249 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1250 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1251 | /* |
| 1252 | * Convert a string of up to 4 hex digits into the corresponding IPv6 form. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1253 | */ |
| 1254 | |
| 1255 | static int ipv6_hex(unsigned char *out, const char *in, int inlen) |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1256 | { |
| 1257 | unsigned char c; |
| 1258 | unsigned int num = 0; |
| 1259 | if (inlen > 4) |
| 1260 | return 0; |
| 1261 | while (inlen--) { |
| 1262 | c = *in++; |
| 1263 | num <<= 4; |
| 1264 | if ((c >= '0') && (c <= '9')) |
| 1265 | num |= c - '0'; |
| 1266 | else if ((c >= 'A') && (c <= 'F')) |
| 1267 | num |= c - 'A' + 10; |
| 1268 | else if ((c >= 'a') && (c <= 'f')) |
| 1269 | num |= c - 'a' + 10; |
| 1270 | else |
| 1271 | return 0; |
| 1272 | } |
| 1273 | out[0] = num >> 8; |
| 1274 | out[1] = num & 0xff; |
| 1275 | return 1; |
| 1276 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1277 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1278 | int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF (CONF_VALUE) * dn_sk, |
| 1279 | unsigned long chtype) |
| 1280 | { |
| 1281 | CONF_VALUE *v; |
| 1282 | int mval; |
| 1283 | size_t i; |
| 1284 | char *p, *type; |
| 1285 | if (!nm) |
| 1286 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1287 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1288 | for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) { |
| 1289 | v = sk_CONF_VALUE_value(dn_sk, i); |
| 1290 | type = v->name; |
| 1291 | /* |
| 1292 | * Skip past any leading X. X: X, etc to allow for multiple instances |
| 1293 | */ |
| 1294 | for (p = type; *p; p++) |
| 1295 | if ((*p == ':') || (*p == ',') || (*p == '.')) { |
| 1296 | p++; |
| 1297 | if (*p) |
| 1298 | type = p; |
| 1299 | break; |
| 1300 | } |
| 1301 | if (*type == '+') { |
| 1302 | mval = -1; |
| 1303 | type++; |
| 1304 | } else |
| 1305 | mval = 0; |
| 1306 | if (!X509_NAME_add_entry_by_txt(nm, type, chtype, |
| 1307 | (unsigned char *)v->value, -1, -1, |
| 1308 | mval)) |
| 1309 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1310 | |
Adam Langley | 57707c7 | 2016-01-14 11:25:12 -0800 | [diff] [blame] | 1311 | } |
| 1312 | return 1; |
| 1313 | } |