gost_ctl.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**********************************************************************
  2. * gost_ctl.c *
  3. * Copyright (c) 2005-2006 Cryptocom LTD *
  4. * This file is distributed under the same license as OpenSSL *
  5. * *
  6. * Implementation of control commands for GOST engine *
  7. * OpenSSL 0.9.9 libraries required *
  8. **********************************************************************/
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <openssl/crypto.h>
  12. #include <openssl/err.h>
  13. #include <openssl/engine.h>
  14. #include <openssl/buffer.h>
  15. #include "gost_lcl.h"
  16. static char *gost_params[GOST_PARAM_MAX + 1] = { NULL };
  17. static const char *gost_envnames[] = { "CRYPT_PARAMS" };
  18. const ENGINE_CMD_DEFN gost_cmds[] = {
  19. /*- { GOST_CTRL_RNG,
  20. "RNG",
  21. "Type of random number generator to use",
  22. ENGINE_CMD_FLAG_STRING
  23. },
  24. { GOST_CTRL_RNG_PARAMS,
  25. "RNG_PARAMS",
  26. "Parameter for random number generator",
  27. ENGINE_CMD_FLAG_STRING
  28. },
  29. */ {GOST_CTRL_CRYPT_PARAMS,
  30. "CRYPT_PARAMS",
  31. "OID of default GOST 28147-89 parameters",
  32. ENGINE_CMD_FLAG_STRING},
  33. {0, NULL, NULL, 0}
  34. };
  35. void gost_param_free()
  36. {
  37. int i;
  38. for (i = 0; i <= GOST_PARAM_MAX; i++)
  39. if (gost_params[i] != NULL) {
  40. OPENSSL_free(gost_params[i]);
  41. gost_params[i] = NULL;
  42. }
  43. }
  44. int gost_control_func(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  45. {
  46. int param = cmd - ENGINE_CMD_BASE;
  47. int ret = 0;
  48. if (param < 0 || param > GOST_PARAM_MAX)
  49. return -1;
  50. ret = gost_set_default_param(param, p);
  51. return ret;
  52. }
  53. const char *get_gost_engine_param(int param)
  54. {
  55. char *tmp;
  56. if (param < 0 || param > GOST_PARAM_MAX)
  57. return NULL;
  58. if (gost_params[param] != NULL) {
  59. return gost_params[param];
  60. }
  61. tmp = getenv(gost_envnames[param]);
  62. if (tmp) {
  63. if (gost_params[param])
  64. OPENSSL_free(gost_params[param]);
  65. gost_params[param] = BUF_strdup(tmp);
  66. return gost_params[param];
  67. }
  68. return NULL;
  69. }
  70. int gost_set_default_param(int param, const char *value)
  71. {
  72. const char *tmp;
  73. if (param < 0 || param > GOST_PARAM_MAX)
  74. return 0;
  75. tmp = getenv(gost_envnames[param]);
  76. /*
  77. * if there is value in the environment, use it, else -passed string *
  78. */
  79. if (!tmp)
  80. tmp = value;
  81. if (gost_params[param])
  82. OPENSSL_free(gost_params[param]);
  83. gost_params[param] = BUF_strdup(tmp);
  84. return 1;
  85. }