x_attrib.c revision 296341
141502Swpaul/* crypto/asn1/x_attrib.c */
241502Swpaul/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
341502Swpaul * All rights reserved.
441502Swpaul *
541502Swpaul * This package is an SSL implementation written
641502Swpaul * by Eric Young (eay@cryptsoft.com).
741502Swpaul * The implementation was written so as to conform with Netscapes SSL.
841502Swpaul *
941502Swpaul * This library is free for commercial and non-commercial use as long as
1041502Swpaul * the following conditions are aheared to.  The following conditions
1141502Swpaul * apply to all code found in this distribution, be it the RC4, RSA,
1241502Swpaul * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1341502Swpaul * included with this distribution is covered by the same copyright terms
1441502Swpaul * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1541502Swpaul *
1641502Swpaul * Copyright remains Eric Young's, and as such any Copyright notices in
1741502Swpaul * the code are not to be removed.
1841502Swpaul * If this package is used in a product, Eric Young should be given attribution
1941502Swpaul * as the author of the parts of the library used.
2041502Swpaul * This can be in the form of a textual message at program startup or
2141502Swpaul * in documentation (online or textual) provided with the package.
2241502Swpaul *
2341502Swpaul * Redistribution and use in source and binary forms, with or without
2441502Swpaul * modification, are permitted provided that the following conditions
2541502Swpaul * are met:
2641502Swpaul * 1. Redistributions of source code must retain the copyright
2741502Swpaul *    notice, this list of conditions and the following disclaimer.
2841502Swpaul * 2. Redistributions in binary form must reproduce the above copyright
2941502Swpaul *    notice, this list of conditions and the following disclaimer in the
3041502Swpaul *    documentation and/or other materials provided with the distribution.
3141502Swpaul * 3. All advertising materials mentioning features or use of this software
3241502Swpaul *    must display the following acknowledgement:
33122678Sobrien *    "This product includes cryptographic software written by
34122678Sobrien *     Eric Young (eay@cryptsoft.com)"
35122678Sobrien *    The word 'cryptographic' can be left out if the rouines from the library
3641502Swpaul *    being used are not cryptographic related :-).
3741502Swpaul * 4. If you include any Windows specific code (or a derivative thereof) from
3841502Swpaul *    the apps directory (application code) you must include an acknowledgement:
3941502Swpaul *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4041502Swpaul *
4141502Swpaul * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4241502Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4341502Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4441502Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4541502Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4641502Swpaul * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4741502Swpaul * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4841502Swpaul * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4941502Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5041502Swpaul * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5141502Swpaul * SUCH DAMAGE.
5241502Swpaul *
5341502Swpaul * The licence and distribution terms for any publically available version or
5441502Swpaul * derivative of this code cannot be changed.  i.e. this code cannot simply be
5541502Swpaul * copied and put under another distribution licence
5641502Swpaul * [including the GNU Public Licence.]
5741502Swpaul */
5841502Swpaul
5941502Swpaul#include <stdio.h>
6041502Swpaul#include "cryptlib.h"
6141502Swpaul#include <openssl/objects.h>
6241502Swpaul#include <openssl/asn1t.h>
6341502Swpaul#include <openssl/x509.h>
6441502Swpaul
6541502Swpaul/*-
6641502Swpaul * X509_ATTRIBUTE: this has the following form:
6741502Swpaul *
6841502Swpaul * typedef struct x509_attributes_st
6941502Swpaul *      {
7041502Swpaul *      ASN1_OBJECT *object;
7141502Swpaul *      int single;
7241502Swpaul *      union   {
7341502Swpaul *              char            *ptr;
7441502Swpaul *              STACK_OF(ASN1_TYPE) *set;
7541502Swpaul *              ASN1_TYPE       *single;
7641502Swpaul *              } value;
7741502Swpaul *      } X509_ATTRIBUTE;
7841502Swpaul *
7941502Swpaul * this needs some extra thought because the CHOICE type is
8041502Swpaul * merged with the main structure and because the value can
8141502Swpaul * be anything at all we *must* try the SET OF first because
8241502Swpaul * the ASN1_ANY type will swallow anything including the whole
8341502Swpaul * SET OF structure.
8441502Swpaul */
8541502Swpaul
8648745SwpaulASN1_CHOICE(X509_ATTRIBUTE_SET) = {
8741502Swpaul        ASN1_SET_OF(X509_ATTRIBUTE, value.set, ASN1_ANY),
8841502Swpaul        ASN1_SIMPLE(X509_ATTRIBUTE, value.single, ASN1_ANY)
8941502Swpaul} ASN1_CHOICE_END_selector(X509_ATTRIBUTE, X509_ATTRIBUTE_SET, single)
9041502Swpaul
9141502SwpaulASN1_SEQUENCE(X509_ATTRIBUTE) = {
9241502Swpaul        ASN1_SIMPLE(X509_ATTRIBUTE, object, ASN1_OBJECT),
93129878Sphk        /* CHOICE type merged with parent */
9441502Swpaul        ASN1_EX_COMBINE(0, 0, X509_ATTRIBUTE_SET)
9541502Swpaul} ASN1_SEQUENCE_END(X509_ATTRIBUTE)
9650675Swpaul
9741502SwpaulIMPLEMENT_ASN1_FUNCTIONS(X509_ATTRIBUTE)
9841502SwpaulIMPLEMENT_ASN1_DUP_FUNCTION(X509_ATTRIBUTE)
9941502Swpaul
10041502SwpaulX509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value)
10141502Swpaul{
10241502Swpaul    X509_ATTRIBUTE *ret = NULL;
10341502Swpaul    ASN1_TYPE *val = NULL;
10441502Swpaul
10541502Swpaul    if ((ret = X509_ATTRIBUTE_new()) == NULL)
10641502Swpaul        return (NULL);
10741502Swpaul    ret->object = OBJ_nid2obj(nid);
10841502Swpaul    ret->single = 0;
10941502Swpaul    if ((ret->value.set = sk_ASN1_TYPE_new_null()) == NULL)
11041502Swpaul        goto err;
11149611Swpaul    if ((val = ASN1_TYPE_new()) == NULL)
11249611Swpaul        goto err;
11349611Swpaul    if (!sk_ASN1_TYPE_push(ret->value.set, val))
11441502Swpaul        goto err;
115119288Simp
116119288Simp    ASN1_TYPE_set(val, atrtype, value);
11741502Swpaul    return (ret);
11850675Swpaul err:
11950675Swpaul    if (ret != NULL)
12050675Swpaul        X509_ATTRIBUTE_free(ret);
12151089Speter    if (val != NULL)
12250675Swpaul        ASN1_TYPE_free(val);
12350675Swpaul    return (NULL);
12441502Swpaul}
12541502Swpaul