EVP_PKEY_DSA_paramfromdata.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*-
  2. * Copyright 2022-2023 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. * Example showing how to load DSA params from raw data
  11. * using EVP_PKEY_fromdata()
  12. */
  13. #include <openssl/param_build.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/core_names.h>
  16. #include "dsa.inc"
  17. int main(int argc, char **argv)
  18. {
  19. int ret = EXIT_FAILURE;
  20. OSSL_LIB_CTX *libctx = NULL;
  21. const char *propq = NULL;
  22. EVP_PKEY_CTX *ctx = NULL;
  23. EVP_PKEY *dsaparamkey = NULL;
  24. OSSL_PARAM_BLD *bld = NULL;
  25. OSSL_PARAM *params = NULL;
  26. BIGNUM *p = NULL, *q = NULL, *g = NULL;
  27. p = BN_bin2bn(dsa_p, sizeof(dsa_p), NULL);
  28. q = BN_bin2bn(dsa_q, sizeof(dsa_q), NULL);
  29. g = BN_bin2bn(dsa_g, sizeof(dsa_g), NULL);
  30. if (p == NULL || q == NULL || g == NULL)
  31. goto cleanup;
  32. /* Use OSSL_PARAM_BLD if you need to handle BIGNUM Parameters */
  33. bld = OSSL_PARAM_BLD_new();
  34. if (bld == NULL)
  35. goto cleanup;
  36. if (!OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p)
  37. || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q)
  38. || !OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g))
  39. goto cleanup;
  40. params = OSSL_PARAM_BLD_to_param(bld);
  41. if (params == NULL)
  42. goto cleanup;
  43. ctx = EVP_PKEY_CTX_new_from_name(libctx, "DSA", propq);
  44. if (ctx == NULL) {
  45. fprintf(stderr, "EVP_PKEY_CTX_new_from_name() failed\n");
  46. goto cleanup;
  47. }
  48. if (EVP_PKEY_fromdata_init(ctx) <= 0
  49. || EVP_PKEY_fromdata(ctx, &dsaparamkey, EVP_PKEY_KEY_PARAMETERS, params) <= 0) {
  50. fprintf(stderr, "EVP_PKEY_fromdata() failed\n");
  51. goto cleanup;
  52. }
  53. if (!dsa_print_key(dsaparamkey, 0, libctx, propq))
  54. goto cleanup;
  55. ret = EXIT_SUCCESS;
  56. cleanup:
  57. EVP_PKEY_free(dsaparamkey);
  58. EVP_PKEY_CTX_free(ctx);
  59. OSSL_PARAM_free(params);
  60. OSSL_PARAM_BLD_free(bld);
  61. BN_free(g);
  62. BN_free(q);
  63. BN_free(p);
  64. return ret;
  65. }