param_build_set.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2020-2021 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. return 0;
  69. p->data_size = sz;
  70. return OSSL_PARAM_set_BN(p, bn);
  71. }
  72. return 1;
  73. }
  74. int ossl_param_build_set_bn(OSSL_PARAM_BLD *bld, OSSL_PARAM *p,
  75. const char *key, const BIGNUM *bn)
  76. {
  77. if (bld != NULL)
  78. return OSSL_PARAM_BLD_push_BN(bld, key, bn);
  79. p = OSSL_PARAM_locate(p, key);
  80. if (p != NULL)
  81. return OSSL_PARAM_set_BN(p, bn) > 0;
  82. return 1;
  83. }
  84. int ossl_param_build_set_multi_key_bn(OSSL_PARAM_BLD *bld, OSSL_PARAM *params,
  85. const char *names[],
  86. STACK_OF(BIGNUM_const) *stk)
  87. {
  88. int i, sz = sk_BIGNUM_const_num(stk);
  89. OSSL_PARAM *p;
  90. if (bld != NULL) {
  91. for (i = 0; i < sz && names[i] != NULL; ++i) {
  92. if (!OSSL_PARAM_BLD_push_BN(bld, names[i],
  93. sk_BIGNUM_const_value(stk, i)))
  94. return 0;
  95. }
  96. return 1;
  97. }
  98. for (i = 0; i < sz && names[i] != NULL; ++i) {
  99. p = OSSL_PARAM_locate(params, names[i]);
  100. if (p != NULL) {
  101. if (!OSSL_PARAM_set_BN(p, sk_BIGNUM_const_value(stk, i)))
  102. return 0;
  103. }
  104. }
  105. return 1;
  106. }