px5g.c 7.3 KB

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