File: tlscert.c

package info (click to toggle)
ctrlproxy 3.0.7-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 1,668 kB
  • ctags: 2,045
  • sloc: ansic: 15,358; sh: 2,805; xml: 1,078; python: 346; makefile: 302; perl: 18
file content (171 lines) | stat: -rw-r--r-- 6,335 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* 
   auto-generate self signed TLS certificates. Imported from Samba.

   Copyright (C) Andrew Tridgell 2005
   Copyright (C) Jelmer Vernooij 2006-2007
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "ctrlproxy.h"

#include <glib.h>

#include "ssl.h"

#include <gnutls/gnutls.h>
#include <gnutls/x509.h>

#define ORGANISATION_NAME "CtrlProxy"
#define UNIT_NAME         "CtrlProxy - temporary autogenerated certificate"
#define COMMON_NAME       "CtrlProxy"
#define LIFETIME          700*24*60*60
#define DH_BITS 		  1024

/* 
   auto-generate a set of self signed certificates
*/
void ssl_cert_generate(const char *keyfile, const char *certfile,
		       const char *cafile)
{
	gnutls_x509_crt cacrt, crt;
	gnutls_x509_privkey key, cakey;
	guint32 serial = (guint32)time(NULL);
	unsigned char keyid[100];
	char buf[4096];
	size_t bufsize;
	size_t keyidsize = sizeof(keyid);
	time_t activation = time(NULL), expiry = activation + LIFETIME;
	int ret;

	if (g_file_test(keyfile, G_FILE_TEST_EXISTS) || 
		g_file_test(certfile, G_FILE_TEST_EXISTS) || 
		g_file_test(cafile, G_FILE_TEST_EXISTS)) {
		log_global(LOG_WARNING, "TLS autogeneration skipped - some TLS files already exist");
		return;
	}

#define TLSCHECK(call) do { \
	ret = call; \
	if (ret < 0) { \
		log_global(LOG_WARNING, "TLS %s - %s", #call, gnutls_strerror(ret)); \
		goto failed; \
	} \
} while (0)

	TLSCHECK(gnutls_global_init());

	log_global(LOG_INFO, 
			   "Attempting to autogenerate TLS self-signed keys");
	
	log_global(LOG_TRACE, "Generating private key");
	TLSCHECK(gnutls_x509_privkey_init(&key));
	TLSCHECK(gnutls_x509_privkey_generate(key,   GNUTLS_PK_RSA, DH_BITS, 0));

	log_global(LOG_TRACE, "Generating CA private key");
	TLSCHECK(gnutls_x509_privkey_init(&cakey));
	TLSCHECK(gnutls_x509_privkey_generate(cakey, GNUTLS_PK_RSA, DH_BITS, 0));

	log_global(LOG_TRACE, "Generating CA certificate");
	TLSCHECK(gnutls_x509_crt_init(&cacrt));
	TLSCHECK(gnutls_x509_crt_set_dn_by_oid(cacrt, 
				      GNUTLS_OID_X520_ORGANIZATION_NAME, 0,
				      ORGANISATION_NAME, strlen(ORGANISATION_NAME)));
	TLSCHECK(gnutls_x509_crt_set_dn_by_oid(cacrt, 
				      GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME, 0,
				      UNIT_NAME, strlen(UNIT_NAME)));
	TLSCHECK(gnutls_x509_crt_set_dn_by_oid(cacrt,
				      GNUTLS_OID_X520_COMMON_NAME, 0,
				      COMMON_NAME, strlen(COMMON_NAME)));
	TLSCHECK(gnutls_x509_crt_set_key(cacrt, cakey));
	TLSCHECK(gnutls_x509_crt_set_serial(cacrt, &serial, sizeof(serial)));
	TLSCHECK(gnutls_x509_crt_set_activation_time(cacrt, activation));
	TLSCHECK(gnutls_x509_crt_set_expiration_time(cacrt, expiry));
	TLSCHECK(gnutls_x509_crt_set_ca_status(cacrt, 0));
	TLSCHECK(gnutls_x509_crt_set_version(cacrt, 3));
	TLSCHECK(gnutls_x509_crt_get_key_id(cacrt, 0, keyid, &keyidsize));
	TLSCHECK(gnutls_x509_crt_set_subject_key_id(cacrt, keyid, keyidsize));
	TLSCHECK(gnutls_x509_crt_sign(cacrt, cacrt, cakey));

	log_global(LOG_TRACE, "Generating TLS certificaten");
	TLSCHECK(gnutls_x509_crt_init(&crt));
	TLSCHECK(gnutls_x509_crt_set_dn_by_oid(crt, 
				      GNUTLS_OID_X520_ORGANIZATION_NAME, 0,
				      ORGANISATION_NAME, strlen(ORGANISATION_NAME)));
	TLSCHECK(gnutls_x509_crt_set_dn_by_oid(crt, 
				      GNUTLS_OID_X520_ORGANIZATIONAL_UNIT_NAME, 0,
				      UNIT_NAME, strlen(UNIT_NAME)));
	TLSCHECK(gnutls_x509_crt_set_dn_by_oid(crt,
				      GNUTLS_OID_X520_COMMON_NAME, 0,
				      COMMON_NAME, strlen(COMMON_NAME)));
	TLSCHECK(gnutls_x509_crt_set_key(crt, key));
	TLSCHECK(gnutls_x509_crt_set_serial(crt, &serial, sizeof(serial)));
	TLSCHECK(gnutls_x509_crt_set_activation_time(crt, activation));
	TLSCHECK(gnutls_x509_crt_set_expiration_time(crt, expiry));
	TLSCHECK(gnutls_x509_crt_set_ca_status(crt, 0));
	TLSCHECK(gnutls_x509_crt_set_version(crt, 3));
	TLSCHECK(gnutls_x509_crt_get_key_id(crt, 0, keyid, &keyidsize));
	TLSCHECK(gnutls_x509_crt_set_subject_key_id(crt, keyid, keyidsize));
	TLSCHECK(gnutls_x509_crt_sign(crt, crt, key));

	log_global(LOG_TRACE, "Exporting TLS keys");

	bufsize = sizeof(buf);
	TLSCHECK(gnutls_x509_crt_export(crt, GNUTLS_X509_FMT_PEM, buf, &bufsize));
	g_file_set_contents(certfile, buf, bufsize, NULL);

	bufsize = sizeof(buf);
	TLSCHECK(gnutls_x509_crt_export(cacrt, GNUTLS_X509_FMT_PEM, buf, &bufsize));
	g_file_set_contents(cafile, buf, bufsize, NULL);

	bufsize = sizeof(buf);
	TLSCHECK(gnutls_x509_privkey_export(key, GNUTLS_X509_FMT_PEM, buf, &bufsize));
	g_file_set_contents(keyfile, buf, bufsize, NULL);

	gnutls_x509_privkey_deinit(key);
	gnutls_x509_privkey_deinit(cakey);
	gnutls_x509_crt_deinit(cacrt);
	gnutls_x509_crt_deinit(crt);

	log_global(LOG_INFO, "TLS self-signed keys generated OK");
	return;

failed:
	log_global(LOG_WARNING, "TLS certificate generation failed");
}

gpointer ssl_create_server_credentials(struct ctrlproxy_config *config,
									   GKeyFile *kf, const char *group)
{
	if (!g_key_file_has_key(kf, group, "keyfile", NULL) &&
		!g_key_file_has_key(kf, group, "certfile", NULL)) {
		char *keyfile = g_build_filename(config->config_dir, "key.pem", NULL);
		char *certfile = g_build_filename(config->config_dir, "cert.pem", NULL);
		g_key_file_set_string(kf, group, "keyfile", keyfile);
		g_key_file_set_string(kf, group, "certfile", certfile);
		if (!g_file_test(keyfile, G_FILE_TEST_EXISTS) && 
			!g_file_test(certfile, G_FILE_TEST_EXISTS)) {
			char *cafile = g_build_filename(config->config_dir, "ca.pem", NULL);
			ssl_cert_generate(keyfile, certfile, cafile);
			g_free(cafile);
		}
		g_free(keyfile);
		g_free(certfile);
	}

	return ssl_get_server_credentials(
					g_key_file_get_string(kf, group, "certfile", NULL),
					g_key_file_get_string(kf, group, "keyfile", NULL));
}