OpenSSL::config: Fix VMS guesses
[openssl.git] / crypto / params_from_text.c
1 /*
2  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3  * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
4  *
5  * Licensed under the Apache License 2.0 (the "License").  You may not use
6  * this file except in compliance with the License.  You can obtain a copy
7  * in the file LICENSE in the source distribution or at
8  * https://www.openssl.org/source/license.html
9  */
10
11 #include <string.h>
12 #include <openssl/ebcdic.h>
13 #include <openssl/err.h>
14 #include <openssl/params.h>
15
16 /*
17  * When processing text to params, we're trying to be smart with numbers.
18  * Instead of handling each specific separate integer type, we use a bignum
19  * and ensure that it isn't larger than the expected size, and we then make
20  * sure it is the expected size...  if there is one given.
21  * (if the size can be arbitrary, then we give whatever we have)
22  */
23
24 static int prepare_from_text(const OSSL_PARAM *paramdefs, const char *key,
25                              const char *value, size_t value_n,
26                              /* Output parameters */
27                              const OSSL_PARAM **paramdef, int *ishex,
28                              size_t *buf_n, BIGNUM **tmpbn, int *found)
29 {
30     const OSSL_PARAM *p;
31     size_t buf_bits;
32     int r;
33
34     /*
35      * ishex is used to translate legacy style string controls in hex format
36      * to octet string parameters.
37      */
38     *ishex = strncmp(key, "hex", 3) == 0;
39
40     if (*ishex)
41         key += 3;
42
43     p = *paramdef = OSSL_PARAM_locate_const(paramdefs, key);
44     if (found != NULL)
45         *found = p != NULL;
46     if (p == NULL)
47         return 0;
48
49     switch (p->data_type) {
50     case OSSL_PARAM_INTEGER:
51     case OSSL_PARAM_UNSIGNED_INTEGER:
52         if (*ishex)
53             r = BN_hex2bn(tmpbn, value);
54         else
55             r = BN_asc2bn(tmpbn, value);
56
57         if (r == 0 || *tmpbn == NULL)
58             return 0;
59
60         if (p->data_type == OSSL_PARAM_UNSIGNED_INTEGER
61             && BN_is_negative(*tmpbn)) {
62             ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_INVALID_NEGATIVE_VALUE);
63             return 0;
64         }
65
66         /*
67          * 2's complement negate, part 1
68          *
69          * BN_bn2nativepad puts the absolute value of the number in the
70          * buffer, i.e. if it's negative, we need to deal with it.  We do
71          * it by subtracting 1 here and inverting the bytes in
72          * construct_from_text() below.
73          * To subtract 1 from an absolute value of a negative number we
74          * actually have to add 1: -3 - 1 = -4, |-3| = 3 + 1 = 4.
75          */
76         if (p->data_type == OSSL_PARAM_INTEGER && BN_is_negative(*tmpbn)
77             && !BN_add_word(*tmpbn, 1)) {
78             return 0;
79         }
80
81         buf_bits = (size_t)BN_num_bits(*tmpbn);
82
83         /*
84          * Compensate for cases where the most significant bit in
85          * the resulting OSSL_PARAM buffer will be set after the
86          * BN_bn2nativepad() call, as the implied sign may not be
87          * correct after the second part of the 2's complement
88          * negation has been performed.
89          * We fix these cases by extending the buffer by one byte
90          * (8 bits), which will give some padding.  The second part
91          * of the 2's complement negation will do the rest.
92          */
93         if (p->data_type == OSSL_PARAM_INTEGER && buf_bits % 8 == 0)
94             buf_bits += 8;
95
96         *buf_n = (buf_bits + 7) / 8;
97
98         /*
99          * A zero data size means "arbitrary size", so only do the
100          * range checking if a size is specified.
101          */
102         if (p->data_size > 0) {
103             if (buf_bits > p->data_size * 8) {
104                 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
105                 /* Since this is a different error, we don't break */
106                 return 0;
107             }
108             /* Change actual size to become the desired size. */
109             *buf_n = p->data_size;
110         }
111         break;
112     case OSSL_PARAM_UTF8_STRING:
113         if (*ishex) {
114             ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
115             return 0;
116         }
117         *buf_n = strlen(value) + 1;
118         break;
119     case OSSL_PARAM_OCTET_STRING:
120         if (*ishex) {
121             *buf_n = strlen(value) >> 1;
122         } else {
123             *buf_n = value_n;
124         }
125         break;
126     }
127
128     return 1;
129 }
130
131 static int construct_from_text(OSSL_PARAM *to, const OSSL_PARAM *paramdef,
132                                const char *value, size_t value_n, int ishex,
133                                void *buf, size_t buf_n, BIGNUM *tmpbn)
134 {
135     if (buf == NULL)
136         return 0;
137
138     if (buf_n > 0) {
139         switch (paramdef->data_type) {
140         case OSSL_PARAM_INTEGER:
141         case OSSL_PARAM_UNSIGNED_INTEGER:
142             /*
143             {
144                 if ((new_value = OPENSSL_malloc(new_value_n)) == NULL) {
145                     BN_free(a);
146                     break;
147                 }
148             */
149
150             BN_bn2nativepad(tmpbn, buf, buf_n);
151
152             /*
153              * 2's complement negation, part two.
154              *
155              * Because we did the first part on the BIGNUM itself, we can just
156              * invert all the bytes here and be done with it.
157              */
158             if (paramdef->data_type == OSSL_PARAM_INTEGER
159                 && BN_is_negative(tmpbn)) {
160                 unsigned char *cp;
161                 size_t i = buf_n;
162
163                 for (cp = buf; i-- > 0; cp++)
164                     *cp ^= 0xFF;
165             }
166             break;
167         case OSSL_PARAM_UTF8_STRING:
168 #ifdef CHARSET_EBCDIC
169             ebcdic2ascii(buf, value, buf_n);
170 #else
171             strncpy(buf, value, buf_n);
172 #endif
173             /* Don't count the terminating NUL byte as data */
174             buf_n--;
175             break;
176         case OSSL_PARAM_OCTET_STRING:
177             if (ishex) {
178                 size_t l = 0;
179
180                 if (!OPENSSL_hexstr2buf_ex(buf, buf_n, &l, value, ':'))
181                     return 0;
182             } else {
183                 memcpy(buf, value, buf_n);
184             }
185             break;
186         }
187     }
188
189     *to = *paramdef;
190     to->data = buf;
191     to->data_size = buf_n;
192     to->return_size = OSSL_PARAM_UNMODIFIED;
193
194     return 1;
195 }
196
197 int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to,
198                                   const OSSL_PARAM *paramdefs,
199                                   const char *key, const char *value,
200                                   size_t value_n, int *found)
201 {
202     const OSSL_PARAM *paramdef = NULL;
203     int ishex = 0;
204     void *buf = NULL;
205     size_t buf_n = 0;
206     BIGNUM *tmpbn = NULL;
207     int ok = 0;
208
209     if (to == NULL || paramdefs == NULL)
210         return 0;
211
212     if (!prepare_from_text(paramdefs, key, value, value_n,
213                            &paramdef, &ishex, &buf_n, &tmpbn, found))
214         goto err;
215
216     if ((buf = OPENSSL_zalloc(buf_n > 0 ? buf_n : 1)) == NULL) {
217         ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
218         goto err;
219     }
220
221     ok = construct_from_text(to, paramdef, value, value_n, ishex,
222                              buf, buf_n, tmpbn);
223     BN_free(tmpbn);
224     if (!ok)
225         OPENSSL_free(buf);
226     return ok;
227  err:
228     BN_free(tmpbn);
229     return 0;
230 }