d94089b9cbaf9b07b4ade54705bcb85429c044d5
[openssl.git] / engines / ccgost / gost_ctl.c
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/engine.h>
12 #include <openssl/buffer.h>
13 #include "gost_lcl.h"
14
15 static char *gost_params[GOST_PARAM_MAX+1]={NULL};
16 static const char *gost_envnames[]={"CRYPT_PARAMS"};
17 const ENGINE_CMD_DEFN gost_cmds[]=
18         {
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                         },
34 {0,NULL,NULL,0}
35         };
36
37
38 int gost_control_func(ENGINE *e,int cmd,long i, void *p, void (*f)(void))
39         {
40         int param = cmd-ENGINE_CMD_BASE;
41         int ret=0;
42         if (param <0 || param >GOST_PARAM_MAX) return -1;
43         ret=gost_set_default_param(param,p);
44         return ret;
45         }
46
47 const char *get_gost_engine_param(int param) 
48         {
49         char *tmp;
50         if (param <0 || param >GOST_PARAM_MAX) return NULL;
51         if (gost_params[param]!=NULL) 
52                 {
53                 return gost_params[param];
54                 }
55         tmp = getenv(gost_envnames[param]);
56         if (tmp) 
57                 {
58                 gost_params[param] = BUF_strdup(tmp);
59                 return gost_params[param];
60                 }       
61         return NULL;
62         }       
63
64 int gost_set_default_param(int param, const char *value) 
65         {
66         const char *tmp;
67         if (param <0 || param >GOST_PARAM_MAX) return 0;
68         tmp = getenv(gost_envnames[param]);
69         /* if there is value in the environment, use it, else -passed string * */
70         if (!tmp) tmp=value;
71         if (gost_params[param]) free(gost_params[param]);
72         gost_params[param] = BUF_strdup(tmp);
73
74         return 1;
75         }