a_type.c revision 296341
176278Sjoerg/* crypto/asn1/a_type.c */
276278Sjoerg/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
376278Sjoerg * All rights reserved.
476278Sjoerg *
580258Sdd * This package is an SSL implementation written
680258Sdd * by Eric Young (eay@cryptsoft.com).
776278Sjoerg * The implementation was written so as to conform with Netscapes SSL.
876278Sjoerg *
976278Sjoerg * This library is free for commercial and non-commercial use as long as
1076278Sjoerg * the following conditions are aheared to.  The following conditions
1176278Sjoerg * apply to all code found in this distribution, be it the RC4, RSA,
12132006Smurray * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13132006Smurray * included with this distribution is covered by the same copyright terms
14132006Smurray * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15100709Sru *
16100709Sru * Copyright remains Eric Young's, and as such any Copyright notices in
17100709Sru * the code are not to be removed.
18100709Sru * If this package is used in a product, Eric Young should be given attribution
19100709Sru * as the author of the parts of the library used.
20100709Sru * This can be in the form of a textual message at program startup or
2176278Sjoerg * in documentation (online or textual) provided with the package.
2276278Sjoerg *
23118424Sjhay * Redistribution and use in source and binary forms, with or without
24134603Sru * modification, are permitted provided that the following conditions
25134603Sru * are met:
26134603Sru * 1. Redistributions of source code must retain the copyright
2776278Sjoerg *    notice, this list of conditions and the following disclaimer.
2881349Sjhay * 2. Redistributions in binary form must reproduce the above copyright
29212785Sjhay *    notice, this list of conditions and the following disclaimer in the
30212785Sjhay *    documentation and/or other materials provided with the distribution.
3176278Sjoerg * 3. All advertising materials mentioning features or use of this software
3276278Sjoerg *    must display the following acknowledgement:
33212785Sjhay *    "This product includes cryptographic software written by
34212785Sjhay *     Eric Young (eay@cryptsoft.com)"
35168622Sru *    The word 'cryptographic' can be left out if the rouines from the library
36124375Snyan *    being used are not cryptographic related :-).
3792506Sjhay * 4. If you include any Windows specific code (or a derivative thereof) from
3876278Sjoerg *    the apps directory (application code) you must include an acknowledgement:
3976278Sjoerg *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4076278Sjoerg *
4181646Sbmah * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4276278Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43212785Sjhay * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4481646Sbmah * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45182812Sjhay * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46182812Sjhay * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47132006Smurray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4876278Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49114059Smurray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50114059Smurray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5176278Sjoerg * SUCH DAMAGE.
5276278Sjoerg *
5396822Sbmah * The licence and distribution terms for any publically available version or
54168622Sru * derivative of this code cannot be changed.  i.e. this code cannot simply be
5576278Sjoerg * copied and put under another distribution licence
56168622Sru * [including the GNU Public Licence.]
5776278Sjoerg */
5881250Snik
5981250Snik#include <stdio.h>
6076278Sjoerg#include "cryptlib.h"
61114059Smurray#include <openssl/asn1t.h>
62114059Smurray#include <openssl/objects.h>
6396826Sbmah
64118424Sjhayint ASN1_TYPE_get(ASN1_TYPE *a)
65168622Sru{
6699918Sbmah    if ((a->value.ptr != NULL) || (a->type == V_ASN1_NULL))
6776278Sjoerg        return (a->type);
68134603Sru    else
69106730Stmm        return (0);
70115299Skuriyama}
71115299Skuriyama
7282344Swilkovoid ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value)
7382344Swilko{
7482344Swilko    if (a->value.ptr != NULL) {
75134603Sru        ASN1_TYPE **tmp_a = &a;
76134603Sru        ASN1_primitive_free((ASN1_VALUE **)tmp_a, NULL);
77134603Sru    }
78134603Sru    a->type = type;
79134603Sru    if (type == V_ASN1_BOOLEAN)
80134603Sru        a->value.boolean = value ? 0xff : 0;
81134603Sru    else
82134603Sru        a->value.ptr = value;
83134603Sru}
84134603Sru
85134603Sruint ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value)
86134603Sru{
87134603Sru    if (!value || (type == V_ASN1_BOOLEAN)) {
88206423Skensmith        void *p = (void *)value;
89134603Sru        ASN1_TYPE_set(a, type, p);
90    } else if (type == V_ASN1_OBJECT) {
91        ASN1_OBJECT *odup;
92        odup = OBJ_dup(value);
93        if (!odup)
94            return 0;
95        ASN1_TYPE_set(a, type, odup);
96    } else {
97        ASN1_STRING *sdup;
98        sdup = ASN1_STRING_dup(value);
99        if (!sdup)
100            return 0;
101        ASN1_TYPE_set(a, type, sdup);
102    }
103    return 1;
104}
105
106IMPLEMENT_STACK_OF(ASN1_TYPE)
107
108IMPLEMENT_ASN1_SET_OF(ASN1_TYPE)
109
110/* Returns 0 if they are equal, != 0 otherwise. */
111int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b)
112{
113    int result = -1;
114
115    if (!a || !b || a->type != b->type)
116        return -1;
117
118    switch (a->type) {
119    case V_ASN1_OBJECT:
120        result = OBJ_cmp(a->value.object, b->value.object);
121        break;
122    case V_ASN1_BOOLEAN:
123        result = a->value.boolean - b->value.boolean;
124        break;
125    case V_ASN1_NULL:
126        result = 0;             /* They do not have content. */
127        break;
128    case V_ASN1_INTEGER:
129    case V_ASN1_NEG_INTEGER:
130    case V_ASN1_ENUMERATED:
131    case V_ASN1_NEG_ENUMERATED:
132    case V_ASN1_BIT_STRING:
133    case V_ASN1_OCTET_STRING:
134    case V_ASN1_SEQUENCE:
135    case V_ASN1_SET:
136    case V_ASN1_NUMERICSTRING:
137    case V_ASN1_PRINTABLESTRING:
138    case V_ASN1_T61STRING:
139    case V_ASN1_VIDEOTEXSTRING:
140    case V_ASN1_IA5STRING:
141    case V_ASN1_UTCTIME:
142    case V_ASN1_GENERALIZEDTIME:
143    case V_ASN1_GRAPHICSTRING:
144    case V_ASN1_VISIBLESTRING:
145    case V_ASN1_GENERALSTRING:
146    case V_ASN1_UNIVERSALSTRING:
147    case V_ASN1_BMPSTRING:
148    case V_ASN1_UTF8STRING:
149    case V_ASN1_OTHER:
150    default:
151        result = ASN1_STRING_cmp((ASN1_STRING *)a->value.ptr,
152                                 (ASN1_STRING *)b->value.ptr);
153        break;
154    }
155
156    return result;
157}
158