1 /*
2 * Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <stdio.h>
11 #include "crypto/ctype.h"
12 #include <openssl/crypto.h>
13 #include "internal/cryptlib.h"
14 #include <openssl/conf.h>
15 #include <openssl/x509.h>
16 #include "crypto/asn1.h"
17 #include "crypto/objects.h"
18
19 /* Simple ASN1 OID module: add all objects in a given section */
20
21 static int do_create(const char *value, const char *name);
22
oid_module_init(CONF_IMODULE * md,const CONF * cnf)23 static int oid_module_init(CONF_IMODULE *md, const CONF *cnf)
24 {
25 int i;
26 const char *oid_section;
27 STACK_OF(CONF_VALUE) *sktmp;
28 CONF_VALUE *oval;
29
30 oid_section = CONF_imodule_get_value(md);
31 if ((sktmp = NCONF_get_section(cnf, oid_section)) == NULL) {
32 ERR_raise(ERR_LIB_ASN1, ASN1_R_ERROR_LOADING_SECTION);
33 return 0;
34 }
35 for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
36 oval = sk_CONF_VALUE_value(sktmp, i);
37 if (!do_create(oval->value, oval->name)) {
38 ERR_raise(ERR_LIB_ASN1, ASN1_R_ADDING_OBJECT);
39 return 0;
40 }
41 }
42 return 1;
43 }
44
oid_module_finish(CONF_IMODULE * md)45 static void oid_module_finish(CONF_IMODULE *md)
46 {
47 }
48
ASN1_add_oid_module(void)49 void ASN1_add_oid_module(void)
50 {
51 CONF_module_add("oid_section", oid_module_init, oid_module_finish);
52 }
53
54 /*-
55 * Create an OID based on a name value pair. Accept two formats.
56 * shortname = 1.2.3.4
57 * shortname = some long name, 1.2.3.4
58 */
59
do_create(const char * value,const char * name)60 static int do_create(const char *value, const char *name)
61 {
62 int nid;
63 const char *ln, *ostr, *p;
64 char *lntmp = NULL;
65
66 p = strrchr(value, ',');
67 if (p == NULL) {
68 ln = name;
69 ostr = value;
70 } else if (p == value) {
71 /* we started with a leading comma */
72 ln = name;
73 ostr = p + 1;
74 } else {
75 ln = value;
76 ostr = p + 1;
77 if (*ostr == '\0')
78 return 0;
79 while (ossl_isspace(*ostr))
80 ostr++;
81 while (ossl_isspace(*ln))
82 ln++;
83 p--;
84 while (ossl_isspace(*p)) {
85 if (p == ln)
86 return 0;
87 p--;
88 }
89 p++;
90 if ((lntmp = OPENSSL_malloc((p - ln) + 1)) == NULL)
91 return 0;
92 memcpy(lntmp, ln, p - ln);
93 lntmp[p - ln] = '\0';
94 ln = lntmp;
95 }
96
97 nid = OBJ_create(ostr, name, ln);
98
99 OPENSSL_free(lntmp);
100
101 return nid != NID_undef;
102 }
103