a_type.c revision 280304
1167974Sdelphij/* crypto/asn1/a_type.c */
278556Sobrien/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
378556Sobrien * All rights reserved.
478556Sobrien *
578556Sobrien * This package is an SSL implementation written
678556Sobrien * by Eric Young (eay@cryptsoft.com).
778556Sobrien * The implementation was written so as to conform with Netscapes SSL.
878556Sobrien *
978556Sobrien * This library is free for commercial and non-commercial use as long as
1078556Sobrien * the following conditions are aheared to.  The following conditions
1178556Sobrien * apply to all code found in this distribution, be it the RC4, RSA,
1278556Sobrien * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1378556Sobrien * included with this distribution is covered by the same copyright terms
1478556Sobrien * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1578556Sobrien *
1678556Sobrien * Copyright remains Eric Young's, and as such any Copyright notices in
1778556Sobrien * the code are not to be removed.
1878556Sobrien * If this package is used in a product, Eric Young should be given attribution
1978556Sobrien * as the author of the parts of the library used.
2078556Sobrien * This can be in the form of a textual message at program startup or
2178556Sobrien * in documentation (online or textual) provided with the package.
2278556Sobrien *
2378556Sobrien * Redistribution and use in source and binary forms, with or without
2478556Sobrien * modification, are permitted provided that the following conditions
2578556Sobrien * are met:
2678556Sobrien * 1. Redistributions of source code must retain the copyright
2778556Sobrien *    notice, this list of conditions and the following disclaimer.
2878556Sobrien * 2. Redistributions in binary form must reproduce the above copyright
2978556Sobrien *    notice, this list of conditions and the following disclaimer in the
3078556Sobrien *    documentation and/or other materials provided with the distribution.
3178556Sobrien * 3. All advertising materials mentioning features or use of this software
3278556Sobrien *    must display the following acknowledgement:
3378556Sobrien *    "This product includes cryptographic software written by
3478556Sobrien *     Eric Young (eay@cryptsoft.com)"
3578556Sobrien *    The word 'cryptographic' can be left out if the rouines from the library
3678556Sobrien *    being used are not cryptographic related :-).
3778556Sobrien * 4. If you include any Windows specific code (or a derivative thereof) from
3878556Sobrien *    the apps directory (application code) you must include an acknowledgement:
3978556Sobrien *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4078556Sobrien *
4178556Sobrien * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4278556Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4378556Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4478556Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4578556Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4678556Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4778556Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4878556Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4978556Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5078556Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5178556Sobrien * SUCH DAMAGE.
5278556Sobrien *
5378556Sobrien * The licence and distribution terms for any publically available version or
5478556Sobrien * derivative of this code cannot be changed.  i.e. this code cannot simply be
5578556Sobrien * copied and put under another distribution licence
5678556Sobrien * [including the GNU Public Licence.]
5778556Sobrien */
5878556Sobrien
5978556Sobrien#include <stdio.h>
6078556Sobrien#include "cryptlib.h"
6178556Sobrien#include <openssl/asn1t.h>
6278556Sobrien#include <openssl/objects.h>
6378556Sobrien
6478556Sobrienint ASN1_TYPE_get(ASN1_TYPE *a)
6578556Sobrien{
6678556Sobrien    if ((a->value.ptr != NULL) || (a->type == V_ASN1_NULL))
6778556Sobrien        return (a->type);
6878556Sobrien    else
6978556Sobrien        return (0);
7078556Sobrien}
7178556Sobrien
7278556Sobrienvoid ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value)
7378556Sobrien{
7478556Sobrien    if (a->value.ptr != NULL) {
7578556Sobrien        ASN1_TYPE **tmp_a = &a;
7678556Sobrien        ASN1_primitive_free((ASN1_VALUE **)tmp_a, NULL);
7778556Sobrien    }
7878556Sobrien    a->type = type;
7978556Sobrien    if (type == V_ASN1_BOOLEAN)
8078556Sobrien        a->value.boolean = value ? 0xff : 0;
8178556Sobrien    else
8278556Sobrien        a->value.ptr = value;
8378556Sobrien}
8478556Sobrien
8578556Sobrienint ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value)
8678556Sobrien{
8778556Sobrien    if (!value || (type == V_ASN1_BOOLEAN)) {
8878556Sobrien        void *p = (void *)value;
8978556Sobrien        ASN1_TYPE_set(a, type, p);
9078556Sobrien    } else if (type == V_ASN1_OBJECT) {
9178556Sobrien        ASN1_OBJECT *odup;
9278556Sobrien        odup = OBJ_dup(value);
9378556Sobrien        if (!odup)
9478556Sobrien            return 0;
9578556Sobrien        ASN1_TYPE_set(a, type, odup);
9678556Sobrien    } else {
9778556Sobrien        ASN1_STRING *sdup;
9878556Sobrien        sdup = ASN1_STRING_dup(value);
9978556Sobrien        if (!sdup)
10078556Sobrien            return 0;
10178556Sobrien        ASN1_TYPE_set(a, type, sdup);
10278556Sobrien    }
10378556Sobrien    return 1;
10478556Sobrien}
10578556Sobrien
10678556SobrienIMPLEMENT_STACK_OF(ASN1_TYPE)
10778556Sobrien
10878556SobrienIMPLEMENT_ASN1_SET_OF(ASN1_TYPE)
10978556Sobrien
11078556Sobrien/* Returns 0 if they are equal, != 0 otherwise. */
11178556Sobrienint ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b)
11278556Sobrien{
11378556Sobrien    int result = -1;
11478556Sobrien
11578556Sobrien    if (!a || !b || a->type != b->type)
11678556Sobrien        return -1;
11778556Sobrien
11878556Sobrien    switch (a->type) {
11978556Sobrien    case V_ASN1_OBJECT:
12078556Sobrien        result = OBJ_cmp(a->value.object, b->value.object);
12178556Sobrien        break;
12278556Sobrien    case V_ASN1_BOOLEAN:
12378556Sobrien        result = a->value.boolean - b->value.boolean;
12478556Sobrien        break;
12578556Sobrien    case V_ASN1_NULL:
12678556Sobrien        result = 0;             /* They do not have content. */
12778556Sobrien        break;
12878556Sobrien    case V_ASN1_INTEGER:
12978556Sobrien    case V_ASN1_NEG_INTEGER:
13078556Sobrien    case V_ASN1_ENUMERATED:
13178556Sobrien    case V_ASN1_NEG_ENUMERATED:
13278556Sobrien    case V_ASN1_BIT_STRING:
13378556Sobrien    case V_ASN1_OCTET_STRING:
13478556Sobrien    case V_ASN1_SEQUENCE:
13578556Sobrien    case V_ASN1_SET:
13678556Sobrien    case V_ASN1_NUMERICSTRING:
13778556Sobrien    case V_ASN1_PRINTABLESTRING:
13878556Sobrien    case V_ASN1_T61STRING:
13978556Sobrien    case V_ASN1_VIDEOTEXSTRING:
14078556Sobrien    case V_ASN1_IA5STRING:
14178556Sobrien    case V_ASN1_UTCTIME:
14278556Sobrien    case V_ASN1_GENERALIZEDTIME:
14378556Sobrien    case V_ASN1_GRAPHICSTRING:
14478556Sobrien    case V_ASN1_VISIBLESTRING:
14578556Sobrien    case V_ASN1_GENERALSTRING:
14678556Sobrien    case V_ASN1_UNIVERSALSTRING:
14778556Sobrien    case V_ASN1_BMPSTRING:
14878556Sobrien    case V_ASN1_UTF8STRING:
14978556Sobrien    case V_ASN1_OTHER:
15078556Sobrien    default:
15178556Sobrien        result = ASN1_STRING_cmp((ASN1_STRING *)a->value.ptr,
15278556Sobrien                                 (ASN1_STRING *)b->value.ptr);
15378556Sobrien        break;
15478556Sobrien    }
15578556Sobrien
15678556Sobrien    return result;
15778556Sobrien}
15878556Sobrien