summaryrefslogtreecommitdiffhomepage
path: root/contrib/package/px5g/src/px5g.c
blob: 2b3e78585c0fa019ede30e748de76bdf699ed83e (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * px5g - Embedded x509 key and certificate generator based on PolarSSL
 *
 *   Copyright (C) 2009 Steven Barth <steven@midlink.org>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License, version 2.1 as published by the Free Software Foundation.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301  USA
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "polarssl/havege.h"
#include "polarssl/bignum.h"
#include "polarssl/x509.h"
#include "polarssl/rsa.h"

#define PX5G_VERSION "0.1"
#define PX5G_COPY "Copyright (c) 2009 Steven Barth <steven@midlink.org>"
#define PX5G_LICENSE "Licensed under the GNU Lesser General Public License v2.1"

int rsakey(char **arg) {
	havege_state hs;
	rsa_context rsa;

	unsigned int ksize = 512;
	int exp = 65537;
	char *path = NULL;
	int flag = X509_OUTPUT_PEM;

	while (*arg && **arg == '-') {
		if (!strcmp(*arg, "-out") && arg[1]) {
			path = arg[1];
			arg++;
		} else if (!strcmp(*arg, "-3")) {
			exp = 3;
		} else if (!strcmp(*arg, "-der")) {
			flag = X509_OUTPUT_DER;
		}
		arg++;
	}

	if (*arg) {
		ksize = (unsigned int)atoi(*arg);
	}

	havege_init(&hs);
	rsa_init(&rsa, RSA_PKCS_V15, 0, havege_rand, &hs);

	fprintf(stderr, "Generating RSA private key, %i bit long modulus\n", ksize);
	if (rsa_gen_key(&rsa, ksize, exp)) {
		fprintf(stderr, "error: key generation failed\n");
		return 1;
	}

	if (x509write_keyfile(&rsa, path, flag)) {
		fprintf(stderr, "error: I/O error\n");
		return 1;
	}

	rsa_free(&rsa);
	return 0;
}

int selfsigned(char **arg) {
	havege_state hs;
	rsa_context rsa;
	x509_node node;

	char *subject = "";
	unsigned int ksize = 512;
	int exp = 65537;
	unsigned int days = 30;
	char *keypath = NULL, *certpath = NULL;
	int flag = X509_OUTPUT_PEM;
	time_t from = time(NULL), to;
	char fstr[20], tstr[20];

	while (*arg && **arg == '-') {
		if (!strcmp(*arg, "-der")) {
			flag = X509_OUTPUT_DER;
		} else if (!strcmp(*arg, "-newkey") && arg[1]) {
			if (strncmp(arg[1], "rsa:", 4)) {
				fprintf(stderr, "error: invalid algorithm");
				return 1;
			}
			ksize = (unsigned int)atoi(arg[1] + 4);
			arg++;
		} else if (!strcmp(*arg, "-days") && arg[1]) {
			days = (unsigned int)atoi(arg[1]);
			arg++;
		} else if (!strcmp(*arg, "-keyout") && arg[1]) {
			keypath = arg[1];
			arg++;
		} else if (!strcmp(*arg, "-out") && arg[1]) {
			certpath = arg[1];
			arg++;
		} else if (!strcmp(*arg, "-subj") && arg[1]) {
			if (arg[1][0] != '/' || strchr(arg[1], ';')) {
				fprintf(stderr, "error: invalid subject");
				return 1;
			}
			subject = calloc(strlen(arg[1]) + 1, 1);
			char *oldc = arg[1] + 1, *newc = subject, *delim;
			do {
				delim = strchr(oldc, '=');
				if (!delim) {
					fprintf(stderr, "error: invalid subject");
					return 1;
				}
				memcpy(newc, oldc, delim - oldc + 1);
				newc += delim - oldc + 1;
				oldc = delim + 1;

				delim = strchr(oldc, '/');
				if (!delim) {
					delim = arg[1] + strlen(arg[1]);
				}
				memcpy(newc, oldc, delim - oldc);
				newc += delim - oldc;
				*newc++ = ';';
				oldc = delim + 1;
			} while(*delim);
			arg++;
		}
		arg++;
	}

	havege_init(&hs);
	rsa_init(&rsa, RSA_PKCS_V15, 0, havege_rand, &hs);
	x509write_init_node(&node);
	fprintf(stderr, "Generating RSA private key, %i bit long modulus\n", ksize);
	if (rsa_gen_key(&rsa, ksize, exp)) {
		fprintf(stderr, "error: key generation failed\n");
		return 1;
	}

	if (keypath) {
		if (x509write_keyfile(&rsa, keypath, flag)) {
			fprintf(stderr, "error: I/O error\n");
			return 1;
		}
	}

	from = (from < 1000000000) ? 1000000000 : from;
	strftime(fstr, sizeof(fstr), "%F %H:%M:%S", gmtime(&from));
	to = from + 60 * 60 * 24 * days;
	strftime(tstr, sizeof(tstr), "%F %H:%M:%S", gmtime(&to));

	x509_raw cert;
	x509write_init_raw(&cert);
	x509write_add_pubkey(&cert, &rsa);
	x509write_add_subject(&cert, (unsigned char*)subject);
	x509write_add_validity(&cert, (unsigned char*)fstr, (unsigned char*)tstr);
	fprintf(stderr, "Generating selfsigned certificate with subject '%s'"
			" and validity %s-%s\n", subject, fstr, tstr);
	if (x509write_create_selfsign(&cert, &rsa)) {
		fprintf(stderr, "error: certificate generation failed\n");
	}

	if (x509write_crtfile(&cert, (unsigned char*)certpath, flag)) {
		fprintf(stderr, "error: I/O error\n");
		return 1;
	}

	x509write_free_raw(&cert);
	rsa_free(&rsa);
	return 0;
}

int main(int argc, char *argv[]) {
	if (!argv[1]) {
		//Usage
	} else if (!strcmp(argv[1], "rsakey")) {
		return rsakey(argv+2);
	} else if (!strcmp(argv[1], "selfsigned")) {
		return selfsigned(argv+2);
	}

	fprintf(stderr,
		"PX5G X.509 Certificate Generator Utility v" PX5G_VERSION "\n" PX5G_COPY
		"\nbased on PolarSSL by Christophe Devine and Paul Bakker\n\n");
	fprintf(stderr, "Usage: %s [rsakey|selfsigned]\n", *argv);
	return 1;
}