160662d10Schristos /*
2*1dcdf01fSchristos  * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
360662d10Schristos  *
4*1dcdf01fSchristos  * Licensed under the OpenSSL license (the "License").  You may not use
5*1dcdf01fSchristos  * this file except in compliance with the License.  You can obtain a copy
6*1dcdf01fSchristos  * in the file LICENSE in the source distribution or at
7*1dcdf01fSchristos  * https://www.openssl.org/source/license.html
860662d10Schristos  */
960662d10Schristos 
1060662d10Schristos #include <stdio.h>
11*1dcdf01fSchristos #include "crypto/ctype.h"
1260662d10Schristos #include <openssl/crypto.h>
13*1dcdf01fSchristos #include "internal/cryptlib.h"
1460662d10Schristos #include <openssl/conf.h>
1560662d10Schristos #include <openssl/x509.h>
16*1dcdf01fSchristos #include "crypto/asn1.h"
17*1dcdf01fSchristos #include "crypto/objects.h"
1860662d10Schristos 
1960662d10Schristos /* Simple ASN1 OID module: add all objects in a given section */
2060662d10Schristos 
21*1dcdf01fSchristos static int do_create(const char *value, const char *name);
2260662d10Schristos 
oid_module_init(CONF_IMODULE * md,const CONF * cnf)2360662d10Schristos static int oid_module_init(CONF_IMODULE *md, const CONF *cnf)
2460662d10Schristos {
2560662d10Schristos     int i;
2660662d10Schristos     const char *oid_section;
2760662d10Schristos     STACK_OF(CONF_VALUE) *sktmp;
2860662d10Schristos     CONF_VALUE *oval;
29*1dcdf01fSchristos 
3060662d10Schristos     oid_section = CONF_imodule_get_value(md);
31*1dcdf01fSchristos     if ((sktmp = NCONF_get_section(cnf, oid_section)) == NULL) {
3260662d10Schristos         ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ERROR_LOADING_SECTION);
3360662d10Schristos         return 0;
3460662d10Schristos     }
3560662d10Schristos     for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
3660662d10Schristos         oval = sk_CONF_VALUE_value(sktmp, i);
3760662d10Schristos         if (!do_create(oval->value, oval->name)) {
3860662d10Schristos             ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ADDING_OBJECT);
3960662d10Schristos             return 0;
4060662d10Schristos         }
4160662d10Schristos     }
4260662d10Schristos     return 1;
4360662d10Schristos }
4460662d10Schristos 
oid_module_finish(CONF_IMODULE * md)4560662d10Schristos static void oid_module_finish(CONF_IMODULE *md)
4660662d10Schristos {
4760662d10Schristos }
4860662d10Schristos 
ASN1_add_oid_module(void)4960662d10Schristos void ASN1_add_oid_module(void)
5060662d10Schristos {
5160662d10Schristos     CONF_module_add("oid_section", oid_module_init, oid_module_finish);
5260662d10Schristos }
5360662d10Schristos 
5460662d10Schristos /*-
5560662d10Schristos  * Create an OID based on a name value pair. Accept two formats.
5660662d10Schristos  * shortname = 1.2.3.4
5760662d10Schristos  * shortname = some long name, 1.2.3.4
5860662d10Schristos  */
5960662d10Schristos 
do_create(const char * value,const char * name)60*1dcdf01fSchristos static int do_create(const char *value, const char *name)
6160662d10Schristos {
6260662d10Schristos     int nid;
63*1dcdf01fSchristos     const char *ln, *ostr, *p;
64*1dcdf01fSchristos     char *lntmp = NULL;
65*1dcdf01fSchristos 
6660662d10Schristos     p = strrchr(value, ',');
67*1dcdf01fSchristos     if (p == NULL) {
6860662d10Schristos         ln = name;
6960662d10Schristos         ostr = value;
7060662d10Schristos     } else {
7160662d10Schristos         ln = value;
72*1dcdf01fSchristos         ostr = p + 1;
73*1dcdf01fSchristos         if (*ostr == '\0')
74*1dcdf01fSchristos             return 0;
75*1dcdf01fSchristos         while (ossl_isspace(*ostr))
76*1dcdf01fSchristos             ostr++;
77*1dcdf01fSchristos         while (ossl_isspace(*ln))
7860662d10Schristos             ln++;
7960662d10Schristos         p--;
80*1dcdf01fSchristos         while (ossl_isspace(*p)) {
8160662d10Schristos             if (p == ln)
8260662d10Schristos                 return 0;
8360662d10Schristos             p--;
8460662d10Schristos         }
8560662d10Schristos         p++;
86*1dcdf01fSchristos         if ((lntmp = OPENSSL_malloc((p - ln) + 1)) == NULL) {
87*1dcdf01fSchristos             ASN1err(ASN1_F_DO_CREATE, ERR_R_MALLOC_FAILURE);
8860662d10Schristos             return 0;
89*1dcdf01fSchristos         }
9060662d10Schristos         memcpy(lntmp, ln, p - ln);
91*1dcdf01fSchristos         lntmp[p - ln] = '\0';
92*1dcdf01fSchristos         ln = lntmp;
9360662d10Schristos     }
9460662d10Schristos 
95*1dcdf01fSchristos     nid = OBJ_create(ostr, name, ln);
96*1dcdf01fSchristos 
97*1dcdf01fSchristos     OPENSSL_free(lntmp);
98*1dcdf01fSchristos 
99*1dcdf01fSchristos     return nid != NID_undef;
10060662d10Schristos }
101