b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * px5g - Embedded x509 key and certificate generator based on PolarSSL |
| 3 | * |
| 4 | * Copyright (C) 2009 Steven Barth <steven@midlink.org> |
| 5 | * Copyright (C) 2014 Felix Fietkau <nbd@nbd.name> |
| 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, version 2.1 as published by the Free Software Foundation. |
| 10 | * |
| 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. |
| 15 | * |
| 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, |
| 19 | * MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <sys/random.h> |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <time.h> |
| 29 | #include <limits.h> |
| 30 | #include <unistd.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <stdbool.h> |
| 33 | #include <errno.h> |
| 34 | |
| 35 | #include <mbedtls/bignum.h> |
| 36 | #include <mbedtls/entropy.h> |
| 37 | #include <mbedtls/x509_crt.h> |
| 38 | #include <mbedtls/ecp.h> |
| 39 | #include <mbedtls/rsa.h> |
| 40 | #include <mbedtls/pk.h> |
| 41 | #include <mbedtls/asn1.h> |
| 42 | #include <mbedtls/oid.h> |
| 43 | |
| 44 | #define SET_OID(x, oid) \ |
| 45 | do { x.len = MBEDTLS_OID_SIZE(oid); x.p = (unsigned char *) oid; } while (0) |
| 46 | |
| 47 | #define PX5G_VERSION "0.3" |
| 48 | #define PX5G_COPY "Copyright (c) 2009 Steven Barth <steven@midlink.org>" |
| 49 | #define PX5G_LICENSE "Licensed under the GNU Lesser General Public License v2.1" |
| 50 | |
| 51 | static char buf[16384]; |
| 52 | |
| 53 | static int _urandom(void *ctx, unsigned char *out, size_t len) |
| 54 | { |
| 55 | ssize_t ret; |
| 56 | |
| 57 | ret = getrandom(out, len, 0); |
| 58 | if (ret < 0 || (size_t)ret != len) |
| 59 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static void write_file(const char *path, size_t len, bool pem, bool cert) |
| 65 | { |
| 66 | mode_t mode = S_IRUSR | S_IWUSR; |
| 67 | const char *buf_start = buf; |
| 68 | int fd = STDERR_FILENO; |
| 69 | ssize_t written; |
| 70 | int err; |
| 71 | |
| 72 | if (!pem) |
| 73 | buf_start += sizeof(buf) - len; |
| 74 | |
| 75 | if (!len) { |
| 76 | fprintf(stderr, "No data to write\n"); |
| 77 | exit(1); |
| 78 | } |
| 79 | |
| 80 | if (cert) |
| 81 | mode |= S_IRGRP | S_IROTH; |
| 82 | |
| 83 | if (path) |
| 84 | fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode); |
| 85 | |
| 86 | if (fd < 0) { |
| 87 | fprintf(stderr, "error: I/O error\n"); |
| 88 | exit(1); |
| 89 | } |
| 90 | |
| 91 | written = write(fd, buf_start, len); |
| 92 | if (written != len) { |
| 93 | fprintf(stderr, "writing key failed with: %s\n", strerror(errno)); |
| 94 | exit(1); |
| 95 | } |
| 96 | err = fsync(fd); |
| 97 | if (err < 0) { |
| 98 | fprintf(stderr, "syncing key failed with: %s\n", strerror(errno)); |
| 99 | exit(1); |
| 100 | } |
| 101 | if (path) |
| 102 | close(fd); |
| 103 | } |
| 104 | |
| 105 | static mbedtls_ecp_group_id ecp_curve(const char *name) |
| 106 | { |
| 107 | const mbedtls_ecp_curve_info *curve_info; |
| 108 | |
| 109 | if (!strcmp(name, "P-256")) |
| 110 | return MBEDTLS_ECP_DP_SECP256R1; |
| 111 | else if (!strcmp(name, "P-384")) |
| 112 | return MBEDTLS_ECP_DP_SECP384R1; |
| 113 | else if (!strcmp(name, "P-521")) |
| 114 | return MBEDTLS_ECP_DP_SECP521R1; |
| 115 | curve_info = mbedtls_ecp_curve_info_from_name(name); |
| 116 | if (curve_info == NULL) |
| 117 | return MBEDTLS_ECP_DP_NONE; |
| 118 | else |
| 119 | return curve_info->grp_id; |
| 120 | } |
| 121 | |
| 122 | static void write_key(mbedtls_pk_context *key, const char *path, bool pem) |
| 123 | { |
| 124 | int len = 0; |
| 125 | |
| 126 | if (pem) { |
| 127 | if (mbedtls_pk_write_key_pem(key, (void *) buf, sizeof(buf)) == 0) |
| 128 | len = strlen(buf); |
| 129 | } else { |
| 130 | len = mbedtls_pk_write_key_der(key, (void *) buf, sizeof(buf)); |
| 131 | if (len < 0) |
| 132 | len = 0; |
| 133 | } |
| 134 | |
| 135 | write_file(path, len, pem, false); |
| 136 | } |
| 137 | |
| 138 | static void gen_key(mbedtls_pk_context *key, bool rsa, int ksize, int exp, |
| 139 | mbedtls_ecp_group_id curve, bool pem) |
| 140 | { |
| 141 | mbedtls_pk_init(key); |
| 142 | if (rsa) { |
| 143 | fprintf(stderr, "Generating RSA private key, %i bit long modulus\n", ksize); |
| 144 | mbedtls_pk_setup(key, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA)); |
| 145 | if (!mbedtls_rsa_gen_key(mbedtls_pk_rsa(*key), _urandom, NULL, ksize, exp)) |
| 146 | return; |
| 147 | } else { |
| 148 | fprintf(stderr, "Generating EC private key\n"); |
| 149 | mbedtls_pk_setup(key, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY)); |
| 150 | if (!mbedtls_ecp_gen_key(curve, mbedtls_pk_ec(*key), _urandom, NULL)) |
| 151 | return; |
| 152 | } |
| 153 | fprintf(stderr, "error: key generation failed\n"); |
| 154 | exit(1); |
| 155 | } |
| 156 | |
| 157 | int dokey(bool rsa, char **arg) |
| 158 | { |
| 159 | mbedtls_pk_context key; |
| 160 | unsigned int ksize = 512; |
| 161 | int exp = 65537; |
| 162 | char *path = NULL; |
| 163 | bool pem = true; |
| 164 | mbedtls_ecp_group_id curve = MBEDTLS_ECP_DP_SECP256R1; |
| 165 | |
| 166 | while (*arg && **arg == '-') { |
| 167 | if (!strcmp(*arg, "-out") && arg[1]) { |
| 168 | path = arg[1]; |
| 169 | arg++; |
| 170 | } else if (!strcmp(*arg, "-3")) { |
| 171 | exp = 3; |
| 172 | } else if (!strcmp(*arg, "-der")) { |
| 173 | pem = false; |
| 174 | } |
| 175 | arg++; |
| 176 | } |
| 177 | |
| 178 | if (*arg && rsa) { |
| 179 | ksize = (unsigned int)atoi(*arg); |
| 180 | } else if (*arg) { |
| 181 | curve = ecp_curve((const char *)*arg); |
| 182 | if (curve == MBEDTLS_ECP_DP_NONE) { |
| 183 | fprintf(stderr, "error: invalid curve name: %s\n", *arg); |
| 184 | return 1; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | gen_key(&key, rsa, ksize, exp, curve, pem); |
| 189 | write_key(&key, path, pem); |
| 190 | |
| 191 | mbedtls_pk_free(&key); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | int selfsigned(char **arg) |
| 197 | { |
| 198 | mbedtls_pk_context key; |
| 199 | mbedtls_x509write_cert cert; |
| 200 | mbedtls_mpi serial; |
| 201 | mbedtls_x509_san_list *san_list = NULL, *san_prev = NULL, *san_cur = NULL; |
| 202 | /*support |
| 203 | - MBEDTLS_X509_SAN_DNS_NAME |
| 204 | - MBEDTLS_X509_SAN_IP_ADDRESS |
| 205 | - MBEDTLS_X509_SAN_RFC822_NAME |
| 206 | - MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER |
| 207 | */ |
| 208 | mbedtls_asn1_sequence *eku = NULL, *ext_key_usage = NULL; |
| 209 | char *sanval, *santype; |
| 210 | uint8_t ipaddr[16] = { 0 }; |
| 211 | |
| 212 | char *subject = ""; |
| 213 | unsigned int ksize = 512; |
| 214 | int exp = 65537; |
| 215 | unsigned int days = 30; |
| 216 | char *keypath = NULL, *certpath = NULL; |
| 217 | bool pem = true; |
| 218 | time_t from = time(NULL), to; |
| 219 | char fstr[20], tstr[20], sstr[17]; |
| 220 | int len; |
| 221 | bool rsa = true; |
| 222 | mbedtls_ecp_group_id curve = MBEDTLS_ECP_DP_SECP256R1; |
| 223 | |
| 224 | while (*arg && **arg == '-') { |
| 225 | if (!strcmp(*arg, "-der")) { |
| 226 | pem = false; |
| 227 | } else if (!strcmp(*arg, "-newkey") && arg[1]) { |
| 228 | if (!strncmp(arg[1], "rsa:", 4)) { |
| 229 | rsa = true; |
| 230 | ksize = (unsigned int)atoi(arg[1] + 4); |
| 231 | } else if (!strcmp(arg[1], "ec")) { |
| 232 | rsa = false; |
| 233 | } else { |
| 234 | fprintf(stderr, "error: invalid algorithm\n"); |
| 235 | return 1; |
| 236 | } |
| 237 | arg++; |
| 238 | } else if (!strcmp(*arg, "-days") && arg[1]) { |
| 239 | days = (unsigned int)atoi(arg[1]); |
| 240 | arg++; |
| 241 | } else if (!strcmp(*arg, "-pkeyopt") && arg[1]) { |
| 242 | if (strncmp(arg[1], "ec_paramgen_curve:", 18)) { |
| 243 | fprintf(stderr, "error: invalid pkey option: %s\n", arg[1]); |
| 244 | return 1; |
| 245 | } |
| 246 | curve = ecp_curve((const char *)(arg[1] + 18)); |
| 247 | if (curve == MBEDTLS_ECP_DP_NONE) { |
| 248 | fprintf(stderr, "error: invalid curve name: %s\n", arg[1] + 18); |
| 249 | return 1; |
| 250 | } |
| 251 | arg++; |
| 252 | } else if (!strcmp(*arg, "-keyout") && arg[1]) { |
| 253 | keypath = arg[1]; |
| 254 | arg++; |
| 255 | } else if (!strcmp(*arg, "-out") && arg[1]) { |
| 256 | certpath = arg[1]; |
| 257 | arg++; |
| 258 | } else if (!strcmp(*arg, "-subj") && arg[1]) { |
| 259 | if (arg[1][0] != '/' || strchr(arg[1], ';')) { |
| 260 | fprintf(stderr, "error: invalid subject"); |
| 261 | return 1; |
| 262 | } |
| 263 | subject = calloc(strlen(arg[1]) + 1, 1); |
| 264 | char *oldc = arg[1] + 1, *newc = subject, *delim; |
| 265 | do { |
| 266 | delim = strchr(oldc, '='); |
| 267 | if (!delim) { |
| 268 | fprintf(stderr, "error: invalid subject"); |
| 269 | return 1; |
| 270 | } |
| 271 | memcpy(newc, oldc, delim - oldc + 1); |
| 272 | newc += delim - oldc + 1; |
| 273 | oldc = delim + 1; |
| 274 | |
| 275 | delim = strchr(oldc, '/'); |
| 276 | if (!delim) { |
| 277 | delim = arg[1] + strlen(arg[1]); |
| 278 | } |
| 279 | memcpy(newc, oldc, delim - oldc); |
| 280 | newc += delim - oldc; |
| 281 | *newc++ = ','; |
| 282 | oldc = delim + 1; |
| 283 | } while(*delim); |
| 284 | arg++; |
| 285 | } else if (!strcmp(*arg, "-addext") && arg[1]) { |
| 286 | mbedtls_asn1_sequence **tail = &eku; |
| 287 | if (!strncmp(arg[1], "extendedKeyUsage=", strlen("extendedKeyUsage="))) { |
| 288 | ext_key_usage = calloc(1, sizeof(mbedtls_asn1_sequence)); |
| 289 | ext_key_usage->buf.tag = MBEDTLS_ASN1_OID; |
| 290 | if (!strncmp(arg[1] + strlen("extendedKeyUsage="), "serverAuth", strlen("serverAuth"))) { |
| 291 | SET_OID(ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH); |
| 292 | } else if (!strncmp(arg[1] + strlen("extendedKeyUsage="), "any", strlen("any"))) { |
| 293 | SET_OID(ext_key_usage->buf, MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE); |
| 294 | } // there are other extendedKeyUsage OIDs but none conceivably useful here |
| 295 | *tail = ext_key_usage; |
| 296 | tail = &ext_key_usage->next; |
| 297 | arg++; |
| 298 | } else if (!strncmp(arg[1], "subjectAltName=", strlen("subjectAltName=")) && strchr(arg[1], ':') != NULL) { |
| 299 | santype = strchr(arg[1], '=') + 1; |
| 300 | sanval = strchr(arg[1], ':') + 1; |
| 301 | //build sAN list |
| 302 | san_cur = calloc(1, sizeof(mbedtls_x509_san_list)); |
| 303 | san_cur->next = NULL; |
| 304 | if (!strncmp(santype, "DNS:", strlen("DNS:"))) { |
| 305 | san_cur->node.type = MBEDTLS_X509_SAN_DNS_NAME; |
| 306 | san_cur->node.san.unstructured_name.p = (unsigned char *) sanval; |
| 307 | san_cur->node.san.unstructured_name.len = strlen(sanval); |
| 308 | } else if (!strncmp(santype, "EMAIL:", strlen("EMAIL:"))) { |
| 309 | san_cur->node.type = MBEDTLS_X509_SAN_RFC822_NAME; |
| 310 | san_cur->node.san.unstructured_name.p = (unsigned char *) sanval; |
| 311 | san_cur->node.san.unstructured_name.len = strlen(sanval); |
| 312 | } else if (!strncmp(santype, "IP:", strlen("IP:"))) { |
| 313 | san_cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS; |
| 314 | mbedtls_x509_crt_parse_cn_inet_pton(sanval, ipaddr); |
| 315 | san_cur->node.san.unstructured_name.p = (unsigned char *) ipaddr; |
| 316 | san_cur->node.san.unstructured_name.len = sizeof(ipaddr); |
| 317 | } else if (!strncmp(santype, "URI:", strlen("URI:"))) { |
| 318 | san_cur->node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER; |
| 319 | san_cur->node.san.unstructured_name.p = (unsigned char *) sanval; |
| 320 | san_cur->node.san.unstructured_name.len = strlen(sanval); |
| 321 | } |
| 322 | else fprintf(stderr, "No match to subjectAltName content type.\n"); |
| 323 | arg++; |
| 324 | } |
| 325 | } |
| 326 | arg++; |
| 327 | |
| 328 | //set the pointers in our san_list linked list |
| 329 | if (san_prev == NULL) { |
| 330 | san_list = san_cur; |
| 331 | } else { |
| 332 | san_prev->next = san_cur; |
| 333 | } |
| 334 | san_prev = san_cur; |
| 335 | } |
| 336 | gen_key(&key, rsa, ksize, exp, curve, pem); |
| 337 | |
| 338 | if (keypath) |
| 339 | write_key(&key, keypath, pem); |
| 340 | |
| 341 | from = (from < 1000000000) ? 1000000000 : from; |
| 342 | strftime(fstr, sizeof(fstr), "%Y%m%d%H%M%S", gmtime(&from)); |
| 343 | to = from + 60 * 60 * 24 * days; |
| 344 | if (to < from) |
| 345 | to = INT_MAX; |
| 346 | strftime(tstr, sizeof(tstr), "%Y%m%d%H%M%S", gmtime(&to)); |
| 347 | |
| 348 | fprintf(stderr, "Generating selfsigned certificate with subject '%s'" |
| 349 | " and validity %s-%s\n", subject, fstr, tstr); |
| 350 | |
| 351 | mbedtls_x509write_crt_init(&cert); |
| 352 | mbedtls_x509write_crt_set_md_alg(&cert, MBEDTLS_MD_SHA256); |
| 353 | mbedtls_x509write_crt_set_issuer_key(&cert, &key); |
| 354 | mbedtls_x509write_crt_set_subject_key(&cert, &key); |
| 355 | mbedtls_x509write_crt_set_subject_name(&cert, subject); |
| 356 | mbedtls_x509write_crt_set_issuer_name(&cert, subject); |
| 357 | mbedtls_x509write_crt_set_validity(&cert, fstr, tstr); |
| 358 | mbedtls_x509write_crt_set_basic_constraints(&cert, 0, -1); |
| 359 | mbedtls_x509write_crt_set_subject_key_identifier(&cert); |
| 360 | mbedtls_x509write_crt_set_authority_key_identifier(&cert); |
| 361 | mbedtls_x509write_crt_set_subject_alternative_name(&cert, san_list); |
| 362 | mbedtls_x509write_crt_set_ext_key_usage(&cert, ext_key_usage); |
| 363 | |
| 364 | _urandom(NULL, (void *) buf, 8); |
| 365 | for (len = 0; len < 8; len++) |
| 366 | sprintf(sstr + len*2, "%02x", (unsigned char) buf[len]); |
| 367 | |
| 368 | mbedtls_mpi_init(&serial); |
| 369 | mbedtls_mpi_read_string(&serial, 16, sstr); |
| 370 | mbedtls_x509write_crt_set_serial(&cert, &serial); |
| 371 | |
| 372 | if (pem) { |
| 373 | if (mbedtls_x509write_crt_pem(&cert, (void *) buf, sizeof(buf), _urandom, NULL) < 0) { |
| 374 | fprintf(stderr, "Failed to generate certificate\n"); |
| 375 | return 1; |
| 376 | } |
| 377 | |
| 378 | len = strlen(buf); |
| 379 | } else { |
| 380 | len = mbedtls_x509write_crt_der(&cert, (void *) buf, sizeof(buf), _urandom, NULL); |
| 381 | if (len < 0) { |
| 382 | fprintf(stderr, "Failed to generate certificate: %d\n", len); |
| 383 | return 1; |
| 384 | } |
| 385 | } |
| 386 | write_file(certpath, len, pem, true); |
| 387 | |
| 388 | mbedtls_x509write_crt_free(&cert); |
| 389 | mbedtls_mpi_free(&serial); |
| 390 | mbedtls_pk_free(&key); |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | int main(int argc, char *argv[]) |
| 396 | { |
| 397 | if (!argv[1]) { |
| 398 | //Usage |
| 399 | } else if (!strcmp(argv[1], "eckey")) { |
| 400 | return dokey(false, argv+2); |
| 401 | } else if (!strcmp(argv[1], "rsakey")) { |
| 402 | return dokey(true, argv+2); |
| 403 | } else if (!strcmp(argv[1], "selfsigned")) { |
| 404 | return selfsigned(argv+2); |
| 405 | } |
| 406 | |
| 407 | fprintf(stderr, |
| 408 | "PX5G X.509 Certificate Generator Utility v" PX5G_VERSION "\n" PX5G_COPY |
| 409 | "\nbased on PolarSSL by Christophe Devine and Paul Bakker\n\n"); |
| 410 | fprintf(stderr, "Usage: %s [eckey|rsakey|selfsigned]\n", *argv); |
| 411 | return 1; |
| 412 | } |