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@nbd.name>
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 <mbedtls/bignum.h>
34 #include <mbedtls/x509_crt.h>
35 #include <mbedtls/rsa.h>
36 #include <mbedtls/pk.h>
38 #define PX5G_VERSION "0.2"
39 #define PX5G_COPY "Copyright (c) 2009 Steven Barth <steven@midlink.org>"
40 #define PX5G_LICENSE "Licensed under the GNU Lesser General Public License v2.1"
42 static int urandom_fd;
43 static char buf[16384];
45 static int _urandom(void *ctx, unsigned char *out, size_t len)
47 read(urandom_fd, out, len);
51 static void write_file(const char *path, int len, bool pem)
54 const char *buf_start = buf;
57 buf_start += sizeof(buf) - len;
60 fprintf(stderr, "No data to write\n");
65 fprintf(stderr, "error: I/O error\n");
72 fwrite(buf_start, 1, len, f);
76 static void write_key(mbedtls_pk_context *key, const char *path, bool pem)
81 if (mbedtls_pk_write_key_pem(key, (void *) buf, sizeof(buf)) == 0)
84 len = mbedtls_pk_write_key_der(key, (void *) buf, sizeof(buf));
89 write_file(path, len, pem);
92 static void gen_key(mbedtls_pk_context *key, int ksize, int exp, bool pem)
95 fprintf(stderr, "Generating RSA private key, %i bit long modulus\n", ksize);
96 mbedtls_pk_setup(key, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));
97 if (mbedtls_rsa_gen_key(mbedtls_pk_rsa(*key), _urandom, NULL, ksize, exp)) {
98 fprintf(stderr, "error: key generation failed\n");
103 int rsakey(char **arg)
105 mbedtls_pk_context key;
106 unsigned int ksize = 512;
111 while (*arg && **arg == '-') {
112 if (!strcmp(*arg, "-out") && arg[1]) {
115 } else if (!strcmp(*arg, "-3")) {
117 } else if (!strcmp(*arg, "-der")) {
124 ksize = (unsigned int)atoi(*arg);
126 gen_key(&key, ksize, exp, pem);
127 write_key(&key, path, pem);
129 mbedtls_pk_free(&key);
134 int selfsigned(char **arg)
136 mbedtls_pk_context key;
137 mbedtls_x509write_cert cert;
141 unsigned int ksize = 512;
143 unsigned int days = 30;
144 char *keypath = NULL, *certpath = NULL;
146 time_t from = time(NULL), to;
147 char fstr[20], tstr[20], sstr[17];
150 while (*arg && **arg == '-') {
151 if (!strcmp(*arg, "-der")) {
153 } else if (!strcmp(*arg, "-newkey") && arg[1]) {
154 if (strncmp(arg[1], "rsa:", 4)) {
155 fprintf(stderr, "error: invalid algorithm");
158 ksize = (unsigned int)atoi(arg[1] + 4);
160 } else if (!strcmp(*arg, "-days") && arg[1]) {
161 days = (unsigned int)atoi(arg[1]);
163 } else if (!strcmp(*arg, "-keyout") && arg[1]) {
166 } else if (!strcmp(*arg, "-out") && arg[1]) {
169 } else if (!strcmp(*arg, "-subj") && arg[1]) {
170 if (arg[1][0] != '/' || strchr(arg[1], ';')) {
171 fprintf(stderr, "error: invalid subject");
174 subject = calloc(strlen(arg[1]) + 1, 1);
175 char *oldc = arg[1] + 1, *newc = subject, *delim;
177 delim = strchr(oldc, '=');
179 fprintf(stderr, "error: invalid subject");
182 memcpy(newc, oldc, delim - oldc + 1);
183 newc += delim - oldc + 1;
186 delim = strchr(oldc, '/');
188 delim = arg[1] + strlen(arg[1]);
190 memcpy(newc, oldc, delim - oldc);
191 newc += delim - oldc;
200 gen_key(&key, ksize, exp, pem);
203 write_key(&key, keypath, pem);
205 from = (from < 1000000000) ? 1000000000 : from;
206 strftime(fstr, sizeof(fstr), "%Y%m%d%H%M%S", gmtime(&from));
207 to = from + 60 * 60 * 24 * days;
210 strftime(tstr, sizeof(tstr), "%Y%m%d%H%M%S", gmtime(&to));
212 fprintf(stderr, "Generating selfsigned certificate with subject '%s'"
213 " and validity %s-%s\n", subject, fstr, tstr);
215 mbedtls_x509write_crt_init(&cert);
216 mbedtls_x509write_crt_set_md_alg(&cert, MBEDTLS_MD_SHA256);
217 mbedtls_x509write_crt_set_issuer_key(&cert, &key);
218 mbedtls_x509write_crt_set_subject_key(&cert, &key);
219 mbedtls_x509write_crt_set_subject_name(&cert, subject);
220 mbedtls_x509write_crt_set_issuer_name(&cert, subject);
221 mbedtls_x509write_crt_set_validity(&cert, fstr, tstr);
222 mbedtls_x509write_crt_set_basic_constraints(&cert, 0, -1);
223 mbedtls_x509write_crt_set_subject_key_identifier(&cert);
224 mbedtls_x509write_crt_set_authority_key_identifier(&cert);
226 _urandom(NULL, buf, 8);
227 for (len = 0; len < 8; len++)
228 sprintf(sstr + len*2, "%02x", (unsigned char) buf[len]);
230 mbedtls_mpi_init(&serial);
231 mbedtls_mpi_read_string(&serial, 16, sstr);
232 mbedtls_x509write_crt_set_serial(&cert, &serial);
235 if (mbedtls_x509write_crt_pem(&cert, (void *) buf, sizeof(buf), _urandom, NULL) < 0) {
236 fprintf(stderr, "Failed to generate certificate\n");
242 len = mbedtls_x509write_crt_der(&cert, (void *) buf, sizeof(buf), _urandom, NULL);
244 fprintf(stderr, "Failed to generate certificate: %d\n", len);
248 write_file(certpath, len, pem);
250 mbedtls_x509write_crt_free(&cert);
251 mbedtls_mpi_free(&serial);
252 mbedtls_pk_free(&key);
257 int main(int argc, char *argv[])
259 urandom_fd = open("/dev/urandom", O_RDONLY);
263 } else if (!strcmp(argv[1], "rsakey")) {
264 return rsakey(argv+2);
265 } else if (!strcmp(argv[1], "selfsigned")) {
266 return selfsigned(argv+2);
270 "PX5G X.509 Certificate Generator Utility v" PX5G_VERSION "\n" PX5G_COPY
271 "\nbased on PolarSSL by Christophe Devine and Paul Bakker\n\n");
272 fprintf(stderr, "Usage: %s [rsakey|selfsigned]\n", *argv);