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