2 * px5g - Embedded x509 key and certificate generator based on PolarSSL
4 * Copyright (C) 2009 Steven Barth <steven@midlink.org>
5 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
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, version 2.1 as published by the Free Software Foundation.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #include <sys/types.h>
33 #include <polarssl/bignum.h>
34 #include <polarssl/x509_crt.h>
35 #include <polarssl/rsa.h>
37 #define PX5G_VERSION "0.2"
38 #define PX5G_COPY "Copyright (c) 2009 Steven Barth <steven@midlink.org>"
39 #define PX5G_LICENSE "Licensed under the GNU Lesser General Public License v2.1"
41 static int urandom_fd;
42 static char buf[16384];
44 static int _urandom(void *ctx, unsigned char *out, size_t len)
46 read(urandom_fd, out, len);
50 static void write_file(const char *path, int len, bool pem)
53 const char *buf_start = buf;
56 buf_start += sizeof(buf) - len;
59 fprintf(stderr, "No data to write\n");
64 fprintf(stderr, "error: I/O error\n");
71 fwrite(buf_start, 1, len, f);
75 static void write_key(pk_context *key, const char *path, bool pem)
80 if (pk_write_key_pem(key, (void *) buf, sizeof(buf)) == 0)
83 len = pk_write_key_der(key, (void *) buf, sizeof(buf));
88 write_file(path, len, pem);
91 static void gen_key(pk_context *key, int ksize, int exp, bool pem)
94 pk_init_ctx(key, pk_info_from_type(POLARSSL_PK_RSA));
95 fprintf(stderr, "Generating RSA private key, %i bit long modulus\n", ksize);
96 if (rsa_gen_key(pk_rsa(*key), _urandom, NULL, ksize, exp)) {
97 fprintf(stderr, "error: key generation failed\n");
102 int rsakey(char **arg)
105 unsigned int ksize = 512;
110 while (*arg && **arg == '-') {
111 if (!strcmp(*arg, "-out") && arg[1]) {
114 } else if (!strcmp(*arg, "-3")) {
116 } else if (!strcmp(*arg, "-der")) {
123 ksize = (unsigned int)atoi(*arg);
125 gen_key(&key, ksize, exp, pem);
126 write_key(&key, path, pem);
133 int selfsigned(char **arg)
140 unsigned int ksize = 512;
142 unsigned int days = 30;
143 char *keypath = NULL, *certpath = NULL;
145 time_t from = time(NULL), to;
146 char fstr[20], tstr[20], sstr[17];
149 while (*arg && **arg == '-') {
150 if (!strcmp(*arg, "-der")) {
152 } else if (!strcmp(*arg, "-newkey") && arg[1]) {
153 if (strncmp(arg[1], "rsa:", 4)) {
154 fprintf(stderr, "error: invalid algorithm");
157 ksize = (unsigned int)atoi(arg[1] + 4);
159 } else if (!strcmp(*arg, "-days") && arg[1]) {
160 days = (unsigned int)atoi(arg[1]);
162 } else if (!strcmp(*arg, "-keyout") && arg[1]) {
165 } else if (!strcmp(*arg, "-out") && arg[1]) {
168 } else if (!strcmp(*arg, "-subj") && arg[1]) {
169 if (arg[1][0] != '/' || strchr(arg[1], ';')) {
170 fprintf(stderr, "error: invalid subject");
173 subject = calloc(strlen(arg[1]) + 1, 1);
174 char *oldc = arg[1] + 1, *newc = subject, *delim;
176 delim = strchr(oldc, '=');
178 fprintf(stderr, "error: invalid subject");
181 memcpy(newc, oldc, delim - oldc + 1);
182 newc += delim - oldc + 1;
185 delim = strchr(oldc, '/');
187 delim = arg[1] + strlen(arg[1]);
189 memcpy(newc, oldc, delim - oldc);
190 newc += delim - oldc;
199 gen_key(&key, ksize, exp, pem);
202 write_key(&key, keypath, pem);
204 from = (from < 1000000000) ? 1000000000 : from;
205 strftime(fstr, sizeof(fstr), "%Y%m%d%H%M%S", gmtime(&from));
206 to = from + 60 * 60 * 24 * days;
209 strftime(tstr, sizeof(tstr), "%Y%m%d%H%M%S", gmtime(&to));
211 fprintf(stderr, "Generating selfsigned certificate with subject '%s'"
212 " and validity %s-%s\n", subject, fstr, tstr);
214 x509write_crt_init(&cert);
215 x509write_crt_set_md_alg(&cert, POLARSSL_MD_SHA256);
216 x509write_crt_set_issuer_key(&cert, &key);
217 x509write_crt_set_subject_key(&cert, &key);
218 x509write_crt_set_subject_name(&cert, subject);
219 x509write_crt_set_issuer_name(&cert, subject);
220 x509write_crt_set_validity(&cert, fstr, tstr);
221 x509write_crt_set_basic_constraints(&cert, 0, -1);
222 x509write_crt_set_subject_key_identifier(&cert);
223 x509write_crt_set_authority_key_identifier(&cert);
225 _urandom(NULL, buf, 8);
226 for (len = 0; len < 8; len++)
227 sprintf(sstr + len*2, "%02x", (unsigned char) buf[len]);
230 mpi_read_string(&serial, 16, sstr);
231 x509write_crt_set_serial(&cert, &serial);
234 if (x509write_crt_pem(&cert, (void *) buf, sizeof(buf), _urandom, NULL) < 0) {
235 fprintf(stderr, "Failed to generate certificate\n");
241 len = x509write_crt_der(&cert, (void *) buf, sizeof(buf), _urandom, NULL);
243 fprintf(stderr, "Failed to generate certificate: %d\n", len);
247 write_file(certpath, len, pem);
249 x509write_crt_free(&cert);
256 int main(int argc, char *argv[])
258 urandom_fd = open("/dev/urandom", O_RDONLY);
262 } else if (!strcmp(argv[1], "rsakey")) {
263 return rsakey(argv+2);
264 } else if (!strcmp(argv[1], "selfsigned")) {
265 return selfsigned(argv+2);
269 "PX5G X.509 Certificate Generator Utility v" PX5G_VERSION "\n" PX5G_COPY
270 "\nbased on PolarSSL by Christophe Devine and Paul Bakker\n\n");
271 fprintf(stderr, "Usage: %s [rsakey|selfsigned]\n", *argv);