/src/openssl/crypto/asn1/asn_moid.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 "internal/ctype.h" |
12 | | #include <openssl/crypto.h> |
13 | | #include "internal/cryptlib.h" |
14 | | #include <openssl/conf.h> |
15 | | #include <openssl/x509.h> |
16 | | #include "internal/asn1_int.h" |
17 | | #include "internal/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 | | |
23 | | static int oid_module_init(CONF_IMODULE *md, const CONF *cnf) |
24 | 0 | { |
25 | 0 | int i; |
26 | 0 | const char *oid_section; |
27 | 0 | STACK_OF(CONF_VALUE) *sktmp; |
28 | 0 | CONF_VALUE *oval; |
29 | 0 |
|
30 | 0 | oid_section = CONF_imodule_get_value(md); |
31 | 0 | if ((sktmp = NCONF_get_section(cnf, oid_section)) == NULL) { |
32 | 0 | ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ERROR_LOADING_SECTION); |
33 | 0 | return 0; |
34 | 0 | } |
35 | 0 | for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) { |
36 | 0 | oval = sk_CONF_VALUE_value(sktmp, i); |
37 | 0 | if (!do_create(oval->value, oval->name)) { |
38 | 0 | ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ADDING_OBJECT); |
39 | 0 | return 0; |
40 | 0 | } |
41 | 0 | } |
42 | 0 | return 1; |
43 | 0 | } |
44 | | |
45 | | static void oid_module_finish(CONF_IMODULE *md) |
46 | 0 | { |
47 | 0 | } |
48 | | |
49 | | void ASN1_add_oid_module(void) |
50 | 0 | { |
51 | 0 | CONF_module_add("oid_section", oid_module_init, oid_module_finish); |
52 | 0 | } |
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 | | |
60 | | static int do_create(const char *value, const char *name) |
61 | 0 | { |
62 | 0 | int nid; |
63 | 0 | const char *ln, *ostr, *p; |
64 | 0 | char *lntmp = NULL; |
65 | 0 |
|
66 | 0 | p = strrchr(value, ','); |
67 | 0 | if (p == NULL) { |
68 | 0 | ln = name; |
69 | 0 | ostr = value; |
70 | 0 | } else { |
71 | 0 | ln = value; |
72 | 0 | ostr = p + 1; |
73 | 0 | if (*ostr == '\0') |
74 | 0 | return 0; |
75 | 0 | while (ossl_isspace(*ostr)) |
76 | 0 | ostr++; |
77 | 0 | while (ossl_isspace(*ln)) |
78 | 0 | ln++; |
79 | 0 | p--; |
80 | 0 | while (ossl_isspace(*p)) { |
81 | 0 | if (p == ln) |
82 | 0 | return 0; |
83 | 0 | p--; |
84 | 0 | } |
85 | 0 | p++; |
86 | 0 | if ((lntmp = OPENSSL_malloc((p - ln) + 1)) == NULL) { |
87 | 0 | ASN1err(ASN1_F_DO_CREATE, ERR_R_MALLOC_FAILURE); |
88 | 0 | return 0; |
89 | 0 | } |
90 | 0 | memcpy(lntmp, ln, p - ln); |
91 | 0 | lntmp[p - ln] = '\0'; |
92 | 0 | ln = lntmp; |
93 | 0 | } |
94 | 0 |
|
95 | 0 | nid = OBJ_create(ostr, name, ln); |
96 | 0 |
|
97 | 0 | OPENSSL_free(lntmp); |
98 | 0 |
|
99 | 0 | return nid != NID_undef; |
100 | 0 | } |