os/ossrv/ssl/libcrypto/src/crypto/x509/x509_v3.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/x509_v3.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
#include <openssl/x509v3.h>
sl@0
    67
sl@0
    68
EXPORT_C int X509v3_get_ext_count(const STACK_OF(X509_EXTENSION) *x)
sl@0
    69
	{
sl@0
    70
	if (x == NULL) return(0);
sl@0
    71
	return(sk_X509_EXTENSION_num(x));
sl@0
    72
	}
sl@0
    73
sl@0
    74
EXPORT_C int X509v3_get_ext_by_NID(const STACK_OF(X509_EXTENSION) *x, int nid,
sl@0
    75
			  int lastpos)
sl@0
    76
	{
sl@0
    77
	ASN1_OBJECT *obj;
sl@0
    78
sl@0
    79
	obj=OBJ_nid2obj(nid);
sl@0
    80
	if (obj == NULL) return(-2);
sl@0
    81
	return(X509v3_get_ext_by_OBJ(x,obj,lastpos));
sl@0
    82
	}
sl@0
    83
sl@0
    84
EXPORT_C int X509v3_get_ext_by_OBJ(const STACK_OF(X509_EXTENSION) *sk, ASN1_OBJECT *obj,
sl@0
    85
			  int lastpos)
sl@0
    86
	{
sl@0
    87
	int n;
sl@0
    88
	X509_EXTENSION *ex;
sl@0
    89
sl@0
    90
	if (sk == NULL) return(-1);
sl@0
    91
	lastpos++;
sl@0
    92
	if (lastpos < 0)
sl@0
    93
		lastpos=0;
sl@0
    94
	n=sk_X509_EXTENSION_num(sk);
sl@0
    95
	for ( ; lastpos < n; lastpos++)
sl@0
    96
		{
sl@0
    97
		ex=sk_X509_EXTENSION_value(sk,lastpos);
sl@0
    98
		if (OBJ_cmp(ex->object,obj) == 0)
sl@0
    99
			return(lastpos);
sl@0
   100
		}
sl@0
   101
	return(-1);
sl@0
   102
	}
sl@0
   103
sl@0
   104
EXPORT_C int X509v3_get_ext_by_critical(const STACK_OF(X509_EXTENSION) *sk, int crit,
sl@0
   105
			       int lastpos)
sl@0
   106
	{
sl@0
   107
	int n;
sl@0
   108
	X509_EXTENSION *ex;
sl@0
   109
sl@0
   110
	if (sk == NULL) return(-1);
sl@0
   111
	lastpos++;
sl@0
   112
	if (lastpos < 0)
sl@0
   113
		lastpos=0;
sl@0
   114
	n=sk_X509_EXTENSION_num(sk);
sl@0
   115
	for ( ; lastpos < n; lastpos++)
sl@0
   116
		{
sl@0
   117
		ex=sk_X509_EXTENSION_value(sk,lastpos);
sl@0
   118
		if (	((ex->critical > 0) && crit) ||
sl@0
   119
			((ex->critical <= 0) && !crit))
sl@0
   120
			return(lastpos);
sl@0
   121
		}
sl@0
   122
	return(-1);
sl@0
   123
	}
sl@0
   124
sl@0
   125
EXPORT_C X509_EXTENSION *X509v3_get_ext(const STACK_OF(X509_EXTENSION) *x, int loc)
sl@0
   126
	{
sl@0
   127
	if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
sl@0
   128
		return NULL;
sl@0
   129
	else
sl@0
   130
		return sk_X509_EXTENSION_value(x,loc);
sl@0
   131
	}
sl@0
   132
sl@0
   133
EXPORT_C X509_EXTENSION *X509v3_delete_ext(STACK_OF(X509_EXTENSION) *x, int loc)
sl@0
   134
	{
sl@0
   135
	X509_EXTENSION *ret;
sl@0
   136
sl@0
   137
	if (x == NULL || sk_X509_EXTENSION_num(x) <= loc || loc < 0)
sl@0
   138
		return(NULL);
sl@0
   139
	ret=sk_X509_EXTENSION_delete(x,loc);
sl@0
   140
	return(ret);
sl@0
   141
	}
sl@0
   142
sl@0
   143
EXPORT_C STACK_OF(X509_EXTENSION) *X509v3_add_ext(STACK_OF(X509_EXTENSION) **x,
sl@0
   144
					 X509_EXTENSION *ex, int loc)
sl@0
   145
	{
sl@0
   146
	X509_EXTENSION *new_ex=NULL;
sl@0
   147
	int n;
sl@0
   148
	STACK_OF(X509_EXTENSION) *sk=NULL;
sl@0
   149
sl@0
   150
	if (x == NULL)
sl@0
   151
		{
sl@0
   152
		X509err(X509_F_X509V3_ADD_EXT,ERR_R_PASSED_NULL_PARAMETER);
sl@0
   153
		goto err2;
sl@0
   154
		}
sl@0
   155
sl@0
   156
	if (*x == NULL)
sl@0
   157
		{
sl@0
   158
		if ((sk=sk_X509_EXTENSION_new_null()) == NULL)
sl@0
   159
			goto err;
sl@0
   160
		}
sl@0
   161
	else
sl@0
   162
		sk= *x;
sl@0
   163
sl@0
   164
	n=sk_X509_EXTENSION_num(sk);
sl@0
   165
	if (loc > n) loc=n;
sl@0
   166
	else if (loc < 0) loc=n;
sl@0
   167
sl@0
   168
	if ((new_ex=X509_EXTENSION_dup(ex)) == NULL)
sl@0
   169
		goto err2;
sl@0
   170
	if (!sk_X509_EXTENSION_insert(sk,new_ex,loc))
sl@0
   171
		goto err;
sl@0
   172
	if (*x == NULL)
sl@0
   173
		*x=sk;
sl@0
   174
	return(sk);
sl@0
   175
err:
sl@0
   176
	X509err(X509_F_X509V3_ADD_EXT,ERR_R_MALLOC_FAILURE);
sl@0
   177
err2:
sl@0
   178
	if (new_ex != NULL) X509_EXTENSION_free(new_ex);
sl@0
   179
	if (sk != NULL) sk_X509_EXTENSION_free(sk);
sl@0
   180
	return(NULL);
sl@0
   181
	}
