162053Smarkm/* crypto/asn1/x_attrib.c */
262765Smarkm/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
362053Smarkm * All rights reserved.
462053Smarkm *
562053Smarkm * This package is an SSL implementation written
662053Smarkm * by Eric Young (eay@cryptsoft.com).
762053Smarkm * The implementation was written so as to conform with Netscapes SSL.
862053Smarkm *
962053Smarkm * This library is free for commercial and non-commercial use as long as
1062053Smarkm * the following conditions are aheared to.  The following conditions
1162053Smarkm * apply to all code found in this distribution, be it the RC4, RSA,
1262053Smarkm * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1362053Smarkm * included with this distribution is covered by the same copyright terms
1462053Smarkm * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1562053Smarkm *
1662053Smarkm * Copyright remains Eric Young's, and as such any Copyright notices in
1762053Smarkm * the code are not to be removed.
1862053Smarkm * If this package is used in a product, Eric Young should be given attribution
1962053Smarkm * as the author of the parts of the library used.
2062053Smarkm * This can be in the form of a textual message at program startup or
2162053Smarkm * in documentation (online or textual) provided with the package.
2262053Smarkm *
2362053Smarkm * Redistribution and use in source and binary forms, with or without
2462053Smarkm * modification, are permitted provided that the following conditions
2562053Smarkm * are met:
2662053Smarkm * 1. Redistributions of source code must retain the copyright
2762053Smarkm *    notice, this list of conditions and the following disclaimer.
2862053Smarkm * 2. Redistributions in binary form must reproduce the above copyright
2962053Smarkm *    notice, this list of conditions and the following disclaimer in the
3063771Smarkm *    documentation and/or other materials provided with the distribution.
3163771Smarkm * 3. All advertising materials mentioning features or use of this software
3262053Smarkm *    must display the following acknowledgement:
3362053Smarkm *    "This product includes cryptographic software written by
3462053Smarkm *     Eric Young (eay@cryptsoft.com)"
3562053Smarkm *    The word 'cryptographic' can be left out if the rouines from the library
3665686Smarkm *    being used are not cryptographic related :-).
3765686Smarkm * 4. If you include any Windows specific code (or a derivative thereof) from
3862053Smarkm *    the apps directory (application code) you must include an acknowledgement:
3965686Smarkm *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4067365Sjhb *
4165686Smarkm * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4267112Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4362053Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4462765Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4562053Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4665712Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4762053Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4862053Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4967112Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5067112Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5162053Smarkm * SUCH DAMAGE.
5262765Smarkm *
5365686Smarkm * The licence and distribution terms for any publically available version or
5462053Smarkm * derivative of this code cannot be changed.  i.e. this code cannot simply be
5562765Smarkm * copied and put under another distribution licence
5662765Smarkm * [including the GNU Public Licence.]
5763855Smarkm */
5862053Smarkm
5965686Smarkm#include <stdio.h>
6065686Smarkm#include "cryptlib.h"
6162765Smarkm#include <openssl/objects.h>
6262765Smarkm#include <openssl/asn1t.h>
6362765Smarkm#include <openssl/x509.h>
6465686Smarkm
6565686Smarkm/*-
6665686Smarkm * X509_ATTRIBUTE: this has the following form:
6765686Smarkm *
6865686Smarkm * typedef struct x509_attributes_st
6967112Smarkm *      {
7063771Smarkm *      ASN1_OBJECT *object;
7165686Smarkm *      int single;
7265686Smarkm *      union   {
7367112Smarkm *              char            *ptr;
7465686Smarkm *              STACK_OF(ASN1_TYPE) *set;
7565686Smarkm *              ASN1_TYPE       *single;
7665686Smarkm *              } value;
7765686Smarkm *      } X509_ATTRIBUTE;
7865686Smarkm *
7962765Smarkm * this needs some extra thought because the CHOICE type is
8065686Smarkm * merged with the main structure and because the value can
8165856Sjhb * be anything at all we *must* try the SET OF first because
8262765Smarkm * the ASN1_ANY type will swallow anything including the whole
8367112Smarkm * SET OF structure.
8467112Smarkm */
8567112Smarkm
8665856SjhbASN1_CHOICE(X509_ATTRIBUTE_SET) = {
8765686Smarkm        ASN1_SET_OF(X509_ATTRIBUTE, value.set, ASN1_ANY),
8865686Smarkm        ASN1_SIMPLE(X509_ATTRIBUTE, value.single, ASN1_ANY)
8965686Smarkm} ASN1_CHOICE_END_selector(X509_ATTRIBUTE, X509_ATTRIBUTE_SET, single)
9065686Smarkm
9165686SmarkmASN1_SEQUENCE(X509_ATTRIBUTE) = {
9265686Smarkm        ASN1_SIMPLE(X509_ATTRIBUTE, object, ASN1_OBJECT),
9362765Smarkm        /* CHOICE type merged with parent */
9467112Smarkm        ASN1_EX_COMBINE(0, 0, X509_ATTRIBUTE_SET)
9562765Smarkm} ASN1_SEQUENCE_END(X509_ATTRIBUTE)
9665686Smarkm
9765686SmarkmIMPLEMENT_ASN1_FUNCTIONS(X509_ATTRIBUTE)
9865686SmarkmIMPLEMENT_ASN1_DUP_FUNCTION(X509_ATTRIBUTE)
9965686Smarkm
10065686SmarkmX509_ATTRIBUTE *X509_ATTRIBUTE_create(int nid, int atrtype, void *value)
10165686Smarkm{
10265686Smarkm    X509_ATTRIBUTE *ret = NULL;
10362765Smarkm    ASN1_TYPE *val = NULL;
10467112Smarkm
10562765Smarkm    if ((ret = X509_ATTRIBUTE_new()) == NULL)
10665686Smarkm        return (NULL);
10765686Smarkm    ret->object = OBJ_nid2obj(nid);
10865686Smarkm    ret->single = 0;
10965686Smarkm    if ((ret->value.set = sk_ASN1_TYPE_new_null()) == NULL)
11065686Smarkm        goto err;
11165686Smarkm    if ((val = ASN1_TYPE_new()) == NULL)
11265686Smarkm        goto err;
11365686Smarkm    if (!sk_ASN1_TYPE_push(ret->value.set, val))
11465686Smarkm        goto err;
11565686Smarkm
11665686Smarkm    ASN1_TYPE_set(val, atrtype, value);
11765686Smarkm    return (ret);
11865686Smarkm err:
11965686Smarkm    if (ret != NULL)
12065686Smarkm        X509_ATTRIBUTE_free(ret);
12165686Smarkm    if (val != NULL)
12265686Smarkm        ASN1_TYPE_free(val);
12365686Smarkm    return (NULL);
12465686Smarkm}
12565686Smarkm