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