1*c0ebdaf2Sbeck /* $OpenBSD: x509_genn.c,v 1.7 2024/07/08 14:47:44 beck Exp $ */
2e500e238Sjsing /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3e500e238Sjsing * project 1999.
4e500e238Sjsing */
5e500e238Sjsing /* ====================================================================
6e500e238Sjsing * Copyright (c) 1999-2008 The OpenSSL Project. All rights reserved.
7e500e238Sjsing *
8e500e238Sjsing * Redistribution and use in source and binary forms, with or without
9e500e238Sjsing * modification, are permitted provided that the following conditions
10e500e238Sjsing * are met:
11e500e238Sjsing *
12e500e238Sjsing * 1. Redistributions of source code must retain the above copyright
13e500e238Sjsing * notice, this list of conditions and the following disclaimer.
14e500e238Sjsing *
15e500e238Sjsing * 2. Redistributions in binary form must reproduce the above copyright
16e500e238Sjsing * notice, this list of conditions and the following disclaimer in
17e500e238Sjsing * the documentation and/or other materials provided with the
18e500e238Sjsing * distribution.
19e500e238Sjsing *
20e500e238Sjsing * 3. All advertising materials mentioning features or use of this
21e500e238Sjsing * software must display the following acknowledgment:
22e500e238Sjsing * "This product includes software developed by the OpenSSL Project
23e500e238Sjsing * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24e500e238Sjsing *
25e500e238Sjsing * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26e500e238Sjsing * endorse or promote products derived from this software without
27e500e238Sjsing * prior written permission. For written permission, please contact
28e500e238Sjsing * licensing@OpenSSL.org.
29e500e238Sjsing *
30e500e238Sjsing * 5. Products derived from this software may not be called "OpenSSL"
31e500e238Sjsing * nor may "OpenSSL" appear in their names without prior written
32e500e238Sjsing * permission of the OpenSSL Project.
33e500e238Sjsing *
34e500e238Sjsing * 6. Redistributions of any form whatsoever must retain the following
35e500e238Sjsing * acknowledgment:
36e500e238Sjsing * "This product includes software developed by the OpenSSL Project
37e500e238Sjsing * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38e500e238Sjsing *
39e500e238Sjsing * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40e500e238Sjsing * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41e500e238Sjsing * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42e500e238Sjsing * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43e500e238Sjsing * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44e500e238Sjsing * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45e500e238Sjsing * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46e500e238Sjsing * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47e500e238Sjsing * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48e500e238Sjsing * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49e500e238Sjsing * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50e500e238Sjsing * OF THE POSSIBILITY OF SUCH DAMAGE.
51e500e238Sjsing * ====================================================================
52e500e238Sjsing *
53e500e238Sjsing * This product includes cryptographic software written by Eric Young
54e500e238Sjsing * (eay@cryptsoft.com). This product includes software written by Tim
55e500e238Sjsing * Hudson (tjh@cryptsoft.com).
56e500e238Sjsing *
57e500e238Sjsing */
58e500e238Sjsing
59e500e238Sjsing
60e500e238Sjsing #include <stdio.h>
61e500e238Sjsing
62e500e238Sjsing #include <openssl/asn1t.h>
63e500e238Sjsing #include <openssl/conf.h>
64e500e238Sjsing #include <openssl/x509v3.h>
65e500e238Sjsing
66e500e238Sjsing static const ASN1_TEMPLATE OTHERNAME_seq_tt[] = {
67e500e238Sjsing {
68e500e238Sjsing .flags = 0,
69e500e238Sjsing .tag = 0,
70e500e238Sjsing .offset = offsetof(OTHERNAME, type_id),
71e500e238Sjsing .field_name = "type_id",
72e500e238Sjsing .item = &ASN1_OBJECT_it,
73e500e238Sjsing },
74e500e238Sjsing /* Maybe have a true ANY DEFINED BY later */
75e500e238Sjsing {
76e500e238Sjsing .flags = ASN1_TFLG_EXPLICIT,
77e500e238Sjsing .tag = 0,
78e500e238Sjsing .offset = offsetof(OTHERNAME, value),
79e500e238Sjsing .field_name = "value",
80e500e238Sjsing .item = &ASN1_ANY_it,
81e500e238Sjsing },
82e500e238Sjsing };
83e500e238Sjsing
84e500e238Sjsing const ASN1_ITEM OTHERNAME_it = {
85e500e238Sjsing .itype = ASN1_ITYPE_SEQUENCE,
86e500e238Sjsing .utype = V_ASN1_SEQUENCE,
87e500e238Sjsing .templates = OTHERNAME_seq_tt,
88e500e238Sjsing .tcount = sizeof(OTHERNAME_seq_tt) / sizeof(ASN1_TEMPLATE),
89e500e238Sjsing .funcs = NULL,
90e500e238Sjsing .size = sizeof(OTHERNAME),
91e500e238Sjsing .sname = "OTHERNAME",
92e500e238Sjsing };
93*c0ebdaf2Sbeck LCRYPTO_ALIAS(OTHERNAME_it);
94e500e238Sjsing
95e500e238Sjsing
96e500e238Sjsing OTHERNAME *
d2i_OTHERNAME(OTHERNAME ** a,const unsigned char ** in,long len)97e500e238Sjsing d2i_OTHERNAME(OTHERNAME **a, const unsigned char **in, long len)
98e500e238Sjsing {
99e500e238Sjsing return (OTHERNAME *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
100e500e238Sjsing &OTHERNAME_it);
101e500e238Sjsing }
102cedac418Stb LCRYPTO_ALIAS(d2i_OTHERNAME);
103e500e238Sjsing
104e500e238Sjsing int
i2d_OTHERNAME(OTHERNAME * a,unsigned char ** out)105e500e238Sjsing i2d_OTHERNAME(OTHERNAME *a, unsigned char **out)
106e500e238Sjsing {
107e500e238Sjsing return ASN1_item_i2d((ASN1_VALUE *)a, out, &OTHERNAME_it);
108e500e238Sjsing }
109cedac418Stb LCRYPTO_ALIAS(i2d_OTHERNAME);
110e500e238Sjsing
111e500e238Sjsing OTHERNAME *
OTHERNAME_new(void)112e500e238Sjsing OTHERNAME_new(void)
113e500e238Sjsing {
114e500e238Sjsing return (OTHERNAME *)ASN1_item_new(&OTHERNAME_it);
115e500e238Sjsing }
116cedac418Stb LCRYPTO_ALIAS(OTHERNAME_new);
117e500e238Sjsing
118e500e238Sjsing void
OTHERNAME_free(OTHERNAME * a)119e500e238Sjsing OTHERNAME_free(OTHERNAME *a)
120e500e238Sjsing {
121e500e238Sjsing ASN1_item_free((ASN1_VALUE *)a, &OTHERNAME_it);
122e500e238Sjsing }
123cedac418Stb LCRYPTO_ALIAS(OTHERNAME_free);
124e500e238Sjsing
125bb15212cStb /* Uses explicit tagging since DIRECTORYSTRING is a CHOICE type */
126e500e238Sjsing static const ASN1_TEMPLATE EDIPARTYNAME_seq_tt[] = {
127e500e238Sjsing {
128bb15212cStb .flags = ASN1_TFLG_EXPLICIT | ASN1_TFLG_OPTIONAL,
129e500e238Sjsing .tag = 0,
130e500e238Sjsing .offset = offsetof(EDIPARTYNAME, nameAssigner),
131e500e238Sjsing .field_name = "nameAssigner",
132e500e238Sjsing .item = &DIRECTORYSTRING_it,
133e500e238Sjsing },
134e500e238Sjsing {
135bb15212cStb .flags = ASN1_TFLG_EXPLICIT,
136e500e238Sjsing .tag = 1,
137e500e238Sjsing .offset = offsetof(EDIPARTYNAME, partyName),
138e500e238Sjsing .field_name = "partyName",
139e500e238Sjsing .item = &DIRECTORYSTRING_it,
140e500e238Sjsing },
141e500e238Sjsing };
142e500e238Sjsing
143e500e238Sjsing const ASN1_ITEM EDIPARTYNAME_it = {
144e500e238Sjsing .itype = ASN1_ITYPE_SEQUENCE,
145e500e238Sjsing .utype = V_ASN1_SEQUENCE,
146e500e238Sjsing .templates = EDIPARTYNAME_seq_tt,
147e500e238Sjsing .tcount = sizeof(EDIPARTYNAME_seq_tt) / sizeof(ASN1_TEMPLATE),
148e500e238Sjsing .funcs = NULL,
149e500e238Sjsing .size = sizeof(EDIPARTYNAME),
150e500e238Sjsing .sname = "EDIPARTYNAME",
151e500e238Sjsing };
152*c0ebdaf2Sbeck LCRYPTO_ALIAS(EDIPARTYNAME_it);
153e500e238Sjsing
154e500e238Sjsing
155e500e238Sjsing EDIPARTYNAME *
d2i_EDIPARTYNAME(EDIPARTYNAME ** a,const unsigned char ** in,long len)156e500e238Sjsing d2i_EDIPARTYNAME(EDIPARTYNAME **a, const unsigned char **in, long len)
157e500e238Sjsing {
158e500e238Sjsing return (EDIPARTYNAME *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
159e500e238Sjsing &EDIPARTYNAME_it);
160e500e238Sjsing }
161cedac418Stb LCRYPTO_ALIAS(d2i_EDIPARTYNAME);
162e500e238Sjsing
163e500e238Sjsing int
i2d_EDIPARTYNAME(EDIPARTYNAME * a,unsigned char ** out)164e500e238Sjsing i2d_EDIPARTYNAME(EDIPARTYNAME *a, unsigned char **out)
165e500e238Sjsing {
166e500e238Sjsing return ASN1_item_i2d((ASN1_VALUE *)a, out, &EDIPARTYNAME_it);
167e500e238Sjsing }
168cedac418Stb LCRYPTO_ALIAS(i2d_EDIPARTYNAME);
169e500e238Sjsing
170e500e238Sjsing EDIPARTYNAME *
EDIPARTYNAME_new(void)171e500e238Sjsing EDIPARTYNAME_new(void)
172e500e238Sjsing {
173e500e238Sjsing return (EDIPARTYNAME *)ASN1_item_new(&EDIPARTYNAME_it);
174e500e238Sjsing }
175cedac418Stb LCRYPTO_ALIAS(EDIPARTYNAME_new);
176e500e238Sjsing
177e500e238Sjsing void
EDIPARTYNAME_free(EDIPARTYNAME * a)178e500e238Sjsing EDIPARTYNAME_free(EDIPARTYNAME *a)
179e500e238Sjsing {
180e500e238Sjsing ASN1_item_free((ASN1_VALUE *)a, &EDIPARTYNAME_it);
181e500e238Sjsing }
182cedac418Stb LCRYPTO_ALIAS(EDIPARTYNAME_free);
183e500e238Sjsing
184e500e238Sjsing static const ASN1_TEMPLATE GENERAL_NAME_ch_tt[] = {
185e500e238Sjsing {
186e500e238Sjsing .flags = ASN1_TFLG_IMPLICIT,
187e500e238Sjsing .tag = GEN_OTHERNAME,
188e500e238Sjsing .offset = offsetof(GENERAL_NAME, d.otherName),
189e500e238Sjsing .field_name = "d.otherName",
190e500e238Sjsing .item = &OTHERNAME_it,
191e500e238Sjsing },
192e500e238Sjsing {
193e500e238Sjsing .flags = ASN1_TFLG_IMPLICIT,
194e500e238Sjsing .tag = GEN_EMAIL,
195e500e238Sjsing .offset = offsetof(GENERAL_NAME, d.rfc822Name),
196e500e238Sjsing .field_name = "d.rfc822Name",
197e500e238Sjsing .item = &ASN1_IA5STRING_it,
198e500e238Sjsing },
199e500e238Sjsing {
200e500e238Sjsing .flags = ASN1_TFLG_IMPLICIT,
201e500e238Sjsing .tag = GEN_DNS,
202e500e238Sjsing .offset = offsetof(GENERAL_NAME, d.dNSName),
203e500e238Sjsing .field_name = "d.dNSName",
204e500e238Sjsing .item = &ASN1_IA5STRING_it,
205e500e238Sjsing },
206e500e238Sjsing /* Don't decode this */
207e500e238Sjsing {
208e500e238Sjsing .flags = ASN1_TFLG_IMPLICIT,
209e500e238Sjsing .tag = GEN_X400,
210e500e238Sjsing .offset = offsetof(GENERAL_NAME, d.x400Address),
211e500e238Sjsing .field_name = "d.x400Address",
212e500e238Sjsing .item = &ASN1_SEQUENCE_it,
213e500e238Sjsing },
214e500e238Sjsing /* X509_NAME is a CHOICE type so use EXPLICIT */
215e500e238Sjsing {
216e500e238Sjsing .flags = ASN1_TFLG_EXPLICIT,
217e500e238Sjsing .tag = GEN_DIRNAME,
218e500e238Sjsing .offset = offsetof(GENERAL_NAME, d.directoryName),
219e500e238Sjsing .field_name = "d.directoryName",
220e500e238Sjsing .item = &X509_NAME_it,
221e500e238Sjsing },
222e500e238Sjsing {
223e500e238Sjsing .flags = ASN1_TFLG_IMPLICIT,
224e500e238Sjsing .tag = GEN_EDIPARTY,
225e500e238Sjsing .offset = offsetof(GENERAL_NAME, d.ediPartyName),
226e500e238Sjsing .field_name = "d.ediPartyName",
227e500e238Sjsing .item = &EDIPARTYNAME_it,
228e500e238Sjsing },
229e500e238Sjsing {
230e500e238Sjsing .flags = ASN1_TFLG_IMPLICIT,
231e500e238Sjsing .tag = GEN_URI,
232e500e238Sjsing .offset = offsetof(GENERAL_NAME, d.uniformResourceIdentifier),
233e500e238Sjsing .field_name = "d.uniformResourceIdentifier",
234e500e238Sjsing .item = &ASN1_IA5STRING_it,
235e500e238Sjsing },
236e500e238Sjsing {
237e500e238Sjsing .flags = ASN1_TFLG_IMPLICIT,
238e500e238Sjsing .tag = GEN_IPADD,
239e500e238Sjsing .offset = offsetof(GENERAL_NAME, d.iPAddress),
240e500e238Sjsing .field_name = "d.iPAddress",
241e500e238Sjsing .item = &ASN1_OCTET_STRING_it,
242e500e238Sjsing },
243e500e238Sjsing {
244e500e238Sjsing .flags = ASN1_TFLG_IMPLICIT,
245e500e238Sjsing .tag = GEN_RID,
246e500e238Sjsing .offset = offsetof(GENERAL_NAME, d.registeredID),
247e500e238Sjsing .field_name = "d.registeredID",
248e500e238Sjsing .item = &ASN1_OBJECT_it,
249e500e238Sjsing },
250e500e238Sjsing };
251e500e238Sjsing
252e500e238Sjsing const ASN1_ITEM GENERAL_NAME_it = {
253e500e238Sjsing .itype = ASN1_ITYPE_CHOICE,
254e500e238Sjsing .utype = offsetof(GENERAL_NAME, type),
255e500e238Sjsing .templates = GENERAL_NAME_ch_tt,
256e500e238Sjsing .tcount = sizeof(GENERAL_NAME_ch_tt) / sizeof(ASN1_TEMPLATE),
257e500e238Sjsing .funcs = NULL,
258e500e238Sjsing .size = sizeof(GENERAL_NAME),
259e500e238Sjsing .sname = "GENERAL_NAME",
260e500e238Sjsing };
261*c0ebdaf2Sbeck LCRYPTO_ALIAS(GENERAL_NAME_it);
262e500e238Sjsing
263e500e238Sjsing
264e500e238Sjsing GENERAL_NAME *
d2i_GENERAL_NAME(GENERAL_NAME ** a,const unsigned char ** in,long len)265e500e238Sjsing d2i_GENERAL_NAME(GENERAL_NAME **a, const unsigned char **in, long len)
266e500e238Sjsing {
267e500e238Sjsing return (GENERAL_NAME *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
268e500e238Sjsing &GENERAL_NAME_it);
269e500e238Sjsing }
270cedac418Stb LCRYPTO_ALIAS(d2i_GENERAL_NAME);
271e500e238Sjsing
272e500e238Sjsing int
i2d_GENERAL_NAME(GENERAL_NAME * a,unsigned char ** out)273e500e238Sjsing i2d_GENERAL_NAME(GENERAL_NAME *a, unsigned char **out)
274e500e238Sjsing {
275e500e238Sjsing return ASN1_item_i2d((ASN1_VALUE *)a, out, &GENERAL_NAME_it);
276e500e238Sjsing }
277cedac418Stb LCRYPTO_ALIAS(i2d_GENERAL_NAME);
278e500e238Sjsing
279e500e238Sjsing GENERAL_NAME *
GENERAL_NAME_new(void)280e500e238Sjsing GENERAL_NAME_new(void)
281e500e238Sjsing {
282e500e238Sjsing return (GENERAL_NAME *)ASN1_item_new(&GENERAL_NAME_it);
283e500e238Sjsing }
284cedac418Stb LCRYPTO_ALIAS(GENERAL_NAME_new);
285e500e238Sjsing
286e500e238Sjsing void
GENERAL_NAME_free(GENERAL_NAME * a)287e500e238Sjsing GENERAL_NAME_free(GENERAL_NAME *a)
288e500e238Sjsing {
289e500e238Sjsing ASN1_item_free((ASN1_VALUE *)a, &GENERAL_NAME_it);
290e500e238Sjsing }
291cedac418Stb LCRYPTO_ALIAS(GENERAL_NAME_free);
292e500e238Sjsing
293e500e238Sjsing static const ASN1_TEMPLATE GENERAL_NAMES_item_tt = {
294e500e238Sjsing .flags = ASN1_TFLG_SEQUENCE_OF,
295e500e238Sjsing .tag = 0,
296e500e238Sjsing .offset = 0,
297e500e238Sjsing .field_name = "GeneralNames",
298e500e238Sjsing .item = &GENERAL_NAME_it,
299e500e238Sjsing };
300e500e238Sjsing
301e500e238Sjsing const ASN1_ITEM GENERAL_NAMES_it = {
302e500e238Sjsing .itype = ASN1_ITYPE_PRIMITIVE,
303e500e238Sjsing .utype = -1,
304e500e238Sjsing .templates = &GENERAL_NAMES_item_tt,
305e500e238Sjsing .tcount = 0,
306e500e238Sjsing .funcs = NULL,
307e500e238Sjsing .size = 0,
308e500e238Sjsing .sname = "GENERAL_NAMES",
309e500e238Sjsing };
310*c0ebdaf2Sbeck LCRYPTO_ALIAS(GENERAL_NAMES_it);
311e500e238Sjsing
312e500e238Sjsing
313e500e238Sjsing GENERAL_NAMES *
d2i_GENERAL_NAMES(GENERAL_NAMES ** a,const unsigned char ** in,long len)314e500e238Sjsing d2i_GENERAL_NAMES(GENERAL_NAMES **a, const unsigned char **in, long len)
315e500e238Sjsing {
316e500e238Sjsing return (GENERAL_NAMES *)ASN1_item_d2i((ASN1_VALUE **)a, in, len,
317e500e238Sjsing &GENERAL_NAMES_it);
318e500e238Sjsing }
319cedac418Stb LCRYPTO_ALIAS(d2i_GENERAL_NAMES);
320e500e238Sjsing
321e500e238Sjsing int
i2d_GENERAL_NAMES(GENERAL_NAMES * a,unsigned char ** out)322e500e238Sjsing i2d_GENERAL_NAMES(GENERAL_NAMES *a, unsigned char **out)
323e500e238Sjsing {
324e500e238Sjsing return ASN1_item_i2d((ASN1_VALUE *)a, out, &GENERAL_NAMES_it);
325e500e238Sjsing }
326cedac418Stb LCRYPTO_ALIAS(i2d_GENERAL_NAMES);
327e500e238Sjsing
328e500e238Sjsing GENERAL_NAMES *
GENERAL_NAMES_new(void)329e500e238Sjsing GENERAL_NAMES_new(void)
330e500e238Sjsing {
331e500e238Sjsing return (GENERAL_NAMES *)ASN1_item_new(&GENERAL_NAMES_it);
332e500e238Sjsing }
333cedac418Stb LCRYPTO_ALIAS(GENERAL_NAMES_new);
334e500e238Sjsing
335e500e238Sjsing void
GENERAL_NAMES_free(GENERAL_NAMES * a)336e500e238Sjsing GENERAL_NAMES_free(GENERAL_NAMES *a)
337e500e238Sjsing {
338e500e238Sjsing ASN1_item_free((ASN1_VALUE *)a, &GENERAL_NAMES_it);
339e500e238Sjsing }
340cedac418Stb LCRYPTO_ALIAS(GENERAL_NAMES_free);
341e500e238Sjsing
342e500e238Sjsing GENERAL_NAME *
GENERAL_NAME_dup(GENERAL_NAME * a)343e500e238Sjsing GENERAL_NAME_dup(GENERAL_NAME *a)
344e500e238Sjsing {
345e500e238Sjsing return ASN1_item_dup(&GENERAL_NAME_it, a);
346e500e238Sjsing }
347cedac418Stb LCRYPTO_ALIAS(GENERAL_NAME_dup);
348e500e238Sjsing
349bb15212cStb static int
EDIPARTYNAME_cmp(const EDIPARTYNAME * a,const EDIPARTYNAME * b)350bb15212cStb EDIPARTYNAME_cmp(const EDIPARTYNAME *a, const EDIPARTYNAME *b)
351bb15212cStb {
352bb15212cStb int res;
353bb15212cStb
354bb15212cStb /*
355bb15212cStb * Shouldn't be possible in a valid GENERAL_NAME, but we handle it
356bb15212cStb * anyway. OTHERNAME_cmp treats NULL != NULL, so we do the same here.
357bb15212cStb */
358bb15212cStb if (a == NULL || b == NULL)
359bb15212cStb return -1;
360bb15212cStb if (a->nameAssigner == NULL && b->nameAssigner != NULL)
361bb15212cStb return -1;
362bb15212cStb if (a->nameAssigner != NULL && b->nameAssigner == NULL)
363bb15212cStb return 1;
364bb15212cStb /* If we get here, both have nameAssigner set or both unset. */
365bb15212cStb if (a->nameAssigner != NULL) {
366bb15212cStb res = ASN1_STRING_cmp(a->nameAssigner, b->nameAssigner);
367bb15212cStb if (res != 0)
368bb15212cStb return res;
369bb15212cStb }
370bb15212cStb /*
371bb15212cStb * partyName is required, so these should never be NULL. We treat it in
372bb15212cStb * the same way as the a == NULL || b == NULL case above.
373bb15212cStb */
374bb15212cStb if (a->partyName == NULL || b->partyName == NULL)
375bb15212cStb return -1;
376bb15212cStb
377bb15212cStb return ASN1_STRING_cmp(a->partyName, b->partyName);
378bb15212cStb }
379bb15212cStb
380e500e238Sjsing /* Returns 0 if they are equal, != 0 otherwise. */
381e500e238Sjsing int
GENERAL_NAME_cmp(GENERAL_NAME * a,GENERAL_NAME * b)382e500e238Sjsing GENERAL_NAME_cmp(GENERAL_NAME *a, GENERAL_NAME *b)
383e500e238Sjsing {
384e500e238Sjsing int result = -1;
385e500e238Sjsing
386e500e238Sjsing if (!a || !b || a->type != b->type)
387e500e238Sjsing return -1;
388e500e238Sjsing switch (a->type) {
389e500e238Sjsing case GEN_X400:
3907603ff28Stb result = ASN1_STRING_cmp(a->d.x400Address, b->d.x400Address);
391bb15212cStb break;
392bb15212cStb
393e500e238Sjsing case GEN_EDIPARTY:
394bb15212cStb result = EDIPARTYNAME_cmp(a->d.ediPartyName, b->d.ediPartyName);
395e500e238Sjsing break;
396e500e238Sjsing
397e500e238Sjsing case GEN_OTHERNAME:
398e500e238Sjsing result = OTHERNAME_cmp(a->d.otherName, b->d.otherName);
399e500e238Sjsing break;
400e500e238Sjsing
401e500e238Sjsing case GEN_EMAIL:
402e500e238Sjsing case GEN_DNS:
403e500e238Sjsing case GEN_URI:
404e500e238Sjsing result = ASN1_STRING_cmp(a->d.ia5, b->d.ia5);
405e500e238Sjsing break;
406e500e238Sjsing
407e500e238Sjsing case GEN_DIRNAME:
408e500e238Sjsing result = X509_NAME_cmp(a->d.dirn, b->d.dirn);
409e500e238Sjsing break;
410e500e238Sjsing
411e500e238Sjsing case GEN_IPADD:
412e500e238Sjsing result = ASN1_OCTET_STRING_cmp(a->d.ip, b->d.ip);
413e500e238Sjsing break;
414e500e238Sjsing
415e500e238Sjsing case GEN_RID:
416e500e238Sjsing result = OBJ_cmp(a->d.rid, b->d.rid);
417e500e238Sjsing break;
418e500e238Sjsing }
419e500e238Sjsing return result;
420e500e238Sjsing }
421cedac418Stb LCRYPTO_ALIAS(GENERAL_NAME_cmp);
422e500e238Sjsing
423e500e238Sjsing /* Returns 0 if they are equal, != 0 otherwise. */
424e500e238Sjsing int
OTHERNAME_cmp(OTHERNAME * a,OTHERNAME * b)425e500e238Sjsing OTHERNAME_cmp(OTHERNAME *a, OTHERNAME *b)
426e500e238Sjsing {
427e500e238Sjsing int result = -1;
428e500e238Sjsing
429e500e238Sjsing if (!a || !b)
430e500e238Sjsing return -1;
431e500e238Sjsing /* Check their type first. */
432e500e238Sjsing if ((result = OBJ_cmp(a->type_id, b->type_id)) != 0)
433e500e238Sjsing return result;
434e500e238Sjsing /* Check the value. */
435e500e238Sjsing result = ASN1_TYPE_cmp(a->value, b->value);
436e500e238Sjsing return result;
437e500e238Sjsing }
438cedac418Stb LCRYPTO_ALIAS(OTHERNAME_cmp);
439e500e238Sjsing
440e500e238Sjsing void
GENERAL_NAME_set0_value(GENERAL_NAME * a,int type,void * value)441e500e238Sjsing GENERAL_NAME_set0_value(GENERAL_NAME *a, int type, void *value)
442e500e238Sjsing {
443e500e238Sjsing switch (type) {
444e500e238Sjsing case GEN_X400:
445bb15212cStb a->d.x400Address = value;
446bb15212cStb break;
447bb15212cStb
448e500e238Sjsing case GEN_EDIPARTY:
449bb15212cStb a->d.ediPartyName = value;
450e500e238Sjsing break;
451e500e238Sjsing
452e500e238Sjsing case GEN_OTHERNAME:
453e500e238Sjsing a->d.otherName = value;
454e500e238Sjsing break;
455e500e238Sjsing
456e500e238Sjsing case GEN_EMAIL:
457e500e238Sjsing case GEN_DNS:
458e500e238Sjsing case GEN_URI:
459e500e238Sjsing a->d.ia5 = value;
460e500e238Sjsing break;
461e500e238Sjsing
462e500e238Sjsing case GEN_DIRNAME:
463e500e238Sjsing a->d.dirn = value;
464e500e238Sjsing break;
465e500e238Sjsing
466e500e238Sjsing case GEN_IPADD:
467e500e238Sjsing a->d.ip = value;
468e500e238Sjsing break;
469e500e238Sjsing
470e500e238Sjsing case GEN_RID:
471e500e238Sjsing a->d.rid = value;
472e500e238Sjsing break;
473e500e238Sjsing }
474e500e238Sjsing a->type = type;
475e500e238Sjsing }
476cedac418Stb LCRYPTO_ALIAS(GENERAL_NAME_set0_value);
477e500e238Sjsing
478e500e238Sjsing void *
GENERAL_NAME_get0_value(GENERAL_NAME * a,int * ptype)479e500e238Sjsing GENERAL_NAME_get0_value(GENERAL_NAME *a, int *ptype)
480e500e238Sjsing {
481e500e238Sjsing if (ptype)
482e500e238Sjsing *ptype = a->type;
483e500e238Sjsing switch (a->type) {
484e500e238Sjsing case GEN_X400:
485bb15212cStb return a->d.x400Address;
486bb15212cStb
487e500e238Sjsing case GEN_EDIPARTY:
488bb15212cStb return a->d.ediPartyName;
489e500e238Sjsing
490e500e238Sjsing case GEN_OTHERNAME:
491e500e238Sjsing return a->d.otherName;
492e500e238Sjsing
493e500e238Sjsing case GEN_EMAIL:
494e500e238Sjsing case GEN_DNS:
495e500e238Sjsing case GEN_URI:
496e500e238Sjsing return a->d.ia5;
497e500e238Sjsing
498e500e238Sjsing case GEN_DIRNAME:
499e500e238Sjsing return a->d.dirn;
500e500e238Sjsing
501e500e238Sjsing case GEN_IPADD:
502e500e238Sjsing return a->d.ip;
503e500e238Sjsing
504e500e238Sjsing case GEN_RID:
505e500e238Sjsing return a->d.rid;
506e500e238Sjsing
507e500e238Sjsing default:
508e500e238Sjsing return NULL;
509e500e238Sjsing }
510e500e238Sjsing }
511cedac418Stb LCRYPTO_ALIAS(GENERAL_NAME_get0_value);
512e500e238Sjsing
513e500e238Sjsing int
GENERAL_NAME_set0_othername(GENERAL_NAME * gen,ASN1_OBJECT * oid,ASN1_TYPE * value)514e500e238Sjsing GENERAL_NAME_set0_othername(GENERAL_NAME *gen, ASN1_OBJECT *oid,
515e500e238Sjsing ASN1_TYPE *value)
516e500e238Sjsing {
517e500e238Sjsing OTHERNAME *oth;
518e500e238Sjsing
519e500e238Sjsing oth = OTHERNAME_new();
520e500e238Sjsing if (!oth)
521e500e238Sjsing return 0;
522e500e238Sjsing oth->type_id = oid;
523e500e238Sjsing oth->value = value;
524e500e238Sjsing GENERAL_NAME_set0_value(gen, GEN_OTHERNAME, oth);
525e500e238Sjsing return 1;
526e500e238Sjsing }
527cedac418Stb LCRYPTO_ALIAS(GENERAL_NAME_set0_othername);
528e500e238Sjsing
529e500e238Sjsing int
GENERAL_NAME_get0_otherName(GENERAL_NAME * gen,ASN1_OBJECT ** poid,ASN1_TYPE ** pvalue)530e500e238Sjsing GENERAL_NAME_get0_otherName(GENERAL_NAME *gen, ASN1_OBJECT **poid,
531e500e238Sjsing ASN1_TYPE **pvalue)
532e500e238Sjsing {
533e500e238Sjsing if (gen->type != GEN_OTHERNAME)
534e500e238Sjsing return 0;
535e500e238Sjsing if (poid)
536e500e238Sjsing *poid = gen->d.otherName->type_id;
537e500e238Sjsing if (pvalue)
538e500e238Sjsing *pvalue = gen->d.otherName->value;
539e500e238Sjsing return 1;
540e500e238Sjsing }
541cedac418Stb LCRYPTO_ALIAS(GENERAL_NAME_get0_otherName);
542