190075Sobrien/* crypto/x509/x509_att.c */
2169689Skan/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3169689Skan * All rights reserved.
490075Sobrien *
590075Sobrien * This package is an SSL implementation written
690075Sobrien * by Eric Young (eay@cryptsoft.com).
7132718Skan * The implementation was written so as to conform with Netscapes SSL.
890075Sobrien *
9132718Skan * This library is free for commercial and non-commercial use as long as
1090075Sobrien * the following conditions are aheared to.  The following conditions
1190075Sobrien * apply to all code found in this distribution, be it the RC4, RSA,
1290075Sobrien * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1390075Sobrien * included with this distribution is covered by the same copyright terms
14132718Skan * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1590075Sobrien *
1690075Sobrien * Copyright remains Eric Young's, and as such any Copyright notices in
1790075Sobrien * the code are not to be removed.
1890075Sobrien * If this package is used in a product, Eric Young should be given attribution
1990075Sobrien * as the author of the parts of the library used.
20132718Skan * This can be in the form of a textual message at program startup or
21169689Skan * in documentation (online or textual) provided with the package.
22169689Skan *
2390075Sobrien * Redistribution and use in source and binary forms, with or without
2490075Sobrien * modification, are permitted provided that the following conditions
2590075Sobrien * are met:
2690075Sobrien * 1. Redistributions of source code must retain the copyright
2790075Sobrien *    notice, this list of conditions and the following disclaimer.
2890075Sobrien * 2. Redistributions in binary form must reproduce the above copyright
2990075Sobrien *    notice, this list of conditions and the following disclaimer in the
3090075Sobrien *    documentation and/or other materials provided with the distribution.
3190075Sobrien * 3. All advertising materials mentioning features or use of this software
3290075Sobrien *    must display the following acknowledgement:
33117395Skan *    "This product includes cryptographic software written by
34117395Skan *     Eric Young (eay@cryptsoft.com)"
35117395Skan *    The word 'cryptographic' can be left out if the rouines from the library
36117395Skan *    being used are not cryptographic related :-).
37117395Skan * 4. If you include any Windows specific code (or a derivative thereof) from
38117395Skan *    the apps directory (application code) you must include an acknowledgement:
39117395Skan *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40117395Skan *
41117395Skan * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42117395Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43117395Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44117395Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45132718Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46132718Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47132718Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48132718Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4996263Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50132718Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51132718Skan * SUCH DAMAGE.
5290075Sobrien *
5390075Sobrien * The licence and distribution terms for any publically available version or
5490075Sobrien * derivative of this code cannot be changed.  i.e. this code cannot simply be
5596263Sobrien * copied and put under another distribution licence
5696263Sobrien * [including the GNU Public Licence.]
57169689Skan */
58169689Skan
59169689Skan#include <stdio.h>
6096263Sobrien#include <openssl/stack.h>
61132718Skan#include <openssl/local/cryptlib.h>
62132718Skan#include <openssl/asn1.h>
63132718Skan#include <openssl/objects.h>
64132718Skan#include <openssl/evp.h>
65132718Skan#include <openssl/x509.h>
66132718Skan#include <openssl/x509v3.h>
67132718Skan
68132718Skanint X509at_get_attr_count(const STACK_OF(X509_ATTRIBUTE) *x)
69132718Skan{
70117395Skan	return sk_X509_ATTRIBUTE_num(x);
71117395Skan}
72117395Skan
73117395Skanint X509at_get_attr_by_NID(const STACK_OF(X509_ATTRIBUTE) *x, int nid,
74132718Skan			  int lastpos)
75117395Skan{
76117395Skan	ASN1_OBJECT *obj;
77169689Skan
78169689Skan	obj=OBJ_nid2obj(nid);
79169689Skan	if (obj == NULL) return(-2);
80169689Skan	return(X509at_get_attr_by_OBJ(x,obj,lastpos));
81117395Skan}
82117395Skan
83117395Skanint X509at_get_attr_by_OBJ(const STACK_OF(X509_ATTRIBUTE) *sk, ASN1_OBJECT *obj,
84117395Skan			  int lastpos)
85169689Skan{
86169689Skan	int n;
8790075Sobrien	X509_ATTRIBUTE *ex;
88169689Skan
89169689Skan	if (sk == NULL) return(-1);
90169689Skan	lastpos++;
91169689Skan	if (lastpos < 0)
92169689Skan		lastpos=0;
93169689Skan	n=sk_X509_ATTRIBUTE_num(sk);
9490075Sobrien	for ( ; lastpos < n; lastpos++)
9590075Sobrien		{
9690075Sobrien		ex=sk_X509_ATTRIBUTE_value(sk,lastpos);
9790075Sobrien		if (OBJ_cmp(ex->object,obj) == 0)
98169689Skan			return(lastpos);
9990075Sobrien		}
10090075Sobrien	return(-1);
10190075Sobrien}
10290075Sobrien
10390075SobrienX509_ATTRIBUTE *X509at_get_attr(const STACK_OF(X509_ATTRIBUTE) *x, int loc)
10490075Sobrien{
105132718Skan	if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
106169689Skan		return NULL;
107132718Skan	else
108132718Skan		return sk_X509_ATTRIBUTE_value(x,loc);
109132718Skan}
110132718Skan
111169689SkanX509_ATTRIBUTE *X509at_delete_attr(STACK_OF(X509_ATTRIBUTE) *x, int loc)
112132718Skan{
113132718Skan	X509_ATTRIBUTE *ret;
114132718Skan
115132718Skan	if (x == NULL || sk_X509_ATTRIBUTE_num(x) <= loc || loc < 0)
116132718Skan		return(NULL);
117132718Skan	ret=sk_X509_ATTRIBUTE_delete(x,loc);
11890075Sobrien	return(ret);
11990075Sobrien}
12090075Sobrien
12190075SobrienSTACK_OF(X509_ATTRIBUTE) *X509at_add1_attr(STACK_OF(X509_ATTRIBUTE) **x,
12290075Sobrien					 X509_ATTRIBUTE *attr)
12390075Sobrien{
12490075Sobrien	X509_ATTRIBUTE *new_attr=NULL;
12590075Sobrien	STACK_OF(X509_ATTRIBUTE) *sk=NULL;
12690075Sobrien
12790075Sobrien	if (x == NULL)
12890075Sobrien		{
12990075Sobrien		X509err(X509_F_X509AT_ADD1_ATTR, ERR_R_PASSED_NULL_PARAMETER);
13090075Sobrien		goto err2;
13190075Sobrien		}
13290075Sobrien
13390075Sobrien	if (*x == NULL)
13490075Sobrien		{
135132718Skan		if ((sk=sk_X509_ATTRIBUTE_new_null()) == NULL)
136132718Skan			goto err;
137132718Skan		}
13890075Sobrien	else
13990075Sobrien		sk= *x;
14090075Sobrien
14190075Sobrien	if ((new_attr=X509_ATTRIBUTE_dup(attr)) == NULL)
142132718Skan		goto err2;
143132718Skan	if (!sk_X509_ATTRIBUTE_push(sk,new_attr))
144132718Skan		goto err;
14590075Sobrien	if (*x == NULL)
14690075Sobrien		*x=sk;
14790075Sobrien	return(sk);
14890075Sobrienerr:
14990075Sobrien	X509err(X509_F_X509AT_ADD1_ATTR,ERR_R_MALLOC_FAILURE);
15090075Sobrienerr2:
15190075Sobrien	if (new_attr != NULL) X509_ATTRIBUTE_free(new_attr);
15290075Sobrien	if (sk != NULL) sk_X509_ATTRIBUTE_free(sk);
15390075Sobrien	return(NULL);
15490075Sobrien}
15590075Sobrien
15690075SobrienSTACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_OBJ(STACK_OF(X509_ATTRIBUTE) **x,
15790075Sobrien			const ASN1_OBJECT *obj, int type,
15890075Sobrien			const unsigned char *bytes, int len)
15990075Sobrien{
16090075Sobrien	X509_ATTRIBUTE *attr;
16190075Sobrien	STACK_OF(X509_ATTRIBUTE) *ret;
16290075Sobrien	attr = X509_ATTRIBUTE_create_by_OBJ(NULL, obj, type, bytes, len);
16390075Sobrien	if(!attr) return 0;
16490075Sobrien	ret = X509at_add1_attr(x, attr);
16590075Sobrien	X509_ATTRIBUTE_free(attr);
16690075Sobrien	return ret;
16790075Sobrien}
16890075Sobrien
16990075SobrienSTACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_NID(STACK_OF(X509_ATTRIBUTE) **x,
17090075Sobrien			int nid, int type,
17190075Sobrien			const unsigned char *bytes, int len)
17290075Sobrien{
17390075Sobrien	X509_ATTRIBUTE *attr;
174132718Skan	STACK_OF(X509_ATTRIBUTE) *ret;
17590075Sobrien	attr = X509_ATTRIBUTE_create_by_NID(NULL, nid, type, bytes, len);
17690075Sobrien	if(!attr) return 0;
17790075Sobrien	ret = X509at_add1_attr(x, attr);
17890075Sobrien	X509_ATTRIBUTE_free(attr);
17990075Sobrien	return ret;
18090075Sobrien}
18190075Sobrien
18290075SobrienSTACK_OF(X509_ATTRIBUTE) *X509at_add1_attr_by_txt(STACK_OF(X509_ATTRIBUTE) **x,
18390075Sobrien			const char *attrname, int type,
18490075Sobrien			const unsigned char *bytes, int len)
18590075Sobrien{
18690075Sobrien	X509_ATTRIBUTE *attr;
18790075Sobrien	STACK_OF(X509_ATTRIBUTE) *ret;
18890075Sobrien	attr = X509_ATTRIBUTE_create_by_txt(NULL, attrname, type, bytes, len);
18990075Sobrien	if(!attr) return 0;
19090075Sobrien	ret = X509at_add1_attr(x, attr);
19190075Sobrien	X509_ATTRIBUTE_free(attr);
19290075Sobrien	return ret;
19390075Sobrien}
19490075Sobrien
19590075Sobrienvoid *X509at_get0_data_by_OBJ(STACK_OF(X509_ATTRIBUTE) *x,
19690075Sobrien				ASN1_OBJECT *obj, int lastpos, int type)
19790075Sobrien{
19890075Sobrien	int i;
19990075Sobrien	X509_ATTRIBUTE *at;
20090075Sobrien	i = X509at_get_attr_by_OBJ(x, obj, lastpos);
20190075Sobrien	if (i == -1)
20290075Sobrien		return NULL;
20390075Sobrien	if ((lastpos <= -2) && (X509at_get_attr_by_OBJ(x, obj, i) != -1))
20490075Sobrien		return NULL;
20590075Sobrien	at = X509at_get_attr(x, i);
20690075Sobrien	if (lastpos <= -3 && (X509_ATTRIBUTE_count(at) != 1))
20790075Sobrien		return NULL;
20890075Sobrien	return X509_ATTRIBUTE_get0_data(at, 0, type, NULL);
20990075Sobrien}
21090075Sobrien
21190075SobrienX509_ATTRIBUTE *X509_ATTRIBUTE_create_by_NID(X509_ATTRIBUTE **attr, int nid,
21290075Sobrien	     int atrtype, const void *data, int len)
21390075Sobrien{
21490075Sobrien	ASN1_OBJECT *obj;
21590075Sobrien	X509_ATTRIBUTE *ret;
21690075Sobrien
21790075Sobrien	obj=OBJ_nid2obj(nid);
21890075Sobrien	if (obj == NULL)
21990075Sobrien		{
22090075Sobrien		X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_NID,X509_R_UNKNOWN_NID);
22190075Sobrien		return(NULL);
22290075Sobrien		}
22390075Sobrien	ret=X509_ATTRIBUTE_create_by_OBJ(attr,obj,atrtype,data,len);
22490075Sobrien	if (ret == NULL) ASN1_OBJECT_free(obj);
22590075Sobrien	return(ret);
22690075Sobrien}
22790075Sobrien
22890075SobrienX509_ATTRIBUTE *X509_ATTRIBUTE_create_by_OBJ(X509_ATTRIBUTE **attr,
22990075Sobrien	     const ASN1_OBJECT *obj, int atrtype, const void *data, int len)
23090075Sobrien{
23190075Sobrien	X509_ATTRIBUTE *ret;
23290075Sobrien
23390075Sobrien	if ((attr == NULL) || (*attr == NULL))
23490075Sobrien		{
235117395Skan		if ((ret=X509_ATTRIBUTE_new()) == NULL)
23690075Sobrien			{
23790075Sobrien			X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_OBJ,ERR_R_MALLOC_FAILURE);
238117395Skan			return(NULL);
23990075Sobrien			}
24090075Sobrien		}
24190075Sobrien	else
24290075Sobrien		ret= *attr;
24390075Sobrien
24490075Sobrien	if (!X509_ATTRIBUTE_set1_object(ret,obj))
24590075Sobrien		goto err;
24690075Sobrien	if (!X509_ATTRIBUTE_set1_data(ret,atrtype,data,len))
24790075Sobrien		goto err;
24890075Sobrien
249117395Skan	if ((attr != NULL) && (*attr == NULL)) *attr=ret;
25090075Sobrien	return(ret);
25196263Sobrienerr:
25290075Sobrien	if ((attr == NULL) || (ret != *attr))
253117395Skan		X509_ATTRIBUTE_free(ret);
254117395Skan	return(NULL);
255117395Skan}
256117395Skan
257117395SkanX509_ATTRIBUTE *X509_ATTRIBUTE_create_by_txt(X509_ATTRIBUTE **attr,
258117395Skan		const char *atrname, int type, const unsigned char *bytes, int len)
259117395Skan	{
260117395Skan	ASN1_OBJECT *obj;
261117395Skan	X509_ATTRIBUTE *nattr;
262117395Skan
263117395Skan	obj=OBJ_txt2obj(atrname, 0);
264117395Skan	if (obj == NULL)
265117395Skan		{
266117395Skan		X509err(X509_F_X509_ATTRIBUTE_CREATE_BY_TXT,
267117395Skan						X509_R_INVALID_FIELD_NAME);
26890075Sobrien		ERR_add_error_data(2, "name=", atrname);
26990075Sobrien		return(NULL);
27090075Sobrien		}
27190075Sobrien	nattr = X509_ATTRIBUTE_create_by_OBJ(attr,obj,type,bytes,len);
27290075Sobrien	ASN1_OBJECT_free(obj);
27390075Sobrien	return nattr;
27490075Sobrien	}
27590075Sobrien
27690075Sobrienint X509_ATTRIBUTE_set1_object(X509_ATTRIBUTE *attr, const ASN1_OBJECT *obj)
27790075Sobrien{
27890075Sobrien	if ((attr == NULL) || (obj == NULL))
27990075Sobrien		return(0);
28090075Sobrien	ASN1_OBJECT_free(attr->object);
28190075Sobrien	attr->object=OBJ_dup(obj);
28290075Sobrien	return(1);
283132718Skan}
284169689Skan
28590075Sobrienint X509_ATTRIBUTE_set1_data(X509_ATTRIBUTE *attr, int attrtype, const void *data, int len)
286132718Skan{
287169689Skan	ASN1_TYPE *ttmp;
28890075Sobrien	ASN1_STRING *stmp = NULL;
28990075Sobrien	int atype = 0;
29090075Sobrien	if (!attr) return 0;
29190075Sobrien	if(attrtype & MBSTRING_FLAG) {
29290075Sobrien		stmp = ASN1_STRING_set_by_NID(NULL, data, len, attrtype,
29390075Sobrien						OBJ_obj2nid(attr->object));
29490075Sobrien		if(!stmp) {
29590075Sobrien			X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_ASN1_LIB);
29690075Sobrien			return 0;
29790075Sobrien		}
29890075Sobrien		atype = stmp->type;
29990075Sobrien	} else if (len != -1){
30090075Sobrien		if(!(stmp = ASN1_STRING_type_new(attrtype))) goto err;
30190075Sobrien		if(!ASN1_STRING_set(stmp, data, len)) goto err;
30290075Sobrien		atype = attrtype;
30390075Sobrien	}
30490075Sobrien	if(!(attr->value.set = sk_ASN1_TYPE_new_null())) goto err;
30590075Sobrien	attr->single = 0;
30690075Sobrien	/* This is a bit naughty because the attribute should really have
30790075Sobrien	 * at least one value but some types use and zero length SET and
30890075Sobrien	 * require this.
30990075Sobrien	 */
31090075Sobrien	if (attrtype == 0)
31190075Sobrien		return 1;
31290075Sobrien	if(!(ttmp = ASN1_TYPE_new())) goto err;
31390075Sobrien	if ((len == -1) && !(attrtype & MBSTRING_FLAG))
31490075Sobrien		{
31590075Sobrien		if (!ASN1_TYPE_set1(ttmp, attrtype, data))
31696263Sobrien			goto err;
31790075Sobrien		}
31890075Sobrien	else
31990075Sobrien		ASN1_TYPE_set(ttmp, atype, stmp);
32090075Sobrien	if(!sk_ASN1_TYPE_push(attr->value.set, ttmp)) goto err;
321122180Skan	return 1;
32290075Sobrien	err:
32390075Sobrien	X509err(X509_F_X509_ATTRIBUTE_SET1_DATA, ERR_R_MALLOC_FAILURE);
32490075Sobrien	return 0;
32590075Sobrien}
32690075Sobrien
327169689Skanint X509_ATTRIBUTE_count(X509_ATTRIBUTE *attr)
32890075Sobrien{
32990075Sobrien	if(!attr->single) return sk_ASN1_TYPE_num(attr->value.set);
33090075Sobrien	if(attr->value.single) return 1;
331122180Skan	return 0;
33290075Sobrien}
33390075Sobrien
33490075SobrienASN1_OBJECT *X509_ATTRIBUTE_get0_object(X509_ATTRIBUTE *attr)
33590075Sobrien{
33690075Sobrien	if (attr == NULL) return(NULL);
33790075Sobrien	return(attr->object);
33890075Sobrien}
33990075Sobrien
34090075Sobrienvoid *X509_ATTRIBUTE_get0_data(X509_ATTRIBUTE *attr, int idx,
341122180Skan					int atrtype, void *data)
342122180Skan{
343122180Skan	ASN1_TYPE *ttmp;
344122180Skan	ttmp = X509_ATTRIBUTE_get0_type(attr, idx);
345122180Skan	if(!ttmp) return NULL;
34690075Sobrien	if(atrtype != ASN1_TYPE_get(ttmp)){
34790075Sobrien		X509err(X509_F_X509_ATTRIBUTE_GET0_DATA, X509_R_WRONG_TYPE);
34890075Sobrien		return NULL;
34990075Sobrien	}
35090075Sobrien	return ttmp->value.ptr;
35190075Sobrien}
35290075Sobrien
35390075SobrienASN1_TYPE *X509_ATTRIBUTE_get0_type(X509_ATTRIBUTE *attr, int idx)
35490075Sobrien{
35590075Sobrien	if (attr == NULL) return(NULL);
35690075Sobrien	if(idx >= X509_ATTRIBUTE_count(attr)) return NULL;
35790075Sobrien	if(!attr->single) return sk_ASN1_TYPE_value(attr->value.set, idx);
35890075Sobrien	else return attr->value.single;
35990075Sobrien}
36090075Sobrien