spkac.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* apps/spkac.c */
  2. /*
  3. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  4. * 1999. Based on an original idea by Massimiliano Pala (madwolf@openca.org).
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include <time.h>
  63. #include "apps.h"
  64. #include <openssl/bio.h>
  65. #include <openssl/conf.h>
  66. #include <openssl/err.h>
  67. #include <openssl/evp.h>
  68. #include <openssl/lhash.h>
  69. #include <openssl/x509.h>
  70. #include <openssl/pem.h>
  71. #undef PROG
  72. #define PROG spkac_main
  73. /*-
  74. * -in arg - input file - default stdin
  75. * -out arg - output file - default stdout
  76. */
  77. int MAIN(int, char **);
  78. int MAIN(int argc, char **argv)
  79. {
  80. ENGINE *e = NULL;
  81. int i, badops = 0, ret = 1;
  82. BIO *in = NULL, *out = NULL;
  83. int verify = 0, noout = 0, pubkey = 0;
  84. char *infile = NULL, *outfile = NULL, *prog;
  85. char *passargin = NULL, *passin = NULL;
  86. const char *spkac = "SPKAC", *spksect = "default";
  87. char *spkstr = NULL;
  88. char *challenge = NULL, *keyfile = NULL;
  89. CONF *conf = NULL;
  90. NETSCAPE_SPKI *spki = NULL;
  91. EVP_PKEY *pkey = NULL;
  92. #ifndef OPENSSL_NO_ENGINE
  93. char *engine = NULL;
  94. #endif
  95. apps_startup();
  96. if (!bio_err)
  97. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  98. if (!load_config(bio_err, NULL))
  99. goto end;
  100. prog = argv[0];
  101. argc--;
  102. argv++;
  103. while (argc >= 1) {
  104. if (strcmp(*argv, "-in") == 0) {
  105. if (--argc < 1)
  106. goto bad;
  107. infile = *(++argv);
  108. } else if (strcmp(*argv, "-out") == 0) {
  109. if (--argc < 1)
  110. goto bad;
  111. outfile = *(++argv);
  112. } else if (strcmp(*argv, "-passin") == 0) {
  113. if (--argc < 1)
  114. goto bad;
  115. passargin = *(++argv);
  116. } else if (strcmp(*argv, "-key") == 0) {
  117. if (--argc < 1)
  118. goto bad;
  119. keyfile = *(++argv);
  120. } else if (strcmp(*argv, "-challenge") == 0) {
  121. if (--argc < 1)
  122. goto bad;
  123. challenge = *(++argv);
  124. } else if (strcmp(*argv, "-spkac") == 0) {
  125. if (--argc < 1)
  126. goto bad;
  127. spkac = *(++argv);
  128. } else if (strcmp(*argv, "-spksect") == 0) {
  129. if (--argc < 1)
  130. goto bad;
  131. spksect = *(++argv);
  132. }
  133. #ifndef OPENSSL_NO_ENGINE
  134. else if (strcmp(*argv, "-engine") == 0) {
  135. if (--argc < 1)
  136. goto bad;
  137. engine = *(++argv);
  138. }
  139. #endif
  140. else if (strcmp(*argv, "-noout") == 0)
  141. noout = 1;
  142. else if (strcmp(*argv, "-pubkey") == 0)
  143. pubkey = 1;
  144. else if (strcmp(*argv, "-verify") == 0)
  145. verify = 1;
  146. else
  147. badops = 1;
  148. argc--;
  149. argv++;
  150. }
  151. if (badops) {
  152. bad:
  153. BIO_printf(bio_err, "%s [options]\n", prog);
  154. BIO_printf(bio_err, "where options are\n");
  155. BIO_printf(bio_err, " -in arg input file\n");
  156. BIO_printf(bio_err, " -out arg output file\n");
  157. BIO_printf(bio_err,
  158. " -key arg create SPKAC using private key\n");
  159. BIO_printf(bio_err,
  160. " -passin arg input file pass phrase source\n");
  161. BIO_printf(bio_err, " -challenge arg challenge string\n");
  162. BIO_printf(bio_err, " -spkac arg alternative SPKAC name\n");
  163. BIO_printf(bio_err, " -noout don't print SPKAC\n");
  164. BIO_printf(bio_err, " -pubkey output public key\n");
  165. BIO_printf(bio_err, " -verify verify SPKAC signature\n");
  166. #ifndef OPENSSL_NO_ENGINE
  167. BIO_printf(bio_err,
  168. " -engine e use engine e, possibly a hardware device.\n");
  169. #endif
  170. goto end;
  171. }
  172. ERR_load_crypto_strings();
  173. if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
  174. BIO_printf(bio_err, "Error getting password\n");
  175. goto end;
  176. }
  177. #ifndef OPENSSL_NO_ENGINE
  178. e = setup_engine(bio_err, engine, 0);
  179. #endif
  180. if (keyfile) {
  181. pkey = load_key(bio_err,
  182. strcmp(keyfile, "-") ? keyfile : NULL,
  183. FORMAT_PEM, 1, passin, e, "private key");
  184. if (!pkey) {
  185. goto end;
  186. }
  187. spki = NETSCAPE_SPKI_new();
  188. if (challenge)
  189. ASN1_STRING_set(spki->spkac->challenge,
  190. challenge, (int)strlen(challenge));
  191. NETSCAPE_SPKI_set_pubkey(spki, pkey);
  192. NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
  193. spkstr = NETSCAPE_SPKI_b64_encode(spki);
  194. if (outfile)
  195. out = BIO_new_file(outfile, "w");
  196. else {
  197. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  198. #ifdef OPENSSL_SYS_VMS
  199. {
  200. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  201. out = BIO_push(tmpbio, out);
  202. }
  203. #endif
  204. }
  205. if (!out) {
  206. BIO_printf(bio_err, "Error opening output file\n");
  207. ERR_print_errors(bio_err);
  208. goto end;
  209. }
  210. BIO_printf(out, "SPKAC=%s\n", spkstr);
  211. OPENSSL_free(spkstr);
  212. ret = 0;
  213. goto end;
  214. }
  215. if (infile)
  216. in = BIO_new_file(infile, "r");
  217. else
  218. in = BIO_new_fp(stdin, BIO_NOCLOSE);
  219. if (!in) {
  220. BIO_printf(bio_err, "Error opening input file\n");
  221. ERR_print_errors(bio_err);
  222. goto end;
  223. }
  224. conf = NCONF_new(NULL);
  225. i = NCONF_load_bio(conf, in, NULL);
  226. if (!i) {
  227. BIO_printf(bio_err, "Error parsing config file\n");
  228. ERR_print_errors(bio_err);
  229. goto end;
  230. }
  231. spkstr = NCONF_get_string(conf, spksect, spkac);
  232. if (!spkstr) {
  233. BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
  234. ERR_print_errors(bio_err);
  235. goto end;
  236. }
  237. spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
  238. if (!spki) {
  239. BIO_printf(bio_err, "Error loading SPKAC\n");
  240. ERR_print_errors(bio_err);
  241. goto end;
  242. }
  243. if (outfile)
  244. out = BIO_new_file(outfile, "w");
  245. else {
  246. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  247. #ifdef OPENSSL_SYS_VMS
  248. {
  249. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  250. out = BIO_push(tmpbio, out);
  251. }
  252. #endif
  253. }
  254. if (!out) {
  255. BIO_printf(bio_err, "Error opening output file\n");
  256. ERR_print_errors(bio_err);
  257. goto end;
  258. }
  259. if (!noout)
  260. NETSCAPE_SPKI_print(out, spki);
  261. pkey = NETSCAPE_SPKI_get_pubkey(spki);
  262. if (verify) {
  263. i = NETSCAPE_SPKI_verify(spki, pkey);
  264. if (i > 0)
  265. BIO_printf(bio_err, "Signature OK\n");
  266. else {
  267. BIO_printf(bio_err, "Signature Failure\n");
  268. ERR_print_errors(bio_err);
  269. goto end;
  270. }
  271. }
  272. if (pubkey)
  273. PEM_write_bio_PUBKEY(out, pkey);
  274. ret = 0;
  275. end:
  276. NCONF_free(conf);
  277. NETSCAPE_SPKI_free(spki);
  278. BIO_free(in);
  279. BIO_free_all(out);
  280. EVP_PKEY_free(pkey);
  281. if (passin)
  282. OPENSSL_free(passin);
  283. apps_shutdown();
  284. OPENSSL_EXIT(ret);
  285. }