a_type.c revision 296465
1168404Spjd/* crypto/asn1/a_type.c */
2168404Spjd/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3168404Spjd * All rights reserved.
4168404Spjd *
5168404Spjd * This package is an SSL implementation written
6168404Spjd * by Eric Young (eay@cryptsoft.com).
7168404Spjd * The implementation was written so as to conform with Netscapes SSL.
8168404Spjd *
9168404Spjd * This library is free for commercial and non-commercial use as long as
10168404Spjd * the following conditions are aheared to.  The following conditions
11168404Spjd * apply to all code found in this distribution, be it the RC4, RSA,
12168404Spjd * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13168404Spjd * included with this distribution is covered by the same copyright terms
14168404Spjd * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15168404Spjd *
16168404Spjd * Copyright remains Eric Young's, and as such any Copyright notices in
17168404Spjd * the code are not to be removed.
18168404Spjd * If this package is used in a product, Eric Young should be given attribution
19168404Spjd * as the author of the parts of the library used.
20168404Spjd * This can be in the form of a textual message at program startup or
21168404Spjd * in documentation (online or textual) provided with the package.
22219089Spjd *
23269002Sdelphij * Redistribution and use in source and binary forms, with or without
24168404Spjd * modification, are permitted provided that the following conditions
25168404Spjd * are met:
26169195Spjd * 1. Redistributions of source code must retain the copyright
27226732Smm *    notice, this list of conditions and the following disclaimer.
28169195Spjd * 2. Redistributions in binary form must reproduce the above copyright
29168404Spjd *    notice, this list of conditions and the following disclaimer in the
30168404Spjd *    documentation and/or other materials provided with the distribution.
31168404Spjd * 3. All advertising materials mentioning features or use of this software
32168404Spjd *    must display the following acknowledgement:
33168404Spjd *    "This product includes cryptographic software written by
34168404Spjd *     Eric Young (eay@cryptsoft.com)"
35168404Spjd *    The word 'cryptographic' can be left out if the rouines from the library
36168404Spjd *    being used are not cryptographic related :-).
37185029Spjd * 4. If you include any Windows specific code (or a derivative thereof) from
38185029Spjd *    the apps directory (application code) you must include an acknowledgement:
39168404Spjd *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40168404Spjd *
41168404Spjd * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42168404Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43168404Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44168404Spjd * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45168404Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46168404Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47168404Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48168404Spjd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49168404Spjd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50185029Spjd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51219089Spjd * SUCH DAMAGE.
52168404Spjd *
53185029Spjd * The licence and distribution terms for any publically available version or
54168404Spjd * derivative of this code cannot be changed.  i.e. this code cannot simply be
55168404Spjd * copied and put under another distribution licence
56168404Spjd * [including the GNU Public Licence.]
57168404Spjd */
58168404Spjd
59168404Spjd#include <stdio.h>
60168404Spjd#include "cryptlib.h"
61219089Spjd#include <openssl/asn1t.h>
62219089Spjd#include <openssl/objects.h>
63219089Spjd
64168404Spjdint ASN1_TYPE_get(ASN1_TYPE *a)
65168404Spjd{
66185029Spjd    if ((a->value.ptr != NULL) || (a->type == V_ASN1_NULL))
67219089Spjd        return (a->type);
68185029Spjd    else
69173268Slulf        return (0);
70173268Slulf}
71173268Slulf
72173268Slulfvoid ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value)
73168404Spjd{
74185029Spjd    if (a->value.ptr != NULL) {
75185029Spjd        ASN1_TYPE **tmp_a = &a;
76185029Spjd        ASN1_primitive_free((ASN1_VALUE **)tmp_a, NULL);
77185029Spjd    }
78185029Spjd    a->type = type;
79185029Spjd    a->value.ptr = value;
80185029Spjd}
81185029Spjd
82185029Spjdint ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value)
83185029Spjd{
84185029Spjd    if (!value || (type == V_ASN1_BOOLEAN)) {
85185029Spjd        void *p = (void *)value;
86185029Spjd        ASN1_TYPE_set(a, type, p);
87185029Spjd    } else if (type == V_ASN1_OBJECT) {
88168404Spjd        ASN1_OBJECT *odup;
89168404Spjd        odup = OBJ_dup(value);
90168404Spjd        if (!odup)
91168404Spjd            return 0;
92168404Spjd        ASN1_TYPE_set(a, type, odup);
93210470Smm    } else {
94210470Smm        ASN1_STRING *sdup;
95210470Smm        sdup = ASN1_STRING_dup((ASN1_STRING *)value);
96210470Smm        if (!sdup)
97210470Smm            return 0;
98210470Smm        ASN1_TYPE_set(a, type, sdup);
99185029Spjd    }
100168404Spjd    return 1;
101168404Spjd}
102168404Spjd
103185029SpjdIMPLEMENT_STACK_OF(ASN1_TYPE)
104168404Spjd
105185029SpjdIMPLEMENT_ASN1_SET_OF(ASN1_TYPE)
106185029Spjd
107185029Spjd/* Returns 0 if they are equal, != 0 otherwise. */
108185029Spjdint ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b)
109185029Spjd{
110168404Spjd    int result = -1;
111168404Spjd
112168404Spjd    if (!a || !b || a->type != b->type)
113168404Spjd        return -1;
114209962Smm
115168404Spjd    switch (a->type) {
116168404Spjd    case V_ASN1_OBJECT:
117185029Spjd        result = OBJ_cmp(a->value.object, b->value.object);
118168404Spjd        break;
119168404Spjd    case V_ASN1_BOOLEAN:
120168404Spjd        result = a->value.boolean - b->value.boolean;
121185029Spjd        break;
122185029Spjd    case V_ASN1_NULL:
123185029Spjd        result = 0;             /* They do not have content. */
124185029Spjd        break;
125168404Spjd    case V_ASN1_INTEGER:
126168404Spjd    case V_ASN1_NEG_INTEGER:
127168404Spjd    case V_ASN1_ENUMERATED:
128168404Spjd    case V_ASN1_NEG_ENUMERATED:
129168404Spjd    case V_ASN1_BIT_STRING:
130168404Spjd    case V_ASN1_OCTET_STRING:
131168404Spjd    case V_ASN1_SEQUENCE:
132168404Spjd    case V_ASN1_SET:
133168404Spjd    case V_ASN1_NUMERICSTRING:
134185029Spjd    case V_ASN1_PRINTABLESTRING:
135211932Smm    case V_ASN1_T61STRING:
136253603Savg    case V_ASN1_VIDEOTEXSTRING:
137219089Spjd    case V_ASN1_IA5STRING:
138168404Spjd    case V_ASN1_UTCTIME:
139168404Spjd    case V_ASN1_GENERALIZEDTIME:
140168404Spjd    case V_ASN1_GRAPHICSTRING:
141168404Spjd    case V_ASN1_VISIBLESTRING:
142168404Spjd    case V_ASN1_GENERALSTRING:
143185029Spjd    case V_ASN1_UNIVERSALSTRING:
144168404Spjd    case V_ASN1_BMPSTRING:
145168404Spjd    case V_ASN1_UTF8STRING:
146168404Spjd    case V_ASN1_OTHER:
147185029Spjd    default:
148185029Spjd        result = ASN1_STRING_cmp((ASN1_STRING *)a->value.ptr,
149185029Spjd                                 (ASN1_STRING *)b->value.ptr);
150185029Spjd        break;
151168404Spjd    }
152168404Spjd
153168404Spjd    return result;
154168404Spjd}
155185029Spjd