1 *** x509name.c Wed Jul 2 09:35:35 1997
2 --- /home/eay/play/x Sat Jul 5 01:39:56 1997
5 ! /* crypto/x509/x509name.c */
6 ! /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
7 ! * All rights reserved.
9 ! * This package is an SSL implementation written
10 ! * by Eric Young (eay@cryptsoft.com).
11 ! * The implementation was written so as to conform with Netscapes SSL.
13 ! * This library is free for commercial and non-commercial use as long as
14 ! * the following conditions are aheared to. The following conditions
15 ! * apply to all code found in this distribution, be it the RC4, RSA,
16 ! * lhash, DES, etc., code; not just the SSL code. The SSL documentation
17 ! * included with this distribution is covered by the same copyright terms
18 ! * except that the holder is Tim Hudson (tjh@cryptsoft.com).
20 ! * Copyright remains Eric Young's, and as such any Copyright notices in
21 ! * the code are not to be removed.
22 ! * If this package is used in a product, Eric Young should be given attribution
23 ! * as the author of the parts of the library used.
24 ! * This can be in the form of a textual message at program startup or
25 ! * in documentation (online or textual) provided with the package.
27 ! * Redistribution and use in source and binary forms, with or without
28 ! * modification, are permitted provided that the following conditions
30 ! * 1. Redistributions of source code must retain the copyright
31 ! * notice, this list of conditions and the following disclaimer.
32 ! * 2. Redistributions in binary form must reproduce the above copyright
33 ! * notice, this list of conditions and the following disclaimer in the
34 ! * documentation and/or other materials provided with the distribution.
35 ! * 3. All advertising materials mentioning features or use of this software
36 ! * must display the following acknowledgement:
37 ! * "This product includes cryptographic software written by
38 ! * Eric Young (eay@cryptsoft.com)"
39 ! * The word 'cryptographic' can be left out if the rouines from the library
40 ! * being used are not cryptographic related :-).
41 ! * 4. If you include any Windows specific code (or a derivative thereof) from
42 ! * the apps directory (application code) you must include an acknowledgement:
43 ! * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
45 ! * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
46 ! * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 ! * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 ! * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 ! * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 ! * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 ! * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 ! * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 ! * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 ! * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 ! * The licence and distribution terms for any publically available version or
58 ! * derivative of this code cannot be changed. i.e. this code cannot simply be
59 ! * copied and put under another distribution licence
60 ! * [including the GNU Public Licence.]
65 ! #include "cryptlib.h"
67 ! #include "objects.h"
71 ! int X509_NAME_get_text_by_NID(name,nid,buf,len)
80 ! if (obj == NULL) return(-1);
81 ! return(X509_NAME_get_text_by_OBJ(name,obj,buf,len));
84 - int X509_NAME_get_text_by_OBJ(name,obj,buf,len)
93 ! i=X509_NAME_get_index_by_OBJ(name,obj,0);
94 ! if (i < 0) return(-1);
95 ! data=X509_NAME_ENTRY_get_data(X509_NAME_get_entry(name,i));
96 ! i=(data->length > (len-1))?(len-1):data->length;
97 ! if (buf == NULL) return(data->length);
98 ! memcpy(buf,data->data,i);
103 ! int X509_NAME_entry_count(name)
106 ! if (name == NULL) return(0);
107 ! return(sk_num(name->entries));
110 ! int X509_NAME_get_index_by_NID(name,nid,oldpos)
117 ! obj=OBJ_nid2obj(nid);
118 ! if (obj == NULL) return(-2);
119 ! return(X509_NAME_get_index_by_OBJ(name,obj,oldpos));
122 - int X509_NAME_get_index_by_OBJ(name,obj,oldpos)
128 - X509_NAME_ENTRY *ne;
131 ! if (name == NULL) return(-1);
136 ! for (oldpos++; oldpos < n; oldpos++)
138 ! ne=(X509_NAME_ENTRY *)sk_value(sk,oldpos);
139 ! if (OBJ_cmp(ne->object,obj) == 0)
145 - X509_NAME_ENTRY *X509_NAME_get_entry(name,loc)
149 - if ( (name == NULL) || (sk_num(name->entries) <= loc) || (loc < 0))
152 - return((X509_NAME_ENTRY *)sk_value(name->entries,loc));
155 ! X509_NAME_ENTRY *X509_NAME_delete_entry(name,loc)
159 ! X509_NAME_ENTRY *ret;
160 ! int i,j,n,set_prev,set_next;
163 ! if ((name == NULL) || (sk_num(name->entries) <= loc) || (loc < 0))
166 ! ret=(X509_NAME_ENTRY *)sk_delete(sk,loc);
169 ! if (loc == n) return(ret);
171 ! /* else we need to fixup the set field */
173 ! set_prev=((X509_NAME_ENTRY *)sk_value(sk,loc-1))->set;
175 ! set_prev=ret->set-1;
176 ! set_next=((X509_NAME_ENTRY *)sk_value(sk,loc))->set;
178 ! /* set_prev is the previous set
179 ! * set is the current set
180 ! * set_next is the following
181 ! * prev 1 1 1 1 1 1 1 1
183 ! * next 1 1 2 2 2 2 3 2
184 ! * so basically only if prev and next differ by 2, then
185 ! * re-number down by 1 */
186 ! if (set_prev+1 < set_next)
188 ! j=set_next-set_prev-1;
189 ! for (i=loc; i<n; i++)
190 ! ((X509_NAME_ENTRY *)sk_value(sk,loc-1))->set-=j;
195 /* if set is -1, append to previous set, 0 'a new one', and 1,
196 * prepend to the guy we are about to stomp on. */
197 ! int X509_NAME_add_entry(name,ne,loc,set)
199 ! X509_NAME_ENTRY *ne;
203 ! X509_NAME_ENTRY *new_name=NULL;
208 ! X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(X509_NAME_ENTRY **ne, int nid,
209 ! int type,unsigned char *bytes, int len)
213 obj=OBJ_nid2obj(nid);
216 ! X509err(X509_F_X509_NAME_ENTRY_CREATE_BY_NID,X509_R_UNKNOWN_NID);
219 ! return(X509_NAME_ENTRY_create_by_OBJ(ne,obj,type,bytes,len));
223 ! X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(X509_NAME_ENTRY **ne,
224 ! ASN1_OBJECT *obj, int type,unsigned char *bytes,
227 ! X509_NAME_ENTRY *ret;
229 ! if ((ne == NULL) || (*ne == NULL))
231 ! if ((ret=X509_NAME_ENTRY_new()) == NULL)
237 ! if (!X509_NAME_ENTRY_set_object(ret,obj))
239 ! if (!X509_NAME_ENTRY_set_data(ret,type,bytes,len))
242 ! if ((ne != NULL) && (*ne == NULL)) *ne=ret;
245 ! if ((ne == NULL) || (ret != *ne))
246 ! X509_NAME_ENTRY_free(ret);
251 ! int X509_NAME_ENTRY_set_object(X509_NAME_ENTRY *ne, ASN1_OBJECT *obj)
253 ! if ((ne == NULL) || (obj == NULL))
255 ! X509err(X509_F_X509_NAME_ENTRY_SET_OBJECT,ERR_R_PASSED_NULL_PARAMETER);
258 ! ASN1_OBJECT_free(ne->object);
259 ! ne->object=OBJ_dup(obj);
260 ! return((ne->object == NULL)?0:1);
264 ! int X509_NAME_ENTRY_set_data(X509_NAME_ENTRY *ne,int type,unsigned char *bytes,int len)
268 ! if ((ne == NULL) || ((bytes == NULL) && (len != 0))) return(0);
269 ! if (len < 0) len=strlen((char *)bytes);
270 ! i=ASN1_STRING_set(ne->value,bytes,len);
272 ! ne->value->type=ASN1_PRINTABLE_type(bytes,len);
276 /* if set is -1, append to previous set, 0 'a new one', and 1,
277 * prepend to the guy we are about to stomp on. */
278 ! int X509_NAME_add_entry(X509_NAME *name,X509_NAME_ENTRY *ne,int loc,int set)
280 ! /* ERIC: renamed new to nenew for C++ users --tjh */
281 ! X509_NAME_ENTRY *nenew;
288 else if (loc < 0) loc=n;
299 else /* if (set >= 0) */
301 - inc=(set == 0)?1:0;
305 set=((X509_NAME_ENTRY *)
306 ! sk_value(sk,n-1))->set+1;
311 set=((X509_NAME_ENTRY *)sk_value(sk,loc))->set;
314 ! if ((new_name=X509_NAME_ENTRY_dup(ne)) == NULL)
317 ! if (!sk_insert(sk,(char *)new_name,loc))
319 X509err(X509_F_X509_NAME_ADD_ENTRY,ERR_R_MALLOC_FAILURE);
323 else /* if (set >= 0) */
328 set=((X509_NAME_ENTRY *)
329 ! sk_value(sk,loc-1))->set+1;
334 set=((X509_NAME_ENTRY *)sk_value(sk,loc))->set;
335 + inc=(set == 0)?1:0;
338 ! if ((nenew=X509_NAME_ENTRY_dup(ne)) == NULL)
340 ! /* eric forgot to put this in when he cut the nice
341 ! * interface so that I don't have to do the icky things
342 ! * that req.c does --tjh :-)
345 ! if (!sk_insert(sk,(char *)nenew,loc))
347 X509err(X509_F_X509_NAME_ADD_ENTRY,ERR_R_MALLOC_FAILURE);
354 ! if (new_name != NULL)
355 X509_NAME_ENTRY_free(ne);
359 - X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_NID(ne,nid,type,bytes,len)
360 - X509_NAME_ENTRY **ne;
363 - unsigned char *bytes;
368 - obj=OBJ_nid2obj(nid);
371 - X509err(X509_F_X509_NAME_ENTRY_CREATE_BY_NID,X509_R_UNKNOWN_NID);
374 - return(X509_NAME_ENTRY_create_by_OBJ(ne,obj,type,bytes,len));
377 - X509_NAME_ENTRY *X509_NAME_ENTRY_create_by_OBJ(ne,obj,type,bytes,len)
378 - X509_NAME_ENTRY **ne;
381 - unsigned char *bytes;
384 - X509_NAME_ENTRY *ret;
386 - if ((ne == NULL) || (*ne == NULL))
388 - if ((ret=X509_NAME_ENTRY_new()) == NULL)
394 - if (!X509_NAME_ENTRY_set_object(ret,obj))
396 - if (!X509_NAME_ENTRY_set_data(ret,type,bytes,len))
399 - if ((ne != NULL) && (*ne == NULL)) *ne=ret;
402 - if ((ne == NULL) || (ret != *ne))
403 - X509_NAME_ENTRY_free(ret);
407 - int X509_NAME_ENTRY_set_object(ne,obj)
408 - X509_NAME_ENTRY *ne;
411 - if ((ne == NULL) || (obj == NULL))
413 - X509err(X509_F_X509_NAME_ENTRY_SET_OBJECT,ERR_R_PASSED_NULL_PARAMETER);
416 - ASN1_OBJECT_free(ne->object);
417 - ne->object=OBJ_dup(obj);
418 - return((ne->object == NULL)?0:1);
421 - int X509_NAME_ENTRY_set_data(ne,type,bytes,len)
422 - X509_NAME_ENTRY *ne;
424 - unsigned char *bytes;
429 - if ((ne == NULL) || ((bytes == NULL) && (len != 0))) return(0);
430 - if (len < 0) len=strlen((char *)bytes);
431 - i=ASN1_STRING_set(ne->value,bytes,len);
433 - if (type != V_ASN1_UNDEF)
435 - if (type == V_ASN1_APP_CHOOSE)
436 - ne->value->type=ASN1_PRINTABLE_type(bytes,len);
438 - ne->value->type=type;
443 - ASN1_OBJECT *X509_NAME_ENTRY_get_object(ne)
444 - X509_NAME_ENTRY *ne;
446 - if (ne == NULL) return(NULL);
447 - return(ne->object);
450 - ASN1_STRING *X509_NAME_ENTRY_get_data(ne)
451 - X509_NAME_ENTRY *ne;
453 - if (ne == NULL) return(NULL);
462 X509_NAME_ENTRY_free(ne);