px5g.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. #include <sys/types.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <limits.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <stdbool.h>
  30. #include <polarssl/bignum.h>
  31. #include <polarssl/x509_crt.h>
  32. #include <polarssl/rsa.h>
  33. #define PX5G_VERSION "0.2"
  34. #define PX5G_COPY "Copyright (c) 2009 Steven Barth <steven@midlink.org>"
  35. #define PX5G_LICENSE "Licensed under the GNU Lesser General Public License v2.1"
  36. static int urandom_fd;
  37. static char buf[16384];
  38. static int _urandom(void *ctx, unsigned char *out, size_t len)
  39. {
  40. read(urandom_fd, out, len);
  41. return 0;
  42. }
  43. static void write_file(const char *path, int len, bool pem)
  44. {
  45. FILE *f = stdout;
  46. const char *buf_start = buf;
  47. if (!pem)
  48. buf_start += sizeof(buf) - len;
  49. if (!len) {
  50. fprintf(stderr, "No data to write\n");
  51. exit(1);
  52. }
  53. if (!f) {
  54. fprintf(stderr, "error: I/O error\n");
  55. exit(1);
  56. }
  57. if (path)
  58. f = fopen(path, "w");
  59. fwrite(buf_start, 1, len, f);
  60. fclose(f);
  61. }
  62. static void write_key(pk_context *key, const char *path, bool pem)
  63. {
  64. int len = 0;
  65. if (pem) {
  66. if (pk_write_key_pem(key, (void *) buf, sizeof(buf)) == 0)
  67. len = strlen(buf);
  68. } else {
  69. len = pk_write_key_der(key, (void *) buf, sizeof(buf));
  70. if (len < 0)
  71. len = 0;
  72. }
  73. write_file(path, len, pem);
  74. }
  75. static void gen_key(pk_context *key, int ksize, int exp, bool pem)
  76. {
  77. pk_init(key);
  78. pk_init_ctx(key, pk_info_from_type(POLARSSL_PK_RSA));
  79. fprintf(stderr, "Generating RSA private key, %i bit long modulus\n", ksize);
  80. if (rsa_gen_key(pk_rsa(*key), _urandom, NULL, ksize, exp)) {
  81. fprintf(stderr, "error: key generation failed\n");
  82. exit(1);
  83. }
  84. }
  85. int rsakey(char **arg)
  86. {
  87. pk_context key;
  88. unsigned int ksize = 512;
  89. int exp = 65537;
  90. char *path = NULL;
  91. bool pem = true;
  92. while (*arg && **arg == '-') {
  93. if (!strcmp(*arg, "-out") && arg[1]) {
  94. path = arg[1];
  95. arg++;
  96. } else if (!strcmp(*arg, "-3")) {
  97. exp = 3;
  98. } else if (!strcmp(*arg, "-der")) {
  99. pem = false;
  100. }
  101. arg++;
  102. }
  103. if (*arg)
  104. ksize = (unsigned int)atoi(*arg);
  105. gen_key(&key, ksize, exp, pem);
  106. write_key(&key, path, pem);
  107. pk_free(&key);
  108. return 0;
  109. }
  110. int selfsigned(char **arg)
  111. {
  112. pk_context key;
  113. x509write_cert cert;
  114. mpi serial;
  115. char *subject = "";
  116. unsigned int ksize = 512;
  117. int exp = 65537;
  118. unsigned int days = 30;
  119. char *keypath = NULL, *certpath = NULL;
  120. bool pem = true;
  121. time_t from = time(NULL), to;
  122. char fstr[20], tstr[20], sstr[17];
  123. int len;
  124. while (*arg && **arg == '-') {
  125. if (!strcmp(*arg, "-der")) {
  126. pem = false;
  127. } else if (!strcmp(*arg, "-newkey") && arg[1]) {
  128. if (strncmp(arg[1], "rsa:", 4)) {
  129. fprintf(stderr, "error: invalid algorithm");
  130. return 1;
  131. }
  132. ksize = (unsigned int)atoi(arg[1] + 4);
  133. arg++;
  134. } else if (!strcmp(*arg, "-days") && arg[1]) {
  135. days = (unsigned int)atoi(arg[1]);
  136. arg++;
  137. } else if (!strcmp(*arg, "-keyout") && arg[1]) {
  138. keypath = arg[1];
  139. arg++;
  140. } else if (!strcmp(*arg, "-out") && arg[1]) {
  141. certpath = arg[1];
  142. arg++;
  143. } else if (!strcmp(*arg, "-subj") && arg[1]) {
  144. if (arg[1][0] != '/' || strchr(arg[1], ';')) {
  145. fprintf(stderr, "error: invalid subject");
  146. return 1;
  147. }
  148. subject = calloc(strlen(arg[1]) + 1, 1);
  149. char *oldc = arg[1] + 1, *newc = subject, *delim;
  150. do {
  151. delim = strchr(oldc, '=');
  152. if (!delim) {
  153. fprintf(stderr, "error: invalid subject");
  154. return 1;
  155. }
  156. memcpy(newc, oldc, delim - oldc + 1);
  157. newc += delim - oldc + 1;
  158. oldc = delim + 1;
  159. delim = strchr(oldc, '/');
  160. if (!delim) {
  161. delim = arg[1] + strlen(arg[1]);
  162. }
  163. memcpy(newc, oldc, delim - oldc);
  164. newc += delim - oldc;
  165. *newc++ = ',';
  166. oldc = delim + 1;
  167. } while(*delim);
  168. arg++;
  169. }
  170. arg++;
  171. }
  172. gen_key(&key, ksize, exp, pem);
  173. if (keypath)
  174. write_key(&key, keypath, pem);
  175. from = (from < 1000000000) ? 1000000000 : from;
  176. strftime(fstr, sizeof(fstr), "%Y%m%d%H%M%S", gmtime(&from));
  177. to = from + 60 * 60 * 24 * days;
  178. if (to < from)
  179. to = INT_MAX;
  180. strftime(tstr, sizeof(tstr), "%Y%m%d%H%M%S", gmtime(&to));
  181. fprintf(stderr, "Generating selfsigned certificate with subject '%s'"
  182. " and validity %s-%s\n", subject, fstr, tstr);
  183. x509write_crt_init(&cert);
  184. x509write_crt_set_md_alg(&cert, POLARSSL_MD_SHA256);
  185. x509write_crt_set_issuer_key(&cert, &key);
  186. x509write_crt_set_subject_key(&cert, &key);
  187. x509write_crt_set_subject_name(&cert, subject);
  188. x509write_crt_set_issuer_name(&cert, subject);
  189. x509write_crt_set_validity(&cert, fstr, tstr);
  190. x509write_crt_set_basic_constraints(&cert, 0, -1);
  191. x509write_crt_set_subject_key_identifier(&cert);
  192. x509write_crt_set_authority_key_identifier(&cert);
  193. _urandom(NULL, buf, 8);
  194. for (len = 0; len < 8; len++)
  195. sprintf(sstr + len*2, "%02x", (unsigned char) buf[len]);
  196. mpi_init(&serial);
  197. mpi_read_string(&serial, 16, sstr);
  198. x509write_crt_set_serial(&cert, &serial);
  199. if (pem) {
  200. if (x509write_crt_pem(&cert, (void *) buf, sizeof(buf), _urandom, NULL) < 0) {
  201. fprintf(stderr, "Failed to generate certificate\n");
  202. return 1;
  203. }
  204. len = strlen(buf);
  205. } else {
  206. len = x509write_crt_der(&cert, (void *) buf, sizeof(buf), _urandom, NULL);
  207. if (len < 0) {
  208. fprintf(stderr, "Failed to generate certificate: %d\n", len);
  209. return 1;
  210. }
  211. }
  212. write_file(certpath, len, pem);
  213. x509write_crt_free(&cert);
  214. mpi_free(&serial);
  215. pk_free(&key);
  216. return 0;
  217. }
  218. int main(int argc, char *argv[])
  219. {
  220. urandom_fd = open("/dev/urandom", O_RDONLY);
  221. if (!argv[1]) {
  222. //Usage
  223. } else if (!strcmp(argv[1], "rsakey")) {
  224. return rsakey(argv+2);
  225. } else if (!strcmp(argv[1], "selfsigned")) {
  226. return selfsigned(argv+2);
  227. }
  228. fprintf(stderr,
  229. "PX5G X.509 Certificate Generator Utility v" PX5G_VERSION "\n" PX5G_COPY
  230. "\nbased on PolarSSL by Christophe Devine and Paul Bakker\n\n");
  231. fprintf(stderr, "Usage: %s [rsakey|selfsigned]\n", *argv);
  232. return 1;
  233. }