spkac.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 1999. Based on an original idea by Massimiliano Pala (madwolf@openca.org).
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <time.h>
  62. #include "apps.h"
  63. #include <openssl/bio.h>
  64. #include <openssl/conf.h>
  65. #include <openssl/err.h>
  66. #include <openssl/evp.h>
  67. #include <openssl/lhash.h>
  68. #include <openssl/x509.h>
  69. #include <openssl/pem.h>
  70. typedef enum OPTION_choice {
  71. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  72. OPT_NOOUT, OPT_PUBKEY, OPT_VERIFY, OPT_IN, OPT_OUT,
  73. OPT_ENGINE, OPT_KEY, OPT_CHALLENGE, OPT_PASSIN, OPT_SPKAC,
  74. OPT_SPKSECT
  75. } OPTION_CHOICE;
  76. OPTIONS spkac_options[] = {
  77. {"help", OPT_HELP, '-', "Display this summary"},
  78. {"in", OPT_IN, '<', "Input file"},
  79. {"out", OPT_OUT, '>', "Output file"},
  80. {"key", OPT_KEY, '<', "Create SPKAC using private key"},
  81. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  82. {"challenge", OPT_CHALLENGE, 's', "Challenge string"},
  83. {"spkac", OPT_SPKAC, 's', "Alternative SPKAC name"},
  84. {"noout", OPT_NOOUT, '-', "Don't print SPKAC"},
  85. {"pubkey", OPT_PUBKEY, '-', "Output public key"},
  86. {"verify", OPT_VERIFY, '-', "Verify SPKAC signature"},
  87. {"spksect", OPT_SPKSECT, 's'},
  88. #ifndef OPENSSL_NO_ENGINE
  89. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  90. #endif
  91. {NULL}
  92. };
  93. int spkac_main(int argc, char **argv)
  94. {
  95. BIO *out = NULL;
  96. CONF *conf = NULL;
  97. ENGINE *e = NULL;
  98. EVP_PKEY *pkey = NULL;
  99. NETSCAPE_SPKI *spki = NULL;
  100. char *challenge = NULL, *keyfile = NULL;
  101. char *infile = NULL, *outfile = NULL, *passinarg = NULL, *passin = NULL;
  102. char *spkstr = NULL, *prog;
  103. const char *spkac = "SPKAC", *spksect = "default";
  104. int i, ret = 1, verify = 0, noout = 0, pubkey = 0;
  105. OPTION_CHOICE o;
  106. prog = opt_init(argc, argv, spkac_options);
  107. while ((o = opt_next()) != OPT_EOF) {
  108. switch (o) {
  109. case OPT_EOF:
  110. case OPT_ERR:
  111. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  112. goto end;
  113. case OPT_HELP:
  114. opt_help(spkac_options);
  115. ret = 0;
  116. goto end;
  117. case OPT_IN:
  118. infile = opt_arg();
  119. break;
  120. case OPT_OUT:
  121. outfile = opt_arg();
  122. break;
  123. case OPT_NOOUT:
  124. noout = 1;
  125. break;
  126. case OPT_PUBKEY:
  127. pubkey = 1;
  128. break;
  129. case OPT_VERIFY:
  130. verify = 1;
  131. break;
  132. case OPT_PASSIN:
  133. passinarg = opt_arg();
  134. break;
  135. case OPT_KEY:
  136. keyfile = opt_arg();
  137. break;
  138. case OPT_CHALLENGE:
  139. challenge = opt_arg();
  140. break;
  141. case OPT_SPKAC:
  142. spkac = opt_arg();
  143. break;
  144. case OPT_SPKSECT:
  145. spksect = opt_arg();
  146. break;
  147. case OPT_ENGINE:
  148. e = setup_engine(opt_arg(), 0);
  149. break;
  150. }
  151. }
  152. argc = opt_num_rest();
  153. argv = opt_rest();
  154. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  155. BIO_printf(bio_err, "Error getting password\n");
  156. goto end;
  157. }
  158. if (keyfile) {
  159. pkey = load_key(strcmp(keyfile, "-") ? keyfile : NULL,
  160. FORMAT_PEM, 1, passin, e, "private key");
  161. if (!pkey) {
  162. goto end;
  163. }
  164. spki = NETSCAPE_SPKI_new();
  165. if (challenge)
  166. ASN1_STRING_set(spki->spkac->challenge,
  167. challenge, (int)strlen(challenge));
  168. NETSCAPE_SPKI_set_pubkey(spki, pkey);
  169. NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
  170. spkstr = NETSCAPE_SPKI_b64_encode(spki);
  171. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  172. if (out == NULL)
  173. goto end;
  174. BIO_printf(out, "SPKAC=%s\n", spkstr);
  175. OPENSSL_free(spkstr);
  176. ret = 0;
  177. goto end;
  178. }
  179. if ((conf = app_load_config(infile)) == NULL)
  180. goto end;
  181. spkstr = NCONF_get_string(conf, spksect, spkac);
  182. if (spkstr == NULL) {
  183. BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
  184. ERR_print_errors(bio_err);
  185. goto end;
  186. }
  187. spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
  188. if (!spki) {
  189. BIO_printf(bio_err, "Error loading SPKAC\n");
  190. ERR_print_errors(bio_err);
  191. goto end;
  192. }
  193. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  194. if (out == NULL)
  195. goto end;
  196. if (!noout)
  197. NETSCAPE_SPKI_print(out, spki);
  198. pkey = NETSCAPE_SPKI_get_pubkey(spki);
  199. if (verify) {
  200. i = NETSCAPE_SPKI_verify(spki, pkey);
  201. if (i > 0)
  202. BIO_printf(bio_err, "Signature OK\n");
  203. else {
  204. BIO_printf(bio_err, "Signature Failure\n");
  205. ERR_print_errors(bio_err);
  206. goto end;
  207. }
  208. }
  209. if (pubkey)
  210. PEM_write_bio_PUBKEY(out, pkey);
  211. ret = 0;
  212. end:
  213. NCONF_free(conf);
  214. NETSCAPE_SPKI_free(spki);
  215. BIO_free_all(out);
  216. EVP_PKEY_free(pkey);
  217. OPENSSL_free(passin);
  218. return (ret);
  219. }