hkdf: when HMAC key is all zeros, still set a valid key length
[openssl.git] / crypto / asn1 / asn_moid.c
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 DEFINE_STACK_OF(CONF_VALUE)
20
21 /* Simple ASN1 OID module: add all objects in a given section */
22
23 static int do_create(const char *value, const char *name);
24
25 static int oid_module_init(CONF_IMODULE *md, const CONF *cnf)
26 {
27     int i;
28     const char *oid_section;
29     STACK_OF(CONF_VALUE) *sktmp;
30     CONF_VALUE *oval;
31
32     oid_section = CONF_imodule_get_value(md);
33     if ((sktmp = NCONF_get_section(cnf, oid_section)) == NULL) {
34         ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ERROR_LOADING_SECTION);
35         return 0;
36     }
37     for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
38         oval = sk_CONF_VALUE_value(sktmp, i);
39         if (!do_create(oval->value, oval->name)) {
40             ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ADDING_OBJECT);
41             return 0;
42         }
43     }
44     return 1;
45 }
46
47 static void oid_module_finish(CONF_IMODULE *md)
48 {
49 }
50
51 void ASN1_add_oid_module(void)
52 {
53     CONF_module_add("oid_section", oid_module_init, oid_module_finish);
54 }
55
56 /*-
57  * Create an OID based on a name value pair. Accept two formats.
58  * shortname = 1.2.3.4
59  * shortname = some long name, 1.2.3.4
60  */
61
62 static int do_create(const char *value, const char *name)
63 {
64     int nid;
65     const char *ln, *ostr, *p;
66     char *lntmp = NULL;
67
68     p = strrchr(value, ',');
69     if (p == NULL) {
70         ln = name;
71         ostr = value;
72     } else {
73         ln = value;
74         ostr = p + 1;
75         if (*ostr == '\0')
76             return 0;
77         while (ossl_isspace(*ostr))
78             ostr++;
79         while (ossl_isspace(*ln))
80             ln++;
81         p--;
82         while (ossl_isspace(*p)) {
83             if (p == ln)
84                 return 0;
85             p--;
86         }
87         p++;
88         if ((lntmp = OPENSSL_malloc((p - ln) + 1)) == NULL) {
89             ASN1err(ASN1_F_DO_CREATE, ERR_R_MALLOC_FAILURE);
90             return 0;
91         }
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 }