159191Skris/* v3_info.c */
2296465Sdelphij/*
3296465Sdelphij * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4296465Sdelphij * 1999.
559191Skris */
659191Skris/* ====================================================================
759191Skris * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
859191Skris *
959191Skris * Redistribution and use in source and binary forms, with or without
1059191Skris * modification, are permitted provided that the following conditions
1159191Skris * are met:
1259191Skris *
1359191Skris * 1. Redistributions of source code must retain the above copyright
14296465Sdelphij *    notice, this list of conditions and the following disclaimer.
1559191Skris *
1659191Skris * 2. Redistributions in binary form must reproduce the above copyright
1759191Skris *    notice, this list of conditions and the following disclaimer in
1859191Skris *    the documentation and/or other materials provided with the
1959191Skris *    distribution.
2059191Skris *
2159191Skris * 3. All advertising materials mentioning features or use of this
2259191Skris *    software must display the following acknowledgment:
2359191Skris *    "This product includes software developed by the OpenSSL Project
2459191Skris *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2559191Skris *
2659191Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2759191Skris *    endorse or promote products derived from this software without
2859191Skris *    prior written permission. For written permission, please contact
2959191Skris *    licensing@OpenSSL.org.
3059191Skris *
3159191Skris * 5. Products derived from this software may not be called "OpenSSL"
3259191Skris *    nor may "OpenSSL" appear in their names without prior written
3359191Skris *    permission of the OpenSSL Project.
3459191Skris *
3559191Skris * 6. Redistributions of any form whatsoever must retain the following
3659191Skris *    acknowledgment:
3759191Skris *    "This product includes software developed by the OpenSSL Project
3859191Skris *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3959191Skris *
4059191Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4159191Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4259191Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4359191Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4459191Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4559191Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4659191Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4759191Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4859191Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4959191Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5059191Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
5159191Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
5259191Skris * ====================================================================
5359191Skris *
5459191Skris * This product includes cryptographic software written by Eric Young
5559191Skris * (eay@cryptsoft.com).  This product includes software written by Tim
5659191Skris * Hudson (tjh@cryptsoft.com).
5759191Skris *
5859191Skris */
5959191Skris
6059191Skris#include <stdio.h>
6159191Skris#include "cryptlib.h"
6259191Skris#include <openssl/conf.h>
6359191Skris#include <openssl/asn1.h>
64109998Smarkm#include <openssl/asn1t.h>
6559191Skris#include <openssl/x509v3.h>
6659191Skris
67296465Sdelphijstatic STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD
68296465Sdelphij                                                       *method, AUTHORITY_INFO_ACCESS
69296465Sdelphij                                                       *ainfo, STACK_OF(CONF_VALUE)
70296465Sdelphij                                                       *ret);
71296465Sdelphijstatic AUTHORITY_INFO_ACCESS *v2i_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD
72296465Sdelphij                                                        *method,
73296465Sdelphij                                                        X509V3_CTX *ctx,
74296465Sdelphij                                                        STACK_OF(CONF_VALUE)
75296465Sdelphij                                                        *nval);
7659191Skris
77296465Sdelphijconst X509V3_EXT_METHOD v3_info = { NID_info_access, X509V3_EXT_MULTILINE,
78296465Sdelphij    ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS),
79296465Sdelphij    0, 0, 0, 0,
80296465Sdelphij    0, 0,
81296465Sdelphij    (X509V3_EXT_I2V) i2v_AUTHORITY_INFO_ACCESS,
82296465Sdelphij    (X509V3_EXT_V2I)v2i_AUTHORITY_INFO_ACCESS,
83296465Sdelphij    0, 0,
84296465Sdelphij    NULL
85296465Sdelphij};
8659191Skris
87296465Sdelphijconst X509V3_EXT_METHOD v3_sinfo = { NID_sinfo_access, X509V3_EXT_MULTILINE,
88296465Sdelphij    ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS),
89296465Sdelphij    0, 0, 0, 0,
90296465Sdelphij    0, 0,
91296465Sdelphij    (X509V3_EXT_I2V) i2v_AUTHORITY_INFO_ACCESS,
92296465Sdelphij    (X509V3_EXT_V2I)v2i_AUTHORITY_INFO_ACCESS,
93296465Sdelphij    0, 0,
94296465Sdelphij    NULL
95296465Sdelphij};
96109998Smarkm
97109998SmarkmASN1_SEQUENCE(ACCESS_DESCRIPTION) = {
98296465Sdelphij        ASN1_SIMPLE(ACCESS_DESCRIPTION, method, ASN1_OBJECT),
99296465Sdelphij        ASN1_SIMPLE(ACCESS_DESCRIPTION, location, GENERAL_NAME)
100109998Smarkm} ASN1_SEQUENCE_END(ACCESS_DESCRIPTION)
101109998Smarkm
102109998SmarkmIMPLEMENT_ASN1_FUNCTIONS(ACCESS_DESCRIPTION)
103109998Smarkm
104296465SdelphijASN1_ITEM_TEMPLATE(AUTHORITY_INFO_ACCESS) =
105296465Sdelphij        ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, GeneralNames, ACCESS_DESCRIPTION)
106109998SmarkmASN1_ITEM_TEMPLATE_END(AUTHORITY_INFO_ACCESS)
107109998Smarkm
108109998SmarkmIMPLEMENT_ASN1_FUNCTIONS(AUTHORITY_INFO_ACCESS)
109109998Smarkm
110296465Sdelphijstatic STACK_OF(CONF_VALUE) *i2v_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD
111296465Sdelphij                                                       *method, AUTHORITY_INFO_ACCESS
112296465Sdelphij                                                       *ainfo, STACK_OF(CONF_VALUE)
113296465Sdelphij                                                       *ret)
11459191Skris{
115296465Sdelphij    ACCESS_DESCRIPTION *desc;
116296465Sdelphij    int i, nlen;
117296465Sdelphij    char objtmp[80], *ntmp;
118296465Sdelphij    CONF_VALUE *vtmp;
119296465Sdelphij    for (i = 0; i < sk_ACCESS_DESCRIPTION_num(ainfo); i++) {
120296465Sdelphij        desc = sk_ACCESS_DESCRIPTION_value(ainfo, i);
121296465Sdelphij        ret = i2v_GENERAL_NAME(method, desc->location, ret);
122296465Sdelphij        if (!ret)
123296465Sdelphij            break;
124296465Sdelphij        vtmp = sk_CONF_VALUE_value(ret, i);
125296465Sdelphij        i2t_ASN1_OBJECT(objtmp, sizeof objtmp, desc->method);
126296465Sdelphij        nlen = strlen(objtmp) + strlen(vtmp->name) + 5;
127296465Sdelphij        ntmp = OPENSSL_malloc(nlen);
128296465Sdelphij        if (!ntmp) {
129296465Sdelphij            X509V3err(X509V3_F_I2V_AUTHORITY_INFO_ACCESS,
130296465Sdelphij                      ERR_R_MALLOC_FAILURE);
131296465Sdelphij            return NULL;
132296465Sdelphij        }
133296465Sdelphij        BUF_strlcpy(ntmp, objtmp, nlen);
134296465Sdelphij        BUF_strlcat(ntmp, " - ", nlen);
135296465Sdelphij        BUF_strlcat(ntmp, vtmp->name, nlen);
136296465Sdelphij        OPENSSL_free(vtmp->name);
137296465Sdelphij        vtmp->name = ntmp;
138296465Sdelphij
139296465Sdelphij    }
140296465Sdelphij    if (!ret)
141296465Sdelphij        return sk_CONF_VALUE_new_null();
142296465Sdelphij    return ret;
14359191Skris}
14459191Skris
145296465Sdelphijstatic AUTHORITY_INFO_ACCESS *v2i_AUTHORITY_INFO_ACCESS(X509V3_EXT_METHOD
146296465Sdelphij                                                        *method,
147296465Sdelphij                                                        X509V3_CTX *ctx,
148296465Sdelphij                                                        STACK_OF(CONF_VALUE)
149296465Sdelphij                                                        *nval)
15059191Skris{
151296465Sdelphij    AUTHORITY_INFO_ACCESS *ainfo = NULL;
152296465Sdelphij    CONF_VALUE *cnf, ctmp;
153296465Sdelphij    ACCESS_DESCRIPTION *acc;
154296465Sdelphij    int i, objlen;
155296465Sdelphij    char *objtmp, *ptmp;
156296465Sdelphij    if (!(ainfo = sk_ACCESS_DESCRIPTION_new_null())) {
157296465Sdelphij        X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS, ERR_R_MALLOC_FAILURE);
158296465Sdelphij        return NULL;
159296465Sdelphij    }
160296465Sdelphij    for (i = 0; i < sk_CONF_VALUE_num(nval); i++) {
161296465Sdelphij        cnf = sk_CONF_VALUE_value(nval, i);
162296465Sdelphij        if (!(acc = ACCESS_DESCRIPTION_new())
163296465Sdelphij            || !sk_ACCESS_DESCRIPTION_push(ainfo, acc)) {
164296465Sdelphij            X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,
165296465Sdelphij                      ERR_R_MALLOC_FAILURE);
166296465Sdelphij            goto err;
167296465Sdelphij        }
168296465Sdelphij        ptmp = strchr(cnf->name, ';');
169296465Sdelphij        if (!ptmp) {
170296465Sdelphij            X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,
171296465Sdelphij                      X509V3_R_INVALID_SYNTAX);
172296465Sdelphij            goto err;
173296465Sdelphij        }
174296465Sdelphij        objlen = ptmp - cnf->name;
175296465Sdelphij        ctmp.name = ptmp + 1;
176296465Sdelphij        ctmp.value = cnf->value;
177296465Sdelphij        if (!v2i_GENERAL_NAME_ex(acc->location, method, ctx, &ctmp, 0))
178296465Sdelphij            goto err;
179296465Sdelphij        if (!(objtmp = OPENSSL_malloc(objlen + 1))) {
180296465Sdelphij            X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,
181296465Sdelphij                      ERR_R_MALLOC_FAILURE);
182296465Sdelphij            goto err;
183296465Sdelphij        }
184296465Sdelphij        strncpy(objtmp, cnf->name, objlen);
185296465Sdelphij        objtmp[objlen] = 0;
186296465Sdelphij        acc->method = OBJ_txt2obj(objtmp, 0);
187296465Sdelphij        if (!acc->method) {
188296465Sdelphij            X509V3err(X509V3_F_V2I_AUTHORITY_INFO_ACCESS,
189296465Sdelphij                      X509V3_R_BAD_OBJECT);
190296465Sdelphij            ERR_add_error_data(2, "value=", objtmp);
191296465Sdelphij            OPENSSL_free(objtmp);
192296465Sdelphij            goto err;
193296465Sdelphij        }
194296465Sdelphij        OPENSSL_free(objtmp);
19559191Skris
196296465Sdelphij    }
197296465Sdelphij    return ainfo;
198296465Sdelphij err:
199296465Sdelphij    sk_ACCESS_DESCRIPTION_pop_free(ainfo, ACCESS_DESCRIPTION_free);
200296465Sdelphij    return NULL;
20159191Skris}
20259191Skris
203296465Sdelphijint i2a_ACCESS_DESCRIPTION(BIO *bp, ACCESS_DESCRIPTION *a)
204296465Sdelphij{
205296465Sdelphij    i2a_ASN1_OBJECT(bp, a->method);
206109998Smarkm#ifdef UNDEF
207296465Sdelphij    i2a_GENERAL_NAME(bp, a->location);
208109998Smarkm#endif
209296465Sdelphij    return 2;
210296465Sdelphij}
211