/src/openssl/crypto/x509/v3_genn.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1999-2024 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (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/conf.h> |
14 | | #include <openssl/x509v3.h> |
15 | | |
16 | | ASN1_SEQUENCE(OTHERNAME) = { |
17 | | ASN1_SIMPLE(OTHERNAME, type_id, ASN1_OBJECT), |
18 | | /* Maybe have a true ANY DEFINED BY later */ |
19 | | ASN1_EXP(OTHERNAME, value, ASN1_ANY, 0) |
20 | 0 | } ASN1_SEQUENCE_END(OTHERNAME) |
21 | 0 |
|
22 | 0 | IMPLEMENT_ASN1_FUNCTIONS(OTHERNAME) |
23 | 0 |
|
24 | 0 | ASN1_SEQUENCE(EDIPARTYNAME) = { |
25 | 0 | /* DirectoryString is a CHOICE type so use explicit tagging */ |
26 | 0 | ASN1_EXP_OPT(EDIPARTYNAME, nameAssigner, DIRECTORYSTRING, 0), |
27 | 0 | ASN1_EXP(EDIPARTYNAME, partyName, DIRECTORYSTRING, 1) |
28 | 0 | } ASN1_SEQUENCE_END(EDIPARTYNAME) |
29 | 0 |
|
30 | 0 | IMPLEMENT_ASN1_FUNCTIONS(EDIPARTYNAME) |
31 | 0 |
|
32 | 0 | ASN1_CHOICE(GENERAL_NAME) = { |
33 | 0 | ASN1_IMP(GENERAL_NAME, d.otherName, OTHERNAME, GEN_OTHERNAME), |
34 | 0 | ASN1_IMP(GENERAL_NAME, d.rfc822Name, ASN1_IA5STRING, GEN_EMAIL), |
35 | 0 | ASN1_IMP(GENERAL_NAME, d.dNSName, ASN1_IA5STRING, GEN_DNS), |
36 | 0 | /* Don't decode this */ |
37 | 0 | ASN1_IMP(GENERAL_NAME, d.x400Address, ASN1_SEQUENCE, GEN_X400), |
38 | 0 | /* X509_NAME is a CHOICE type so use EXPLICIT */ |
39 | 0 | ASN1_EXP(GENERAL_NAME, d.directoryName, X509_NAME, GEN_DIRNAME), |
40 | 0 | ASN1_IMP(GENERAL_NAME, d.ediPartyName, EDIPARTYNAME, GEN_EDIPARTY), |
41 | 0 | ASN1_IMP(GENERAL_NAME, d.uniformResourceIdentifier, ASN1_IA5STRING, GEN_URI), |
42 | 0 | ASN1_IMP(GENERAL_NAME, d.iPAddress, ASN1_OCTET_STRING, GEN_IPADD), |
43 | 0 | ASN1_IMP(GENERAL_NAME, d.registeredID, ASN1_OBJECT, GEN_RID) |
44 | 0 | } ASN1_CHOICE_END(GENERAL_NAME) |
45 | 0 |
|
46 | 0 | IMPLEMENT_ASN1_FUNCTIONS(GENERAL_NAME) |
47 | 0 |
|
48 | 0 | ASN1_ITEM_TEMPLATE(GENERAL_NAMES) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, GeneralNames, GENERAL_NAME) |
49 | 0 | ASN1_ITEM_TEMPLATE_END(GENERAL_NAMES) |
50 | | |
51 | | IMPLEMENT_ASN1_FUNCTIONS(GENERAL_NAMES) |
52 | | IMPLEMENT_ASN1_DUP_FUNCTION(GENERAL_NAME) |
53 | | |
54 | | int GENERAL_NAME_set1_X509_NAME(GENERAL_NAME **tgt, const X509_NAME *src) |
55 | 0 | { |
56 | 0 | GENERAL_NAME *name; |
57 | |
|
58 | 0 | if (tgt == NULL) { |
59 | 0 | ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NULL_ARGUMENT); |
60 | 0 | return 0; |
61 | 0 | } |
62 | | |
63 | 0 | if ((name = GENERAL_NAME_new()) == NULL) |
64 | 0 | return 0; |
65 | 0 | name->type = GEN_DIRNAME; |
66 | |
|
67 | 0 | if (src == NULL) { /* NULL-DN */ |
68 | 0 | if ((name->d.directoryName = X509_NAME_new()) == NULL) |
69 | 0 | goto err; |
70 | 0 | } else if (!X509_NAME_set(&name->d.directoryName, src)) { |
71 | 0 | goto err; |
72 | 0 | } |
73 | | |
74 | 0 | GENERAL_NAME_free(*tgt); |
75 | 0 | *tgt = name; |
76 | 0 | return 1; |
77 | | |
78 | 0 | err: |
79 | 0 | GENERAL_NAME_free(name); |
80 | 0 | return 0; |
81 | 0 | } |
82 | | |
83 | | static int edipartyname_cmp(const EDIPARTYNAME *a, const EDIPARTYNAME *b) |
84 | 0 | { |
85 | 0 | int res; |
86 | |
|
87 | 0 | if (a == NULL || b == NULL) { |
88 | | /* |
89 | | * Shouldn't be possible in a valid GENERAL_NAME, but we handle it |
90 | | * anyway. OTHERNAME_cmp treats NULL != NULL so we do the same here |
91 | | */ |
92 | 0 | return -1; |
93 | 0 | } |
94 | 0 | if (a->nameAssigner == NULL && b->nameAssigner != NULL) |
95 | 0 | return -1; |
96 | 0 | if (a->nameAssigner != NULL && b->nameAssigner == NULL) |
97 | 0 | return 1; |
98 | | /* If we get here then both have nameAssigner set, or both unset */ |
99 | 0 | if (a->nameAssigner != NULL) { |
100 | 0 | res = ASN1_STRING_cmp(a->nameAssigner, b->nameAssigner); |
101 | 0 | if (res != 0) |
102 | 0 | return res; |
103 | 0 | } |
104 | | /* |
105 | | * partyName is required, so these should never be NULL. We treat it in |
106 | | * the same way as the a == NULL || b == NULL case above |
107 | | */ |
108 | 0 | if (a->partyName == NULL || b->partyName == NULL) |
109 | 0 | return -1; |
110 | | |
111 | 0 | return ASN1_STRING_cmp(a->partyName, b->partyName); |
112 | 0 | } |
113 | | |
114 | | /* Returns 0 if they are equal, != 0 otherwise. */ |
115 | | int GENERAL_NAME_cmp(GENERAL_NAME *a, GENERAL_NAME *b) |
116 | 0 | { |
117 | 0 | int result = -1; |
118 | |
|
119 | 0 | if (!a || !b || a->type != b->type) |
120 | 0 | return -1; |
121 | 0 | switch (a->type) { |
122 | 0 | case GEN_X400: |
123 | 0 | result = ASN1_STRING_cmp(a->d.x400Address, b->d.x400Address); |
124 | 0 | break; |
125 | | |
126 | 0 | case GEN_EDIPARTY: |
127 | 0 | result = edipartyname_cmp(a->d.ediPartyName, b->d.ediPartyName); |
128 | 0 | break; |
129 | | |
130 | 0 | case GEN_OTHERNAME: |
131 | 0 | result = OTHERNAME_cmp(a->d.otherName, b->d.otherName); |
132 | 0 | break; |
133 | | |
134 | 0 | case GEN_EMAIL: |
135 | 0 | case GEN_DNS: |
136 | 0 | case GEN_URI: |
137 | 0 | result = ASN1_STRING_cmp(a->d.ia5, b->d.ia5); |
138 | 0 | break; |
139 | | |
140 | 0 | case GEN_DIRNAME: |
141 | 0 | result = X509_NAME_cmp(a->d.dirn, b->d.dirn); |
142 | 0 | break; |
143 | | |
144 | 0 | case GEN_IPADD: |
145 | 0 | result = ASN1_OCTET_STRING_cmp(a->d.ip, b->d.ip); |
146 | 0 | break; |
147 | | |
148 | 0 | case GEN_RID: |
149 | 0 | result = OBJ_cmp(a->d.rid, b->d.rid); |
150 | 0 | break; |
151 | 0 | } |
152 | 0 | return result; |
153 | 0 | } |
154 | | |
155 | | /* Returns 0 if they are equal, != 0 otherwise. */ |
156 | | int OTHERNAME_cmp(OTHERNAME *a, OTHERNAME *b) |
157 | 0 | { |
158 | 0 | int result = -1; |
159 | |
|
160 | 0 | if (!a || !b) |
161 | 0 | return -1; |
162 | | /* Check their type first. */ |
163 | 0 | if ((result = OBJ_cmp(a->type_id, b->type_id)) != 0) |
164 | 0 | return result; |
165 | | /* Check the value. */ |
166 | 0 | result = ASN1_TYPE_cmp(a->value, b->value); |
167 | 0 | return result; |
168 | 0 | } |
169 | | |
170 | | void GENERAL_NAME_set0_value(GENERAL_NAME *a, int type, void *value) |
171 | 0 | { |
172 | 0 | switch (type) { |
173 | 0 | case GEN_X400: |
174 | 0 | a->d.x400Address = value; |
175 | 0 | break; |
176 | | |
177 | 0 | case GEN_EDIPARTY: |
178 | 0 | a->d.ediPartyName = value; |
179 | 0 | break; |
180 | | |
181 | 0 | case GEN_OTHERNAME: |
182 | 0 | a->d.otherName = value; |
183 | 0 | break; |
184 | | |
185 | 0 | case GEN_EMAIL: |
186 | 0 | case GEN_DNS: |
187 | 0 | case GEN_URI: |
188 | 0 | a->d.ia5 = value; |
189 | 0 | break; |
190 | | |
191 | 0 | case GEN_DIRNAME: |
192 | 0 | a->d.dirn = value; |
193 | 0 | break; |
194 | | |
195 | 0 | case GEN_IPADD: |
196 | 0 | a->d.ip = value; |
197 | 0 | break; |
198 | | |
199 | 0 | case GEN_RID: |
200 | 0 | a->d.rid = value; |
201 | 0 | break; |
202 | 0 | } |
203 | 0 | a->type = type; |
204 | 0 | } |
205 | | |
206 | | void *GENERAL_NAME_get0_value(const GENERAL_NAME *a, int *ptype) |
207 | 0 | { |
208 | 0 | if (ptype) |
209 | 0 | *ptype = a->type; |
210 | 0 | switch (a->type) { |
211 | 0 | case GEN_X400: |
212 | 0 | return a->d.x400Address; |
213 | | |
214 | 0 | case GEN_EDIPARTY: |
215 | 0 | return a->d.ediPartyName; |
216 | | |
217 | 0 | case GEN_OTHERNAME: |
218 | 0 | return a->d.otherName; |
219 | | |
220 | 0 | case GEN_EMAIL: |
221 | 0 | case GEN_DNS: |
222 | 0 | case GEN_URI: |
223 | 0 | return a->d.ia5; |
224 | | |
225 | 0 | case GEN_DIRNAME: |
226 | 0 | return a->d.dirn; |
227 | | |
228 | 0 | case GEN_IPADD: |
229 | 0 | return a->d.ip; |
230 | | |
231 | 0 | case GEN_RID: |
232 | 0 | return a->d.rid; |
233 | | |
234 | 0 | default: |
235 | 0 | return NULL; |
236 | 0 | } |
237 | 0 | } |
238 | | |
239 | | int GENERAL_NAME_set0_othername(GENERAL_NAME *gen, |
240 | | ASN1_OBJECT *oid, ASN1_TYPE *value) |
241 | 0 | { |
242 | 0 | OTHERNAME *oth; |
243 | 0 | oth = OTHERNAME_new(); |
244 | 0 | if (oth == NULL) |
245 | 0 | return 0; |
246 | 0 | ASN1_TYPE_free(oth->value); |
247 | 0 | oth->type_id = oid; |
248 | 0 | oth->value = value; |
249 | 0 | GENERAL_NAME_set0_value(gen, GEN_OTHERNAME, oth); |
250 | 0 | return 1; |
251 | 0 | } |
252 | | |
253 | | int GENERAL_NAME_get0_otherName(const GENERAL_NAME *gen, |
254 | | ASN1_OBJECT **poid, ASN1_TYPE **pvalue) |
255 | 0 | { |
256 | 0 | if (gen->type != GEN_OTHERNAME) |
257 | 0 | return 0; |
258 | 0 | if (poid) |
259 | 0 | *poid = gen->d.otherName->type_id; |
260 | 0 | if (pvalue) |
261 | 0 | *pvalue = gen->d.otherName->value; |
262 | 0 | return 1; |
263 | 0 | } |