/*
- * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
#include <openssl/err.h>
#include <openssl/cryptoerr.h>
#include <openssl/params.h>
+#include <openssl/types.h>
+#include <openssl/safestack.h>
#include "internal/cryptlib.h"
-#include "internal/param_build.h"
+#include "openssl/param_build.h"
+/*
+ * Special internal param type to indicate the end of an allocate OSSL_PARAM
+ * array.
+ */
#define OSSL_PARAM_ALLOCATED_END 127
+typedef struct {
+ const char *key;
+ int type;
+ int secure;
+ size_t size;
+ size_t alloc_blocks;
+ const BIGNUM *bn;
+ const void *string;
+ union {
+ /*
+ * These fields are never directly addressed, but their sizes are
+ * imporant so that all native types can be copied here without overrun.
+ */
+ ossl_intmax_t i;
+ ossl_uintmax_t u;
+ double d;
+ } num;
+} OSSL_PARAM_BLD_DEF;
+
+DEFINE_STACK_OF(OSSL_PARAM_BLD_DEF)
+
+struct ossl_param_bld_st {
+ size_t total_blocks;
+ size_t secure_blocks;
+ STACK_OF(OSSL_PARAM_BLD_DEF) *params;
+};
+
typedef union {
OSSL_UNION_ALIGN;
} OSSL_PARAM_BLD_BLOCK;
int size, size_t alloc, int type,
int secure)
{
- OSSL_PARAM_BLD_DEF *pd;
+ OSSL_PARAM_BLD_DEF *pd = OPENSSL_zalloc(sizeof(*pd));
- if (bld->curr >= OSSL_PARAM_BLD_MAX) {
- CRYPTOerr(CRYPTO_F_PARAM_PUSH, CRYPTO_R_TOO_MANY_RECORDS);
+ if (pd == NULL) {
+ CRYPTOerr(CRYPTO_F_PARAM_PUSH, ERR_R_MALLOC_FAILURE);
return NULL;
}
- pd = bld->params + bld->curr++;
- memset(pd, 0, sizeof(*pd));
pd->key = key;
pd->type = type;
pd->size = size;
bld->secure_blocks += pd->alloc_blocks;
else
bld->total_blocks += pd->alloc_blocks;
+ if (sk_OSSL_PARAM_BLD_DEF_push(bld->params, pd) <= 0) {
+ OPENSSL_free(pd);
+ pd = NULL;
+ }
return pd;
}
return 1;
}
-void ossl_param_bld_init(OSSL_PARAM_BLD *bld)
+OSSL_PARAM_BLD *OSSL_PARAM_BLD_new(void)
+{
+ OSSL_PARAM_BLD *r = OPENSSL_zalloc(sizeof(OSSL_PARAM_BLD));
+
+ if (r != NULL) {
+ r->params = sk_OSSL_PARAM_BLD_DEF_new_null();
+ if (r->params == NULL) {
+ OPENSSL_free(r);
+ r = NULL;
+ }
+ }
+ return r;
+}
+
+static void free_all_params(OSSL_PARAM_BLD *bld)
+{
+ int i, n = sk_OSSL_PARAM_BLD_DEF_num(bld->params);
+
+ for (i = 0; i < n; i++)
+ OPENSSL_free(sk_OSSL_PARAM_BLD_DEF_pop(bld->params));
+}
+
+void OSSL_PARAM_BLD_free(OSSL_PARAM_BLD *bld)
{
- memset(bld, 0, sizeof(*bld));
+ if (bld == NULL)
+ return;
+ free_all_params(bld);
+ sk_OSSL_PARAM_BLD_DEF_free(bld->params);
+ OPENSSL_free(bld);
}
-int ossl_param_bld_push_int(OSSL_PARAM_BLD *bld, const char *key, int num)
+int OSSL_PARAM_BLD_push_int(OSSL_PARAM_BLD *bld, const char *key, int num)
{
return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
}
-int ossl_param_bld_push_uint(OSSL_PARAM_BLD *bld, const char *key,
+int OSSL_PARAM_BLD_push_uint(OSSL_PARAM_BLD *bld, const char *key,
unsigned int num)
{
return param_push_num(bld, key, &num, sizeof(num),
OSSL_PARAM_UNSIGNED_INTEGER);
}
-int ossl_param_bld_push_long(OSSL_PARAM_BLD *bld, const char *key,
+int OSSL_PARAM_BLD_push_long(OSSL_PARAM_BLD *bld, const char *key,
long int num)
{
return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
}
-int ossl_param_bld_push_ulong(OSSL_PARAM_BLD *bld, const char *key,
+int OSSL_PARAM_BLD_push_ulong(OSSL_PARAM_BLD *bld, const char *key,
unsigned long int num)
{
return param_push_num(bld, key, &num, sizeof(num),
OSSL_PARAM_UNSIGNED_INTEGER);
}
-int ossl_param_bld_push_int32(OSSL_PARAM_BLD *bld, const char *key,
+int OSSL_PARAM_BLD_push_int32(OSSL_PARAM_BLD *bld, const char *key,
int32_t num)
{
return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
}
-int ossl_param_bld_push_uint32(OSSL_PARAM_BLD *bld, const char *key,
+int OSSL_PARAM_BLD_push_uint32(OSSL_PARAM_BLD *bld, const char *key,
uint32_t num)
{
return param_push_num(bld, key, &num, sizeof(num),
OSSL_PARAM_UNSIGNED_INTEGER);
}
-int ossl_param_bld_push_int64(OSSL_PARAM_BLD *bld, const char *key,
+int OSSL_PARAM_BLD_push_int64(OSSL_PARAM_BLD *bld, const char *key,
int64_t num)
{
return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_INTEGER);
}
-int ossl_param_bld_push_uint64(OSSL_PARAM_BLD *bld, const char *key,
+int OSSL_PARAM_BLD_push_uint64(OSSL_PARAM_BLD *bld, const char *key,
uint64_t num)
{
return param_push_num(bld, key, &num, sizeof(num),
OSSL_PARAM_UNSIGNED_INTEGER);
}
-int ossl_param_bld_push_size_t(OSSL_PARAM_BLD *bld, const char *key,
+int OSSL_PARAM_BLD_push_size_t(OSSL_PARAM_BLD *bld, const char *key,
size_t num)
{
return param_push_num(bld, key, &num, sizeof(num),
OSSL_PARAM_UNSIGNED_INTEGER);
}
-int ossl_param_bld_push_double(OSSL_PARAM_BLD *bld, const char *key,
+int OSSL_PARAM_BLD_push_double(OSSL_PARAM_BLD *bld, const char *key,
double num)
{
return param_push_num(bld, key, &num, sizeof(num), OSSL_PARAM_REAL);
}
-int ossl_param_bld_push_BN(OSSL_PARAM_BLD *bld, const char *key,
+int OSSL_PARAM_BLD_push_BN(OSSL_PARAM_BLD *bld, const char *key,
const BIGNUM *bn)
{
- return ossl_param_bld_push_BN_pad(bld, key, bn,
+ return OSSL_PARAM_BLD_push_BN_pad(bld, key, bn,
bn == NULL ? 0 : BN_num_bytes(bn));
}
-int ossl_param_bld_push_BN_pad(OSSL_PARAM_BLD *bld, const char *key,
+int OSSL_PARAM_BLD_push_BN_pad(OSSL_PARAM_BLD *bld, const char *key,
const BIGNUM *bn, size_t sz)
{
int n, secure = 0;
return 1;
}
-int ossl_param_bld_push_utf8_string(OSSL_PARAM_BLD *bld, const char *key,
+int OSSL_PARAM_BLD_push_utf8_string(OSSL_PARAM_BLD *bld, const char *key,
const char *buf, size_t bsize)
{
OSSL_PARAM_BLD_DEF *pd;
return 1;
}
-int ossl_param_bld_push_utf8_ptr(OSSL_PARAM_BLD *bld, const char *key,
+int OSSL_PARAM_BLD_push_utf8_ptr(OSSL_PARAM_BLD *bld, const char *key,
char *buf, size_t bsize)
{
OSSL_PARAM_BLD_DEF *pd;
return 1;
}
-int ossl_param_bld_push_octet_string(OSSL_PARAM_BLD *bld, const char *key,
+int OSSL_PARAM_BLD_push_octet_string(OSSL_PARAM_BLD *bld, const char *key,
const void *buf, size_t bsize)
{
OSSL_PARAM_BLD_DEF *pd;
return 1;
}
-int ossl_param_bld_push_octet_ptr(OSSL_PARAM_BLD *bld, const char *key,
+int OSSL_PARAM_BLD_push_octet_ptr(OSSL_PARAM_BLD *bld, const char *key,
void *buf, size_t bsize)
{
OSSL_PARAM_BLD_DEF *pd;
OSSL_PARAM_BLD_BLOCK *blk,
OSSL_PARAM_BLD_BLOCK *secure)
{
- size_t i;
+ int i, num = sk_OSSL_PARAM_BLD_DEF_num(bld->params);
OSSL_PARAM_BLD_DEF *pd;
void *p;
- for (i = 0; i < bld->curr; i++) {
- pd = bld->params + i;
+ for (i = 0; i < num; i++) {
+ pd = sk_OSSL_PARAM_BLD_DEF_value(bld->params, i);
param[i].key = pd->key;
param[i].data_type = pd->type;
param[i].data_size = pd->size;
- param[i].return_size = 0;
+ param[i].return_size = OSSL_PARAM_UNMODIFIED;
if (pd->secure) {
p = secure;
return param + i;
}
-OSSL_PARAM *ossl_param_bld_to_param(OSSL_PARAM_BLD *bld)
+OSSL_PARAM *OSSL_PARAM_BLD_to_param(OSSL_PARAM_BLD *bld)
{
OSSL_PARAM_BLD_BLOCK *blk, *s = NULL;
OSSL_PARAM *params, *last;
- const size_t p_blks = bytes_to_blocks((1 + bld->curr) * sizeof(*params));
+ const int num = sk_OSSL_PARAM_BLD_DEF_num(bld->params);
+ const size_t p_blks = bytes_to_blocks((1 + num) * sizeof(*params));
const size_t total = ALIGN_SIZE * (p_blks + bld->total_blocks);
const size_t ss = ALIGN_SIZE * bld->secure_blocks;
last->data_size = ss;
last->data = s;
last->data_type = OSSL_PARAM_ALLOCATED_END;
+
+ /* Reset builder for reuse */
+ bld->total_blocks = 0;
+ bld->secure_blocks = 0;
+ free_all_params(bld);
return params;
}
-void ossl_param_bld_free(OSSL_PARAM *params)
+void OSSL_PARAM_BLD_free_params(OSSL_PARAM *params)
{
if (params != NULL) {
OSSL_PARAM *p;