x_attrib.c revision 256281
1171802Sdelphij/* crypto/asn1/x_attrib.c */
2170808Sdelphij/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3182739Sdelphij * All rights reserved.
4171802Sdelphij *
5170808Sdelphij * This package is an SSL implementation written
6170808Sdelphij * by Eric Young (eay@cryptsoft.com).
7170808Sdelphij * The implementation was written so as to conform with Netscapes SSL.
8170808Sdelphij *
9170808Sdelphij * This library is free for commercial and non-commercial use as long as
10170808Sdelphij * the following conditions are aheared to.  The following conditions
11170808Sdelphij * apply to all code found in this distribution, be it the RC4, RSA,
12170808Sdelphij * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13170808Sdelphij * included with this distribution is covered by the same copyright terms
14170808Sdelphij * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15170808Sdelphij *
16170808Sdelphij * Copyright remains Eric Young's, and as such any Copyright notices in
17170808Sdelphij * the code are not to be removed.
18170808Sdelphij * If this package is used in a product, Eric Young should be given attribution
19170808Sdelphij * as the author of the parts of the library used.
20170808Sdelphij * This can be in the form of a textual message at program startup or
21170808Sdelphij * in documentation (online or textual) provided with the package.
22170808Sdelphij *
23170808Sdelphij * Redistribution and use in source and binary forms, with or without
24170808Sdelphij * modification, are permitted provided that the following conditions
25170808Sdelphij * are met:
26170808Sdelphij * 1. Redistributions of source code must retain the copyright
27170808Sdelphij *    notice, this list of conditions and the following disclaimer.
28170808Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
29170808Sdelphij *    notice, this list of conditions and the following disclaimer in the
30170808Sdelphij *    documentation and/or other materials provided with the distribution.
31170808Sdelphij * 3. All advertising materials mentioning features or use of this software
32170808Sdelphij *    must display the following acknowledgement:
33170808Sdelphij *    "This product includes cryptographic software written by
34170808Sdelphij *     Eric Young (eay@cryptsoft.com)"
35170808Sdelphij *    The word 'cryptographic' can be left out if the rouines from the library
36170808Sdelphij *    being used are not cryptographic related :-).
37170808Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from
38170808Sdelphij *    the apps directory (application code) you must include an acknowledgement:
39170808Sdelphij *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40170808Sdelphij *
41170808Sdelphij * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42170808Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43170808Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44170808Sdelphij * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45170808Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46170808Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47170808Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48170808Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49170808Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50170808Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51170808Sdelphij * SUCH DAMAGE.
52170808Sdelphij *
53170808Sdelphij * The licence and distribution terms for any publically available version or
54170808Sdelphij * derivative of this code cannot be changed.  i.e. this code cannot simply be
55188929Salc * copied and put under another distribution licence
56170808Sdelphij * [including the GNU Public Licence.]
57170808Sdelphij */
58170808Sdelphij
59170808Sdelphij#include <stdio.h>
60170808Sdelphij#include "cryptlib.h"
61170808Sdelphij#include <openssl/objects.h>
62170808Sdelphij#include <openssl/asn1t.h>
63170808Sdelphij#include <openssl/x509.h>
64171069Sdelphij
65170808Sdelphij/* X509_ATTRIBUTE: this has the following form:
66170808Sdelphij *
67170808Sdelphij * typedef struct x509_attributes_st
68170808Sdelphij *	{
69170808Sdelphij *	ASN1_OBJECT *object;
70170808Sdelphij *	int single;
71170808Sdelphij *	union	{
72170808Sdelphij *		char		*ptr;
73170808Sdelphij * 		STACK_OF(ASN1_TYPE) *set;
74170808Sdelphij * 		ASN1_TYPE	*single;
75170808Sdelphij *		} value;
76170808Sdelphij *	} X509_ATTRIBUTE;
77170808Sdelphij *
78170808Sdelphij * this needs some extra thought because the CHOICE type is
79191990Sattilio * merged with the main structure and because the value can
80170808Sdelphij * be anything at all we *must* try the SET OF first because
81170808Sdelphij * the ASN1_ANY type will swallow anything including the whole
82170808Sdelphij * SET OF structure.
83170808Sdelphij */
84170808Sdelphij
85170808SdelphijASN1_CHOICE(X509_ATTRIBUTE_SET) = {
86170808Sdelphij	ASN1_SET_OF(X509_ATTRIBUTE, value.set, ASN1_ANY),
87170808Sdelphij	ASN1_SIMPLE(X509_ATTRIBUTE, value.single, ASN1_ANY)
88170808Sdelphij} ASN1_CHOICE_END_selector(X509_ATTRIBUTE, X509_ATTRIBUTE_SET, single)
89171799Sdelphij
90171799SdelphijASN1_SEQUENCE(X509_ATTRIBUTE) = {
91176559Sattilio	ASN1_SIMPLE(X509_ATTRIBUTE, object, ASN1_OBJECT),
92171802Sdelphij	/* CHOICE type merged with parent */
93175294Sattilio	ASN1_EX_COMBINE(0, 0, X509_ATTRIBUTE_SET)
94170808Sdelphij} ASN1_SEQUENCE_END(X509_ATTRIBUTE)
95171799Sdelphij
96191990SattilioIMPLEMENT_ASN1_FUNCTIONS(X509_ATTRIBUTE)
97170808SdelphijIMPLEMENT_ASN1_DUP_FUNCTION(X509_ATTRIBUTE)
98175202Sattilio
99171802SdelphijX509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value)
100170808Sdelphij	{
101170808Sdelphij	X509_ATTRIBUTE *ret=NULL;
102170808Sdelphij	ASN1_TYPE *val=NULL;
103170808Sdelphij
104170808Sdelphij	if ((ret=X509_ATTRIBUTE_new()) == NULL)
105188318Skib		return(NULL);
106170808Sdelphij	ret->object=OBJ_nid2obj(nid);
107170808Sdelphij	ret->single=0;
108170808Sdelphij	if ((ret->value.set=sk_ASN1_TYPE_new_null()) == NULL) goto err;
109170808Sdelphij	if ((val=ASN1_TYPE_new()) == NULL) goto err;
110170808Sdelphij	if (!sk_ASN1_TYPE_push(ret->value.set,val)) goto err;
111170808Sdelphij
112170808Sdelphij	ASN1_TYPE_set(val,atrtype,value);
113170808Sdelphij	return(ret);
114170808Sdelphijerr:
115170808Sdelphij	if (ret != NULL) X509_ATTRIBUTE_free(ret);
116170808Sdelphij	if (val != NULL) ASN1_TYPE_free(val);
117170808Sdelphij	return(NULL);
118170808Sdelphij	}
119170808Sdelphij