summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/initconf.c115
1 files changed, 115 insertions, 0 deletions
diff --git a/src/initconf.c b/src/initconf.c
new file mode 100644
index 0000000..de583fa
--- /dev/null
+++ b/src/initconf.c
@@ -0,0 +1,115 @@
1/*
2 * userpref.c
3 * contains methods to access user specific certificates IDs and more.
4 *
5 * Copyright (c) 2008 Jonathan Beck All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <gnutls/gnutls.h>
23#include <glib.h>
24#include "userpref.h"
25
26int debug = 1;
27
28int main(int argc, char *argv[]) {
29
30 gnutls_global_init();
31
32 char* host_id = NULL;
33 gnutls_x509_privkey_t root_privkey;
34 gnutls_x509_privkey_t host_privkey;
35
36 gnutls_x509_crt_t root_cert;
37 gnutls_x509_crt_t host_cert;
38
39 gnutls_x509_privkey_init(&root_privkey);
40 gnutls_x509_privkey_init(&host_privkey);
41
42 gnutls_x509_crt_init(&root_cert);
43 gnutls_x509_crt_init(&host_cert);
44
45 /* generate keys */
46 int ret1 = gnutls_x509_privkey_generate(root_privkey, GNUTLS_PK_RSA, 2048, 0);
47 int ret2 = gnutls_x509_privkey_generate(host_privkey, GNUTLS_PK_RSA, 2048, 0);
48
49 /* generate certificates */
50 gnutls_x509_crt_set_key(root_cert, root_privkey);
51 gnutls_x509_crt_set_serial(root_cert, "\x00", 1);
52 gnutls_x509_crt_set_version(root_cert, 3);
53 gnutls_x509_crt_set_ca_status(root_cert, 1);
54 gnutls_x509_crt_set_activation_time(root_cert, time(NULL));
55 gnutls_x509_crt_set_expiration_time(root_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
56 gnutls_x509_crt_sign(root_cert, root_cert, root_privkey);
57
58
59 gnutls_x509_crt_set_key(host_cert, host_privkey);
60 gnutls_x509_crt_set_serial(host_cert, "\x00", 1);
61 gnutls_x509_crt_set_version(host_cert, 3);
62 gnutls_x509_crt_set_ca_status(host_cert, 1);
63 gnutls_x509_crt_set_activation_time(host_cert, time(NULL));
64 gnutls_x509_crt_set_expiration_time(host_cert, time(NULL) + (60 * 60 * 24 * 365 * 10));
65 gnutls_x509_crt_sign(host_cert, root_cert, root_privkey);
66
67
68 /* export to PEM format */
69 gnutls_datum_t root_key_pem = {NULL, 0};
70 gnutls_datum_t host_key_pem = {NULL, 0};
71
72 gnutls_x509_privkey_export (root_privkey, GNUTLS_X509_FMT_PEM, NULL, &root_key_pem.size);
73 gnutls_x509_privkey_export (host_privkey, GNUTLS_X509_FMT_PEM, NULL, &host_key_pem.size);
74
75 root_key_pem.data = gnutls_malloc(root_key_pem.size);
76 root_key_pem.data = gnutls_malloc(root_key_pem.size);
77
78 gnutls_x509_privkey_export (root_privkey, GNUTLS_X509_FMT_PEM, root_key_pem.data, &root_key_pem.size);
79 gnutls_x509_privkey_export (host_privkey, GNUTLS_X509_FMT_PEM, host_key_pem.data, &host_key_pem.size);
80
81 gnutls_datum_t root_cert_pem = {NULL, 0};
82 gnutls_datum_t host_cert_pem = {NULL, 0};
83
84 gnutls_x509_crt_export (root_cert, GNUTLS_X509_FMT_PEM, NULL, &root_cert_pem.size);
85 gnutls_x509_crt_export (host_cert, GNUTLS_X509_FMT_PEM, NULL, &host_cert_pem.size);
86
87 root_cert_pem.data = gnutls_malloc(root_cert_pem.size);
88 root_cert_pem.data = gnutls_malloc(root_cert_pem.size);
89
90 gnutls_x509_crt_export (root_cert, GNUTLS_X509_FMT_PEM, root_cert_pem.data, &root_cert_pem.size);
91 gnutls_x509_crt_export (host_cert, GNUTLS_X509_FMT_PEM, host_cert_pem.data, &host_cert_pem.size);
92
93 /* encode in base64 for storage */
94 char* root_key_b64 = g_base64_encode (root_key_pem.data,root_key_pem.size);
95 char* host_key_b64 = g_base64_encode (host_key_pem.data,host_key_pem.size);
96
97 char* root_cert_b64 = g_base64_encode (root_cert_pem.data,root_cert_pem.size);
98 char* host_cert_b64 = g_base64_encode (host_cert_pem.data,host_cert_pem.size);
99
100 /* store values in config file */
101
102 init_config_file(host_id, root_key_b64, host_key_b64, root_cert_b64, host_cert_b64);
103
104 gnutls_free(root_key_pem.data);
105 gnutls_free(host_key_pem.data);
106 gnutls_free(root_cert_pem.data);
107 gnutls_free(host_cert_pem.data);
108 g_free(root_key_b64);
109 g_free(host_key_b64);
110 g_free(root_cert_b64);
111 g_free(host_cert_b64);
112
113 return 0;
114}
115