1238384Sjkim/**********************************************************************
2238384Sjkim *                        gost_ctl.c                                  *
3238384Sjkim *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4238384Sjkim *       This file is distributed under the same license as OpenSSL   *
5238384Sjkim *                                                                    *
6238384Sjkim *        Implementation of control commands for GOST engine          *
7238384Sjkim *            OpenSSL 0.9.9 libraries required                        *
8296341Sdelphij **********************************************************************/
9238384Sjkim#include <stdlib.h>
10238384Sjkim#include <string.h>
11238384Sjkim#include <openssl/crypto.h>
12238384Sjkim#include <openssl/err.h>
13238384Sjkim#include <openssl/engine.h>
14238384Sjkim#include <openssl/buffer.h>
15238384Sjkim#include "gost_lcl.h"
16238384Sjkim
17296341Sdelphijstatic char *gost_params[GOST_PARAM_MAX + 1] = { NULL };
18296341Sdelphijstatic const char *gost_envnames[] = { "CRYPT_PARAMS" };
19238384Sjkim
20296341Sdelphijconst ENGINE_CMD_DEFN gost_cmds[] = {
21296341Sdelphij/*- { GOST_CTRL_RNG,
22296341Sdelphij    "RNG",
23296341Sdelphij    "Type of random number generator to use",
24296341Sdelphij    ENGINE_CMD_FLAG_STRING
25296341Sdelphij    },
26296341Sdelphij    { GOST_CTRL_RNG_PARAMS,
27296341Sdelphij    "RNG_PARAMS",
28296341Sdelphij    "Parameter for random number generator",
29296341Sdelphij    ENGINE_CMD_FLAG_STRING
30296341Sdelphij    },
31296341Sdelphij*/ {GOST_CTRL_CRYPT_PARAMS,
32296341Sdelphij           "CRYPT_PARAMS",
33296341Sdelphij           "OID of default GOST 28147-89 parameters",
34296341Sdelphij           ENGINE_CMD_FLAG_STRING},
35296341Sdelphij    {0, NULL, NULL, 0}
36296341Sdelphij};
37296341Sdelphij
38296341Sdelphijvoid gost_param_free()
39238384Sjkim{
40296341Sdelphij    int i;
41296341Sdelphij    for (i = 0; i <= GOST_PARAM_MAX; i++)
42296341Sdelphij        if (gost_params[i] != NULL) {
43296341Sdelphij            OPENSSL_free(gost_params[i]);
44296341Sdelphij            gost_params[i] = NULL;
45296341Sdelphij        }
46296341Sdelphij
47238384Sjkim}
48238384Sjkim
49296341Sdelphijint gost_control_func(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
50296341Sdelphij{
51296341Sdelphij    int param = cmd - ENGINE_CMD_BASE;
52296341Sdelphij    int ret = 0;
53296341Sdelphij    if (param < 0 || param > GOST_PARAM_MAX)
54296341Sdelphij        return -1;
55296341Sdelphij    ret = gost_set_default_param(param, p);
56296341Sdelphij    return ret;
57296341Sdelphij}
58238384Sjkim
59296341Sdelphijconst char *get_gost_engine_param(int param)
60296341Sdelphij{
61296341Sdelphij    char *tmp;
62296341Sdelphij    if (param < 0 || param > GOST_PARAM_MAX)
63296341Sdelphij        return NULL;
64296341Sdelphij    if (gost_params[param] != NULL) {
65296341Sdelphij        return gost_params[param];
66296341Sdelphij    }
67296341Sdelphij    tmp = getenv(gost_envnames[param]);
68296341Sdelphij    if (tmp) {
69296341Sdelphij        if (gost_params[param])
70296341Sdelphij            OPENSSL_free(gost_params[param]);
71296341Sdelphij        gost_params[param] = BUF_strdup(tmp);
72296341Sdelphij        return gost_params[param];
73296341Sdelphij    }
74296341Sdelphij    return NULL;
75296341Sdelphij}
76238384Sjkim
77296341Sdelphijint gost_set_default_param(int param, const char *value)
78296341Sdelphij{
79296341Sdelphij    const char *tmp;
80296341Sdelphij    if (param < 0 || param > GOST_PARAM_MAX)
81296341Sdelphij        return 0;
82296341Sdelphij    tmp = getenv(gost_envnames[param]);
83296341Sdelphij    /*
84296341Sdelphij     * if there is value in the environment, use it, else -passed string *
85296341Sdelphij     */
86296341Sdelphij    if (!tmp)
87296341Sdelphij        tmp = value;
88296341Sdelphij    if (gost_params[param])
89296341Sdelphij        OPENSSL_free(gost_params[param]);
90296341Sdelphij    gost_params[param] = BUF_strdup(tmp);
91238384Sjkim
92296341Sdelphij    return 1;
93296341Sdelphij}
94