xref: /netbsd-src/crypto/external/bsd/openssl/dist/crypto/params_dup.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1*b0d17251Schristos /*
2*b0d17251Schristos  * Copyright 2021-2022 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b0d17251Schristos  * this file except in compliance with the License.  You can obtain a copy
6*b0d17251Schristos  * in the file LICENSE in the source distribution or at
7*b0d17251Schristos  * https://www.openssl.org/source/license.html
8*b0d17251Schristos  */
9*b0d17251Schristos 
10*b0d17251Schristos #include <string.h>
11*b0d17251Schristos #include <openssl/params.h>
12*b0d17251Schristos #include <openssl/param_build.h>
13*b0d17251Schristos #include "internal/param_build_set.h"
14*b0d17251Schristos 
15*b0d17251Schristos #define OSSL_PARAM_ALLOCATED_END    127
16*b0d17251Schristos #define OSSL_PARAM_MERGE_LIST_MAX   128
17*b0d17251Schristos 
18*b0d17251Schristos #define OSSL_PARAM_BUF_PUBLIC 0
19*b0d17251Schristos #define OSSL_PARAM_BUF_SECURE 1
20*b0d17251Schristos #define OSSL_PARAM_BUF_MAX    (OSSL_PARAM_BUF_SECURE + 1)
21*b0d17251Schristos 
22*b0d17251Schristos typedef struct {
23*b0d17251Schristos     OSSL_PARAM_ALIGNED_BLOCK *alloc; /* The allocated buffer */
24*b0d17251Schristos     OSSL_PARAM_ALIGNED_BLOCK *cur;   /* Current position in the allocated buf */
25*b0d17251Schristos     size_t blocks;    /* Number of aligned blocks */
26*b0d17251Schristos     size_t alloc_sz;  /* The size of the allocated buffer (in bytes) */
27*b0d17251Schristos } OSSL_PARAM_BUF;
28*b0d17251Schristos 
ossl_param_bytes_to_blocks(size_t bytes)29*b0d17251Schristos size_t ossl_param_bytes_to_blocks(size_t bytes)
30*b0d17251Schristos {
31*b0d17251Schristos     return (bytes + OSSL_PARAM_ALIGN_SIZE - 1) / OSSL_PARAM_ALIGN_SIZE;
32*b0d17251Schristos }
33*b0d17251Schristos 
ossl_param_buf_alloc(OSSL_PARAM_BUF * out,size_t extra_blocks,int is_secure)34*b0d17251Schristos static int ossl_param_buf_alloc(OSSL_PARAM_BUF *out, size_t extra_blocks,
35*b0d17251Schristos                                 int is_secure)
36*b0d17251Schristos {
37*b0d17251Schristos     size_t sz = OSSL_PARAM_ALIGN_SIZE * (extra_blocks + out->blocks);
38*b0d17251Schristos 
39*b0d17251Schristos     out->alloc = is_secure ? OPENSSL_secure_zalloc(sz) : OPENSSL_zalloc(sz);
40*b0d17251Schristos     if (out->alloc == NULL) {
41*b0d17251Schristos         ERR_raise(ERR_LIB_CRYPTO, is_secure ? CRYPTO_R_SECURE_MALLOC_FAILURE
42*b0d17251Schristos                                             : ERR_R_MALLOC_FAILURE);
43*b0d17251Schristos         return 0;
44*b0d17251Schristos     }
45*b0d17251Schristos     out->alloc_sz = sz;
46*b0d17251Schristos     out->cur = out->alloc + extra_blocks;
47*b0d17251Schristos     return 1;
48*b0d17251Schristos }
49*b0d17251Schristos 
ossl_param_set_secure_block(OSSL_PARAM * last,void * secure_buffer,size_t secure_buffer_sz)50*b0d17251Schristos void ossl_param_set_secure_block(OSSL_PARAM *last, void *secure_buffer,
51*b0d17251Schristos                                  size_t secure_buffer_sz)
52*b0d17251Schristos {
53*b0d17251Schristos     last->key = NULL;
54*b0d17251Schristos     last->data_size = secure_buffer_sz;
55*b0d17251Schristos     last->data = secure_buffer;
56*b0d17251Schristos     last->data_type = OSSL_PARAM_ALLOCATED_END;
57*b0d17251Schristos }
58*b0d17251Schristos 
ossl_param_dup(const OSSL_PARAM * src,OSSL_PARAM * dst,OSSL_PARAM_BUF buf[OSSL_PARAM_BUF_MAX],int * param_count)59*b0d17251Schristos static OSSL_PARAM *ossl_param_dup(const OSSL_PARAM *src, OSSL_PARAM *dst,
60*b0d17251Schristos                                   OSSL_PARAM_BUF buf[OSSL_PARAM_BUF_MAX],
61*b0d17251Schristos                                   int *param_count)
62*b0d17251Schristos {
63*b0d17251Schristos     const OSSL_PARAM *in;
64*b0d17251Schristos     int has_dst = (dst != NULL);
65*b0d17251Schristos     int is_secure;
66*b0d17251Schristos     size_t param_sz, blks;
67*b0d17251Schristos 
68*b0d17251Schristos     for (in = src; in->key != NULL; in++) {
69*b0d17251Schristos         is_secure = CRYPTO_secure_allocated(in->data);
70*b0d17251Schristos         if (has_dst) {
71*b0d17251Schristos             *dst = *in;
72*b0d17251Schristos             dst->data = buf[is_secure].cur;
73*b0d17251Schristos         }
74*b0d17251Schristos 
75*b0d17251Schristos         if (in->data_type == OSSL_PARAM_OCTET_PTR
76*b0d17251Schristos             || in->data_type == OSSL_PARAM_UTF8_PTR) {
77*b0d17251Schristos             param_sz = sizeof(in->data);
78*b0d17251Schristos             if (has_dst)
79*b0d17251Schristos                 *((const void **)dst->data) = *(const void **)in->data;
80*b0d17251Schristos         } else {
81*b0d17251Schristos             param_sz = in->data_size;
82*b0d17251Schristos             if (has_dst)
83*b0d17251Schristos                 memcpy(dst->data, in->data, param_sz);
84*b0d17251Schristos         }
85*b0d17251Schristos         if (in->data_type == OSSL_PARAM_UTF8_STRING)
86*b0d17251Schristos             param_sz++; /* NULL terminator */
87*b0d17251Schristos         blks = ossl_param_bytes_to_blocks(param_sz);
88*b0d17251Schristos 
89*b0d17251Schristos         if (has_dst) {
90*b0d17251Schristos             dst++;
91*b0d17251Schristos             buf[is_secure].cur += blks;
92*b0d17251Schristos         } else {
93*b0d17251Schristos             buf[is_secure].blocks += blks;
94*b0d17251Schristos         }
95*b0d17251Schristos         if (param_count != NULL)
96*b0d17251Schristos             ++*param_count;
97*b0d17251Schristos     }
98*b0d17251Schristos     return dst;
99*b0d17251Schristos }
100*b0d17251Schristos 
OSSL_PARAM_dup(const OSSL_PARAM * src)101*b0d17251Schristos OSSL_PARAM *OSSL_PARAM_dup(const OSSL_PARAM *src)
102*b0d17251Schristos {
103*b0d17251Schristos     size_t param_blocks;
104*b0d17251Schristos     OSSL_PARAM_BUF buf[OSSL_PARAM_BUF_MAX];
105*b0d17251Schristos     OSSL_PARAM *last, *dst;
106*b0d17251Schristos     int param_count = 1; /* Include terminator in the count */
107*b0d17251Schristos 
108*b0d17251Schristos     if (src == NULL)
109*b0d17251Schristos         return NULL;
110*b0d17251Schristos 
111*b0d17251Schristos     memset(buf, 0, sizeof(buf));
112*b0d17251Schristos 
113*b0d17251Schristos     /* First Pass: get the param_count and block sizes required */
114*b0d17251Schristos     (void)ossl_param_dup(src, NULL, buf, &param_count);
115*b0d17251Schristos 
116*b0d17251Schristos     param_blocks = ossl_param_bytes_to_blocks(param_count * sizeof(*src));
117*b0d17251Schristos     /*
118*b0d17251Schristos      * The allocated buffer consists of an array of OSSL_PARAM followed by
119*b0d17251Schristos      * aligned data bytes that the array elements will point to.
120*b0d17251Schristos      */
121*b0d17251Schristos     if (!ossl_param_buf_alloc(&buf[OSSL_PARAM_BUF_PUBLIC], param_blocks, 0))
122*b0d17251Schristos         return NULL;
123*b0d17251Schristos 
124*b0d17251Schristos     /* Allocate a secure memory buffer if required */
125*b0d17251Schristos     if (buf[OSSL_PARAM_BUF_SECURE].blocks > 0
126*b0d17251Schristos         && !ossl_param_buf_alloc(&buf[OSSL_PARAM_BUF_SECURE], 0, 1)) {
127*b0d17251Schristos         OPENSSL_free(buf[OSSL_PARAM_BUF_PUBLIC].alloc);
128*b0d17251Schristos         return NULL;
129*b0d17251Schristos     }
130*b0d17251Schristos 
131*b0d17251Schristos     dst = (OSSL_PARAM *)buf[OSSL_PARAM_BUF_PUBLIC].alloc;
132*b0d17251Schristos     last = ossl_param_dup(src, dst, buf, NULL);
133*b0d17251Schristos     /* Store the allocated secure memory buffer in the last param block */
134*b0d17251Schristos     ossl_param_set_secure_block(last, buf[OSSL_PARAM_BUF_SECURE].alloc,
135*b0d17251Schristos                                 buf[OSSL_PARAM_BUF_SECURE].alloc_sz);
136*b0d17251Schristos     return dst;
137*b0d17251Schristos }
138*b0d17251Schristos 
compare_params(const void * left,const void * right)139*b0d17251Schristos static int compare_params(const void *left, const void *right)
140*b0d17251Schristos {
141*b0d17251Schristos     const OSSL_PARAM *l = *(const OSSL_PARAM **)left;
142*b0d17251Schristos     const OSSL_PARAM *r = *(const OSSL_PARAM **)right;
143*b0d17251Schristos 
144*b0d17251Schristos     return OPENSSL_strcasecmp(l->key, r->key);
145*b0d17251Schristos }
146*b0d17251Schristos 
OSSL_PARAM_merge(const OSSL_PARAM * p1,const OSSL_PARAM * p2)147*b0d17251Schristos OSSL_PARAM *OSSL_PARAM_merge(const OSSL_PARAM *p1, const OSSL_PARAM *p2)
148*b0d17251Schristos {
149*b0d17251Schristos     const OSSL_PARAM *list1[OSSL_PARAM_MERGE_LIST_MAX + 1];
150*b0d17251Schristos     const OSSL_PARAM *list2[OSSL_PARAM_MERGE_LIST_MAX + 1];
151*b0d17251Schristos     const OSSL_PARAM *p = NULL;
152*b0d17251Schristos     const OSSL_PARAM **p1cur, **p2cur;
153*b0d17251Schristos     OSSL_PARAM *params, *dst;
154*b0d17251Schristos     size_t  list1_sz = 0, list2_sz = 0;
155*b0d17251Schristos     int diff;
156*b0d17251Schristos 
157*b0d17251Schristos     if (p1 == NULL && p2 == NULL)
158*b0d17251Schristos         return NULL;
159*b0d17251Schristos 
160*b0d17251Schristos     /* Copy p1 to list1 */
161*b0d17251Schristos     if (p1 != NULL) {
162*b0d17251Schristos         for (p = p1; p->key != NULL && list1_sz < OSSL_PARAM_MERGE_LIST_MAX; p++)
163*b0d17251Schristos             list1[list1_sz++] = p;
164*b0d17251Schristos     }
165*b0d17251Schristos     list1[list1_sz] = NULL;
166*b0d17251Schristos 
167*b0d17251Schristos     /* copy p2 to a list2 */
168*b0d17251Schristos     if (p2 != NULL) {
169*b0d17251Schristos         for (p = p2; p->key != NULL && list2_sz < OSSL_PARAM_MERGE_LIST_MAX; p++)
170*b0d17251Schristos             list2[list2_sz++] = p;
171*b0d17251Schristos     }
172*b0d17251Schristos     list2[list2_sz] = NULL;
173*b0d17251Schristos     if (list1_sz == 0 && list2_sz == 0)
174*b0d17251Schristos         return NULL;
175*b0d17251Schristos 
176*b0d17251Schristos     /* Sort the 2 lists */
177*b0d17251Schristos     qsort(list1, list1_sz, sizeof(OSSL_PARAM *), compare_params);
178*b0d17251Schristos     qsort(list2, list2_sz, sizeof(OSSL_PARAM *), compare_params);
179*b0d17251Schristos 
180*b0d17251Schristos    /* Allocate enough space to store the merged parameters */
181*b0d17251Schristos     params = OPENSSL_zalloc((list1_sz + list2_sz + 1) * sizeof(*p1));
182*b0d17251Schristos     if (params == NULL) {
183*b0d17251Schristos         ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
184*b0d17251Schristos         return NULL;
185*b0d17251Schristos     }
186*b0d17251Schristos     dst = params;
187*b0d17251Schristos     p1cur = list1;
188*b0d17251Schristos     p2cur = list2;
189*b0d17251Schristos     while (1) {
190*b0d17251Schristos         /* If list1 is finished just tack list2 onto the end */
191*b0d17251Schristos         if (*p1cur == NULL) {
192*b0d17251Schristos             do {
193*b0d17251Schristos                 *dst++ = **p2cur;
194*b0d17251Schristos                 p2cur++;
195*b0d17251Schristos             } while (*p2cur != NULL);
196*b0d17251Schristos             break;
197*b0d17251Schristos         }
198*b0d17251Schristos         /* If list2 is finished just tack list1 onto the end */
199*b0d17251Schristos         if (*p2cur == NULL) {
200*b0d17251Schristos             do {
201*b0d17251Schristos                 *dst++ = **p1cur;
202*b0d17251Schristos                 p1cur++;
203*b0d17251Schristos             } while (*p1cur != NULL);
204*b0d17251Schristos             break;
205*b0d17251Schristos         }
206*b0d17251Schristos         /* consume the list element with the smaller key */
207*b0d17251Schristos         diff = OPENSSL_strcasecmp((*p1cur)->key, (*p2cur)->key);
208*b0d17251Schristos         if (diff == 0) {
209*b0d17251Schristos             /* If the keys are the same then throw away the list1 element */
210*b0d17251Schristos             *dst++ = **p2cur;
211*b0d17251Schristos             p2cur++;
212*b0d17251Schristos             p1cur++;
213*b0d17251Schristos         } else if (diff > 0) {
214*b0d17251Schristos             *dst++ = **p2cur;
215*b0d17251Schristos             p2cur++;
216*b0d17251Schristos         } else {
217*b0d17251Schristos             *dst++ = **p1cur;
218*b0d17251Schristos             p1cur++;
219*b0d17251Schristos         }
220*b0d17251Schristos     }
221*b0d17251Schristos     return params;
222*b0d17251Schristos }
223*b0d17251Schristos 
OSSL_PARAM_free(OSSL_PARAM * params)224*b0d17251Schristos void OSSL_PARAM_free(OSSL_PARAM *params)
225*b0d17251Schristos {
226*b0d17251Schristos     if (params != NULL) {
227*b0d17251Schristos         OSSL_PARAM *p;
228*b0d17251Schristos 
229*b0d17251Schristos         for (p = params; p->key != NULL; p++)
230*b0d17251Schristos             ;
231*b0d17251Schristos         if (p->data_type == OSSL_PARAM_ALLOCATED_END)
232*b0d17251Schristos             OPENSSL_secure_clear_free(p->data, p->data_size);
233*b0d17251Schristos         OPENSSL_free(params);
234*b0d17251Schristos     }
235*b0d17251Schristos }
236