px5g.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * px5g - Embedded x509 key and certificate generator based on PolarSSL
  3. *
  4. * Copyright (C) 2009 Steven Barth <[email protected]>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License, version 2.1 as published by the Free Software Foundation.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. * MA 02110-1301 USA
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <time.h>
  24. #include <limits.h>
  25. #include "polarssl/havege.h"
  26. #include "polarssl/bignum.h"
  27. #include "polarssl/x509.h"
  28. #include "polarssl/rsa.h"
  29. #define PX5G_VERSION "0.1"
  30. #define PX5G_COPY "Copyright (c) 2009 Steven Barth <[email protected]>"
  31. #define PX5G_LICENSE "Licensed under the GNU Lesser General Public License v2.1"
  32. int rsakey(char **arg) {
  33. havege_state hs;
  34. rsa_context rsa;
  35. unsigned int ksize = 512;
  36. int exp = 65537;
  37. char *path = NULL;
  38. int flag = X509_OUTPUT_PEM;
  39. while (*arg && **arg == '-') {
  40. if (!strcmp(*arg, "-out") && arg[1]) {
  41. path = arg[1];
  42. arg++;
  43. } else if (!strcmp(*arg, "-3")) {
  44. exp = 3;
  45. } else if (!strcmp(*arg, "-der")) {
  46. flag = X509_OUTPUT_DER;
  47. }
  48. arg++;
  49. }
  50. if (*arg) {
  51. ksize = (unsigned int)atoi(*arg);
  52. }
  53. havege_init(&hs);
  54. rsa_init(&rsa, RSA_PKCS_V15, 0, havege_rand, &hs);
  55. fprintf(stderr, "Generating RSA private key, %i bit long modulus\n", ksize);
  56. if (rsa_gen_key(&rsa, ksize, exp)) {
  57. fprintf(stderr, "error: key generation failed\n");
  58. return 1;
  59. }
  60. if (x509write_keyfile(&rsa, path, flag)) {
  61. fprintf(stderr, "error: I/O error\n");
  62. return 1;
  63. }
  64. rsa_free(&rsa);
  65. return 0;
  66. }
  67. int selfsigned(char **arg) {
  68. havege_state hs;
  69. rsa_context rsa;
  70. x509_node node;
  71. char *subject = "";
  72. unsigned int ksize = 512;
  73. int exp = 65537;
  74. unsigned int days = 30;
  75. char *keypath = NULL, *certpath = NULL;
  76. int flag = X509_OUTPUT_PEM;
  77. time_t from = time(NULL), to;
  78. char fstr[20], tstr[20];
  79. while (*arg && **arg == '-') {
  80. if (!strcmp(*arg, "-der")) {
  81. flag = X509_OUTPUT_DER;
  82. } else if (!strcmp(*arg, "-newkey") && arg[1]) {
  83. if (strncmp(arg[1], "rsa:", 4)) {
  84. fprintf(stderr, "error: invalid algorithm");
  85. return 1;
  86. }
  87. ksize = (unsigned int)atoi(arg[1] + 4);
  88. arg++;
  89. } else if (!strcmp(*arg, "-days") && arg[1]) {
  90. days = (unsigned int)atoi(arg[1]);
  91. arg++;
  92. } else if (!strcmp(*arg, "-keyout") && arg[1]) {
  93. keypath = arg[1];
  94. arg++;
  95. } else if (!strcmp(*arg, "-out") && arg[1]) {
  96. certpath = arg[1];
  97. arg++;
  98. } else if (!strcmp(*arg, "-subj") && arg[1]) {
  99. if (arg[1][0] != '/' || strchr(arg[1], ';')) {
  100. fprintf(stderr, "error: invalid subject");
  101. return 1;
  102. }
  103. subject = calloc(strlen(arg[1]) + 1, 1);
  104. char *oldc = arg[1] + 1, *newc = subject, *delim;
  105. do {
  106. delim = strchr(oldc, '=');
  107. if (!delim) {
  108. fprintf(stderr, "error: invalid subject");
  109. return 1;
  110. }
  111. memcpy(newc, oldc, delim - oldc + 1);
  112. newc += delim - oldc + 1;
  113. oldc = delim + 1;
  114. delim = strchr(oldc, '/');
  115. if (!delim) {
  116. delim = arg[1] + strlen(arg[1]);
  117. }
  118. memcpy(newc, oldc, delim - oldc);
  119. newc += delim - oldc;
  120. *newc++ = ';';
  121. oldc = delim + 1;
  122. } while(*delim);
  123. arg++;
  124. }
  125. arg++;
  126. }
  127. havege_init(&hs);
  128. rsa_init(&rsa, RSA_PKCS_V15, 0, havege_rand, &hs);
  129. x509write_init_node(&node);
  130. fprintf(stderr, "Generating RSA private key, %i bit long modulus\n", ksize);
  131. if (rsa_gen_key(&rsa, ksize, exp)) {
  132. fprintf(stderr, "error: key generation failed\n");
  133. return 1;
  134. }
  135. if (keypath) {
  136. if (x509write_keyfile(&rsa, keypath, flag)) {
  137. fprintf(stderr, "error: I/O error\n");
  138. return 1;
  139. }
  140. }
  141. from = (from < 1000000000) ? 1000000000 : from;
  142. strftime(fstr, sizeof(fstr), "%F %H:%M:%S", gmtime(&from));
  143. to = from + 60 * 60 * 24 * days;
  144. if (to < from)
  145. to = INT_MAX;
  146. strftime(tstr, sizeof(tstr), "%F %H:%M:%S", gmtime(&to));
  147. x509_raw cert;
  148. x509write_init_raw(&cert);
  149. x509write_add_pubkey(&cert, &rsa);
  150. x509write_add_subject(&cert, (unsigned char*)subject);
  151. x509write_add_validity(&cert, (unsigned char*)fstr, (unsigned char*)tstr);
  152. fprintf(stderr, "Generating selfsigned certificate with subject '%s'"
  153. " and validity %s-%s\n", subject, fstr, tstr);
  154. if (x509write_create_selfsign(&cert, &rsa)) {
  155. fprintf(stderr, "error: certificate generation failed\n");
  156. }
  157. if (x509write_crtfile(&cert, (unsigned char*)certpath, flag)) {
  158. fprintf(stderr, "error: I/O error\n");
  159. return 1;
  160. }
  161. x509write_free_raw(&cert);
  162. rsa_free(&rsa);
  163. return 0;
  164. }
  165. int main(int argc, char *argv[]) {
  166. if (!argv[1]) {
  167. //Usage
  168. } else if (!strcmp(argv[1], "rsakey")) {
  169. return rsakey(argv+2);
  170. } else if (!strcmp(argv[1], "selfsigned")) {
  171. return selfsigned(argv+2);
  172. }
  173. fprintf(stderr,
  174. "PX5G X.509 Certificate Generator Utility v" PX5G_VERSION "\n" PX5G_COPY
  175. "\nbased on PolarSSL by Christophe Devine and Paul Bakker\n\n");
  176. fprintf(stderr, "Usage: %s [rsakey|selfsigned]\n", *argv);
  177. return 1;
  178. }