Fix ASN1_STRING_to_UTF8 could not convert NumericString
[openssl.git] / crypto / asn1 / a_type.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/asn1t.h>
13 #include <openssl/objects.h>
14 #include "asn1_locl.h"
15
16 int ASN1_TYPE_get(ASN1_TYPE *a)
17 {
18     if ((a->value.ptr != NULL) || (a->type == V_ASN1_NULL))
19         return (a->type);
20     else
21         return (0);
22 }
23
24 void ASN1_TYPE_set(ASN1_TYPE *a, int type, void *value)
25 {
26     if (a->value.ptr != NULL) {
27         ASN1_TYPE **tmp_a = &a;
28         asn1_primitive_free((ASN1_VALUE **)tmp_a, NULL);
29     }
30     a->type = type;
31     if (type == V_ASN1_BOOLEAN)
32         a->value.boolean = value ? 0xff : 0;
33     else
34         a->value.ptr = value;
35 }
36
37 int ASN1_TYPE_set1(ASN1_TYPE *a, int type, const void *value)
38 {
39     if (!value || (type == V_ASN1_BOOLEAN)) {
40         void *p = (void *)value;
41         ASN1_TYPE_set(a, type, p);
42     } else if (type == V_ASN1_OBJECT) {
43         ASN1_OBJECT *odup;
44         odup = OBJ_dup(value);
45         if (!odup)
46             return 0;
47         ASN1_TYPE_set(a, type, odup);
48     } else {
49         ASN1_STRING *sdup;
50         sdup = ASN1_STRING_dup(value);
51         if (!sdup)
52             return 0;
53         ASN1_TYPE_set(a, type, sdup);
54     }
55     return 1;
56 }
57
58 /* Returns 0 if they are equal, != 0 otherwise. */
59 int ASN1_TYPE_cmp(const ASN1_TYPE *a, const ASN1_TYPE *b)
60 {
61     int result = -1;
62
63     if (!a || !b || a->type != b->type)
64         return -1;
65
66     switch (a->type) {
67     case V_ASN1_OBJECT:
68         result = OBJ_cmp(a->value.object, b->value.object);
69         break;
70     case V_ASN1_BOOLEAN:
71         result = a->value.boolean - b->value.boolean;
72         break;
73     case V_ASN1_NULL:
74         result = 0;             /* They do not have content. */
75         break;
76     case V_ASN1_INTEGER:
77     case V_ASN1_ENUMERATED:
78     case V_ASN1_BIT_STRING:
79     case V_ASN1_OCTET_STRING:
80     case V_ASN1_SEQUENCE:
81     case V_ASN1_SET:
82     case V_ASN1_NUMERICSTRING:
83     case V_ASN1_PRINTABLESTRING:
84     case V_ASN1_T61STRING:
85     case V_ASN1_VIDEOTEXSTRING:
86     case V_ASN1_IA5STRING:
87     case V_ASN1_UTCTIME:
88     case V_ASN1_GENERALIZEDTIME:
89     case V_ASN1_GRAPHICSTRING:
90     case V_ASN1_VISIBLESTRING:
91     case V_ASN1_GENERALSTRING:
92     case V_ASN1_UNIVERSALSTRING:
93     case V_ASN1_BMPSTRING:
94     case V_ASN1_UTF8STRING:
95     case V_ASN1_OTHER:
96     default:
97         result = ASN1_STRING_cmp((ASN1_STRING *)a->value.ptr,
98                                  (ASN1_STRING *)b->value.ptr);
99         break;
100     }
101
102     return result;
103 }
104
105 ASN1_TYPE *ASN1_TYPE_pack_sequence(const ASN1_ITEM *it, void *s, ASN1_TYPE **t)
106 {
107     ASN1_OCTET_STRING *oct;
108     ASN1_TYPE *rt;
109
110     oct = ASN1_item_pack(s, it, NULL);
111     if (oct == NULL)
112         return NULL;
113
114     if (t && *t) {
115         rt = *t;
116     } else {
117         rt = ASN1_TYPE_new();
118         if (rt == NULL) {
119             ASN1_OCTET_STRING_free(oct);
120             return NULL;
121         }
122         if (t)
123             *t = rt;
124     }
125     ASN1_TYPE_set(rt, V_ASN1_SEQUENCE, oct);
126     return rt;
127 }
128
129 void *ASN1_TYPE_unpack_sequence(const ASN1_ITEM *it, const ASN1_TYPE *t)
130 {
131     if (t == NULL || t->type != V_ASN1_SEQUENCE || t->value.sequence == NULL)
132         return NULL;
133     return ASN1_item_unpack(t->value.sequence, it);
134 }