skeyutl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright 2025 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. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <limits.h>
  13. #include "apps.h"
  14. #include "progs.h"
  15. #include <openssl/bio.h>
  16. #include <openssl/err.h>
  17. #include <openssl/evp.h>
  18. typedef enum OPTION_choice {
  19. OPT_COMMON,
  20. OPT_PROV_ENUM,
  21. OPT_CIPHER,
  22. OPT_SKEYOPT, OPT_SKEYMGMT, OPT_GENKEY
  23. } OPTION_CHOICE;
  24. const OPTIONS skeyutl_options[] = {
  25. OPT_SECTION("General"),
  26. {"help", OPT_HELP, '-', "Display this summary"},
  27. {"skeyopt", OPT_SKEYOPT, 's', "Key options as opt:value for opaque keys handling"},
  28. {"skeymgmt", OPT_SKEYMGMT, 's', "Symmetric key management name for opaque keys handling"},
  29. {"genkey", OPT_GENKEY, '-', "Generate an opaque symmetric key"},
  30. {"cipher", OPT_CIPHER, 's', "The cipher to generate key for"},
  31. OPT_PROV_OPTIONS,
  32. {NULL}
  33. };
  34. int skeyutl_main(int argc, char **argv)
  35. {
  36. EVP_CIPHER *cipher = NULL;
  37. int ret = 1;
  38. OPTION_CHOICE o;
  39. int genkey = 0;
  40. char *prog, *ciphername = NULL;
  41. STACK_OF(OPENSSL_STRING) *skeyopts = NULL;
  42. const char *skeymgmt = NULL;
  43. EVP_SKEY *skey = NULL;
  44. EVP_SKEYMGMT *mgmt = NULL;
  45. prog = opt_init(argc, argv, skeyutl_options);
  46. while ((o = opt_next()) != OPT_EOF) {
  47. switch (o) {
  48. case OPT_EOF:
  49. case OPT_ERR:
  50. opthelp:
  51. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  52. goto end;
  53. case OPT_HELP:
  54. opt_help(skeyutl_options);
  55. ret = 0;
  56. goto end;
  57. case OPT_GENKEY:
  58. genkey = 1;
  59. break;
  60. case OPT_CIPHER:
  61. ciphername = opt_arg();
  62. break;
  63. case OPT_SKEYOPT:
  64. if ((skeyopts == NULL &&
  65. (skeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
  66. sk_OPENSSL_STRING_push(skeyopts, opt_arg()) == 0) {
  67. BIO_printf(bio_err, "%s: out of memory\n", prog);
  68. goto end;
  69. }
  70. break;
  71. case OPT_SKEYMGMT:
  72. skeymgmt = opt_arg();
  73. break;
  74. case OPT_PROV_CASES:
  75. if (!opt_provider(o))
  76. goto end;
  77. break;
  78. }
  79. }
  80. /* Get the cipher name, either from progname (if set) or flag. */
  81. if (!opt_cipher_any(ciphername, &cipher))
  82. goto opthelp;
  83. if (cipher == NULL && skeymgmt == NULL) {
  84. BIO_printf(bio_err, "Either -skeymgmt -or -cipher option should be specified\n");
  85. goto end;
  86. }
  87. if (genkey) {
  88. OSSL_PARAM *params = NULL;
  89. mgmt = EVP_SKEYMGMT_fetch(app_get0_libctx(),
  90. skeymgmt ? skeymgmt : EVP_CIPHER_name(cipher),
  91. app_get0_propq());
  92. if (mgmt == NULL)
  93. goto end;
  94. params = app_params_new_from_opts(skeyopts,
  95. EVP_SKEYMGMT_get0_gen_settable_params(mgmt));
  96. skey = EVP_SKEY_generate(app_get0_libctx(),
  97. skeymgmt ? skeymgmt : EVP_CIPHER_name(cipher),
  98. app_get0_propq(), params);
  99. OSSL_PARAM_free(params);
  100. if (skey == NULL) {
  101. BIO_printf(bio_err, "Error creating opaque key for skeymgmt %s\n",
  102. skeymgmt ? skeymgmt : EVP_CIPHER_name(cipher));
  103. ERR_print_errors(bio_err);
  104. } else {
  105. const char *key_name = EVP_SKEY_get0_key_id(skey);
  106. BIO_printf(bio_out, "An opaque key identified by %s is created\n",
  107. key_name ? key_name : "<unknown>");
  108. BIO_printf(bio_out, "Provider: %s\n", EVP_SKEY_get0_provider_name(skey));
  109. BIO_printf(bio_out, "Key management: %s\n", EVP_SKEY_get0_skeymgmt_name(skey));
  110. ret = 0;
  111. }
  112. goto end;
  113. } else {
  114. BIO_printf(bio_err, "Key generation is the only supported operation as of now\n");
  115. }
  116. end:
  117. ERR_print_errors(bio_err);
  118. sk_OPENSSL_STRING_free(skeyopts);
  119. EVP_SKEYMGMT_free(mgmt);
  120. EVP_SKEY_free(skey);
  121. EVP_CIPHER_free(cipher);
  122. return ret;
  123. }