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