Move more general parts of internal/cryptlib.h to new internal/common.h
[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 "internal/common.h" /* for HAS_PREFIX */
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 = CHECK_AND_SKIP_PREFIX(key, "hex");
39
40     p = *paramdef = OSSL_PARAM_locate_const(paramdefs, key);
41     if (found != NULL)
42         *found = p != NULL;
43     if (p == NULL)
44         return 0;
45
46     switch (p->data_type) {
47     case OSSL_PARAM_INTEGER:
48     case OSSL_PARAM_UNSIGNED_INTEGER:
49         if (*ishex)
50             r = BN_hex2bn(tmpbn, value);
51         else
52             r = BN_asc2bn(tmpbn, value);
53
54         if (r == 0 || *tmpbn == NULL)
55             return 0;
56
57         /*
58          * 2s complement negate, part 1
59          *
60          * BN_bn2nativepad puts the absolute value of the number in the
61          * buffer, i.e. if it's negative, we need to deal with it.  We do
62          * it by subtracting 1 here and inverting the bytes in
63          * construct_from_text() below.
64          * To subtract 1 from an absolute value of a negative number we
65          * actually have to add 1: -3 - 1 = -4, |-3| = 3 + 1 = 4.
66          */
67         if (p->data_type == OSSL_PARAM_INTEGER && BN_is_negative(*tmpbn)
68             && !BN_add_word(*tmpbn, 1)) {
69             return 0;
70         }
71
72         buf_bits = (size_t)BN_num_bits(*tmpbn);
73         *buf_n = (buf_bits + 7) / 8;
74
75         /*
76          * A zero data size means "arbitrary size", so only do the
77          * range checking if a size is specified.
78          */
79         if (p->data_size > 0) {
80             if (buf_bits > p->data_size * 8
81                 || (p->data_type == OSSL_PARAM_INTEGER
82                     && buf_bits == p->data_size * 8)) {
83                 ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
84                 /* Since this is a different error, we don't break */
85                 return 0;
86             }
87             /* Change actual size to become the desired size. */
88             *buf_n = p->data_size;
89         }
90         break;
91     case OSSL_PARAM_UTF8_STRING:
92         if (*ishex) {
93             ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_INVALID_ARGUMENT);
94             return 0;
95         }
96         *buf_n = strlen(value) + 1;
97         break;
98     case OSSL_PARAM_OCTET_STRING:
99         if (*ishex) {
100             *buf_n = strlen(value) >> 1;
101         } else {
102             *buf_n = value_n;
103         }
104         break;
105     }
106
107     return 1;
108 }
109
110 static int construct_from_text(OSSL_PARAM *to, const OSSL_PARAM *paramdef,
111                                const char *value, size_t value_n, int ishex,
112                                void *buf, size_t buf_n, BIGNUM *tmpbn)
113 {
114     if (buf == NULL)
115         return 0;
116
117     if (buf_n > 0) {
118         switch (paramdef->data_type) {
119         case OSSL_PARAM_INTEGER:
120         case OSSL_PARAM_UNSIGNED_INTEGER:
121             /*
122             {
123                 if ((new_value = OPENSSL_malloc(new_value_n)) == NULL) {
124                     BN_free(a);
125                     break;
126                 }
127             */
128
129             BN_bn2nativepad(tmpbn, buf, buf_n);
130
131             /*
132              * 2s complement negate, part two.
133              *
134              * Because we did the first part on the BIGNUM itself, we can just
135              * invert all the bytes here and be done with it.
136              */
137             if (paramdef->data_type == OSSL_PARAM_INTEGER
138                 && BN_is_negative(tmpbn)) {
139                 unsigned char *cp;
140                 size_t i = buf_n;
141
142                 for (cp = buf; i-- > 0; cp++)
143                     *cp ^= 0xFF;
144             }
145             break;
146         case OSSL_PARAM_UTF8_STRING:
147 #ifdef CHARSET_EBCDIC
148             ebcdic2ascii(buf, value, buf_n);
149 #else
150             strncpy(buf, value, buf_n);
151 #endif
152             /* Don't count the terminating NUL byte as data */
153             buf_n--;
154             break;
155         case OSSL_PARAM_OCTET_STRING:
156             if (ishex) {
157                 size_t l = 0;
158
159                 if (!OPENSSL_hexstr2buf_ex(buf, buf_n, &l, value, ':'))
160                     return 0;
161             } else {
162                 memcpy(buf, value, buf_n);
163             }
164             break;
165         }
166     }
167
168     *to = *paramdef;
169     to->data = buf;
170     to->data_size = buf_n;
171     to->return_size = OSSL_PARAM_UNMODIFIED;
172
173     return 1;
174 }
175
176 int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to,
177                                   const OSSL_PARAM *paramdefs,
178                                   const char *key, const char *value,
179                                   size_t value_n, int *found)
180 {
181     const OSSL_PARAM *paramdef = NULL;
182     int ishex = 0;
183     void *buf = NULL;
184     size_t buf_n = 0;
185     BIGNUM *tmpbn = NULL;
186     int ok = 0;
187
188     if (to == NULL || paramdefs == NULL)
189         return 0;
190
191     if (!prepare_from_text(paramdefs, key, value, value_n,
192                            &paramdef, &ishex, &buf_n, &tmpbn, found))
193         goto err;
194
195     if ((buf = OPENSSL_zalloc(buf_n > 0 ? buf_n : 1)) == NULL) {
196         ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
197         goto err;
198     }
199
200     ok = construct_from_text(to, paramdef, value, value_n, ishex,
201                              buf, buf_n, tmpbn);
202     BN_free(tmpbn);
203     if (!ok)
204         OPENSSL_free(buf);
205     return ok;
206  err:
207     BN_free(tmpbn);
208     return 0;
209 }