5 Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
7 Copyright (C) 1997 - 2000 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
21 #include "silcincludes.h"
25 /* List of all PKCS's in SILC. PKCS's don't support SIM's thus
26 only static declarations are possible. XXX: I hope this to change
28 SilcPKCSObject silc_pkcs_list[] =
30 { "rsa", &silc_rsa_data_context,
31 silc_rsa_init, silc_rsa_clear_keys, silc_rsa_get_public_key,
32 silc_rsa_get_private_key, silc_rsa_set_public_key,
33 silc_rsa_set_private_key, silc_rsa_context_len,
34 silc_rsa_data_context_len, silc_rsa_set_arg,
35 silc_rsa_encrypt, silc_rsa_decrypt,
36 silc_rsa_sign, silc_rsa_verify },
38 { NULL, NULL, NULL, NULL, NULL,
39 NULL, NULL, NULL, NULL, NULL, NULL }
42 /* Allocates a new SilcPKCS object. The new allocated object is returned
43 to the 'new_pkcs' argument. This function also initializes the data
44 context structure. Function returns 1 on success and 0 on error.
47 int silc_pkcs_alloc(const unsigned char *name, SilcPKCS *new_pkcs)
51 SILC_LOG_DEBUG(("Allocating new PKCS object"));
53 for (i = 0; silc_pkcs_list[i].name; i++) {
54 if (!strcmp(silc_pkcs_list[i].name, name))
58 if (silc_pkcs_list[i].name == NULL)
61 *new_pkcs = silc_calloc(1, sizeof(**new_pkcs));
62 if (*new_pkcs == NULL) {
63 SILC_LOG_ERROR(("Could not allocate new PKCS object"));
67 /* Set the pointers */
68 (*new_pkcs)->pkcs = &silc_pkcs_list[i];
69 (*new_pkcs)->pkcs->data_context =
70 silc_calloc(1, (*new_pkcs)->pkcs->data_context_len());
71 (*new_pkcs)->context = silc_calloc(1, (*new_pkcs)->pkcs->context_len());
72 (*new_pkcs)->get_key_len = silc_pkcs_get_key_len;
77 /* Free's the PKCS object */
79 void silc_pkcs_free(SilcPKCS pkcs)
82 silc_free(pkcs->context);
85 /* Return TRUE if PKCS algorithm `name' is supported. */
87 int silc_pkcs_is_supported(const unsigned char *name)
91 for (i = 0; silc_pkcs_list[i].name; i++) {
92 if (!strcmp(silc_pkcs_list[i].name, name))
99 /* Returns comma separated list of supported PKCS algorithms */
101 char *silc_pkcs_get_supported()
107 for (i = 0; silc_pkcs_list[i].name; i++) {
108 len += strlen(silc_pkcs_list[i].name);
109 list = silc_realloc(list, len + 1);
111 memcpy(list + (len - strlen(silc_pkcs_list[i].name)),
112 silc_pkcs_list[i].name, strlen(silc_pkcs_list[i].name));
113 memcpy(list + len, ",", 1);
122 /* Returns the length of the key */
124 unsigned int silc_pkcs_get_key_len(SilcPKCS self)
126 return self->key_len;
129 /* Returns SILC style public key */
131 unsigned char *silc_pkcs_get_public_key(SilcPKCS pkcs, unsigned int *len)
133 return pkcs->pkcs->get_public_key(pkcs->context, len);
136 /* Returns SILC style private key */
138 unsigned char *silc_pkcs_get_private_key(SilcPKCS pkcs, unsigned int *len)
140 return pkcs->pkcs->get_private_key(pkcs->context, len);
143 /* Sets public key */
146 int silc_pkcs_set_public_key(SilcPKCS pkcs, unsigned char *pk,
149 return pkcs->pkcs->set_public_key(pkcs->context, pk, pk_len);
152 /* Sets private key */
154 int silc_pkcs_set_private_key(SilcPKCS pkcs, unsigned char *prv,
155 unsigned int prv_len)
157 return pkcs->pkcs->set_private_key(pkcs->context, prv, prv_len);
160 /* Saves public key into file */
162 int silc_pkcs_save_public_key(SilcPKCS pkcs, char *filename,
163 unsigned char *pk, unsigned int pk_len)
168 buf = silc_buffer_alloc(strlen(pkcs->pkcs->name) + 2 + pk_len
169 + strlen(SILC_PKCS_PUBLIC_KEYFILE_BEGIN)
170 + strlen(SILC_PKCS_PUBLIC_KEYFILE_END));
172 silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
174 silc_buffer_format(buf,
175 SILC_STR_UI32_STRING(SILC_PKCS_PUBLIC_KEYFILE_BEGIN),
176 SILC_STR_UI32_STRING(pkcs->pkcs->name),
177 SILC_STR_UI_SHORT(pk_len),
178 SILC_STR_UI_XNSTRING(pk, pk_len),
179 SILC_STR_UI32_STRING(SILC_PKCS_PUBLIC_KEYFILE_END),
182 /* Save into a file */
183 if (silc_file_write(filename, buf->data, buf->len)) {
189 silc_buffer_free(buf);
193 /* XXX The buffer should be encrypted */
196 int silc_pkcs_save_private_key(SilcPKCS pkcs, char *filename,
197 unsigned char *prv, unsigned int prv_len,
203 buf = silc_buffer_alloc(strlen(pkcs->pkcs->name) + 2 + prv_len
204 + strlen(SILC_PKCS_PRIVATE_KEYFILE_BEGIN)
205 + strlen(SILC_PKCS_PRIVATE_KEYFILE_END));
206 silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
208 silc_buffer_format(buf,
209 SILC_STR_UI32_STRING(SILC_PKCS_PRIVATE_KEYFILE_BEGIN),
210 SILC_STR_UI32_STRING(pkcs->pkcs->name),
211 SILC_STR_UI_SHORT(prv_len),
212 SILC_STR_UI_XNSTRING(prv, prv_len),
213 SILC_STR_UI32_STRING(SILC_PKCS_PRIVATE_KEYFILE_END),
216 /* Save into a file */
217 if (silc_file_write(filename, buf->data, buf->len)) {
223 silc_buffer_free(buf);
227 /* Loads public key from file and allocates new PKCS object and
228 sets the loaded key into it. */
230 int silc_pkcs_load_public_key(char *filename, SilcPKCS *ret_pkcs)
236 /* Loads private key from file and allocates new PKCS object and
237 sets the loaded key into it. */
239 int silc_pkcs_load_private_key(char *filename, SilcPKCS *ret_pkcs)