blob: 86c49403b86e7e12acc9a8d71652f892f4a27b06 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* v3_utl.c */
David Benjamin4969cc92016-04-22 15:02:23 -04002/*
3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
Adam Langleyd9e397b2015-01-22 14:27:53 -08004 * 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
David Benjamin4969cc92016-04-22 15:02:23 -040014 * notice, this list of conditions and the following disclaimer.
Adam Langleyd9e397b2015-01-22 14:27:53 -080015 *
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 Langleyd9e397b2015-01-22 14:27:53 -080061#include <ctype.h>
62#include <stdio.h>
63#include <string.h>
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
Kenny Rootb8494592015-09-25 02:29:14 +000073#include "../conf/internal.h"
Robert Sloan69939df2017-01-09 10:53:07 -080074#include "../internal.h"
Robert Sloan6e8c9592018-12-03 11:20:49 -080075#include "internal.h"
Robert Sloan69939df2017-01-09 10:53:07 -080076
Kenny Rootb8494592015-09-25 02:29:14 +000077
Adam Langleyd9e397b2015-01-22 14:27:53 -080078static char *strip_spaces(char *name);
79static int sk_strcmp(const OPENSSL_STRING *a, const OPENSSL_STRING *b);
David Benjamin4969cc92016-04-22 15:02:23 -040080static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
81 GENERAL_NAMES *gens);
Adam Langleyd9e397b2015-01-22 14:27:53 -080082static void str_free(OPENSSL_STRING str);
83static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email);
84
85static int ipv4_from_asc(unsigned char *v4, const char *in);
86static int ipv6_from_asc(unsigned char *v6, const char *in);
87static int ipv6_cb(const char *elem, int len, void *usr);
88static int ipv6_hex(unsigned char *out, const char *in, int inlen);
89
90/* Add a CONF_VALUE name value pair to stack */
91
92int X509V3_add_value(const char *name, const char *value,
David Benjamin4969cc92016-04-22 15:02:23 -040093 STACK_OF(CONF_VALUE) **extlist)
Adam Langleyd9e397b2015-01-22 14:27:53 -080094{
David Benjamin4969cc92016-04-22 15:02:23 -040095 CONF_VALUE *vtmp = NULL;
96 char *tname = NULL, *tvalue = NULL;
97 if (name && !(tname = BUF_strdup(name)))
98 goto err;
99 if (value && !(tvalue = BUF_strdup(value)))
100 goto err;
101 if (!(vtmp = CONF_VALUE_new()))
102 goto err;
103 if (!*extlist && !(*extlist = sk_CONF_VALUE_new_null()))
104 goto err;
105 vtmp->section = NULL;
106 vtmp->name = tname;
107 vtmp->value = tvalue;
108 if (!sk_CONF_VALUE_push(*extlist, vtmp))
109 goto err;
110 return 1;
111 err:
112 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
113 if (vtmp)
114 OPENSSL_free(vtmp);
115 if (tname)
116 OPENSSL_free(tname);
117 if (tvalue)
118 OPENSSL_free(tvalue);
119 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800120}
121
122int X509V3_add_value_uchar(const char *name, const unsigned char *value,
David Benjamin4969cc92016-04-22 15:02:23 -0400123 STACK_OF(CONF_VALUE) **extlist)
124{
125 return X509V3_add_value(name, (const char *)value, extlist);
126}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800127
128/* Free function for STACK_OF(CONF_VALUE) */
129
130void X509V3_conf_free(CONF_VALUE *conf)
131{
David Benjamin4969cc92016-04-22 15:02:23 -0400132 if (!conf)
133 return;
134 if (conf->name)
135 OPENSSL_free(conf->name);
136 if (conf->value)
137 OPENSSL_free(conf->value);
138 if (conf->section)
139 OPENSSL_free(conf->section);
140 OPENSSL_free(conf);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800141}
142
143int X509V3_add_value_bool(const char *name, int asn1_bool,
David Benjamin4969cc92016-04-22 15:02:23 -0400144 STACK_OF(CONF_VALUE) **extlist)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800145{
David Benjamin4969cc92016-04-22 15:02:23 -0400146 if (asn1_bool)
147 return X509V3_add_value(name, "TRUE", extlist);
148 return X509V3_add_value(name, "FALSE", extlist);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800149}
150
151int X509V3_add_value_bool_nf(char *name, int asn1_bool,
David Benjamin4969cc92016-04-22 15:02:23 -0400152 STACK_OF(CONF_VALUE) **extlist)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800153{
David Benjamin4969cc92016-04-22 15:02:23 -0400154 if (asn1_bool)
155 return X509V3_add_value(name, "TRUE", extlist);
156 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800157}
158
Robert Sloana815d5a2017-12-04 11:49:16 -0800159static char *bignum_to_string(const BIGNUM *bn)
160{
161 char *tmp, *ret;
162 size_t len;
163
164 /*
165 * Display large numbers in hex and small numbers in decimal. Converting to
166 * decimal takes quadratic time and is no more useful than hex for large
167 * numbers.
168 */
169 if (BN_num_bits(bn) < 32) {
170 return BN_bn2dec(bn);
171 }
172
173 tmp = BN_bn2hex(bn);
174 if (tmp == NULL) {
175 return NULL;
176 }
177
178 len = strlen(tmp) + 3;
179 ret = OPENSSL_malloc(len);
180 if (ret == NULL) {
181 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
182 OPENSSL_free(tmp);
183 return NULL;
184 }
185
186 /* Prepend "0x", but place it after the "-" if negative. */
187 if (tmp[0] == '-') {
188 BUF_strlcpy(ret, "-0x", len);
189 BUF_strlcat(ret, tmp + 1, len);
190 } else {
191 BUF_strlcpy(ret, "0x", len);
192 BUF_strlcat(ret, tmp, len);
193 }
194 OPENSSL_free(tmp);
195 return ret;
196}
197
Adam Langleyd9e397b2015-01-22 14:27:53 -0800198char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a)
199{
David Benjamin4969cc92016-04-22 15:02:23 -0400200 BIGNUM *bntmp = NULL;
201 char *strtmp = NULL;
202 if (!a)
203 return NULL;
204 if (!(bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) ||
Robert Sloana815d5a2017-12-04 11:49:16 -0800205 !(strtmp = bignum_to_string(bntmp)))
David Benjamin4969cc92016-04-22 15:02:23 -0400206 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
207 BN_free(bntmp);
208 return strtmp;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800209}
210
211char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a)
212{
David Benjamin4969cc92016-04-22 15:02:23 -0400213 BIGNUM *bntmp = NULL;
214 char *strtmp = NULL;
215 if (!a)
216 return NULL;
217 if (!(bntmp = ASN1_INTEGER_to_BN(a, NULL)) ||
Robert Sloana815d5a2017-12-04 11:49:16 -0800218 !(strtmp = bignum_to_string(bntmp)))
David Benjamin4969cc92016-04-22 15:02:23 -0400219 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
220 BN_free(bntmp);
221 return strtmp;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800222}
223
224ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)
225{
David Benjamin4969cc92016-04-22 15:02:23 -0400226 BIGNUM *bn = NULL;
227 ASN1_INTEGER *aint;
228 int isneg, ishex;
229 int ret;
230 if (!value) {
231 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE);
232 return 0;
233 }
234 bn = BN_new();
235 if (value[0] == '-') {
236 value++;
237 isneg = 1;
238 } else
239 isneg = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800240
David Benjamin4969cc92016-04-22 15:02:23 -0400241 if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
242 value += 2;
243 ishex = 1;
244 } else
245 ishex = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800246
David Benjamin4969cc92016-04-22 15:02:23 -0400247 if (ishex)
248 ret = BN_hex2bn(&bn, value);
249 else
250 ret = BN_dec2bn(&bn, value);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800251
David Benjamin4969cc92016-04-22 15:02:23 -0400252 if (!ret || value[ret]) {
253 BN_free(bn);
254 OPENSSL_PUT_ERROR(X509V3, X509V3_R_BN_DEC2BN_ERROR);
255 return 0;
256 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800257
David Benjamin4969cc92016-04-22 15:02:23 -0400258 if (isneg && BN_is_zero(bn))
259 isneg = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800260
David Benjamin4969cc92016-04-22 15:02:23 -0400261 aint = BN_to_ASN1_INTEGER(bn, NULL);
262 BN_free(bn);
263 if (!aint) {
264 OPENSSL_PUT_ERROR(X509V3, X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
265 return 0;
266 }
267 if (isneg)
268 aint->type |= V_ASN1_NEG;
269 return aint;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800270}
271
272int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint,
David Benjamin4969cc92016-04-22 15:02:23 -0400273 STACK_OF(CONF_VALUE) **extlist)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800274{
David Benjamin4969cc92016-04-22 15:02:23 -0400275 char *strtmp;
276 int ret;
277 if (!aint)
278 return 1;
279 if (!(strtmp = i2s_ASN1_INTEGER(NULL, aint)))
280 return 0;
281 ret = X509V3_add_value(name, strtmp, extlist);
282 OPENSSL_free(strtmp);
283 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800284}
285
286int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool)
287{
David Benjamin4969cc92016-04-22 15:02:23 -0400288 char *btmp;
289 if (!(btmp = value->value))
290 goto err;
291 if (!strcmp(btmp, "TRUE") || !strcmp(btmp, "true")
292 || !strcmp(btmp, "Y") || !strcmp(btmp, "y")
293 || !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) {
294 *asn1_bool = 0xff;
295 return 1;
296 } else if (!strcmp(btmp, "FALSE") || !strcmp(btmp, "false")
297 || !strcmp(btmp, "N") || !strcmp(btmp, "n")
298 || !strcmp(btmp, "NO") || !strcmp(btmp, "no")) {
299 *asn1_bool = 0;
300 return 1;
301 }
302 err:
303 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_BOOLEAN_STRING);
304 X509V3_conf_err(value);
305 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800306}
307
308int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint)
309{
David Benjamin4969cc92016-04-22 15:02:23 -0400310 ASN1_INTEGER *itmp;
311 if (!(itmp = s2i_ASN1_INTEGER(NULL, value->value))) {
312 X509V3_conf_err(value);
313 return 0;
314 }
315 *aint = itmp;
316 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800317}
318
David Benjamin4969cc92016-04-22 15:02:23 -0400319#define HDR_NAME 1
320#define HDR_VALUE 2
Adam Langleyd9e397b2015-01-22 14:27:53 -0800321
David Benjamin4969cc92016-04-22 15:02:23 -0400322/*
323 * #define DEBUG
324 */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800325
326STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
327{
David Benjamin4969cc92016-04-22 15:02:23 -0400328 char *p, *q, c;
329 char *ntmp, *vtmp;
330 STACK_OF(CONF_VALUE) *values = NULL;
331 char *linebuf;
332 int state;
333 /* We are going to modify the line so copy it first */
334 linebuf = BUF_strdup(line);
335 if (linebuf == NULL) {
336 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
337 goto err;
338 }
339 state = HDR_NAME;
340 ntmp = NULL;
341 /* Go through all characters */
342 for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');
343 p++) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800344
David Benjamin4969cc92016-04-22 15:02:23 -0400345 switch (state) {
346 case HDR_NAME:
347 if (c == ':') {
348 state = HDR_VALUE;
349 *p = 0;
350 ntmp = strip_spaces(q);
351 if (!ntmp) {
352 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME);
353 goto err;
354 }
355 q = p + 1;
356 } else if (c == ',') {
357 *p = 0;
358 ntmp = strip_spaces(q);
359 q = p + 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800360#if 0
David Benjamin4969cc92016-04-22 15:02:23 -0400361 printf("%s\n", ntmp);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800362#endif
David Benjamin4969cc92016-04-22 15:02:23 -0400363 if (!ntmp) {
364 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME);
365 goto err;
366 }
367 X509V3_add_value(ntmp, NULL, &values);
368 }
369 break;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800370
David Benjamin4969cc92016-04-22 15:02:23 -0400371 case HDR_VALUE:
372 if (c == ',') {
373 state = HDR_NAME;
374 *p = 0;
375 vtmp = strip_spaces(q);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800376#if 0
David Benjamin4969cc92016-04-22 15:02:23 -0400377 printf("%s\n", ntmp);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800378#endif
David Benjamin4969cc92016-04-22 15:02:23 -0400379 if (!vtmp) {
380 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE);
381 goto err;
382 }
383 X509V3_add_value(ntmp, vtmp, &values);
384 ntmp = NULL;
385 q = p + 1;
386 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800387
David Benjamin4969cc92016-04-22 15:02:23 -0400388 }
389 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800390
David Benjamin4969cc92016-04-22 15:02:23 -0400391 if (state == HDR_VALUE) {
392 vtmp = strip_spaces(q);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800393#if 0
David Benjamin4969cc92016-04-22 15:02:23 -0400394 printf("%s=%s\n", ntmp, vtmp);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800395#endif
David Benjamin4969cc92016-04-22 15:02:23 -0400396 if (!vtmp) {
397 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_VALUE);
398 goto err;
399 }
400 X509V3_add_value(ntmp, vtmp, &values);
401 } else {
402 ntmp = strip_spaces(q);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800403#if 0
David Benjamin4969cc92016-04-22 15:02:23 -0400404 printf("%s\n", ntmp);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800405#endif
David Benjamin4969cc92016-04-22 15:02:23 -0400406 if (!ntmp) {
407 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_NAME);
408 goto err;
409 }
410 X509V3_add_value(ntmp, NULL, &values);
411 }
412 OPENSSL_free(linebuf);
413 return values;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800414
David Benjamin4969cc92016-04-22 15:02:23 -0400415 err:
416 OPENSSL_free(linebuf);
417 sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
418 return NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800419
420}
421
422/* Delete leading and trailing spaces from a string */
423static char *strip_spaces(char *name)
424{
David Benjamin4969cc92016-04-22 15:02:23 -0400425 char *p, *q;
426 /* Skip over leading spaces */
427 p = name;
428 while (*p && isspace((unsigned char)*p))
429 p++;
430 if (!*p)
431 return NULL;
432 q = p + strlen(p) - 1;
433 while ((q != p) && isspace((unsigned char)*q))
434 q--;
435 if (p != q)
436 q[1] = 0;
437 if (!*p)
438 return NULL;
439 return p;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800440}
441
442/* hex string utilities */
443
David Benjamin4969cc92016-04-22 15:02:23 -0400444/*
445 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
446 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
447 * on EBCDIC machines)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800448 */
449
Robert Sloan6e8c9592018-12-03 11:20:49 -0800450char *x509v3_bytes_to_hex(const unsigned char *buffer, long len)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800451{
David Benjamin4969cc92016-04-22 15:02:23 -0400452 char *tmp, *q;
453 const unsigned char *p;
454 int i;
455 static const char hexdig[] = "0123456789ABCDEF";
456 if (!buffer || !len)
457 return NULL;
458 if (!(tmp = OPENSSL_malloc(len * 3 + 1))) {
459 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
460 return NULL;
461 }
462 q = tmp;
463 for (i = 0, p = buffer; i < len; i++, p++) {
464 *q++ = hexdig[(*p >> 4) & 0xf];
465 *q++ = hexdig[*p & 0xf];
466 *q++ = ':';
467 }
468 q[-1] = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800469
David Benjamin4969cc92016-04-22 15:02:23 -0400470 return tmp;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800471}
472
Robert Sloan6e8c9592018-12-03 11:20:49 -0800473unsigned char *x509v3_hex_to_bytes(const char *str, long *len)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800474{
David Benjamin4969cc92016-04-22 15:02:23 -0400475 unsigned char *hexbuf, *q;
476 unsigned char ch, cl, *p;
477 if (!str) {
478 OPENSSL_PUT_ERROR(X509V3, X509V3_R_INVALID_NULL_ARGUMENT);
479 return NULL;
480 }
481 if (!(hexbuf = OPENSSL_malloc(strlen(str) >> 1)))
482 goto err;
483 for (p = (unsigned char *)str, q = hexbuf; *p;) {
484 ch = *p++;
485 if (ch == ':')
486 continue;
487 cl = *p++;
488 if (!cl) {
489 OPENSSL_PUT_ERROR(X509V3, X509V3_R_ODD_NUMBER_OF_DIGITS);
490 OPENSSL_free(hexbuf);
491 return NULL;
492 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800493
David Benjamin4969cc92016-04-22 15:02:23 -0400494 if ((ch >= '0') && (ch <= '9'))
495 ch -= '0';
496 else if ((ch >= 'a') && (ch <= 'f'))
497 ch -= 'a' - 10;
Robert Sloanfe7cd212017-08-07 09:03:39 -0700498 else if ((ch >= 'A') && (ch <= 'F'))
499 ch -= 'A' - 10;
David Benjamin4969cc92016-04-22 15:02:23 -0400500 else
501 goto badhex;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800502
David Benjamin4969cc92016-04-22 15:02:23 -0400503 if ((cl >= '0') && (cl <= '9'))
504 cl -= '0';
505 else if ((cl >= 'a') && (cl <= 'f'))
506 cl -= 'a' - 10;
Robert Sloanfe7cd212017-08-07 09:03:39 -0700507 else if ((cl >= 'A') && (cl <= 'F'))
508 cl -= 'A' - 10;
David Benjamin4969cc92016-04-22 15:02:23 -0400509 else
510 goto badhex;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800511
David Benjamin4969cc92016-04-22 15:02:23 -0400512 *q++ = (ch << 4) | cl;
513 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800514
David Benjamin4969cc92016-04-22 15:02:23 -0400515 if (len)
516 *len = q - hexbuf;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800517
David Benjamin4969cc92016-04-22 15:02:23 -0400518 return hexbuf;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800519
David Benjamin4969cc92016-04-22 15:02:23 -0400520 err:
521 if (hexbuf)
522 OPENSSL_free(hexbuf);
523 OPENSSL_PUT_ERROR(X509V3, ERR_R_MALLOC_FAILURE);
524 return NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800525
David Benjamin4969cc92016-04-22 15:02:23 -0400526 badhex:
527 OPENSSL_free(hexbuf);
528 OPENSSL_PUT_ERROR(X509V3, X509V3_R_ILLEGAL_HEX_DIGIT);
529 return NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800530
531}
532
Robert Sloan6e8c9592018-12-03 11:20:49 -0800533int x509v3_name_cmp(const char *name, const char *cmp)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800534{
David Benjamin4969cc92016-04-22 15:02:23 -0400535 int len, ret;
536 char c;
537 len = strlen(cmp);
538 if ((ret = strncmp(name, cmp, len)))
539 return ret;
540 c = name[len];
541 if (!c || (c == '.'))
542 return 0;
543 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800544}
545
546static int sk_strcmp(const OPENSSL_STRING *a, const OPENSSL_STRING *b)
547{
David Benjamin4969cc92016-04-22 15:02:23 -0400548 return strcmp(*a, *b);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800549}
550
551STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x)
552{
David Benjamin4969cc92016-04-22 15:02:23 -0400553 GENERAL_NAMES *gens;
554 STACK_OF(OPENSSL_STRING) *ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800555
David Benjamin4969cc92016-04-22 15:02:23 -0400556 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
557 ret = get_email(X509_get_subject_name(x), gens);
558 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
559 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800560}
561
562STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x)
563{
David Benjamin4969cc92016-04-22 15:02:23 -0400564 AUTHORITY_INFO_ACCESS *info;
565 STACK_OF(OPENSSL_STRING) *ret = NULL;
566 size_t i;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800567
David Benjamin4969cc92016-04-22 15:02:23 -0400568 info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL);
569 if (!info)
570 return NULL;
571 for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) {
572 ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i);
573 if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) {
574 if (ad->location->type == GEN_URI) {
575 if (!append_ia5
576 (&ret, ad->location->d.uniformResourceIdentifier))
577 break;
578 }
579 }
580 }
581 AUTHORITY_INFO_ACCESS_free(info);
582 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800583}
584
585STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x)
586{
David Benjamin4969cc92016-04-22 15:02:23 -0400587 GENERAL_NAMES *gens;
588 STACK_OF(X509_EXTENSION) *exts;
589 STACK_OF(OPENSSL_STRING) *ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800590
David Benjamin4969cc92016-04-22 15:02:23 -0400591 exts = X509_REQ_get_extensions(x);
592 gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
593 ret = get_email(X509_REQ_get_subject_name(x), gens);
594 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
595 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
596 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800597}
598
David Benjamin4969cc92016-04-22 15:02:23 -0400599static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
600 GENERAL_NAMES *gens)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800601{
David Benjamin4969cc92016-04-22 15:02:23 -0400602 STACK_OF(OPENSSL_STRING) *ret = NULL;
603 X509_NAME_ENTRY *ne;
604 ASN1_IA5STRING *email;
605 GENERAL_NAME *gen;
606 int i;
607 size_t j;
608 /* Now add any email address(es) to STACK */
609 i = -1;
610 /* First supplied X509_NAME */
611 while ((i = X509_NAME_get_index_by_NID(name,
612 NID_pkcs9_emailAddress, i)) >= 0) {
613 ne = X509_NAME_get_entry(name, i);
614 email = X509_NAME_ENTRY_get_data(ne);
615 if (!append_ia5(&ret, email))
616 return NULL;
617 }
618 for (j = 0; j < sk_GENERAL_NAME_num(gens); j++) {
619 gen = sk_GENERAL_NAME_value(gens, j);
620 if (gen->type != GEN_EMAIL)
621 continue;
622 if (!append_ia5(&ret, gen->d.ia5))
623 return NULL;
624 }
625 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800626}
627
628static void str_free(OPENSSL_STRING str)
629{
David Benjamin4969cc92016-04-22 15:02:23 -0400630 OPENSSL_free(str);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800631}
632
633static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email)
634{
David Benjamin4969cc92016-04-22 15:02:23 -0400635 char *emtmp;
636 /* First some sanity checks */
637 if (email->type != V_ASN1_IA5STRING)
638 return 1;
639 if (!email->data || !email->length)
640 return 1;
641 if (!*sk)
642 *sk = sk_OPENSSL_STRING_new(sk_strcmp);
643 if (!*sk)
644 return 0;
645 /* Don't add duplicates */
Robert Sloan15c0b352018-04-16 08:36:46 -0700646 sk_OPENSSL_STRING_sort(*sk);
David Benjamin4969cc92016-04-22 15:02:23 -0400647 if (sk_OPENSSL_STRING_find(*sk, NULL, (char *)email->data))
648 return 1;
649 emtmp = BUF_strdup((char *)email->data);
650 if (!emtmp || !sk_OPENSSL_STRING_push(*sk, emtmp)) {
651 X509_email_free(*sk);
652 *sk = NULL;
653 return 0;
654 }
655 return 1;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800656}
657
658void X509_email_free(STACK_OF(OPENSSL_STRING) *sk)
659{
David Benjamin4969cc92016-04-22 15:02:23 -0400660 sk_OPENSSL_STRING_pop_free(sk, str_free);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800661}
662
David Benjamin4969cc92016-04-22 15:02:23 -0400663typedef int (*equal_fn) (const unsigned char *pattern, size_t pattern_len,
664 const unsigned char *subject, size_t subject_len,
665 unsigned int flags);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800666
667/* Skip pattern prefix to match "wildcard" subject */
668static void skip_prefix(const unsigned char **p, size_t *plen,
David Benjamin4969cc92016-04-22 15:02:23 -0400669 const unsigned char *subject, size_t subject_len,
670 unsigned int flags)
671{
672 const unsigned char *pattern = *p;
673 size_t pattern_len = *plen;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800674
David Benjamin4969cc92016-04-22 15:02:23 -0400675 /*
676 * If subject starts with a leading '.' followed by more octets, and
677 * pattern is longer, compare just an equal-length suffix with the
678 * full subject (starting at the '.'), provided the prefix contains
679 * no NULs.
680 */
681 if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0)
682 return;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800683
David Benjamin4969cc92016-04-22 15:02:23 -0400684 while (pattern_len > subject_len && *pattern) {
685 if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) &&
686 *pattern == '.')
687 break;
688 ++pattern;
689 --pattern_len;
690 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800691
David Benjamin4969cc92016-04-22 15:02:23 -0400692 /* Skip if entire prefix acceptable */
693 if (pattern_len == subject_len) {
694 *p = pattern;
695 *plen = pattern_len;
696 }
697}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800698
699/* Compare while ASCII ignoring case. */
700static int equal_nocase(const unsigned char *pattern, size_t pattern_len,
David Benjamin4969cc92016-04-22 15:02:23 -0400701 const unsigned char *subject, size_t subject_len,
702 unsigned int flags)
703{
704 skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
705 if (pattern_len != subject_len)
706 return 0;
707 while (pattern_len) {
708 unsigned char l = *pattern;
709 unsigned char r = *subject;
710 /* The pattern must not contain NUL characters. */
711 if (l == 0)
712 return 0;
713 if (l != r) {
714 if ('A' <= l && l <= 'Z')
715 l = (l - 'A') + 'a';
716 if ('A' <= r && r <= 'Z')
717 r = (r - 'A') + 'a';
718 if (l != r)
719 return 0;
720 }
721 ++pattern;
722 ++subject;
723 --pattern_len;
724 }
725 return 1;
726}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800727
Robert Sloan69939df2017-01-09 10:53:07 -0800728/* Compare using OPENSSL_memcmp. */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800729static int equal_case(const unsigned char *pattern, size_t pattern_len,
David Benjamin4969cc92016-04-22 15:02:23 -0400730 const unsigned char *subject, size_t subject_len,
731 unsigned int flags)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800732{
David Benjamin4969cc92016-04-22 15:02:23 -0400733 skip_prefix(&pattern, &pattern_len, subject, subject_len, flags);
734 if (pattern_len != subject_len)
735 return 0;
Robert Sloan69939df2017-01-09 10:53:07 -0800736 return !OPENSSL_memcmp(pattern, subject, pattern_len);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800737}
738
David Benjamin4969cc92016-04-22 15:02:23 -0400739/*
740 * RFC 5280, section 7.5, requires that only the domain is compared in a
741 * case-insensitive manner.
742 */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800743static int equal_email(const unsigned char *a, size_t a_len,
David Benjamin4969cc92016-04-22 15:02:23 -0400744 const unsigned char *b, size_t b_len,
745 unsigned int unused_flags)
746{
747 size_t i = a_len;
748 if (a_len != b_len)
749 return 0;
750 /*
751 * We search backwards for the '@' character, so that we do not have to
752 * deal with quoted local-parts. The domain part is compared in a
753 * case-insensitive manner.
754 */
755 while (i > 0) {
756 --i;
757 if (a[i] == '@' || b[i] == '@') {
758 if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0))
759 return 0;
760 break;
761 }
762 }
763 if (i == 0)
764 i = a_len;
765 return equal_case(a, i, b, i, 0);
766}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800767
David Benjamin4969cc92016-04-22 15:02:23 -0400768/*
769 * Compare the prefix and suffix with the subject, and check that the
770 * characters in-between are valid.
771 */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800772static int wildcard_match(const unsigned char *prefix, size_t prefix_len,
David Benjamin4969cc92016-04-22 15:02:23 -0400773 const unsigned char *suffix, size_t suffix_len,
774 const unsigned char *subject, size_t subject_len,
775 unsigned int flags)
776{
777 const unsigned char *wildcard_start;
778 const unsigned char *wildcard_end;
779 const unsigned char *p;
780 int allow_multi = 0;
781 int allow_idna = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800782
David Benjamin4969cc92016-04-22 15:02:23 -0400783 if (subject_len < prefix_len + suffix_len)
784 return 0;
785 if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags))
786 return 0;
787 wildcard_start = subject + prefix_len;
788 wildcard_end = subject + (subject_len - suffix_len);
789 if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags))
790 return 0;
791 /*
792 * If the wildcard makes up the entire first label, it must match at
793 * least one character.
794 */
795 if (prefix_len == 0 && *suffix == '.') {
796 if (wildcard_start == wildcard_end)
797 return 0;
798 allow_idna = 1;
799 if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS)
800 allow_multi = 1;
801 }
802 /* IDNA labels cannot match partial wildcards */
803 if (!allow_idna &&
804 subject_len >= 4
805 && OPENSSL_strncasecmp((char *)subject, "xn--", 4) == 0)
806 return 0;
807 /* The wildcard may match a literal '*' */
808 if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*')
809 return 1;
810 /*
811 * Check that the part matched by the wildcard contains only
812 * permitted characters and only matches a single label unless
813 * allow_multi is set.
814 */
815 for (p = wildcard_start; p != wildcard_end; ++p)
816 if (!(('0' <= *p && *p <= '9') ||
817 ('A' <= *p && *p <= 'Z') ||
818 ('a' <= *p && *p <= 'z') ||
819 *p == '-' || (allow_multi && *p == '.')))
820 return 0;
821 return 1;
822}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800823
David Benjamin4969cc92016-04-22 15:02:23 -0400824#define LABEL_START (1 << 0)
825#define LABEL_END (1 << 1)
826#define LABEL_HYPHEN (1 << 2)
827#define LABEL_IDNA (1 << 3)
Adam Langleyd9e397b2015-01-22 14:27:53 -0800828
829static const unsigned char *valid_star(const unsigned char *p, size_t len,
David Benjamin4969cc92016-04-22 15:02:23 -0400830 unsigned int flags)
831{
832 const unsigned char *star = 0;
833 size_t i;
834 int state = LABEL_START;
835 int dots = 0;
836 for (i = 0; i < len; ++i) {
837 /*
838 * Locate first and only legal wildcard, either at the start
839 * or end of a non-IDNA first and not final label.
840 */
841 if (p[i] == '*') {
842 int atstart = (state & LABEL_START);
843 int atend = (i == len - 1 || p[i + 1] == '.');
844 /*
845 * At most one wildcard per pattern.
846 * No wildcards in IDNA labels.
847 * No wildcards after the first label.
848 */
849 if (star != NULL || (state & LABEL_IDNA) != 0 || dots)
850 return NULL;
851 /* Only full-label '*.example.com' wildcards? */
852 if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS)
853 && (!atstart || !atend))
854 return NULL;
855 /* No 'foo*bar' wildcards */
856 if (!atstart && !atend)
857 return NULL;
858 star = &p[i];
859 state &= ~LABEL_START;
David Benjamin4969cc92016-04-22 15:02:23 -0400860 } else if (('a' <= p[i] && p[i] <= 'z')
861 || ('A' <= p[i] && p[i] <= 'Z')
862 || ('0' <= p[i] && p[i] <= '9')) {
Robert Sloan7c50ec52017-02-27 08:17:21 -0800863 if ((state & LABEL_START) != 0
864 && len - i >= 4
865 && OPENSSL_strncasecmp((char *)&p[i], "xn--", 4) == 0)
866 state |= LABEL_IDNA;
867 state &= ~(LABEL_HYPHEN | LABEL_START);
David Benjamin4969cc92016-04-22 15:02:23 -0400868 } else if (p[i] == '.') {
Robert Sloan7c50ec52017-02-27 08:17:21 -0800869 if ((state & (LABEL_HYPHEN | LABEL_START)) != 0)
David Benjamin4969cc92016-04-22 15:02:23 -0400870 return NULL;
871 state = LABEL_START;
872 ++dots;
873 } else if (p[i] == '-') {
874 /* no domain/subdomain starts with '-' */
875 if ((state & LABEL_START) != 0)
876 return NULL;
877 state |= LABEL_HYPHEN;
878 } else
879 return NULL;
880 }
Adam Langleyd9e397b2015-01-22 14:27:53 -0800881
David Benjamin4969cc92016-04-22 15:02:23 -0400882 /*
883 * The final label must not end in a hyphen or ".", and
884 * there must be at least two dots after the star.
885 */
886 if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2)
887 return NULL;
888 return star;
889}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800890
891/* Compare using wildcards. */
892static int equal_wildcard(const unsigned char *pattern, size_t pattern_len,
David Benjamin4969cc92016-04-22 15:02:23 -0400893 const unsigned char *subject, size_t subject_len,
894 unsigned int flags)
895{
896 const unsigned char *star = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800897
David Benjamin4969cc92016-04-22 15:02:23 -0400898 /*
899 * Subject names starting with '.' can only match a wildcard pattern
900 * via a subject sub-domain pattern suffix match.
901 */
902 if (!(subject_len > 1 && subject[0] == '.'))
903 star = valid_star(pattern, pattern_len, flags);
904 if (star == NULL)
905 return equal_nocase(pattern, pattern_len,
906 subject, subject_len, flags);
907 return wildcard_match(pattern, star - pattern,
908 star + 1, (pattern + pattern_len) - star - 1,
909 subject, subject_len, flags);
910}
Adam Langleyd9e397b2015-01-22 14:27:53 -0800911
Pete Bentley0c61efe2019-08-13 09:32:23 +0100912int x509v3_looks_like_dns_name(const unsigned char *in, size_t len) {
913 /* This function is used as a heuristic for whether a common name is a
914 * hostname to be matched, or merely a decorative name to describe the
915 * subject. This heuristic must be applied to both name constraints and the
916 * common name fallback, so it must be loose enough to accept hostname
917 * common names, and tight enough to reject decorative common names. */
918
919 if (len > 0 && in[len - 1] == '.') {
920 len--;
921 }
922
923 /* Wildcards are allowed in front. */
924 if (len >= 2 && in[0] == '*' && in[1] == '.') {
925 in += 2;
926 len -= 2;
927 }
928
929 if (len == 0) {
930 return 0;
931 }
932
933 size_t label_start = 0;
934 for (size_t i = 0; i < len; i++) {
935 unsigned char c = in[i];
936 if ((c >= 'a' && c <= 'z') ||
937 (c >= '0' && c <= '9') ||
938 (c >= 'A' && c <= 'Z') ||
939 (c == '-' && i > label_start) ||
940 /* These are not valid characters in hostnames, but commonly found
941 * in deployments outside the Web PKI. */
942 c == '_' ||
943 c == ':') {
944 continue;
945 }
946
947 /* Labels must not be empty. */
948 if (c == '.' && i > label_start && i < len - 1) {
949 label_start = i + 1;
950 continue;
951 }
952
953 return 0;
954 }
955
956 return 1;
957}
958
David Benjamin4969cc92016-04-22 15:02:23 -0400959/*
960 * Compare an ASN1_STRING to a supplied string. If they match return 1. If
961 * cmp_type > 0 only compare if string matches the type, otherwise convert it
962 * to UTF8.
Adam Langleyd9e397b2015-01-22 14:27:53 -0800963 */
964
965static int do_check_string(ASN1_STRING *a, int cmp_type, equal_fn equal,
Pete Bentley0c61efe2019-08-13 09:32:23 +0100966 unsigned int flags, int check_type, const char *b,
967 size_t blen, char **peername)
David Benjamin4969cc92016-04-22 15:02:23 -0400968{
969 int rv = 0;
Adam Langleye9ada862015-05-11 17:20:37 -0700970
David Benjamin4969cc92016-04-22 15:02:23 -0400971 if (!a->data || !a->length)
972 return 0;
973 if (cmp_type > 0) {
974 if (cmp_type != a->type)
975 return 0;
976 if (cmp_type == V_ASN1_IA5STRING)
977 rv = equal(a->data, a->length, (unsigned char *)b, blen, flags);
Robert Sloan69939df2017-01-09 10:53:07 -0800978 else if (a->length == (int)blen && !OPENSSL_memcmp(a->data, b, blen))
David Benjamin4969cc92016-04-22 15:02:23 -0400979 rv = 1;
980 if (rv > 0 && peername)
981 *peername = BUF_strndup((char *)a->data, a->length);
982 } else {
983 int astrlen;
984 unsigned char *astr;
985 astrlen = ASN1_STRING_to_UTF8(&astr, a);
986 if (astrlen < 0)
987 return -1;
Pete Bentley0c61efe2019-08-13 09:32:23 +0100988 /*
989 * We check the common name against DNS name constraints if it passes
990 * |x509v3_looks_like_dns_name|. Thus we must not consider common names
991 * for DNS fallbacks if they fail this check.
992 */
993 if (check_type == GEN_DNS &&
994 !x509v3_looks_like_dns_name(astr, astrlen)) {
995 rv = 0;
996 } else {
997 rv = equal(astr, astrlen, (unsigned char *)b, blen, flags);
998 }
David Benjamin4969cc92016-04-22 15:02:23 -0400999 if (rv > 0 && peername)
1000 *peername = BUF_strndup((char *)astr, astrlen);
1001 OPENSSL_free(astr);
1002 }
1003 return rv;
1004}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001005
Adam Langleye9ada862015-05-11 17:20:37 -07001006static int do_x509_check(X509 *x, const char *chk, size_t chklen,
David Benjamin4969cc92016-04-22 15:02:23 -04001007 unsigned int flags, int check_type, char **peername)
1008{
1009 GENERAL_NAMES *gens = NULL;
1010 X509_NAME *name = NULL;
1011 size_t i;
1012 int j;
1013 int cnid = NID_undef;
1014 int alt_type;
David Benjamin4969cc92016-04-22 15:02:23 -04001015 int rv = 0;
1016 equal_fn equal;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001017
David Benjamin4969cc92016-04-22 15:02:23 -04001018 /* See below, this flag is internal-only */
1019 flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS;
1020 if (check_type == GEN_EMAIL) {
1021 cnid = NID_pkcs9_emailAddress;
1022 alt_type = V_ASN1_IA5STRING;
1023 equal = equal_email;
1024 } else if (check_type == GEN_DNS) {
1025 cnid = NID_commonName;
1026 /* Implicit client-side DNS sub-domain pattern */
1027 if (chklen > 1 && chk[0] == '.')
1028 flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS;
1029 alt_type = V_ASN1_IA5STRING;
1030 if (flags & X509_CHECK_FLAG_NO_WILDCARDS)
1031 equal = equal_nocase;
1032 else
1033 equal = equal_wildcard;
1034 } else {
1035 alt_type = V_ASN1_OCTET_STRING;
1036 equal = equal_case;
1037 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001038
David Benjamin4969cc92016-04-22 15:02:23 -04001039 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
1040 if (gens) {
1041 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
1042 GENERAL_NAME *gen;
1043 ASN1_STRING *cstr;
1044 gen = sk_GENERAL_NAME_value(gens, i);
1045 if (gen->type != check_type)
1046 continue;
David Benjamin4969cc92016-04-22 15:02:23 -04001047 if (check_type == GEN_EMAIL)
1048 cstr = gen->d.rfc822Name;
1049 else if (check_type == GEN_DNS)
1050 cstr = gen->d.dNSName;
1051 else
1052 cstr = gen->d.iPAddress;
1053 /* Positive on success, negative on error! */
Pete Bentley0c61efe2019-08-13 09:32:23 +01001054 if ((rv = do_check_string(cstr, alt_type, equal, flags, check_type,
David Benjamin4969cc92016-04-22 15:02:23 -04001055 chk, chklen, peername)) != 0)
1056 break;
1057 }
1058 GENERAL_NAMES_free(gens);
Pete Bentley0c61efe2019-08-13 09:32:23 +01001059 return rv;
David Benjamin4969cc92016-04-22 15:02:23 -04001060 }
Kenny Roote99801b2015-11-06 15:31:15 -08001061
David Benjamin4969cc92016-04-22 15:02:23 -04001062 /* We're done if CN-ID is not pertinent */
Pete Bentley0c61efe2019-08-13 09:32:23 +01001063 if (cnid == NID_undef || (flags & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT))
David Benjamin4969cc92016-04-22 15:02:23 -04001064 return 0;
Kenny Roote99801b2015-11-06 15:31:15 -08001065
David Benjamin4969cc92016-04-22 15:02:23 -04001066 j = -1;
1067 name = X509_get_subject_name(x);
1068 while ((j = X509_NAME_get_index_by_NID(name, cnid, j)) >= 0) {
1069 X509_NAME_ENTRY *ne;
1070 ASN1_STRING *str;
1071 ne = X509_NAME_get_entry(name, j);
1072 str = X509_NAME_ENTRY_get_data(ne);
1073 /* Positive on success, negative on error! */
Pete Bentley0c61efe2019-08-13 09:32:23 +01001074 if ((rv = do_check_string(str, -1, equal, flags, check_type,
David Benjamin4969cc92016-04-22 15:02:23 -04001075 chk, chklen, peername)) != 0)
1076 return rv;
1077 }
1078 return 0;
1079}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001080
Adam Langleye9ada862015-05-11 17:20:37 -07001081int X509_check_host(X509 *x, const char *chk, size_t chklen,
David Benjamin4969cc92016-04-22 15:02:23 -04001082 unsigned int flags, char **peername)
1083{
1084 if (chk == NULL)
1085 return -2;
Robert Sloan69939df2017-01-09 10:53:07 -08001086 if (OPENSSL_memchr(chk, '\0', chklen))
David Benjamin4969cc92016-04-22 15:02:23 -04001087 return -2;
1088 return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername);
1089}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001090
Adam Langleye9ada862015-05-11 17:20:37 -07001091int X509_check_email(X509 *x, const char *chk, size_t chklen,
David Benjamin4969cc92016-04-22 15:02:23 -04001092 unsigned int flags)
1093{
1094 if (chk == NULL)
1095 return -2;
Robert Sloan69939df2017-01-09 10:53:07 -08001096 if (OPENSSL_memchr(chk, '\0', chklen))
David Benjamin4969cc92016-04-22 15:02:23 -04001097 return -2;
1098 return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL);
1099}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001100
1101int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,
David Benjamin4969cc92016-04-22 15:02:23 -04001102 unsigned int flags)
1103{
1104 if (chk == NULL)
1105 return -2;
1106 return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL);
1107}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001108
1109int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags)
David Benjamin4969cc92016-04-22 15:02:23 -04001110{
1111 unsigned char ipout[16];
1112 size_t iplen;
Adam Langleye9ada862015-05-11 17:20:37 -07001113
David Benjamin4969cc92016-04-22 15:02:23 -04001114 if (ipasc == NULL)
1115 return -2;
1116 iplen = (size_t)a2i_ipadd(ipout, ipasc);
1117 if (iplen == 0)
1118 return -2;
1119 return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL);
1120}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001121
David Benjamin4969cc92016-04-22 15:02:23 -04001122/*
1123 * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
1124 * with RFC3280.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001125 */
1126
1127ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
David Benjamin4969cc92016-04-22 15:02:23 -04001128{
1129 unsigned char ipout[16];
1130 ASN1_OCTET_STRING *ret;
1131 int iplen;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001132
David Benjamin4969cc92016-04-22 15:02:23 -04001133 /* If string contains a ':' assume IPv6 */
Adam Langleyd9e397b2015-01-22 14:27:53 -08001134
David Benjamin4969cc92016-04-22 15:02:23 -04001135 iplen = a2i_ipadd(ipout, ipasc);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001136
David Benjamin4969cc92016-04-22 15:02:23 -04001137 if (!iplen)
1138 return NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001139
David Benjamin4969cc92016-04-22 15:02:23 -04001140 ret = ASN1_OCTET_STRING_new();
1141 if (!ret)
1142 return NULL;
1143 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) {
1144 ASN1_OCTET_STRING_free(ret);
1145 return NULL;
1146 }
1147 return ret;
1148}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001149
1150ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
David Benjamin4969cc92016-04-22 15:02:23 -04001151{
1152 ASN1_OCTET_STRING *ret = NULL;
1153 unsigned char ipout[32];
1154 char *iptmp = NULL, *p;
1155 int iplen1, iplen2;
1156 p = strchr(ipasc, '/');
1157 if (!p)
1158 return NULL;
1159 iptmp = BUF_strdup(ipasc);
1160 if (!iptmp)
1161 return NULL;
1162 p = iptmp + (p - ipasc);
1163 *p++ = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001164
David Benjamin4969cc92016-04-22 15:02:23 -04001165 iplen1 = a2i_ipadd(ipout, iptmp);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001166
David Benjamin4969cc92016-04-22 15:02:23 -04001167 if (!iplen1)
1168 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001169
David Benjamin4969cc92016-04-22 15:02:23 -04001170 iplen2 = a2i_ipadd(ipout + iplen1, p);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001171
David Benjamin4969cc92016-04-22 15:02:23 -04001172 OPENSSL_free(iptmp);
1173 iptmp = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001174
David Benjamin4969cc92016-04-22 15:02:23 -04001175 if (!iplen2 || (iplen1 != iplen2))
1176 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001177
David Benjamin4969cc92016-04-22 15:02:23 -04001178 ret = ASN1_OCTET_STRING_new();
1179 if (!ret)
1180 goto err;
1181 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
1182 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001183
David Benjamin4969cc92016-04-22 15:02:23 -04001184 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001185
David Benjamin4969cc92016-04-22 15:02:23 -04001186 err:
1187 if (iptmp)
1188 OPENSSL_free(iptmp);
1189 if (ret)
1190 ASN1_OCTET_STRING_free(ret);
1191 return NULL;
1192}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001193
1194int a2i_ipadd(unsigned char *ipout, const char *ipasc)
David Benjamin4969cc92016-04-22 15:02:23 -04001195{
1196 /* If string contains a ':' assume IPv6 */
Adam Langleyd9e397b2015-01-22 14:27:53 -08001197
David Benjamin4969cc92016-04-22 15:02:23 -04001198 if (strchr(ipasc, ':')) {
1199 if (!ipv6_from_asc(ipout, ipasc))
1200 return 0;
1201 return 16;
1202 } else {
1203 if (!ipv4_from_asc(ipout, ipasc))
1204 return 0;
1205 return 4;
1206 }
1207}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001208
1209static int ipv4_from_asc(unsigned char *v4, const char *in)
David Benjamin4969cc92016-04-22 15:02:23 -04001210{
1211 int a0, a1, a2, a3;
1212 if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
1213 return 0;
1214 if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
1215 || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
1216 return 0;
1217 v4[0] = a0;
1218 v4[1] = a1;
1219 v4[2] = a2;
1220 v4[3] = a3;
1221 return 1;
1222}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001223
1224typedef struct {
David Benjamin4969cc92016-04-22 15:02:23 -04001225 /* Temporary store for IPV6 output */
1226 unsigned char tmp[16];
1227 /* Total number of bytes in tmp */
1228 int total;
1229 /* The position of a zero (corresponding to '::') */
1230 int zero_pos;
1231 /* Number of zeroes */
1232 int zero_cnt;
1233} IPV6_STAT;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001234
1235static int ipv6_from_asc(unsigned char *v6, const char *in)
David Benjamin4969cc92016-04-22 15:02:23 -04001236{
1237 IPV6_STAT v6stat;
1238 v6stat.total = 0;
1239 v6stat.zero_pos = -1;
1240 v6stat.zero_cnt = 0;
1241 /*
1242 * Treat the IPv6 representation as a list of values separated by ':'.
1243 * The presence of a '::' will parse as one, two or three zero length
1244 * elements.
1245 */
1246 if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
1247 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001248
David Benjamin4969cc92016-04-22 15:02:23 -04001249 /* Now for some sanity checks */
Adam Langleyd9e397b2015-01-22 14:27:53 -08001250
David Benjamin4969cc92016-04-22 15:02:23 -04001251 if (v6stat.zero_pos == -1) {
1252 /* If no '::' must have exactly 16 bytes */
1253 if (v6stat.total != 16)
1254 return 0;
1255 } else {
1256 /* If '::' must have less than 16 bytes */
1257 if (v6stat.total == 16)
1258 return 0;
1259 /* More than three zeroes is an error */
1260 if (v6stat.zero_cnt > 3)
1261 return 0;
1262 /* Can only have three zeroes if nothing else present */
1263 else if (v6stat.zero_cnt == 3) {
1264 if (v6stat.total > 0)
1265 return 0;
1266 }
1267 /* Can only have two zeroes if at start or end */
1268 else if (v6stat.zero_cnt == 2) {
1269 if ((v6stat.zero_pos != 0)
1270 && (v6stat.zero_pos != v6stat.total))
1271 return 0;
1272 } else
1273 /* Can only have one zero if *not* start or end */
1274 {
1275 if ((v6stat.zero_pos == 0)
1276 || (v6stat.zero_pos == v6stat.total))
1277 return 0;
1278 }
1279 }
Adam Langleyd9e397b2015-01-22 14:27:53 -08001280
David Benjamin4969cc92016-04-22 15:02:23 -04001281 /* Format result */
Adam Langleyd9e397b2015-01-22 14:27:53 -08001282
David Benjamin4969cc92016-04-22 15:02:23 -04001283 if (v6stat.zero_pos >= 0) {
1284 /* Copy initial part */
Robert Sloan69939df2017-01-09 10:53:07 -08001285 OPENSSL_memcpy(v6, v6stat.tmp, v6stat.zero_pos);
David Benjamin4969cc92016-04-22 15:02:23 -04001286 /* Zero middle */
Robert Sloan69939df2017-01-09 10:53:07 -08001287 OPENSSL_memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
David Benjamin4969cc92016-04-22 15:02:23 -04001288 /* Copy final part */
1289 if (v6stat.total != v6stat.zero_pos)
Robert Sloan69939df2017-01-09 10:53:07 -08001290 OPENSSL_memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
1291 v6stat.tmp + v6stat.zero_pos,
1292 v6stat.total - v6stat.zero_pos);
David Benjamin4969cc92016-04-22 15:02:23 -04001293 } else
Robert Sloan69939df2017-01-09 10:53:07 -08001294 OPENSSL_memcpy(v6, v6stat.tmp, 16);
Adam Langleyd9e397b2015-01-22 14:27:53 -08001295
David Benjamin4969cc92016-04-22 15:02:23 -04001296 return 1;
1297}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001298
1299static int ipv6_cb(const char *elem, int len, void *usr)
David Benjamin4969cc92016-04-22 15:02:23 -04001300{
1301 IPV6_STAT *s = usr;
1302 /* Error if 16 bytes written */
1303 if (s->total == 16)
1304 return 0;
1305 if (len == 0) {
1306 /* Zero length element, corresponds to '::' */
1307 if (s->zero_pos == -1)
1308 s->zero_pos = s->total;
1309 /* If we've already got a :: its an error */
1310 else if (s->zero_pos != s->total)
1311 return 0;
1312 s->zero_cnt++;
1313 } else {
1314 /* If more than 4 characters could be final a.b.c.d form */
1315 if (len > 4) {
1316 /* Need at least 4 bytes left */
1317 if (s->total > 12)
1318 return 0;
1319 /* Must be end of string */
1320 if (elem[len])
1321 return 0;
1322 if (!ipv4_from_asc(s->tmp + s->total, elem))
1323 return 0;
1324 s->total += 4;
1325 } else {
1326 if (!ipv6_hex(s->tmp + s->total, elem, len))
1327 return 0;
1328 s->total += 2;
1329 }
1330 }
1331 return 1;
1332}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001333
David Benjamin4969cc92016-04-22 15:02:23 -04001334/*
1335 * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
Adam Langleyd9e397b2015-01-22 14:27:53 -08001336 */
1337
1338static int ipv6_hex(unsigned char *out, const char *in, int inlen)
David Benjamin4969cc92016-04-22 15:02:23 -04001339{
1340 unsigned char c;
1341 unsigned int num = 0;
1342 if (inlen > 4)
1343 return 0;
1344 while (inlen--) {
1345 c = *in++;
1346 num <<= 4;
1347 if ((c >= '0') && (c <= '9'))
1348 num |= c - '0';
1349 else if ((c >= 'A') && (c <= 'F'))
1350 num |= c - 'A' + 10;
1351 else if ((c >= 'a') && (c <= 'f'))
1352 num |= c - 'a' + 10;
1353 else
1354 return 0;
1355 }
1356 out[0] = num >> 8;
1357 out[1] = num & 0xff;
1358 return 1;
1359}
Adam Langleyd9e397b2015-01-22 14:27:53 -08001360
David Benjamin4969cc92016-04-22 15:02:23 -04001361int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF (CONF_VALUE) * dn_sk,
1362 unsigned long chtype)
1363{
1364 CONF_VALUE *v;
1365 int mval;
1366 size_t i;
1367 char *p, *type;
1368 if (!nm)
1369 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001370
David Benjamin4969cc92016-04-22 15:02:23 -04001371 for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
1372 v = sk_CONF_VALUE_value(dn_sk, i);
1373 type = v->name;
1374 /*
1375 * Skip past any leading X. X: X, etc to allow for multiple instances
1376 */
1377 for (p = type; *p; p++)
1378 if ((*p == ':') || (*p == ',') || (*p == '.')) {
1379 p++;
1380 if (*p)
1381 type = p;
1382 break;
1383 }
1384 if (*type == '+') {
1385 mval = -1;
1386 type++;
1387 } else
1388 mval = 0;
1389 if (!X509_NAME_add_entry_by_txt(nm, type, chtype,
1390 (unsigned char *)v->value, -1, -1,
1391 mval))
1392 return 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -08001393
David Benjamin4969cc92016-04-22 15:02:23 -04001394 }
1395 return 1;
1396}