asn_moid.c revision 280304
167217Sn_hibma/* asn_moid.c */
261560Sn_hibma/*
361560Sn_hibma * Written by Stephen Henson (steve@openssl.org) for the OpenSSL project
461560Sn_hibma * 2001.
561560Sn_hibma */
661560Sn_hibma/* ====================================================================
761560Sn_hibma * Copyright (c) 2001-2004 The OpenSSL Project.  All rights reserved.
861560Sn_hibma *
961560Sn_hibma * Redistribution and use in source and binary forms, with or without
1061560Sn_hibma * modification, are permitted provided that the following conditions
1161560Sn_hibma * are met:
1261560Sn_hibma *
1361560Sn_hibma * 1. Redistributions of source code must retain the above copyright
1461560Sn_hibma *    notice, this list of conditions and the following disclaimer.
1561560Sn_hibma *
1661560Sn_hibma * 2. Redistributions in binary form must reproduce the above copyright
1761560Sn_hibma *    notice, this list of conditions and the following disclaimer in
1861560Sn_hibma *    the documentation and/or other materials provided with the
1961560Sn_hibma *    distribution.
2061560Sn_hibma *
2161560Sn_hibma * 3. All advertising materials mentioning features or use of this
2261560Sn_hibma *    software must display the following acknowledgment:
2361560Sn_hibma *    "This product includes software developed by the OpenSSL Project
2461560Sn_hibma *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2561560Sn_hibma *
2661560Sn_hibma * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2761560Sn_hibma *    endorse or promote products derived from this software without
2861560Sn_hibma *    prior written permission. For written permission, please contact
2984224Sdillon *    licensing@OpenSSL.org.
3084224Sdillon *
3184224Sdillon * 5. Products derived from this software may not be called "OpenSSL"
32205728Skaiw *    nor may "OpenSSL" appear in their names without prior written
33113273Smdodd *    permission of the OpenSSL Project.
3461560Sn_hibma *
3561560Sn_hibma * 6. Redistributions of any form whatsoever must retain the following
3661560Sn_hibma *    acknowledgment:
3761560Sn_hibma *    "This product includes software developed by the OpenSSL Project
3861560Sn_hibma *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3961560Sn_hibma *
40113273Smdodd * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4161560Sn_hibma * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4261560Sn_hibma * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4361560Sn_hibma * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4461560Sn_hibma * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4561560Sn_hibma * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4661560Sn_hibma * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4761560Sn_hibma * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4861560Sn_hibma * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4961560Sn_hibma * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5061560Sn_hibma * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
5161560Sn_hibma * OF THE POSSIBILITY OF SUCH DAMAGE.
5261560Sn_hibma * ====================================================================
5361560Sn_hibma *
5461560Sn_hibma * This product includes cryptographic software written by Eric Young
5561560Sn_hibma * (eay@cryptsoft.com).  This product includes software written by Tim
5661560Sn_hibma * Hudson (tjh@cryptsoft.com).
5761560Sn_hibma *
5861560Sn_hibma */
5961560Sn_hibma
6061560Sn_hibma#include <stdio.h>
6161560Sn_hibma#include <ctype.h>
6261560Sn_hibma#include <openssl/crypto.h>
6361560Sn_hibma#include "cryptlib.h"
6461560Sn_hibma#include <openssl/conf.h>
6561560Sn_hibma#include <openssl/dso.h>
6661560Sn_hibma#include <openssl/x509.h>
6761560Sn_hibma
6861560Sn_hibma/* Simple ASN1 OID module: add all objects in a given section */
6961560Sn_hibma
7061560Sn_hibmastatic int do_create(char *value, char *name);
7161560Sn_hibma
7261560Sn_hibmastatic int oid_module_init(CONF_IMODULE *md, const CONF *cnf)
7361560Sn_hibma{
7461560Sn_hibma    int i;
7561560Sn_hibma    const char *oid_section;
7661560Sn_hibma    STACK_OF(CONF_VALUE) *sktmp;
7761560Sn_hibma    CONF_VALUE *oval;
7861560Sn_hibma    oid_section = CONF_imodule_get_value(md);
7961560Sn_hibma    if (!(sktmp = NCONF_get_section(cnf, oid_section))) {
8061560Sn_hibma        ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ERROR_LOADING_SECTION);
8161560Sn_hibma        return 0;
8261560Sn_hibma    }
8361560Sn_hibma    for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
8461560Sn_hibma        oval = sk_CONF_VALUE_value(sktmp, i);
8561560Sn_hibma        if (!do_create(oval->value, oval->name)) {
8661560Sn_hibma            ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ADDING_OBJECT);
8761560Sn_hibma            return 0;
8861560Sn_hibma        }
8961560Sn_hibma    }
9061560Sn_hibma    return 1;
9161560Sn_hibma}
9261560Sn_hibma
9361560Sn_hibmastatic void oid_module_finish(CONF_IMODULE *md)
9461560Sn_hibma{
9561560Sn_hibma    OBJ_cleanup();
9661560Sn_hibma}
9761560Sn_hibma
9861560Sn_hibmavoid ASN1_add_oid_module(void)
9961560Sn_hibma{
10061560Sn_hibma    CONF_module_add("oid_section", oid_module_init, oid_module_finish);
101113193Smdodd}
10261560Sn_hibma
10361560Sn_hibma/*-
10461560Sn_hibma * Create an OID based on a name value pair. Accept two formats.
10561560Sn_hibma * shortname = 1.2.3.4
10661560Sn_hibma * shortname = some long name, 1.2.3.4
10761560Sn_hibma */
10861560Sn_hibma
10961560Sn_hibmastatic int do_create(char *value, char *name)
11061560Sn_hibma{
111113193Smdodd    int nid;
11261560Sn_hibma    ASN1_OBJECT *oid;
11361560Sn_hibma    char *ln, *ostr, *p, *lntmp;
11461560Sn_hibma    p = strrchr(value, ',');
11561560Sn_hibma    if (!p) {
11661560Sn_hibma        ln = name;
11761560Sn_hibma        ostr = value;
11861560Sn_hibma    } else {
11961560Sn_hibma        ln = NULL;
12061560Sn_hibma        ostr = p + 1;
12161560Sn_hibma        if (!*ostr)
12261560Sn_hibma            return 0;
12361560Sn_hibma        while (isspace((unsigned char)*ostr))
12461560Sn_hibma            ostr++;
12561560Sn_hibma    }
12661560Sn_hibma
12761560Sn_hibma    nid = OBJ_create(ostr, name, ln);
12861560Sn_hibma
12961560Sn_hibma    if (nid == NID_undef)
13061560Sn_hibma        return 0;
13161560Sn_hibma
13261560Sn_hibma    if (p) {
13361560Sn_hibma        ln = value;
13461560Sn_hibma        while (isspace((unsigned char)*ln))
13561560Sn_hibma            ln++;
13661560Sn_hibma        p--;
13761560Sn_hibma        while (isspace((unsigned char)*p)) {
13861560Sn_hibma            if (p == ln)
13961560Sn_hibma                return 0;
14061560Sn_hibma            p--;
14161560Sn_hibma        }
14261560Sn_hibma        p++;
14361560Sn_hibma        lntmp = OPENSSL_malloc((p - ln) + 1);
14461560Sn_hibma        if (lntmp == NULL)
14561560Sn_hibma            return 0;
14661560Sn_hibma        memcpy(lntmp, ln, p - ln);
14761560Sn_hibma        lntmp[p - ln] = 0;
14861560Sn_hibma        oid = OBJ_nid2obj(nid);
14961560Sn_hibma        oid->ln = lntmp;
15061560Sn_hibma    }
15161560Sn_hibma
15261560Sn_hibma    return 1;
15361560Sn_hibma}
15461560Sn_hibma