param_build_set.c 3.7 KB

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