2a861c94fe341b2d492fdc7d1ba47960916d4404
[openssl.git] / crypto / params_from_text.c
1 /*
2  * Copyright 2019 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/err.h>
13 #include <openssl/params.h>
14
15 /*
16  * When processing text to params, we're trying to be smart with numbers.
17  * Instead of handling each specific separate integer type, we use a bignum
18  * and ensure that it isn't larger than the expected size, and we then make
19  * sure it is the expected size...  if there is one given.
20  * (if the size can be arbitrary, then we give whatever we have)
21  */
22
23 static int prepare_from_text(const OSSL_PARAM *paramdefs, const char *key,
24                              const char *value, size_t value_n,
25                              /* Output parameters */
26                              const OSSL_PARAM **paramdef, int *ishex,
27                              size_t *buf_n, BIGNUM **tmpbn)
28 {
29     const OSSL_PARAM *p;
30
31     /*
32      * ishex is used to translate legacy style string controls in hex format
33      * to octet string parameters.
34      */
35     *ishex = strncmp(key, "hex", 3) == 0;
36
37     if (*ishex)
38         key += 3;
39
40     p = *paramdef = OSSL_PARAM_locate_const(paramdefs, key);
41     if (p == NULL)
42         return 0;
43
44     switch (p->data_type) {
45     case OSSL_PARAM_INTEGER:
46     case OSSL_PARAM_UNSIGNED_INTEGER:
47         if (*ishex)
48             BN_hex2bn(tmpbn, value);
49         else
50             BN_dec2bn(tmpbn, value);
51
52         if (*tmpbn == NULL)
53             return 0;
54
55         /*
56          * 2s complement negate, part 1
57          *
58          * BN_bn2nativepad puts the absolute value of the number in the
59          * buffer, i.e. if it's negative, we need to deal with it.  We do
60          * it by subtracting 1 here and inverting the bytes in
61          * construct_from_text() below.
62          */
63         if (p->data_type == OSSL_PARAM_INTEGER && BN_is_negative(*tmpbn)
64             && !BN_sub_word(*tmpbn, 1)) {
65             return 0;
66         }
67
68         *buf_n = BN_num_bytes(*tmpbn);
69
70         /*
71          * TODO(v3.0) is this the right way to do this?  This code expects
72          * a zero data size to simply mean "arbitrary size".
73          */
74         if (p->data_size > 0) {
75             if (*buf_n >= p->data_size) {
76                 CRYPTOerr(0, CRYPTO_R_TOO_SMALL_BUFFER);
77                 /* Since this is a different error, we don't break */
78                 return 0;
79             }
80             /* Change actual size to become the desired size. */
81             *buf_n = p->data_size;
82         }
83         break;
84     case OSSL_PARAM_UTF8_STRING:
85         if (*ishex) {
86             CRYPTOerr(0, ERR_R_PASSED_INVALID_ARGUMENT);
87             return 0;
88         }
89         *buf_n = strlen(value) + 1;
90         break;
91     case OSSL_PARAM_OCTET_STRING:
92         if (*ishex) {
93             *buf_n = strlen(value) >> 1;
94         }
95         break;
96     }
97
98     return 1;
99 }
100
101 static int construct_from_text(OSSL_PARAM *to, const OSSL_PARAM *paramdef,
102                                const char *value, size_t value_n, int ishex,
103                                void *buf, size_t buf_n, BIGNUM *tmpbn)
104 {
105     if (buf == NULL)
106         return 0;
107
108     switch (paramdef->data_type) {
109     case OSSL_PARAM_INTEGER:
110     case OSSL_PARAM_UNSIGNED_INTEGER:
111         /*
112         {
113             if ((new_value = OPENSSL_malloc(new_value_n)) == NULL) {
114                 BN_free(a);
115                 break;
116             }
117         */
118
119         BN_bn2nativepad(tmpbn, buf, buf_n);
120
121         /*
122          * 2s complement negate, part two.
123          *
124          * Because we did the first part on the BIGNUM itself, we can just
125          * invert all the bytes here and be done with it.
126          */
127         if (paramdef->data_type == OSSL_PARAM_INTEGER
128             && BN_is_negative(tmpbn)) {
129             unsigned char *cp;
130             size_t i = buf_n;
131
132             for (cp = buf; i-- > 0; cp++)
133                 *cp ^= 0xFF;
134         }
135         break;
136     case OSSL_PARAM_UTF8_STRING:
137         strncpy(buf, value, buf_n);
138         break;
139     case OSSL_PARAM_OCTET_STRING:
140         if (ishex) {
141             size_t l = 0;
142
143             if (!OPENSSL_hexstr2buf_ex(buf, buf_n, &l, value))
144                 return 0;
145         } else {
146             memcpy(buf, value, buf_n);
147
148         }
149         break;
150     }
151
152     *to = *paramdef;
153     to->data = buf;
154     to->data_size = buf_n;
155     to->return_size = 0;
156
157     return 1;
158 }
159
160 int OSSL_PARAM_construct_from_text(OSSL_PARAM *to,
161                                    const OSSL_PARAM *paramdefs,
162                                    const char *key, const char *value,
163                                    size_t value_n,
164                                    void *buf, size_t *buf_n)
165 {
166     const OSSL_PARAM *paramdef = NULL;
167     int ishex = 0;
168     BIGNUM *tmpbn = NULL;
169     int ok = 0;
170
171     if (to == NULL || paramdefs == NULL)
172         return 0;
173
174     if (!prepare_from_text(paramdefs, key, value, value_n,
175                            &paramdef, &ishex, buf_n, &tmpbn))
176         return 0;
177
178     /*
179      * The user gets the expected buffer size back even if the buffer isn't
180      * allocated.
181      */
182     if (buf == NULL)
183         return 1;
184
185     ok = construct_from_text(to, paramdef, value, value_n, ishex,
186                              buf, *buf_n, tmpbn);
187     BN_free(tmpbn);
188     return ok;
189 }
190
191 int OSSL_PARAM_allocate_from_text(OSSL_PARAM *to,
192                                   const OSSL_PARAM *paramdefs,
193                                   const char *key, const char *value,
194                                   size_t value_n)
195 {
196     const OSSL_PARAM *paramdef = NULL;
197     int ishex = 0;
198     void *buf = NULL;
199     size_t buf_n = 0;
200     BIGNUM *tmpbn = NULL;
201     int ok = 0;
202
203     if (to == NULL || paramdefs == NULL)
204         return 0;
205
206     if (!prepare_from_text(paramdefs, key, value, value_n,
207                            &paramdef, &ishex, &buf_n, &tmpbn))
208         return 0;
209
210     if ((buf = OPENSSL_malloc(buf_n)) == NULL) {
211         CRYPTOerr(0, ERR_R_MALLOC_FAILURE);
212         return 0;
213     }
214
215     ok = construct_from_text(to, paramdef, value, value_n, ishex,
216                              buf, buf_n, tmpbn);
217     BN_free(tmpbn);
218     return ok;
219 }