]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_ctl.c
Merge branch 'master' of https://github.com/gost-engine/engine
[openssl-gost/engine.git] / 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/crypto.h>
12 #include <openssl/err.h>
13 #include <openssl/engine.h>
14 #include <openssl/buffer.h>
15 #include "gost_lcl.h"
16
17 static char *gost_params[GOST_PARAM_MAX + 1] = { NULL };
18 static const char *gost_envnames[] =
19     { "CRYPT_PARAMS", "GOST_PBE_HMAC", "GOST_PK_FORMAT" };
20
21 const ENGINE_CMD_DEFN gost_cmds[] = {
22     {GOST_CTRL_CRYPT_PARAMS,
23      "CRYPT_PARAMS",
24      "OID of default GOST 28147-89 parameters",
25      ENGINE_CMD_FLAG_STRING},
26     {GOST_CTRL_PBE_PARAMS,
27      "PBE_PARAMS",
28      "Shortname of default digest alg for PBE",
29      ENGINE_CMD_FLAG_STRING},
30     {GOST_CTRL_PK_FORMAT,
31      "GOST_PK_FORMAT",
32      "Private key format params",
33      ENGINE_CMD_FLAG_STRING},
34     {0, NULL, NULL, 0}
35 };
36
37 void gost_param_free()
38 {
39     int i;
40
41     for (i = 0; i <= GOST_PARAM_MAX; i++) {
42         OPENSSL_free(gost_params[i]);
43         gost_params[i] = NULL;
44     }
45
46 }
47
48 int gost_control_func(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
49 {
50     int param = cmd - ENGINE_CMD_BASE;
51     int ret = 0;
52     if (param < 0 || param > GOST_PARAM_MAX) {
53         return -1;
54     }
55     ret = gost_set_default_param(param, p);
56     return ret;
57 }
58
59 const char *get_gost_engine_param(int param)
60 {
61     char *tmp;
62     if (param < 0 || param >= GOST_PARAM_MAX)
63         return NULL;
64     if (gost_params[param] != NULL) {
65         return gost_params[param];
66     }
67     tmp = getenv(gost_envnames[param]);
68     if (tmp) {
69         OPENSSL_free(gost_params[param]);
70         gost_params[param] = BUF_strdup(tmp);
71         return gost_params[param];
72     }
73     return NULL;
74 }
75
76 int gost_set_default_param(int param, const char *value)
77 {
78     const char *tmp;
79     if (param < 0 || param >= GOST_PARAM_MAX)
80         return 0;
81     tmp = getenv(gost_envnames[param]);
82
83     /*
84      * if there is value in the environment, use it, else -passed string *
85      */
86     if (!tmp) {
87         tmp = value;
88     }
89     OPENSSL_free(gost_params[param]);
90     gost_params[param] = BUF_strdup(tmp);
91
92     return 1;
93 }