1ebfedea0SLionel Sambuc /* v3_genn.c */
2*0a6a1f1dSLionel Sambuc /*
3*0a6a1f1dSLionel Sambuc  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4*0a6a1f1dSLionel Sambuc  * 1999.
5ebfedea0SLionel Sambuc  */
6ebfedea0SLionel Sambuc /* ====================================================================
7ebfedea0SLionel Sambuc  * Copyright (c) 1999-2008 The OpenSSL Project.  All rights reserved.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc  * are met:
12ebfedea0SLionel Sambuc  *
13ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
18ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
19ebfedea0SLionel Sambuc  *    distribution.
20ebfedea0SLionel Sambuc  *
21ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
22ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
23ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
24ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25ebfedea0SLionel Sambuc  *
26ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
28ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
29ebfedea0SLionel Sambuc  *    licensing@OpenSSL.org.
30ebfedea0SLionel Sambuc  *
31ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
32ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
33ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
34ebfedea0SLionel Sambuc  *
35ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
36ebfedea0SLionel Sambuc  *    acknowledgment:
37ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
38ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39ebfedea0SLionel Sambuc  *
40ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
52ebfedea0SLionel Sambuc  * ====================================================================
53ebfedea0SLionel Sambuc  *
54ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
55ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
56ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
57ebfedea0SLionel Sambuc  *
58ebfedea0SLionel Sambuc  */
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc #include <stdio.h>
61ebfedea0SLionel Sambuc #include "cryptlib.h"
62ebfedea0SLionel Sambuc #include <openssl/asn1t.h>
63ebfedea0SLionel Sambuc #include <openssl/conf.h>
64ebfedea0SLionel Sambuc #include <openssl/x509v3.h>
65ebfedea0SLionel Sambuc 
66ebfedea0SLionel Sambuc ASN1_SEQUENCE(OTHERNAME) = {
67ebfedea0SLionel Sambuc         ASN1_SIMPLE(OTHERNAME, type_id, ASN1_OBJECT),
68ebfedea0SLionel Sambuc         /* Maybe have a true ANY DEFINED BY later */
69ebfedea0SLionel Sambuc         ASN1_EXP(OTHERNAME, value, ASN1_ANY, 0)
70ebfedea0SLionel Sambuc } ASN1_SEQUENCE_END(OTHERNAME)
71ebfedea0SLionel Sambuc 
72ebfedea0SLionel Sambuc IMPLEMENT_ASN1_FUNCTIONS(OTHERNAME)
73ebfedea0SLionel Sambuc 
74ebfedea0SLionel Sambuc ASN1_SEQUENCE(EDIPARTYNAME) = {
75ebfedea0SLionel Sambuc         ASN1_IMP_OPT(EDIPARTYNAME, nameAssigner, DIRECTORYSTRING, 0),
76ebfedea0SLionel Sambuc         ASN1_IMP_OPT(EDIPARTYNAME, partyName, DIRECTORYSTRING, 1)
77ebfedea0SLionel Sambuc } ASN1_SEQUENCE_END(EDIPARTYNAME)
78ebfedea0SLionel Sambuc 
79ebfedea0SLionel Sambuc IMPLEMENT_ASN1_FUNCTIONS(EDIPARTYNAME)
80ebfedea0SLionel Sambuc 
81ebfedea0SLionel Sambuc ASN1_CHOICE(GENERAL_NAME) = {
82ebfedea0SLionel Sambuc         ASN1_IMP(GENERAL_NAME, d.otherName, OTHERNAME, GEN_OTHERNAME),
83ebfedea0SLionel Sambuc         ASN1_IMP(GENERAL_NAME, d.rfc822Name, ASN1_IA5STRING, GEN_EMAIL),
84ebfedea0SLionel Sambuc         ASN1_IMP(GENERAL_NAME, d.dNSName, ASN1_IA5STRING, GEN_DNS),
85ebfedea0SLionel Sambuc         /* Don't decode this */
86ebfedea0SLionel Sambuc         ASN1_IMP(GENERAL_NAME, d.x400Address, ASN1_SEQUENCE, GEN_X400),
87ebfedea0SLionel Sambuc         /* X509_NAME is a CHOICE type so use EXPLICIT */
88ebfedea0SLionel Sambuc         ASN1_EXP(GENERAL_NAME, d.directoryName, X509_NAME, GEN_DIRNAME),
89ebfedea0SLionel Sambuc         ASN1_IMP(GENERAL_NAME, d.ediPartyName, EDIPARTYNAME, GEN_EDIPARTY),
90ebfedea0SLionel Sambuc         ASN1_IMP(GENERAL_NAME, d.uniformResourceIdentifier, ASN1_IA5STRING, GEN_URI),
91ebfedea0SLionel Sambuc         ASN1_IMP(GENERAL_NAME, d.iPAddress, ASN1_OCTET_STRING, GEN_IPADD),
92ebfedea0SLionel Sambuc         ASN1_IMP(GENERAL_NAME, d.registeredID, ASN1_OBJECT, GEN_RID)
93ebfedea0SLionel Sambuc } ASN1_CHOICE_END(GENERAL_NAME)
94ebfedea0SLionel Sambuc 
95ebfedea0SLionel Sambuc IMPLEMENT_ASN1_FUNCTIONS(GENERAL_NAME)
96ebfedea0SLionel Sambuc 
97ebfedea0SLionel Sambuc ASN1_ITEM_TEMPLATE(GENERAL_NAMES) =
98ebfedea0SLionel Sambuc         ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, GeneralNames, GENERAL_NAME)
99ebfedea0SLionel Sambuc ASN1_ITEM_TEMPLATE_END(GENERAL_NAMES)
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc IMPLEMENT_ASN1_FUNCTIONS(GENERAL_NAMES)
102ebfedea0SLionel Sambuc 
103ebfedea0SLionel Sambuc GENERAL_NAME *GENERAL_NAME_dup(GENERAL_NAME *a)
104ebfedea0SLionel Sambuc {
105ebfedea0SLionel Sambuc     return (GENERAL_NAME *)ASN1_dup((i2d_of_void *)i2d_GENERAL_NAME,
106ebfedea0SLionel Sambuc                                     (d2i_of_void *)d2i_GENERAL_NAME,
107ebfedea0SLionel Sambuc                                     (char *)a);
108ebfedea0SLionel Sambuc }
109ebfedea0SLionel Sambuc 
110ebfedea0SLionel Sambuc /* Returns 0 if they are equal, != 0 otherwise. */
GENERAL_NAME_cmp(GENERAL_NAME * a,GENERAL_NAME * b)111ebfedea0SLionel Sambuc int GENERAL_NAME_cmp(GENERAL_NAME *a, GENERAL_NAME *b)
112ebfedea0SLionel Sambuc {
113ebfedea0SLionel Sambuc     int result = -1;
114ebfedea0SLionel Sambuc 
115*0a6a1f1dSLionel Sambuc     if (!a || !b || a->type != b->type)
116*0a6a1f1dSLionel Sambuc         return -1;
117*0a6a1f1dSLionel Sambuc     switch (a->type) {
118ebfedea0SLionel Sambuc     case GEN_X400:
119ebfedea0SLionel Sambuc     case GEN_EDIPARTY:
120ebfedea0SLionel Sambuc         result = ASN1_TYPE_cmp(a->d.other, b->d.other);
121ebfedea0SLionel Sambuc         break;
122ebfedea0SLionel Sambuc 
123ebfedea0SLionel Sambuc     case GEN_OTHERNAME:
124ebfedea0SLionel Sambuc         result = OTHERNAME_cmp(a->d.otherName, b->d.otherName);
125ebfedea0SLionel Sambuc         break;
126ebfedea0SLionel Sambuc 
127ebfedea0SLionel Sambuc     case GEN_EMAIL:
128ebfedea0SLionel Sambuc     case GEN_DNS:
129ebfedea0SLionel Sambuc     case GEN_URI:
130ebfedea0SLionel Sambuc         result = ASN1_STRING_cmp(a->d.ia5, b->d.ia5);
131ebfedea0SLionel Sambuc         break;
132ebfedea0SLionel Sambuc 
133ebfedea0SLionel Sambuc     case GEN_DIRNAME:
134ebfedea0SLionel Sambuc         result = X509_NAME_cmp(a->d.dirn, b->d.dirn);
135ebfedea0SLionel Sambuc         break;
136ebfedea0SLionel Sambuc 
137ebfedea0SLionel Sambuc     case GEN_IPADD:
138ebfedea0SLionel Sambuc         result = ASN1_OCTET_STRING_cmp(a->d.ip, b->d.ip);
139ebfedea0SLionel Sambuc         break;
140ebfedea0SLionel Sambuc 
141ebfedea0SLionel Sambuc     case GEN_RID:
142ebfedea0SLionel Sambuc         result = OBJ_cmp(a->d.rid, b->d.rid);
143ebfedea0SLionel Sambuc         break;
144ebfedea0SLionel Sambuc     }
145ebfedea0SLionel Sambuc     return result;
146ebfedea0SLionel Sambuc }
147ebfedea0SLionel Sambuc 
148ebfedea0SLionel Sambuc /* Returns 0 if they are equal, != 0 otherwise. */
OTHERNAME_cmp(OTHERNAME * a,OTHERNAME * b)149ebfedea0SLionel Sambuc int OTHERNAME_cmp(OTHERNAME *a, OTHERNAME *b)
150ebfedea0SLionel Sambuc {
151ebfedea0SLionel Sambuc     int result = -1;
152ebfedea0SLionel Sambuc 
153*0a6a1f1dSLionel Sambuc     if (!a || !b)
154*0a6a1f1dSLionel Sambuc         return -1;
155ebfedea0SLionel Sambuc     /* Check their type first. */
156ebfedea0SLionel Sambuc     if ((result = OBJ_cmp(a->type_id, b->type_id)) != 0)
157ebfedea0SLionel Sambuc         return result;
158ebfedea0SLionel Sambuc     /* Check the value. */
159ebfedea0SLionel Sambuc     result = ASN1_TYPE_cmp(a->value, b->value);
160ebfedea0SLionel Sambuc     return result;
161ebfedea0SLionel Sambuc }
162ebfedea0SLionel Sambuc 
GENERAL_NAME_set0_value(GENERAL_NAME * a,int type,void * value)163ebfedea0SLionel Sambuc void GENERAL_NAME_set0_value(GENERAL_NAME *a, int type, void *value)
164ebfedea0SLionel Sambuc {
165*0a6a1f1dSLionel Sambuc     switch (type) {
166ebfedea0SLionel Sambuc     case GEN_X400:
167ebfedea0SLionel Sambuc     case GEN_EDIPARTY:
168ebfedea0SLionel Sambuc         a->d.other = value;
169ebfedea0SLionel Sambuc         break;
170ebfedea0SLionel Sambuc 
171ebfedea0SLionel Sambuc     case GEN_OTHERNAME:
172ebfedea0SLionel Sambuc         a->d.otherName = value;
173ebfedea0SLionel Sambuc         break;
174ebfedea0SLionel Sambuc 
175ebfedea0SLionel Sambuc     case GEN_EMAIL:
176ebfedea0SLionel Sambuc     case GEN_DNS:
177ebfedea0SLionel Sambuc     case GEN_URI:
178ebfedea0SLionel Sambuc         a->d.ia5 = value;
179ebfedea0SLionel Sambuc         break;
180ebfedea0SLionel Sambuc 
181ebfedea0SLionel Sambuc     case GEN_DIRNAME:
182ebfedea0SLionel Sambuc         a->d.dirn = value;
183ebfedea0SLionel Sambuc         break;
184ebfedea0SLionel Sambuc 
185ebfedea0SLionel Sambuc     case GEN_IPADD:
186ebfedea0SLionel Sambuc         a->d.ip = value;
187ebfedea0SLionel Sambuc         break;
188ebfedea0SLionel Sambuc 
189ebfedea0SLionel Sambuc     case GEN_RID:
190ebfedea0SLionel Sambuc         a->d.rid = value;
191ebfedea0SLionel Sambuc         break;
192ebfedea0SLionel Sambuc     }
193ebfedea0SLionel Sambuc     a->type = type;
194ebfedea0SLionel Sambuc }
195ebfedea0SLionel Sambuc 
GENERAL_NAME_get0_value(GENERAL_NAME * a,int * ptype)196ebfedea0SLionel Sambuc void *GENERAL_NAME_get0_value(GENERAL_NAME *a, int *ptype)
197ebfedea0SLionel Sambuc {
198ebfedea0SLionel Sambuc     if (ptype)
199ebfedea0SLionel Sambuc         *ptype = a->type;
200*0a6a1f1dSLionel Sambuc     switch (a->type) {
201ebfedea0SLionel Sambuc     case GEN_X400:
202ebfedea0SLionel Sambuc     case GEN_EDIPARTY:
203ebfedea0SLionel Sambuc         return a->d.other;
204ebfedea0SLionel Sambuc 
205ebfedea0SLionel Sambuc     case GEN_OTHERNAME:
206ebfedea0SLionel Sambuc         return a->d.otherName;
207ebfedea0SLionel Sambuc 
208ebfedea0SLionel Sambuc     case GEN_EMAIL:
209ebfedea0SLionel Sambuc     case GEN_DNS:
210ebfedea0SLionel Sambuc     case GEN_URI:
211ebfedea0SLionel Sambuc         return a->d.ia5;
212ebfedea0SLionel Sambuc 
213ebfedea0SLionel Sambuc     case GEN_DIRNAME:
214ebfedea0SLionel Sambuc         return a->d.dirn;
215ebfedea0SLionel Sambuc 
216ebfedea0SLionel Sambuc     case GEN_IPADD:
217ebfedea0SLionel Sambuc         return a->d.ip;
218ebfedea0SLionel Sambuc 
219ebfedea0SLionel Sambuc     case GEN_RID:
220ebfedea0SLionel Sambuc         return a->d.rid;
221ebfedea0SLionel Sambuc 
222ebfedea0SLionel Sambuc     default:
223ebfedea0SLionel Sambuc         return NULL;
224ebfedea0SLionel Sambuc     }
225ebfedea0SLionel Sambuc }
226ebfedea0SLionel Sambuc 
GENERAL_NAME_set0_othername(GENERAL_NAME * gen,ASN1_OBJECT * oid,ASN1_TYPE * value)227ebfedea0SLionel Sambuc int GENERAL_NAME_set0_othername(GENERAL_NAME *gen,
228ebfedea0SLionel Sambuc                                 ASN1_OBJECT *oid, ASN1_TYPE *value)
229ebfedea0SLionel Sambuc {
230ebfedea0SLionel Sambuc     OTHERNAME *oth;
231ebfedea0SLionel Sambuc     oth = OTHERNAME_new();
232ebfedea0SLionel Sambuc     if (!oth)
233ebfedea0SLionel Sambuc         return 0;
234ebfedea0SLionel Sambuc     oth->type_id = oid;
235ebfedea0SLionel Sambuc     oth->value = value;
236ebfedea0SLionel Sambuc     GENERAL_NAME_set0_value(gen, GEN_OTHERNAME, oth);
237ebfedea0SLionel Sambuc     return 1;
238ebfedea0SLionel Sambuc }
239ebfedea0SLionel Sambuc 
GENERAL_NAME_get0_otherName(GENERAL_NAME * gen,ASN1_OBJECT ** poid,ASN1_TYPE ** pvalue)240ebfedea0SLionel Sambuc int GENERAL_NAME_get0_otherName(GENERAL_NAME *gen,
241ebfedea0SLionel Sambuc                                 ASN1_OBJECT **poid, ASN1_TYPE **pvalue)
242ebfedea0SLionel Sambuc {
243ebfedea0SLionel Sambuc     if (gen->type != GEN_OTHERNAME)
244ebfedea0SLionel Sambuc         return 0;
245ebfedea0SLionel Sambuc     if (poid)
246ebfedea0SLionel Sambuc         *poid = gen->d.otherName->type_id;
247ebfedea0SLionel Sambuc     if (pvalue)
248ebfedea0SLionel Sambuc         *pvalue = gen->d.otherName->value;
249ebfedea0SLionel Sambuc     return 1;
250ebfedea0SLionel Sambuc }
251