param_build_set.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright 2020-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Key Management utility functions to share functionality between the export()
  11. * and get_params() methods.
  12. * export() uses OSSL_PARAM_BLD, and get_params() used the OSSL_PARAM[] to
  13. * fill in parameter data for the same key and data fields.
  14. */
  15. #include <openssl/core_names.h>
  16. #include "internal/param_build_set.h"
  17. DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
  18. int ossl_param_build_set_int(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
  19. const char *key, int num)
  20. {
  21. if (bld != NULL)
  22. return OSSL_PARAM_BLD_push_int(bld, key, num);
  23. p = OSSL_PARAM_locate(p, key);
  24. if (p != NULL)
  25. return OSSL_PARAM_set_int(p, num);
  26. return 1;
  27. }
  28. int ossl_param_build_set_long(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
  29. const char *key, long num)
  30. {
  31. if (bld != NULL)
  32. return OSSL_PARAM_BLD_push_long(bld, key, num);
  33. p = OSSL_PARAM_locate(p, key);
  34. if (p != NULL)
  35. return OSSL_PARAM_set_long(p, num);
  36. return 1;
  37. }
  38. int ossl_param_build_set_utf8_string(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
  39. const char *key, const char *buf)
  40. {
  41. if (bld != NULL)
  42. return OSSL_PARAM_BLD_push_utf8_string(bld, key, buf, 0);
  43. p = OSSL_PARAM_locate(p, key);
  44. if (p != NULL)
  45. return OSSL_PARAM_set_utf8_string(p, buf);
  46. return 1;
  47. }
  48. int ossl_param_build_set_octet_string(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
  49. const char *key,
  50. const unsigned char *data,
  51. size_t data_len)
  52. {
  53. if (bld != NULL)
  54. return OSSL_PARAM_BLD_push_octet_string(bld, key, data, data_len);
  55. p = OSSL_PARAM_locate(p, key);
  56. if (p != NULL)
  57. return OSSL_PARAM_set_octet_string(p, data, data_len);
  58. return 1;
  59. }
  60. int ossl_param_build_set_bn_pad(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
  61. const char *key, const BIGNUM *bn, size_t sz)
  62. {
  63. if (bld != NULL)
  64. return OSSL_PARAM_BLD_push_BN_pad(bld, key, bn, sz);
  65. p = OSSL_PARAM_locate(p, key);
  66. if (p != NULL) {
  67. if (sz > p->data_size) {
  68. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_TOO_SMALL_BUFFER);
  69. return 0;
  70. }
  71. p->data_size = sz;
  72. return OSSL_PARAM_set_BN(p, bn);
  73. }
  74. return 1;
  75. }
  76. int ossl_param_build_set_bn(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
  77. const char *key, const BIGNUM *bn)
  78. {
  79. if (bld != NULL)
  80. return OSSL_PARAM_BLD_push_BN(bld, key, bn);
  81. p = OSSL_PARAM_locate(p, key);
  82. if (p != NULL)
  83. return OSSL_PARAM_set_BN(p, bn) > 0;
  84. return 1;
  85. }
  86. int ossl_param_build_set_multi_key_bn(OSSL_PARAM_BLD *bld, OSSL_PARAM *params,
  87. const char *names[],
  88. STACK_OF(BIGNUM_const) *stk)
  89. {
  90. int i, sz = sk_BIGNUM_const_num(stk);
  91. OSSL_PARAM *p;
  92. if (bld != NULL) {
  93. for (i = 0; i < sz && names[i] != NULL; ++i) {
  94. if (!OSSL_PARAM_BLD_push_BN(bld, names[i],
  95. sk_BIGNUM_const_value(stk, i)))
  96. return 0;
  97. }
  98. return 1;
  99. }
  100. for (i = 0; i < sz && names[i] != NULL; ++i) {
  101. p = OSSL_PARAM_locate(params, names[i]);
  102. if (p != NULL) {
  103. if (!OSSL_PARAM_set_BN(p, sk_BIGNUM_const_value(stk, i)))
  104. return 0;
  105. }
  106. }
  107. return 1;
  108. }