gost_ctl.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. {
  20. /* { GOST_CTRL_RNG,
  21. "RNG",
  22. "Type of random number generator to use",
  23. ENGINE_CMD_FLAG_STRING
  24. },
  25. { GOST_CTRL_RNG_PARAMS,
  26. "RNG_PARAMS",
  27. "Parameter for random number generator",
  28. ENGINE_CMD_FLAG_STRING
  29. },
  30. */ { GOST_CTRL_CRYPT_PARAMS,
  31. "CRYPT_PARAMS",
  32. "OID of default GOST 28147-89 parameters",
  33. ENGINE_CMD_FLAG_STRING
  34. },
  35. {0,NULL,NULL,0}
  36. };
  37. void gost_param_free()
  38. {
  39. int i;
  40. for (i=0;i<=GOST_PARAM_MAX;i++)
  41. if (gost_params[i]!=NULL)
  42. {
  43. OPENSSL_free(gost_params[i]);
  44. gost_params[i]=NULL;
  45. }
  46. }
  47. int gost_control_func(ENGINE *e,int cmd,long i, void *p, void (*f)(void))
  48. {
  49. int param = cmd-ENGINE_CMD_BASE;
  50. int ret=0;
  51. if (param <0 || param >GOST_PARAM_MAX) return -1;
  52. ret=gost_set_default_param(param,p);
  53. return ret;
  54. }
  55. const char *get_gost_engine_param(int param)
  56. {
  57. char *tmp;
  58. if (param <0 || param >GOST_PARAM_MAX) return NULL;
  59. if (gost_params[param]!=NULL)
  60. {
  61. return gost_params[param];
  62. }
  63. tmp = getenv(gost_envnames[param]);
  64. if (tmp)
  65. {
  66. if (gost_params[param]) OPENSSL_free(gost_params[param]);
  67. gost_params[param] = BUF_strdup(tmp);
  68. return gost_params[param];
  69. }
  70. return NULL;
  71. }
  72. int gost_set_default_param(int param, const char *value)
  73. {
  74. const char *tmp;
  75. if (param <0 || param >GOST_PARAM_MAX) return 0;
  76. tmp = getenv(gost_envnames[param]);
  77. /* if there is value in the environment, use it, else -passed string * */
  78. if (!tmp) tmp=value;
  79. if (gost_params[param]) OPENSSL_free(gost_params[param]);
  80. gost_params[param] = BUF_strdup(tmp);
  81. return 1;
  82. }