sl@0
   182
sl@0
   183
EXPORT_C X509_EXTENSION *X509_EXTENSION_create_by_NID(X509_EXTENSION **ex, int nid,
sl@0
   184
	     int crit, ASN1_OCTET_STRING *data)
sl@0
   185
	{
sl@0
   186
	ASN1_OBJECT *obj;
sl@0
   187
	X509_EXTENSION *ret;
sl@0
   188
sl@0
   189
	obj=OBJ_nid2obj(nid);
sl@0
   190
	if (obj == NULL)
sl@0
   191
		{
sl@0
   192
		X509err(X509_F_X509_EXTENSION_CREATE_BY_NID,X509_R_UNKNOWN_NID);
sl@0
   193
		return(NULL);
sl@0
   194
		}
sl@0
   195
	ret=X509_EXTENSION_create_by_OBJ(ex,obj,crit,data);
sl@0
   196
	if (ret == NULL) ASN1_OBJECT_free(obj);
sl@0
   197
	return(ret);
sl@0
   198
	}
sl@0
   199
sl@0
   200
EXPORT_C X509_EXTENSION *X509_EXTENSION_create_by_OBJ(X509_EXTENSION **ex,
sl@0
   201
	     ASN1_OBJECT *obj, int crit, ASN1_OCTET_STRING *data)
sl@0
   202
	{
sl@0
   203
	X509_EXTENSION *ret;
sl@0
   204
sl@0
   205
	if ((ex == NULL) || (*ex == NULL))
sl@0
   206
		{
sl@0
   207
		if ((ret=X509_EXTENSION_new()) == NULL)
sl@0
   208
			{
sl@0
   209
			X509err(X509_F_X509_EXTENSION_CREATE_BY_OBJ,ERR_R_MALLOC_FAILURE);
sl@0
   210
			return(NULL);
sl@0
   211
			}
sl@0
   212
		}
sl@0
   213
	else
sl@0
   214
		ret= *ex;
sl@0
   215
sl@0
   216
	if (!X509_EXTENSION_set_object(ret,obj))
sl@0
   217
		goto err;
sl@0
   218
	if (!X509_EXTENSION_set_critical(ret,crit))
sl@0
   219
		goto err;
sl@0
   220
	if (!X509_EXTENSION_set_data(ret,data))
sl@0
   221
		goto err;
sl@0
   222
	
sl@0
   223
	if ((ex != NULL) && (*ex == NULL)) *ex=ret;
sl@0
   224
	return(ret);
sl@0
   225
err:
sl@0
   226
	if ((ex == NULL) || (ret != *ex))
sl@0
   227
		X509_EXTENSION_free(ret);
sl@0
   228
	return(NULL);
sl@0
   229
	}
sl@0
   230
sl@0
   231
EXPORT_C int X509_EXTENSION_set_object(X509_EXTENSION *ex, ASN1_OBJECT *obj)
sl@0
   232
	{
sl@0
   233
	if ((ex == NULL) || (obj == NULL))
sl@0
   234
		return(0);
sl@0
   235
	ASN1_OBJECT_free(ex->object);
sl@0
   236
	ex->object=OBJ_dup(obj);
sl@0
   237
	return(1);
sl@0
   238
	}
sl@0
   239
sl@0
   240
EXPORT_C int X509_EXTENSION_set_critical(X509_EXTENSION *ex, int crit)
sl@0
   241
	{
sl@0
   242
	if (ex == NULL) return(0);
sl@0
   243
	ex->critical=(crit)?0xFF:-1;
sl@0
   244
	return(1);
sl@0
   245
	}
sl@0
   246
sl@0
   247
EXPORT_C int X509_EXTENSION_set_data(X509_EXTENSION *ex, ASN1_OCTET_STRING *data)
sl@0
   248
	{
sl@0
   249
	int i;
sl@0
   250
sl@0
   251
	if (ex == NULL) return(0);
sl@0
   252
	i=M_ASN1_OCTET_STRING_set(ex->value,data->data,data->length);
sl@0
   253
	if (!i) return(0);
sl@0
   254
	return(1);
sl@0
   255
	}
sl@0
   256
sl@0
   257
EXPORT_C ASN1_OBJECT *X509_EXTENSION_get_object(X509_EXTENSION *ex)
sl@0
   258
	{
sl@0
   259
	if (ex == NULL) return(NULL);
sl@0
   260
	return(ex->object);
sl@0
   261
	}
sl@0
   262
sl@0
   263
EXPORT_C ASN1_OCTET_STRING *X509_EXTENSION_get_data(X509_EXTENSION *ex)
sl@0
   264
	{
sl@0
   265
	if (ex == NULL) return(NULL);
sl@0
   266
	return(ex->value);
sl@0
   267
	}
sl@0
   268
sl@0
   269
EXPORT_C int X509_EXTENSION_get_critical(X509_EXTENSION *ex)
sl@0
   270
	{
sl@0
   271
	if (ex == NULL) return(0);
sl@0
   272
	if(ex->critical > 0) return 1;
sl@0
   273
	return 0;
sl@0
   274
	}