1d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * All rights reserved.
3d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
4d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * This package is an SSL implementation written
5d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * by Eric Young (eay@cryptsoft.com).
6d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * The implementation was written so as to conform with Netscapes SSL.
7d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
8d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * This library is free for commercial and non-commercial use as long as
9d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * the following conditions are aheared to.  The following conditions
10d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * apply to all code found in this distribution, be it the RC4, RSA,
11d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * included with this distribution is covered by the same copyright terms
13d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
15d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Copyright remains Eric Young's, and as such any Copyright notices in
16d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * the code are not to be removed.
17d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * If this package is used in a product, Eric Young should be given attribution
18d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * as the author of the parts of the library used.
19d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * This can be in the form of a textual message at program startup or
20d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * in documentation (online or textual) provided with the package.
21d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
22d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * Redistribution and use in source and binary forms, with or without
23d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * modification, are permitted provided that the following conditions
24d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * are met:
25d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 1. Redistributions of source code must retain the copyright
26d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    notice, this list of conditions and the following disclaimer.
27d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 2. Redistributions in binary form must reproduce the above copyright
28d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    notice, this list of conditions and the following disclaimer in the
29d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    documentation and/or other materials provided with the distribution.
30d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 3. All advertising materials mentioning features or use of this software
31d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    must display the following acknowledgement:
32d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    "This product includes cryptographic software written by
33d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *     Eric Young (eay@cryptsoft.com)"
34d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    The word 'cryptographic' can be left out if the rouines from the library
35d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    being used are not cryptographic related :-).
36d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * 4. If you include any Windows specific code (or a derivative thereof) from
37d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    the apps directory (application code) you must include an acknowledgement:
38d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
40d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * SUCH DAMAGE.
51d9e397b599b13d642138480a28c14db7a136bf0Adam Langley *
52d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * The licence and distribution terms for any publically available version or
53d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * derivative of this code cannot be changed.  i.e. this code cannot simply be
54d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * copied and put under another distribution licence
55d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * [including the GNU Public Licence.] */
56d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
57d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <string.h>
58d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
59d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/asn1.h>
60d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/err.h>
61d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/evp.h>
62d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/obj.h>
63d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/stack.h>
64d9e397b599b13d642138480a28c14db7a136bf0Adam Langley#include <openssl/x509.h>
65d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
66d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
67d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint X509_NAME_get_text_by_NID(X509_NAME *name, int nid, char *buf, int len)
68d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	{
69d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	const ASN1_OBJECT *obj;
70d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
71d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	obj=OBJ_nid2obj(nid);
72d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (obj == NULL) return(-1);
73d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return(X509_NAME_get_text_by_OBJ(name,obj,buf,len));
74d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	}
75d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
76d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint X509_NAME_get_text_by_OBJ(X509_NAME *name, const ASN1_OBJECT *obj, char *buf,
77d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	     int len)
78d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	{
79d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	int i;
80d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	ASN1_STRING *data;
81d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
82d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	i=X509_NAME_get_index_by_OBJ(name,obj,-1);
83d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (i < 0) return(-1);
84d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	data=X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name,i));
85d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	i=(data->length > (len-1))?(len-1):data->length;
86d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (buf == NULL) return(data->length);
87d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	memcpy(buf,data->data,i);
88d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	buf[i]='\0';
89d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return(i);
90d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	}
91d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
92d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint X509_NAME_entry_count(X509_NAME *name)
93d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	{
94d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (name == NULL) return(0);
95d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return(sk_X509_NAME_ENTRY_num(name->entries));
96d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	}
97d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
98d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint X509_NAME_get_index_by_NID(X509_NAME *name, int nid, int lastpos)
99d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	{
100d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	const ASN1_OBJECT *obj;
101d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
102d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	obj=OBJ_nid2obj(nid);
103d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (obj == NULL) return(-2);
104d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return(X509_NAME_get_index_by_OBJ(name,obj,lastpos));
105d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	}
106d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
107d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* NOTE: you should be passsing -1, not 0 as lastpos */
108d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint X509_NAME_get_index_by_OBJ(X509_NAME *name, const ASN1_OBJECT *obj,
109d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	     int lastpos)
110d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	{
111d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	int n;
112d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	X509_NAME_ENTRY *ne;
113d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	STACK_OF(X509_NAME_ENTRY) *sk;
114d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
115d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (name == NULL) return(-1);
116d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (lastpos < 0)
117d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		lastpos= -1;
118d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	sk=name->entries;
119d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	n=sk_X509_NAME_ENTRY_num(sk);
120d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	for (lastpos++; lastpos < n; lastpos++)
121d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		{
122d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		ne=sk_X509_NAME_ENTRY_value(sk,lastpos);
123d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		if (OBJ_cmp(ne->object,obj) == 0)
124d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			return(lastpos);
125d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		}
126d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return(-1);
127d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	}
128d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
129d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyX509_NAME_ENTRY *X509_NAME_get_entry(X509_NAME *name, int loc)
130d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	{
131d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if(name == NULL || loc < 0 || sk_X509_NAME_ENTRY_num(name->entries) <= (size_t) loc)
132d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		return(NULL);
133d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	else
134d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		return(sk_X509_NAME_ENTRY_value(name->entries,loc));
135d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	}
136d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
137d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyX509_NAME_ENTRY *X509_NAME_delete_entry(X509_NAME *name, int loc)
138d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	{
139d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	X509_NAME_ENTRY *ret;
140d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	int i,n,set_prev,set_next;
141d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	STACK_OF(X509_NAME_ENTRY) *sk;
142d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
143d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (name == NULL || loc < 0 || sk_X509_NAME_ENTRY_num(name->entries) <= (size_t) loc)
144d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		return(NULL);
145d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	sk=name->entries;
146d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	ret=sk_X509_NAME_ENTRY_delete(sk,loc);
147d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	n=sk_X509_NAME_ENTRY_num(sk);
148d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	name->modified=1;
149d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (loc == n) return(ret);
150d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
151d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	/* else we need to fixup the set field */
152d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (loc != 0)
153d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		set_prev=(sk_X509_NAME_ENTRY_value(sk,loc-1))->set;
154d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	else
155d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		set_prev=ret->set-1;
156d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	set_next=sk_X509_NAME_ENTRY_value(sk,loc)->set;
157d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
158d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	/* set_prev is the previous set
159d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	 * set is the current set
160d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	 * set_next is the following
161d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	 * prev  1 1	1 1	1 1	1 1
162d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	 * set   1	1	2	2
163d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	 * next  1 1	2 2	2 2	3 2
164d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	 * so basically only if prev and next differ by 2, then
165d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	 * re-number down by 1 */
166d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (set_prev+1 < set_next)
167d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		for (i=loc; i<n; i++)
168d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			sk_X509_NAME_ENTRY_value(sk,i)->set--;
169d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return(ret);
170d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	}
171d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
172d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint X509_NAME_add_entry_by_OBJ(X509_NAME *name, ASN1_OBJECT *obj, int type,
173d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			unsigned char *bytes, int len, int loc, int set)
174d9e397b599b13d642138480a28c14db7a136bf0Adam Langley{
175d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	X509_NAME_ENTRY *ne;
176d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	int ret;
177d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	ne = X509_NAME_ENTRY_create_by_OBJ(NULL, obj, type, bytes, len);
178d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if(!ne) return 0;
179d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	ret = X509_NAME_add_entry(name, ne, loc, set);
180d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	X509_NAME_ENTRY_free(ne);
181d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return ret;
182d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
183d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
184d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint X509_NAME_add_entry_by_NID(X509_NAME *name, int nid, int type,
185d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			unsigned char *bytes, int len, int loc, int set)
186d9e397b599b13d642138480a28c14db7a136bf0Adam Langley{
187d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	X509_NAME_ENTRY *ne;
188d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	int ret;
189d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	ne = X509_NAME_ENTRY_create_by_NID(NULL, nid, type, bytes, len);
190d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if(!ne) return 0;
191d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	ret = X509_NAME_add_entry(name, ne, loc, set);
192d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	X509_NAME_ENTRY_free(ne);
193d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return ret;
194d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
195d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
196d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint X509_NAME_add_entry_by_txt(X509_NAME *name, const char *field, int type,
197d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			const unsigned char *bytes, int len, int loc, int set)
198d9e397b599b13d642138480a28c14db7a136bf0Adam Langley{
199d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	X509_NAME_ENTRY *ne;
200d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	int ret;
201d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	ne = X509_NAME_ENTRY_create_by_txt(NULL, field, type, bytes, len);
202d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if(!ne) return 0;
203d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	ret = X509_NAME_add_entry(name, ne, loc, set);
204d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	X509_NAME_ENTRY_free(ne);
205d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return ret;
206d9e397b599b13d642138480a28c14db7a136bf0Adam Langley}
207d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
208d9e397b599b13d642138480a28c14db7a136bf0Adam Langley/* if set is -1, append to previous set, 0 'a new one', and 1,
209d9e397b599b13d642138480a28c14db7a136bf0Adam Langley * prepend to the guy we are about to stomp on. */
210d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint X509_NAME_add_entry(X509_NAME *name, X509_NAME_ENTRY *ne, int loc,
211d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	     int set)
212d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	{
213d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	X509_NAME_ENTRY *new_name=NULL;
214d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	int n,i,inc;
215d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	STACK_OF(X509_NAME_ENTRY) *sk;
216d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
217d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (name == NULL) return(0);
218d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	sk=name->entries;
219d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	n=sk_X509_NAME_ENTRY_num(sk);
220d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (loc > n) loc=n;
221d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	else if (loc < 0) loc=n;
222d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
223d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	name->modified=1;
224d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
225d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (set == -1)
226d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		{
227d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		if (loc == 0)
228d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			{
229d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			set=0;
230d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			inc=1;
231d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			}
232d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		else
233d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			{
234d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			set=sk_X509_NAME_ENTRY_value(sk,loc-1)->set;
235d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			inc=0;
236d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			}
237d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		}
238d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	else /* if (set >= 0) */
239d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		{
240d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		if (loc >= n)
241d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			{
242d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			if (loc != 0)
243d9e397b599b13d642138480a28c14db7a136bf0Adam Langley				set=sk_X509_NAME_ENTRY_value(sk,loc-1)->set+1;
244d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			else
245d9e397b599b13d642138480a28c14db7a136bf0Adam Langley				set=0;
246d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			}
247d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		else
248d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			set=sk_X509_NAME_ENTRY_value(sk,loc)->set;
249d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		inc=(set == 0)?1:0;
250d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		}
251d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
252d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if ((new_name=X509_NAME_ENTRY_dup(ne)) == NULL)
253d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		goto err;
254d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	new_name->set=set;
255d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (!sk_X509_NAME_ENTRY_insert(sk,new_name,loc))
256d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		{
257b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root		OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
258d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		goto err;
259d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		}
260d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (inc)
261d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		{
262d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		n=sk_X509_NAME_ENTRY_num(sk);
263d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		for (i=loc+1; i<n; i++)
264d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			sk_X509_NAME_ENTRY_value(sk,i-1)->set+=1;
265d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		}
266d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return(1);
267d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyerr:
268d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (new_name != NULL)
269d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		X509_NAME_ENTRY_free(new_name);
270d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return(0);
271d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	}
272d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
273d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyX509_NAME_ENTRY *X509_NAME_ENTRY_create_by_txt(X509_NAME_ENTRY **ne,
274d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		const char *field, int type, const unsigned char *bytes, int len)
275d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	{
276d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	ASN1_OBJECT *obj;
277d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	X509_NAME_ENTRY *nentry;
278d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
279d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	obj=OBJ_txt2obj(field, 0);
280d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (obj == NULL)
281d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		{
282b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root		OPENSSL_PUT_ERROR(X509, X509_R_INVALID_FIELD_NAME);
283d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		ERR_add_error_data(2, "name=", field);
284d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		return(NULL);
285d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		}
286d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	nentry = X509_NAME_ENTRY_create_by_OBJ(ne,obj,type,bytes,len);
287d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	ASN1_OBJECT_free(obj);
288d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return nentry;
289d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	}
290d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
291d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyX509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY **ne, int nid,
292d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	     int type, unsigned char *bytes, int len)
293d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	{
2944139edb02e59e7ad48e0a8f4c02e45923bc8a344Adam Langley	const ASN1_OBJECT *obj = OBJ_nid2obj(nid);
295d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (obj == NULL)
296d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		{
297b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root		OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_NID);
2984139edb02e59e7ad48e0a8f4c02e45923bc8a344Adam Langley		return NULL;
299d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		}
3004139edb02e59e7ad48e0a8f4c02e45923bc8a344Adam Langley	return X509_NAME_ENTRY_create_by_OBJ(ne,obj,type,bytes,len);
301d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	}
302d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
303d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyX509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne,
304d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	     const ASN1_OBJECT *obj, int type, const unsigned char *bytes, int len)
305d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	{
306d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	X509_NAME_ENTRY *ret;
307d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
308d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if ((ne == NULL) || (*ne == NULL))
309d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		{
310d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		if ((ret=X509_NAME_ENTRY_new()) == NULL)
311d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			return(NULL);
312d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		}
313d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	else
314d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		ret= *ne;
315d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
316d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (!X509_NAME_ENTRY_set_object(ret,obj))
317d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		goto err;
318d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (!X509_NAME_ENTRY_set_data(ret,type,bytes,len))
319d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		goto err;
320d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
321d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if ((ne != NULL) && (*ne == NULL)) *ne=ret;
322d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return(ret);
323d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyerr:
324d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if ((ne == NULL) || (ret != *ne))
325d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		X509_NAME_ENTRY_free(ret);
326d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return(NULL);
327d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	}
328d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
329d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne, const ASN1_OBJECT *obj)
330d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	{
331d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if ((ne == NULL) || (obj == NULL))
332d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		{
333b8494591d1b1a143f3b192d845c238bbf3bc629dKenny Root		OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER);
334d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		return(0);
335d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		}
336d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	ASN1_OBJECT_free(ne->object);
337d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	ne->object=OBJ_dup(obj);
338d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return((ne->object == NULL)?0:1);
339d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	}
340d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
341d9e397b599b13d642138480a28c14db7a136bf0Adam Langleyint X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne, int type,
342d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	     const unsigned char *bytes, int len)
343d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	{
344d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	int i;
345d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
346d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if ((ne == NULL) || ((bytes == NULL) && (len != 0))) return(0);
347d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if((type > 0) && (type & MBSTRING_FLAG))
348d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		return ASN1_STRING_set_by_NID(&ne->value, bytes,
349d9e397b599b13d642138480a28c14db7a136bf0Adam Langley						len, type,
350d9e397b599b13d642138480a28c14db7a136bf0Adam Langley					OBJ_obj2nid(ne->object)) ? 1 : 0;
351d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (len < 0) len=strlen((const char *)bytes);
352d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	i=ASN1_STRING_set(ne->value,bytes,len);
353d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (!i) return(0);
354d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (type != V_ASN1_UNDEF)
355d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		{
356d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		if (type == V_ASN1_APP_CHOOSE)
357d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			ne->value->type=ASN1_PRINTABLE_type(bytes,len);
358d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		else
359d9e397b599b13d642138480a28c14db7a136bf0Adam Langley			ne->value->type=type;
360d9e397b599b13d642138480a28c14db7a136bf0Adam Langley		}
361d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return(1);
362d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	}
363d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
364d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyASN1_OBJECT *X509_NAME_ENTRY_get_object(X509_NAME_ENTRY *ne)
365d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	{
366d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (ne == NULL) return(NULL);
367d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return(ne->object);
368d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	}
369d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
370d9e397b599b13d642138480a28c14db7a136bf0Adam LangleyASN1_STRING *X509_NAME_ENTRY_get_data(X509_NAME_ENTRY *ne)
371d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	{
372d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	if (ne == NULL) return(NULL);
373d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	return(ne->value);
374d9e397b599b13d642138480a28c14db7a136bf0Adam Langley	}
375d9e397b599b13d642138480a28c14db7a136bf0Adam Langley
376