1 // Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <stdio.h>
16
17 #include <openssl/asn1t.h>
18 #include <openssl/conf.h>
19 #include <openssl/obj.h>
20 #include <openssl/x509.h>
21
22 #include "internal.h"
23
24
ASN1_SEQUENCE(OTHERNAME)25 ASN1_SEQUENCE(OTHERNAME) = {
26 ASN1_SIMPLE(OTHERNAME, type_id, ASN1_OBJECT),
27 // Maybe have a true ANY DEFINED BY later
28 ASN1_EXP(OTHERNAME, value, ASN1_ANY, 0),
29 } ASN1_SEQUENCE_END(OTHERNAME)
30
31 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(OTHERNAME)
32
33 ASN1_SEQUENCE(EDIPARTYNAME) = {
34 // DirectoryString is a CHOICE type, so use explicit tagging.
35 ASN1_EXP_OPT(EDIPARTYNAME, nameAssigner, DIRECTORYSTRING, 0),
36 ASN1_EXP(EDIPARTYNAME, partyName, DIRECTORYSTRING, 1),
37 } ASN1_SEQUENCE_END(EDIPARTYNAME)
38
39 IMPLEMENT_ASN1_ALLOC_FUNCTIONS(EDIPARTYNAME)
40
41 ASN1_CHOICE(GENERAL_NAME) = {
42 ASN1_IMP(GENERAL_NAME, d.otherName, OTHERNAME, GEN_OTHERNAME),
43 ASN1_IMP(GENERAL_NAME, d.rfc822Name, ASN1_IA5STRING, GEN_EMAIL),
44 ASN1_IMP(GENERAL_NAME, d.dNSName, ASN1_IA5STRING, GEN_DNS),
45 // Don't decode this
46 ASN1_IMP(GENERAL_NAME, d.x400Address, ASN1_SEQUENCE, GEN_X400),
47 // X509_NAME is a CHOICE type so use EXPLICIT
48 ASN1_EXP(GENERAL_NAME, d.directoryName, X509_NAME, GEN_DIRNAME),
49 ASN1_IMP(GENERAL_NAME, d.ediPartyName, EDIPARTYNAME, GEN_EDIPARTY),
50 ASN1_IMP(GENERAL_NAME, d.uniformResourceIdentifier, ASN1_IA5STRING,
51 GEN_URI),
52 ASN1_IMP(GENERAL_NAME, d.iPAddress, ASN1_OCTET_STRING, GEN_IPADD),
53 ASN1_IMP(GENERAL_NAME, d.registeredID, ASN1_OBJECT, GEN_RID),
54 } ASN1_CHOICE_END(GENERAL_NAME)
55
56 IMPLEMENT_ASN1_FUNCTIONS(GENERAL_NAME)
57
58 ASN1_ITEM_TEMPLATE(GENERAL_NAMES) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF,
59 0, GeneralNames,
60 GENERAL_NAME)
61 ASN1_ITEM_TEMPLATE_END(GENERAL_NAMES)
62
63 IMPLEMENT_ASN1_FUNCTIONS(GENERAL_NAMES)
64
65 IMPLEMENT_ASN1_DUP_FUNCTION(GENERAL_NAME)
66
67 static int edipartyname_cmp(const EDIPARTYNAME *a, const EDIPARTYNAME *b) {
68 // nameAssigner is optional and may be NULL.
69 if (a->nameAssigner == NULL) {
70 if (b->nameAssigner != NULL) {
71 return -1;
72 }
73 } else {
74 if (b->nameAssigner == NULL ||
75 ASN1_STRING_cmp(a->nameAssigner, b->nameAssigner) != 0) {
76 return -1;
77 }
78 }
79
80 // partyName may not be NULL.
81 return ASN1_STRING_cmp(a->partyName, b->partyName);
82 }
83
84 // Returns 0 if they are equal, != 0 otherwise.
othername_cmp(const OTHERNAME * a,const OTHERNAME * b)85 static int othername_cmp(const OTHERNAME *a, const OTHERNAME *b) {
86 int result = -1;
87
88 if (!a || !b) {
89 return -1;
90 }
91 // Check their type first.
92 if ((result = OBJ_cmp(a->type_id, b->type_id)) != 0) {
93 return result;
94 }
95 // Check the value.
96 result = ASN1_TYPE_cmp(a->value, b->value);
97 return result;
98 }
99
100 // Returns 0 if they are equal, != 0 otherwise.
GENERAL_NAME_cmp(const GENERAL_NAME * a,const GENERAL_NAME * b)101 int GENERAL_NAME_cmp(const GENERAL_NAME *a, const GENERAL_NAME *b) {
102 if (!a || !b || a->type != b->type) {
103 return -1;
104 }
105
106 switch (a->type) {
107 case GEN_X400:
108 return ASN1_STRING_cmp(a->d.x400Address, b->d.x400Address);
109
110 case GEN_EDIPARTY:
111 return edipartyname_cmp(a->d.ediPartyName, b->d.ediPartyName);
112
113 case GEN_OTHERNAME:
114 return othername_cmp(a->d.otherName, b->d.otherName);
115
116 case GEN_EMAIL:
117 case GEN_DNS:
118 case GEN_URI:
119 return ASN1_STRING_cmp(a->d.ia5, b->d.ia5);
120
121 case GEN_DIRNAME:
122 return X509_NAME_cmp(a->d.dirn, b->d.dirn);
123
124 case GEN_IPADD:
125 return ASN1_OCTET_STRING_cmp(a->d.ip, b->d.ip);
126
127 case GEN_RID:
128 return OBJ_cmp(a->d.rid, b->d.rid);
129 }
130
131 return -1;
132 }
133
GENERAL_NAME_set0_value(GENERAL_NAME * a,int type,void * value)134 void GENERAL_NAME_set0_value(GENERAL_NAME *a, int type, void *value) {
135 switch (type) {
136 case GEN_X400:
137 a->d.x400Address = reinterpret_cast<ASN1_STRING *>(value);
138 break;
139
140 case GEN_EDIPARTY:
141 a->d.ediPartyName = reinterpret_cast<EDIPARTYNAME *>(value);
142 break;
143
144 case GEN_OTHERNAME:
145 a->d.otherName = reinterpret_cast<OTHERNAME *>(value);
146 break;
147
148 case GEN_EMAIL:
149 case GEN_DNS:
150 case GEN_URI:
151 a->d.ia5 = reinterpret_cast<ASN1_STRING *>(value);
152 break;
153
154 case GEN_DIRNAME:
155 a->d.dirn = reinterpret_cast<X509_NAME *>(value);
156 break;
157
158 case GEN_IPADD:
159 a->d.ip = reinterpret_cast<ASN1_STRING *>(value);
160 break;
161
162 case GEN_RID:
163 a->d.rid = reinterpret_cast<ASN1_OBJECT *>(value);
164 break;
165 }
166 a->type = type;
167 }
168
GENERAL_NAME_get0_value(const GENERAL_NAME * a,int * out_type)169 void *GENERAL_NAME_get0_value(const GENERAL_NAME *a, int *out_type) {
170 if (out_type) {
171 *out_type = a->type;
172 }
173 switch (a->type) {
174 case GEN_X400:
175 return a->d.x400Address;
176
177 case GEN_EDIPARTY:
178 return a->d.ediPartyName;
179
180 case GEN_OTHERNAME:
181 return a->d.otherName;
182
183 case GEN_EMAIL:
184 case GEN_DNS:
185 case GEN_URI:
186 return a->d.ia5;
187
188 case GEN_DIRNAME:
189 return a->d.dirn;
190
191 case GEN_IPADD:
192 return a->d.ip;
193
194 case GEN_RID:
195 return a->d.rid;
196
197 default:
198 return NULL;
199 }
200 }
201
GENERAL_NAME_set0_othername(GENERAL_NAME * gen,ASN1_OBJECT * oid,ASN1_TYPE * value)202 int GENERAL_NAME_set0_othername(GENERAL_NAME *gen, ASN1_OBJECT *oid,
203 ASN1_TYPE *value) {
204 OTHERNAME *oth;
205 oth = OTHERNAME_new();
206 if (!oth) {
207 return 0;
208 }
209 ASN1_TYPE_free(oth->value);
210 oth->type_id = oid;
211 oth->value = value;
212 GENERAL_NAME_set0_value(gen, GEN_OTHERNAME, oth);
213 return 1;
214 }
215
GENERAL_NAME_get0_otherName(const GENERAL_NAME * gen,ASN1_OBJECT ** out_oid,ASN1_TYPE ** out_value)216 int GENERAL_NAME_get0_otherName(const GENERAL_NAME *gen, ASN1_OBJECT **out_oid,
217 ASN1_TYPE **out_value) {
218 if (gen->type != GEN_OTHERNAME) {
219 return 0;
220 }
221 if (out_oid != NULL) {
222 *out_oid = gen->d.otherName->type_id;
223 }
224 if (out_value != NULL) {
225 *out_value = gen->d.otherName->value;
226 }
227 return 1;
228 }
229