| 
									
										
										
										
											2016-05-18 02:52:22 +08:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-04-22 21:38:44 +08:00
										 |  |  |  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-12-06 21:00:36 +08:00
										 |  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use | 
					
						
							| 
									
										
										
										
											2016-05-18 02:52:22 +08:00
										 |  |  |  * this file except in compliance with the License.  You can obtain a copy | 
					
						
							|  |  |  |  * in the file LICENSE in the source distribution or at | 
					
						
							|  |  |  |  * https://www.openssl.org/source/license.html
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:33 +08:00
										 |  |  | #include "crypto/ctype.h"
 | 
					
						
							| 
									
										
										
										
											2015-05-14 22:56:48 +08:00
										 |  |  | #include "internal/cryptlib.h"
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  | #include <openssl/asn1t.h>
 | 
					
						
							| 
									
										
										
										
											1999-07-22 06:10:23 +08:00
										 |  |  | #include <openssl/x509.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:33 +08:00
										 |  |  | #include "crypto/x509.h"
 | 
					
						
							|  |  |  | #include "crypto/asn1.h"
 | 
					
						
							| 
									
										
										
										
											2019-09-28 06:45:40 +08:00
										 |  |  | #include "x509_local.h"
 | 
					
						
							| 
									
										
										
										
											2008-06-04 19:01:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 19:55:29 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Maximum length of X509_NAME: much larger than anything we should | 
					
						
							|  |  |  |  * ever see in practice. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define X509_NAME_MAX (1024 * 1024)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  | static int x509_name_ex_d2i(ASN1_VALUE **val, | 
					
						
							|  |  |  |                             const unsigned char **in, long len, | 
					
						
							|  |  |  |                             const ASN1_ITEM *it, | 
					
						
							|  |  |  |                             int tag, int aclass, char opt, ASN1_TLC *ctx); | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  | static int x509_name_ex_i2d(const ASN1_VALUE **val, unsigned char **out, | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |                             const ASN1_ITEM *it, int tag, int aclass); | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  | static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it); | 
					
						
							|  |  |  | static void x509_name_ex_free(ASN1_VALUE **val, const ASN1_ITEM *it); | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  | static int x509_name_encode(X509_NAME *a); | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  | static int x509_name_canon(X509_NAME *a); | 
					
						
							| 
									
										
										
										
											2016-07-04 04:09:02 +08:00
										 |  |  | static int asn1_string_canon(ASN1_STRING *out, const ASN1_STRING *in); | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  | static int i2d_name_canon(const STACK_OF(STACK_OF_X509_NAME_ENTRY) * intname, | 
					
						
							| 
									
										
										
										
											2008-06-04 19:01:43 +08:00
										 |  |  |                           unsigned char **in); | 
					
						
							| 
									
										
										
										
											2005-09-01 21:59:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  | static int x509_name_ex_print(BIO *out, const ASN1_VALUE **pval, | 
					
						
							| 
									
										
										
										
											2005-09-01 21:59:16 +08:00
										 |  |  |                               int indent, | 
					
						
							|  |  |  |                               const char *fname, const ASN1_PCTX *pctx); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  | ASN1_SEQUENCE(X509_NAME_ENTRY) = { | 
					
						
							|  |  |  |         ASN1_SIMPLE(X509_NAME_ENTRY, object, ASN1_OBJECT), | 
					
						
							|  |  |  |         ASN1_SIMPLE(X509_NAME_ENTRY, value, ASN1_PRINTABLE) | 
					
						
							| 
									
										
										
										
											2001-02-23 20:47:06 +08:00
										 |  |  | } ASN1_SEQUENCE_END(X509_NAME_ENTRY) | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  | IMPLEMENT_ASN1_FUNCTIONS(X509_NAME_ENTRY) | 
					
						
							| 
									
										
										
										
											2001-07-27 10:22:42 +08:00
										 |  |  | IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME_ENTRY) | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * For the "Name" type we need a SEQUENCE OF { SET OF X509_NAME_ENTRY } so | 
					
						
							|  |  |  |  * declare two template wrappers for this | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  | ASN1_ITEM_TEMPLATE(X509_NAME_ENTRIES) = | 
					
						
							|  |  |  |         ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, RDNS, X509_NAME_ENTRY) | 
					
						
							| 
									
										
										
										
											2015-09-05 20:32:58 +08:00
										 |  |  | static_ASN1_ITEM_TEMPLATE_END(X509_NAME_ENTRIES) | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  | ASN1_ITEM_TEMPLATE(X509_NAME_INTERNAL) = | 
					
						
							|  |  |  |         ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Name, X509_NAME_ENTRIES) | 
					
						
							| 
									
										
										
										
											2015-09-05 20:32:58 +08:00
										 |  |  | static_ASN1_ITEM_TEMPLATE_END(X509_NAME_INTERNAL) | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Normally that's where it would end: we'd have two nested STACK structures | 
					
						
							|  |  |  |  * representing the ASN1. Unfortunately X509_NAME uses a completely different | 
					
						
							|  |  |  |  * form and caches encodings so we have to process the internal form and | 
					
						
							|  |  |  |  * convert to the external form. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-05 20:32:58 +08:00
										 |  |  | static const ASN1_EXTERN_FUNCS x509_name_ff = { | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     NULL, | 
					
						
							|  |  |  |     x509_name_ex_new, | 
					
						
							|  |  |  |     x509_name_ex_free, | 
					
						
							|  |  |  |     0,                          /* Default clear behaviour is OK */ | 
					
						
							|  |  |  |     x509_name_ex_d2i, | 
					
						
							| 
									
										
										
										
											2005-09-01 21:59:16 +08:00
										 |  |  |     x509_name_ex_i2d, | 
					
						
							|  |  |  |     x509_name_ex_print | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  | IMPLEMENT_EXTERN_ASN1(X509_NAME, V_ASN1_SEQUENCE, x509_name_ff) | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  | IMPLEMENT_ASN1_FUNCTIONS(X509_NAME) | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-07-27 10:22:42 +08:00
										 |  |  | IMPLEMENT_ASN1_DUP_FUNCTION(X509_NAME) | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  | static int x509_name_ex_new(ASN1_VALUE **val, const ASN1_ITEM *it) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-09-03 21:15:26 +08:00
										 |  |  |     X509_NAME *ret = OPENSSL_zalloc(sizeof(*ret)); | 
					
						
							| 
									
										
										
										
											2015-05-02 11:10:31 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-30 19:12:26 +08:00
										 |  |  |     if (ret == NULL) | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     if ((ret->entries = sk_X509_NAME_ENTRY_new_null()) == NULL) { | 
					
						
							|  |  |  |         ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB); | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if ((ret->bytes = BUF_MEM_new()) == NULL) { | 
					
						
							|  |  |  |         ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB); | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |     ret->modified = 1; | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     *val = (ASN1_VALUE *)ret; | 
					
						
							|  |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2001-03-05 19:09:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |  err: | 
					
						
							| 
									
										
										
										
											2001-03-05 19:09:43 +08:00
										 |  |  |     if (ret) { | 
					
						
							| 
									
										
										
										
											2015-05-01 05:33:59 +08:00
										 |  |  |         sk_X509_NAME_ENTRY_free(ret->entries); | 
					
						
							| 
									
										
										
										
											2001-03-05 19:09:43 +08:00
										 |  |  |         OPENSSL_free(ret); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void x509_name_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     X509_NAME *a; | 
					
						
							| 
									
										
										
										
											2015-05-01 05:33:59 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-17 03:28:57 +08:00
										 |  |  |     if (pval == NULL || *pval == NULL) | 
					
						
							| 
									
										
										
										
											1999-01-08 03:15:59 +08:00
										 |  |  |         return; | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     a = (X509_NAME *)*pval; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |     BUF_MEM_free(a->bytes); | 
					
						
							| 
									
										
										
										
											1999-05-02 01:40:57 +08:00
										 |  |  |     sk_X509_NAME_ENTRY_pop_free(a->entries, X509_NAME_ENTRY_free); | 
					
						
							| 
									
										
										
										
											2015-05-01 22:02:07 +08:00
										 |  |  |     OPENSSL_free(a->canon_enc); | 
					
						
							| 
									
										
										
										
											2000-06-02 06:19:21 +08:00
										 |  |  |     OPENSSL_free(a); | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     *pval = NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-02 01:29:47 +08:00
										 |  |  | static void local_sk_X509_NAME_ENTRY_free(STACK_OF(X509_NAME_ENTRY) *ne) | 
					
						
							| 
									
										
										
										
											2016-10-02 22:21:29 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-02-02 01:29:47 +08:00
										 |  |  |     sk_X509_NAME_ENTRY_free(ne); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void local_sk_X509_NAME_ENTRY_pop_free(STACK_OF(X509_NAME_ENTRY) *ne) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     sk_X509_NAME_ENTRY_pop_free(ne, X509_NAME_ENTRY_free); | 
					
						
							| 
									
										
										
										
											2016-10-02 22:21:29 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  | static int x509_name_ex_d2i(ASN1_VALUE **val, | 
					
						
							|  |  |  |                             const unsigned char **in, long len, | 
					
						
							|  |  |  |                             const ASN1_ITEM *it, int tag, int aclass, | 
					
						
							|  |  |  |                             char opt, ASN1_TLC *ctx) | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2004-03-16 07:15:26 +08:00
										 |  |  |     const unsigned char *p = *in, *q; | 
					
						
							| 
									
										
										
										
											2008-06-04 19:01:43 +08:00
										 |  |  |     union { | 
					
						
							|  |  |  |         STACK_OF(STACK_OF_X509_NAME_ENTRY) *s; | 
					
						
							|  |  |  |         ASN1_VALUE *a; | 
					
						
							|  |  |  |     } intname = { | 
					
						
							| 
									
										
										
										
											2007-07-28 04:34:10 +08:00
										 |  |  |         NULL | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     union { | 
					
						
							|  |  |  |         X509_NAME *x; | 
					
						
							|  |  |  |         ASN1_VALUE *a; | 
					
						
							|  |  |  |     } nm = { | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         NULL | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     int i, j, ret; | 
					
						
							|  |  |  |     STACK_OF(X509_NAME_ENTRY) *entries; | 
					
						
							|  |  |  |     X509_NAME_ENTRY *entry; | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-04 23:09:06 +08:00
										 |  |  |     if (len > X509_NAME_MAX) | 
					
						
							|  |  |  |         len = X509_NAME_MAX; | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     q = p; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     /* Get internal representation of Name */ | 
					
						
							| 
									
										
										
										
											2007-07-28 04:34:10 +08:00
										 |  |  |     ret = ASN1_item_ex_d2i(&intname.a, | 
					
						
							| 
									
										
										
										
											2005-01-27 09:47:31 +08:00
										 |  |  |                            &p, len, ASN1_ITEM_rptr(X509_NAME_INTERNAL), | 
					
						
							|  |  |  |                            tag, aclass, opt, ctx); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     if (ret <= 0) | 
					
						
							|  |  |  |         return ret; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     if (*val) | 
					
						
							|  |  |  |         x509_name_ex_free(val, NULL); | 
					
						
							| 
									
										
										
										
											2007-07-28 04:34:10 +08:00
										 |  |  |     if (!x509_name_ex_new(&nm.a, NULL)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     /* We've decoded it: now cache encoding */ | 
					
						
							| 
									
										
										
										
											2007-07-28 04:34:10 +08:00
										 |  |  |     if (!BUF_MEM_grow(nm.x->bytes, p - q)) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     memcpy(nm.x->bytes->data, q, p - q); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     /* Convert internal representation to X509_NAME structure */ | 
					
						
							| 
									
										
										
										
											2008-06-04 19:01:43 +08:00
										 |  |  |     for (i = 0; i < sk_STACK_OF_X509_NAME_ENTRY_num(intname.s); i++) { | 
					
						
							|  |  |  |         entries = sk_STACK_OF_X509_NAME_ENTRY_value(intname.s, i); | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |         for (j = 0; j < sk_X509_NAME_ENTRY_num(entries); j++) { | 
					
						
							|  |  |  |             entry = sk_X509_NAME_ENTRY_value(entries, j); | 
					
						
							|  |  |  |             entry->set = i; | 
					
						
							| 
									
										
										
										
											2016-10-02 22:21:29 +08:00
										 |  |  |             if (!sk_X509_NAME_ENTRY_push(nm.x->entries, entry)) | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |                 goto err; | 
					
						
							| 
									
										
										
										
											2020-09-10 23:34:17 +08:00
										 |  |  |             (void)sk_X509_NAME_ENTRY_set(entries, j, NULL); | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-07-28 04:34:10 +08:00
										 |  |  |     ret = x509_name_canon(nm.x); | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     if (!ret) | 
					
						
							|  |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2017-02-02 01:29:47 +08:00
										 |  |  |     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s, | 
					
						
							|  |  |  |                                          local_sk_X509_NAME_ENTRY_free); | 
					
						
							| 
									
										
										
										
											2007-07-28 04:34:10 +08:00
										 |  |  |     nm.x->modified = 0; | 
					
						
							|  |  |  |     *val = nm.a; | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     *in = p; | 
					
						
							|  |  |  |     return ret; | 
					
						
							| 
									
										
										
										
											2016-07-02 11:49:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-05 17:57:20 +08:00
										 |  |  |  err: | 
					
						
							| 
									
										
										
										
											2017-02-02 01:29:47 +08:00
										 |  |  |     if (nm.x != NULL) | 
					
						
							|  |  |  |         X509_NAME_free(nm.x); | 
					
						
							|  |  |  |     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s, | 
					
						
							|  |  |  |                                          local_sk_X509_NAME_ENTRY_pop_free); | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |     ERR_raise(ERR_LIB_ASN1, ERR_R_NESTED_ASN1_ERROR); | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  | static int x509_name_ex_i2d(const ASN1_VALUE **val, unsigned char **out, | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |                             const ASN1_ITEM *it, int tag, int aclass) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  |     X509_NAME *a = (X509_NAME *)*val; | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     if (a->modified) { | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |         ret = x509_name_encode(a); | 
					
						
							|  |  |  |         if (ret < 0) | 
					
						
							|  |  |  |             return ret; | 
					
						
							|  |  |  |         ret = x509_name_canon(a); | 
					
						
							| 
									
										
										
										
											2021-11-05 17:16:03 +08:00
										 |  |  |         if (!ret) | 
					
						
							|  |  |  |             return -1; | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     } | 
					
						
							|  |  |  |     ret = a->bytes->length; | 
					
						
							|  |  |  |     if (out != NULL) { | 
					
						
							|  |  |  |         memcpy(*out, a->bytes->data, ret); | 
					
						
							|  |  |  |         *out += ret; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  | static int x509_name_encode(X509_NAME *a) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-06-04 19:01:43 +08:00
										 |  |  |     union { | 
					
						
							|  |  |  |         STACK_OF(STACK_OF_X509_NAME_ENTRY) *s; | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  |         const ASN1_VALUE *a; | 
					
						
							| 
									
										
										
										
											2008-06-04 19:01:43 +08:00
										 |  |  |     } intname = { | 
					
						
							|  |  |  |         NULL | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     int len; | 
					
						
							|  |  |  |     unsigned char *p; | 
					
						
							|  |  |  |     STACK_OF(X509_NAME_ENTRY) *entries = NULL; | 
					
						
							|  |  |  |     X509_NAME_ENTRY *entry; | 
					
						
							|  |  |  |     int i, set = -1; | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-06-04 19:01:43 +08:00
										 |  |  |     intname.s = sk_STACK_OF_X509_NAME_ENTRY_new_null(); | 
					
						
							| 
									
										
										
										
											2007-07-28 04:34:10 +08:00
										 |  |  |     if (!intname.s) | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |         goto cerr; | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { | 
					
						
							|  |  |  |         entry = sk_X509_NAME_ENTRY_value(a->entries, i); | 
					
						
							|  |  |  |         if (entry->set != set) { | 
					
						
							|  |  |  |             entries = sk_X509_NAME_ENTRY_new_null(); | 
					
						
							|  |  |  |             if (!entries) | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |                 goto cerr; | 
					
						
							| 
									
										
										
										
											2017-02-02 01:29:47 +08:00
										 |  |  |             if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname.s, entries)) { | 
					
						
							|  |  |  |                 sk_X509_NAME_ENTRY_free(entries); | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |                 goto cerr; | 
					
						
							| 
									
										
										
										
											2017-02-02 01:29:47 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |             set = entry->set; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!sk_X509_NAME_ENTRY_push(entries, entry)) | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |             goto cerr; | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2007-07-28 04:34:10 +08:00
										 |  |  |     len = ASN1_item_ex_i2d(&intname.a, NULL, | 
					
						
							| 
									
										
										
										
											2005-01-27 09:47:31 +08:00
										 |  |  |                            ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1); | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |     if (!BUF_MEM_grow(a->bytes, len)) { | 
					
						
							|  |  |  |         ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB); | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     p = (unsigned char *)a->bytes->data; | 
					
						
							| 
									
										
										
										
											2007-07-28 04:34:10 +08:00
										 |  |  |     ASN1_item_ex_i2d(&intname.a, | 
					
						
							| 
									
										
										
										
											2005-01-27 09:47:31 +08:00
										 |  |  |                      &p, ASN1_ITEM_rptr(X509_NAME_INTERNAL), -1, -1); | 
					
						
							| 
									
										
										
										
											2008-06-04 19:01:43 +08:00
										 |  |  |     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s, | 
					
						
							|  |  |  |                                          local_sk_X509_NAME_ENTRY_free); | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     a->modified = 0; | 
					
						
							|  |  |  |     return len; | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |  cerr: | 
					
						
							|  |  |  |     ERR_raise(ERR_LIB_ASN1, ERR_R_CRYPTO_LIB); | 
					
						
							|  |  |  |  err: | 
					
						
							| 
									
										
										
										
											2008-06-04 19:01:43 +08:00
										 |  |  |     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname.s, | 
					
						
							|  |  |  |                                          local_sk_X509_NAME_ENTRY_free); | 
					
						
							| 
									
										
										
										
											2000-12-09 03:09:35 +08:00
										 |  |  |     return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  | static int x509_name_ex_print(BIO *out, const ASN1_VALUE **pval, | 
					
						
							| 
									
										
										
										
											2005-09-01 21:59:16 +08:00
										 |  |  |                               int indent, | 
					
						
							|  |  |  |                               const char *fname, const ASN1_PCTX *pctx) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2016-07-08 05:45:55 +08:00
										 |  |  |     if (X509_NAME_print_ex(out, (const X509_NAME *)*pval, | 
					
						
							| 
									
										
										
										
											2005-09-01 21:59:16 +08:00
										 |  |  |                            indent, pctx->nm_flags) <= 0) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2005-09-02 02:02:51 +08:00
										 |  |  |     return 2; | 
					
						
							| 
									
										
										
										
											2005-09-01 21:59:16 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * This function generates the canonical encoding of the Name structure. In | 
					
						
							|  |  |  |  * it all strings are converted to UTF8, leading, trailing and multiple | 
					
						
							|  |  |  |  * spaces collapsed, converted to lower case and the leading SEQUENCE header | 
					
						
							|  |  |  |  * removed. In future we could also normalize the UTF8 too. By doing this | 
					
						
							| 
									
										
										
										
											2016-02-06 04:23:54 +08:00
										 |  |  |  * comparison of Name structures can be rapidly performed by just using | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |  * memcmp() of the canonical encoding. By omitting the leading SEQUENCE name | 
					
						
							|  |  |  |  * constraints of type dirName can also be checked with a simple memcmp(). | 
					
						
							| 
									
										
										
										
											2021-04-13 01:22:04 +08:00
										 |  |  |  * NOTE: For empty X509_NAME (NULL-DN), canon_enclen == 0 && canon_enc == NULL | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static int x509_name_canon(X509_NAME *a) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     unsigned char *p; | 
					
						
							| 
									
										
										
										
											2018-03-29 04:32:31 +08:00
										 |  |  |     STACK_OF(STACK_OF_X509_NAME_ENTRY) *intname; | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     STACK_OF(X509_NAME_ENTRY) *entries = NULL; | 
					
						
							| 
									
										
										
										
											2006-07-22 06:46:19 +08:00
										 |  |  |     X509_NAME_ENTRY *entry, *tmpentry = NULL; | 
					
						
							| 
									
										
										
										
											2016-04-29 00:05:21 +08:00
										 |  |  |     int i, set = -1, ret = 0, len; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-01 22:02:07 +08:00
										 |  |  |     OPENSSL_free(a->canon_enc); | 
					
						
							|  |  |  |     a->canon_enc = NULL; | 
					
						
							| 
									
										
										
										
											2007-09-15 02:11:17 +08:00
										 |  |  |     /* Special case: empty X509_NAME => null encoding */ | 
					
						
							|  |  |  |     if (sk_X509_NAME_ENTRY_num(a->entries) == 0) { | 
					
						
							|  |  |  |         a->canon_enclen = 0; | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2008-06-04 19:01:43 +08:00
										 |  |  |     intname = sk_STACK_OF_X509_NAME_ENTRY_new_null(); | 
					
						
							| 
									
										
										
										
											2018-03-29 04:32:31 +08:00
										 |  |  |     if (intname == NULL) { | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |         ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB); | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2018-03-29 04:32:31 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { | 
					
						
							|  |  |  |         entry = sk_X509_NAME_ENTRY_value(a->entries, i); | 
					
						
							|  |  |  |         if (entry->set != set) { | 
					
						
							|  |  |  |             entries = sk_X509_NAME_ENTRY_new_null(); | 
					
						
							| 
									
										
										
										
											2018-03-29 04:32:31 +08:00
										 |  |  |             if (entries == NULL) | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |                 goto err; | 
					
						
							| 
									
										
										
										
											2017-02-02 01:29:47 +08:00
										 |  |  |             if (!sk_STACK_OF_X509_NAME_ENTRY_push(intname, entries)) { | 
					
						
							|  |  |  |                 sk_X509_NAME_ENTRY_free(entries); | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |                 ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB); | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |                 goto err; | 
					
						
							| 
									
										
										
										
											2017-02-02 01:29:47 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |             set = entry->set; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         tmpentry = X509_NAME_ENTRY_new(); | 
					
						
							| 
									
										
										
										
											2018-03-29 04:32:31 +08:00
										 |  |  |         if (tmpentry == NULL) { | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |             ERR_raise(ERR_LIB_X509, ERR_R_ASN1_LIB); | 
					
						
							| 
									
										
										
										
											2015-01-05 03:51:04 +08:00
										 |  |  |             goto err; | 
					
						
							| 
									
										
										
										
											2018-03-29 04:32:31 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |         tmpentry->object = OBJ_dup(entry->object); | 
					
						
							| 
									
										
										
										
											2018-03-29 04:32:31 +08:00
										 |  |  |         if (tmpentry->object == NULL) { | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |             ERR_raise(ERR_LIB_X509, ERR_R_OBJ_LIB); | 
					
						
							| 
									
										
										
										
											2016-06-04 06:14:52 +08:00
										 |  |  |             goto err; | 
					
						
							| 
									
										
										
										
											2018-03-29 04:32:31 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |         if (!asn1_string_canon(tmpentry->value, entry->value)) | 
					
						
							|  |  |  |             goto err; | 
					
						
							| 
									
										
										
										
											2018-03-29 04:32:31 +08:00
										 |  |  |         if (!sk_X509_NAME_ENTRY_push(entries, tmpentry)) { | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |             ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB); | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |             goto err; | 
					
						
							| 
									
										
										
										
											2018-03-29 04:32:31 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |         tmpentry = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     /* Finally generate encoding */ | 
					
						
							| 
									
										
										
										
											2016-04-29 00:05:21 +08:00
										 |  |  |     len = i2d_name_canon(intname, NULL); | 
					
						
							|  |  |  |     if (len < 0) | 
					
						
							|  |  |  |         goto err; | 
					
						
							|  |  |  |     a->canon_enclen = len; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     p = OPENSSL_malloc(a->canon_enclen); | 
					
						
							| 
									
										
										
										
											2022-09-29 19:57:34 +08:00
										 |  |  |     if (p == NULL) | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |         goto err; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     a->canon_enc = p; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     i2d_name_canon(intname, &p); | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     ret = 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |  err: | 
					
						
							| 
									
										
										
										
											2015-05-01 05:33:59 +08:00
										 |  |  |     X509_NAME_ENTRY_free(tmpentry); | 
					
						
							|  |  |  |     sk_STACK_OF_X509_NAME_ENTRY_pop_free(intname, | 
					
						
							|  |  |  |                                          local_sk_X509_NAME_ENTRY_pop_free); | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     return ret; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Bitmap of all the types of string that will be canonicalized. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define ASN1_MASK_CANON \
 | 
					
						
							|  |  |  |         (B_ASN1_UTF8STRING | B_ASN1_BMPSTRING | B_ASN1_UNIVERSALSTRING \ | 
					
						
							|  |  |  |         | B_ASN1_PRINTABLESTRING | B_ASN1_T61STRING | B_ASN1_IA5STRING \ | 
					
						
							|  |  |  |         | B_ASN1_VISIBLESTRING) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-04 04:09:02 +08:00
										 |  |  | static int asn1_string_canon(ASN1_STRING *out, const ASN1_STRING *in) | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     unsigned char *to, *from; | 
					
						
							|  |  |  |     int len, i; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     /* If type not in bitmask just copy string across */ | 
					
						
							|  |  |  |     if (!(ASN1_tag2bit(in->type) & ASN1_MASK_CANON)) { | 
					
						
							| 
									
										
										
										
											2012-02-28 02:45:28 +08:00
										 |  |  |         if (!ASN1_STRING_copy(out, in)) | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |             return 0; | 
					
						
							| 
									
										
										
										
											2009-01-28 20:32:03 +08:00
										 |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     out->type = V_ASN1_UTF8STRING; | 
					
						
							|  |  |  |     out->length = ASN1_STRING_to_UTF8(&out->data, in); | 
					
						
							|  |  |  |     if (out->length == -1) | 
					
						
							|  |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     to = out->data; | 
					
						
							|  |  |  |     from = to; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     len = out->length; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     /*
 | 
					
						
							|  |  |  |      * Convert string in place to canonical form. Ultimately we may need to | 
					
						
							|  |  |  |      * handle a wider range of characters but for now ignore anything with | 
					
						
							| 
									
										
										
										
											2017-08-21 05:19:17 +08:00
										 |  |  |      * MSB set and rely on the ossl_isspace() to fail on bad characters without | 
					
						
							|  |  |  |      * needing isascii or range checks as well. | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     /* Ignore leading spaces */ | 
					
						
							| 
									
										
										
										
											2017-08-21 05:19:17 +08:00
										 |  |  |     while (len > 0 && ossl_isspace(*from)) { | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |         from++; | 
					
						
							|  |  |  |         len--; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-04 22:25:39 +08:00
										 |  |  |     to = from + len; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     /* Ignore trailing spaces */ | 
					
						
							| 
									
										
										
										
											2017-08-21 05:19:17 +08:00
										 |  |  |     while (len > 0 && ossl_isspace(to[-1])) { | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |         to--; | 
					
						
							|  |  |  |         len--; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     to = out->data; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     i = 0; | 
					
						
							|  |  |  |     while (i < len) { | 
					
						
							| 
									
										
										
										
											2017-08-21 05:19:17 +08:00
										 |  |  |         /* If not ASCII set just copy across */ | 
					
						
							|  |  |  |         if (!ossl_isascii(*from)) { | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |             *to++ = *from++; | 
					
						
							| 
									
										
										
										
											2006-09-22 21:37:15 +08:00
										 |  |  |             i++; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |         /* Collapse multiple spaces */ | 
					
						
							| 
									
										
										
										
											2017-08-21 05:19:17 +08:00
										 |  |  |         else if (ossl_isspace(*from)) { | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |             /* Copy one space across */ | 
					
						
							|  |  |  |             *to++ = ' '; | 
					
						
							|  |  |  |             /*
 | 
					
						
							|  |  |  |              * Ignore subsequent spaces. Note: don't need to check len here | 
					
						
							|  |  |  |              * because we know the last character is a non-space so we can't | 
					
						
							|  |  |  |              * overflow. | 
					
						
							|  |  |  |              */ | 
					
						
							|  |  |  |             do { | 
					
						
							|  |  |  |                 from++; | 
					
						
							|  |  |  |                 i++; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-08-21 05:19:17 +08:00
										 |  |  |             while (ossl_isspace(*from)); | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |         } else { | 
					
						
							| 
									
										
										
										
											2017-08-21 05:19:17 +08:00
										 |  |  |             *to++ = ossl_tolower(*from); | 
					
						
							| 
									
										
										
										
											2011-09-02 19:28:27 +08:00
										 |  |  |             from++; | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |             i++; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     out->length = to - out->data; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  | static int i2d_name_canon(const STACK_OF(STACK_OF_X509_NAME_ENTRY) * _intname, | 
					
						
							| 
									
										
										
										
											2008-06-04 19:01:43 +08:00
										 |  |  |                           unsigned char **in) | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     int i, len, ltmp; | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  |     const ASN1_VALUE *v; | 
					
						
							| 
									
										
										
										
											2008-06-04 19:01:43 +08:00
										 |  |  |     STACK_OF(ASN1_VALUE) *intname = (STACK_OF(ASN1_VALUE) *)_intname; | 
					
						
							| 
									
										
										
										
											2015-01-22 11:40:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |     len = 0; | 
					
						
							| 
									
										
										
										
											2008-06-04 19:01:43 +08:00
										 |  |  |     for (i = 0; i < sk_ASN1_VALUE_num(intname); i++) { | 
					
						
							|  |  |  |         v = sk_ASN1_VALUE_value(intname, i); | 
					
						
							| 
									
										
										
										
											2006-07-18 20:36:19 +08:00
										 |  |  |         ltmp = ASN1_item_ex_i2d(&v, in, | 
					
						
							|  |  |  |                                 ASN1_ITEM_rptr(X509_NAME_ENTRIES), -1, -1); | 
					
						
							|  |  |  |         if (ltmp < 0) | 
					
						
							|  |  |  |             return ltmp; | 
					
						
							|  |  |  |         len += ltmp; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return len; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  | int X509_NAME_set(X509_NAME **xn, const X509_NAME *name) | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  |     X509_NAME *name_copy; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-17 15:53:14 +08:00
										 |  |  |     if (*xn == name) | 
					
						
							|  |  |  |         return *xn != NULL; | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  |     if ((name_copy = X509_NAME_dup(name)) == NULL) | 
					
						
							| 
									
										
										
										
											2017-09-03 23:33:34 +08:00
										 |  |  |         return 0; | 
					
						
							|  |  |  |     X509_NAME_free(*xn); | 
					
						
							| 
									
										
										
										
											2019-01-16 04:51:25 +08:00
										 |  |  |     *xn = name_copy; | 
					
						
							| 
									
										
										
										
											2017-09-03 23:33:34 +08:00
										 |  |  |     return 1; | 
					
						
							| 
									
										
										
										
											1998-12-21 18:52:47 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-22 23:05:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-08 05:45:55 +08:00
										 |  |  | int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase) | 
					
						
							| 
									
										
										
										
											2015-09-22 23:05:33 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     char *s, *c, *b; | 
					
						
							| 
									
										
										
										
											2022-10-20 07:14:53 +08:00
										 |  |  |     int i; | 
					
						
							| 
									
										
										
										
											2015-09-22 23:05:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     b = X509_NAME_oneline(name, NULL, 0); | 
					
						
							| 
									
										
										
										
											2019-09-17 03:28:57 +08:00
										 |  |  |     if (b == NULL) | 
					
						
							| 
									
										
										
										
											2015-09-22 23:05:33 +08:00
										 |  |  |         return 0; | 
					
						
							| 
									
										
										
										
											2019-09-17 03:28:57 +08:00
										 |  |  |     if (*b == '\0') { | 
					
						
							| 
									
										
										
										
											2015-09-22 23:05:33 +08:00
										 |  |  |         OPENSSL_free(b); | 
					
						
							|  |  |  |         return 1; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     s = b + 1;                  /* skip the first slash */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     c = s; | 
					
						
							|  |  |  |     for (;;) { | 
					
						
							|  |  |  |         if (((*s == '/') && | 
					
						
							| 
									
										
										
										
											2017-08-21 05:19:17 +08:00
										 |  |  |              (ossl_isupper(s[1]) && ((s[2] == '=') || | 
					
						
							|  |  |  |                                 (ossl_isupper(s[2]) && (s[3] == '=')) | 
					
						
							| 
									
										
										
										
											2015-09-22 23:05:33 +08:00
										 |  |  |               ))) || (*s == '\0')) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |             i = s - c; | 
					
						
							|  |  |  |             if (BIO_write(bp, c, i) != i) | 
					
						
							|  |  |  |                 goto err; | 
					
						
							|  |  |  |             c = s + 1;          /* skip following slash */ | 
					
						
							|  |  |  |             if (*s != '\0') { | 
					
						
							|  |  |  |                 if (BIO_write(bp, ", ", 2) != 2) | 
					
						
							|  |  |  |                     goto err; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (*s == '\0') | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         s++; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     OPENSSL_free(b); | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  |  err: | 
					
						
							| 
									
										
										
										
											2020-11-04 19:23:19 +08:00
										 |  |  |     ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB); | 
					
						
							| 
									
										
										
										
											2015-09-22 23:05:33 +08:00
										 |  |  |     OPENSSL_free(b); | 
					
						
							|  |  |  |     return 0; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-01-30 10:51:01 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-23 15:30:37 +08:00
										 |  |  | int X509_NAME_get0_der(const X509_NAME *nm, const unsigned char **pder, | 
					
						
							| 
									
										
										
										
											2016-08-15 17:07:30 +08:00
										 |  |  |                        size_t *pderlen) | 
					
						
							| 
									
										
										
										
											2016-01-30 10:51:01 +08:00
										 |  |  | { | 
					
						
							|  |  |  |     /* Make sure encoding is valid */ | 
					
						
							|  |  |  |     if (i2d_X509_NAME(nm, NULL) <= 0) | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     if (pder != NULL) | 
					
						
							|  |  |  |         *pder = (unsigned char *)nm->bytes->data; | 
					
						
							|  |  |  |     if (pderlen != NULL) | 
					
						
							|  |  |  |         *pderlen = nm->bytes->length; | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } |