spkac.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* apps/spkac.c */
  2. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  3. * project 1999. Based on an original idea by Massimiliano Pala
  4. * (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. #include <openssl/engine.h>
  72. #undef PROG
  73. #define PROG spkac_main
  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, *key = NULL;
  83. int verify=0,noout=0,pubkey=0;
  84. char *infile = NULL,*outfile = NULL,*prog;
  85. char *passargin = NULL, *passin = NULL;
  86. char *spkac = "SPKAC", *spksect = "default", *spkstr = NULL;
  87. char *challenge = NULL, *keyfile = NULL;
  88. LHASH *conf = NULL;
  89. NETSCAPE_SPKI *spki = NULL;
  90. EVP_PKEY *pkey = NULL;
  91. char *engine=NULL;
  92. apps_startup();
  93. if (!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  94. prog=argv[0];
  95. argc--;
  96. argv++;
  97. while (argc >= 1)
  98. {
  99. if (strcmp(*argv,"-in") == 0)
  100. {
  101. if (--argc < 1) goto bad;
  102. infile= *(++argv);
  103. }
  104. else if (strcmp(*argv,"-out") == 0)
  105. {
  106. if (--argc < 1) goto bad;
  107. outfile= *(++argv);
  108. }
  109. else if (strcmp(*argv,"-passin") == 0)
  110. {
  111. if (--argc < 1) goto bad;
  112. passargin= *(++argv);
  113. }
  114. else if (strcmp(*argv,"-key") == 0)
  115. {
  116. if (--argc < 1) goto bad;
  117. keyfile= *(++argv);
  118. }
  119. else if (strcmp(*argv,"-challenge") == 0)
  120. {
  121. if (--argc < 1) goto bad;
  122. challenge= *(++argv);
  123. }
  124. else if (strcmp(*argv,"-spkac") == 0)
  125. {
  126. if (--argc < 1) goto bad;
  127. spkac= *(++argv);
  128. }
  129. else if (strcmp(*argv,"-spksect") == 0)
  130. {
  131. if (--argc < 1) goto bad;
  132. spksect= *(++argv);
  133. }
  134. else if (strcmp(*argv,"-engine") == 0)
  135. {
  136. if (--argc < 1) goto bad;
  137. engine= *(++argv);
  138. }
  139. else if (strcmp(*argv,"-noout") == 0)
  140. noout=1;
  141. else if (strcmp(*argv,"-pubkey") == 0)
  142. pubkey=1;
  143. else if (strcmp(*argv,"-verify") == 0)
  144. verify=1;
  145. else badops = 1;
  146. argc--;
  147. argv++;
  148. }
  149. if (badops)
  150. {
  151. bad:
  152. BIO_printf(bio_err,"%s [options]\n",prog);
  153. BIO_printf(bio_err,"where options are\n");
  154. BIO_printf(bio_err," -in arg input file\n");
  155. BIO_printf(bio_err," -out arg output file\n");
  156. BIO_printf(bio_err," -key arg create SPKAC using private key\n");
  157. BIO_printf(bio_err," -passin arg input file pass phrase source\n");
  158. BIO_printf(bio_err," -challenge arg challenge string\n");
  159. BIO_printf(bio_err," -spkac arg alternative SPKAC name\n");
  160. BIO_printf(bio_err," -noout don't print SPKAC\n");
  161. BIO_printf(bio_err," -pubkey output public key\n");
  162. BIO_printf(bio_err," -verify verify SPKAC signature\n");
  163. BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n");
  164. goto end;
  165. }
  166. ERR_load_crypto_strings();
  167. if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
  168. BIO_printf(bio_err, "Error getting password\n");
  169. goto end;
  170. }
  171. if (engine != NULL)
  172. {
  173. if((e = ENGINE_by_id(engine)) == NULL)
  174. {
  175. BIO_printf(bio_err,"invalid engine \"%s\"\n",
  176. engine);
  177. goto end;
  178. }
  179. if(!ENGINE_set_default(e, ENGINE_METHOD_ALL))
  180. {
  181. BIO_printf(bio_err,"can't use that engine\n");
  182. goto end;
  183. }
  184. BIO_printf(bio_err,"engine \"%s\" set.\n", engine);
  185. /* Free our "structural" reference. */
  186. ENGINE_free(e);
  187. }
  188. if(keyfile) {
  189. if(strcmp(keyfile, "-")) key = BIO_new_file(keyfile, "r");
  190. else key = BIO_new_fp(stdin, BIO_NOCLOSE);
  191. if(!key) {
  192. BIO_printf(bio_err, "Error opening key file\n");
  193. ERR_print_errors(bio_err);
  194. goto end;
  195. }
  196. pkey = PEM_read_bio_PrivateKey(key, NULL, NULL, passin);
  197. if(!pkey) {
  198. BIO_printf(bio_err, "Error reading private key\n");
  199. ERR_print_errors(bio_err);
  200. goto end;
  201. }
  202. spki = NETSCAPE_SPKI_new();
  203. if(challenge) ASN1_STRING_set(spki->spkac->challenge,
  204. challenge, strlen(challenge));
  205. NETSCAPE_SPKI_set_pubkey(spki, pkey);
  206. NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
  207. spkstr = NETSCAPE_SPKI_b64_encode(spki);
  208. if (outfile) out = BIO_new_file(outfile, "w");
  209. else {
  210. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  211. #ifdef OPENSSL_SYS_VMS
  212. {
  213. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  214. out = BIO_push(tmpbio, out);
  215. }
  216. #endif
  217. }
  218. if(!out) {
  219. BIO_printf(bio_err, "Error opening output file\n");
  220. ERR_print_errors(bio_err);
  221. goto end;
  222. }
  223. BIO_printf(out, "SPKAC=%s\n", spkstr);
  224. OPENSSL_free(spkstr);
  225. ret = 0;
  226. goto end;
  227. }
  228. if (infile) in = BIO_new_file(infile, "r");
  229. else in = BIO_new_fp(stdin, BIO_NOCLOSE);
  230. if(!in) {
  231. BIO_printf(bio_err, "Error opening input file\n");
  232. ERR_print_errors(bio_err);
  233. goto end;
  234. }
  235. conf = CONF_load_bio(NULL, in, NULL);
  236. if(!conf) {
  237. BIO_printf(bio_err, "Error parsing config file\n");
  238. ERR_print_errors(bio_err);
  239. goto end;
  240. }
  241. spkstr = CONF_get_string(conf, spksect, spkac);
  242. if(!spkstr) {
  243. BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
  244. ERR_print_errors(bio_err);
  245. goto end;
  246. }
  247. spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
  248. if(!spki) {
  249. BIO_printf(bio_err, "Error loading SPKAC\n");
  250. ERR_print_errors(bio_err);
  251. goto end;
  252. }
  253. if (outfile) out = BIO_new_file(outfile, "w");
  254. else {
  255. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  256. #ifdef OPENSSL_SYS_VMS
  257. {
  258. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  259. out = BIO_push(tmpbio, out);
  260. }
  261. #endif
  262. }
  263. if(!out) {
  264. BIO_printf(bio_err, "Error opening output file\n");
  265. ERR_print_errors(bio_err);
  266. goto end;
  267. }
  268. if(!noout) NETSCAPE_SPKI_print(out, spki);
  269. pkey = NETSCAPE_SPKI_get_pubkey(spki);
  270. if(verify) {
  271. i = NETSCAPE_SPKI_verify(spki, pkey);
  272. if(i) BIO_printf(bio_err, "Signature OK\n");
  273. else {
  274. BIO_printf(bio_err, "Signature Failure\n");
  275. ERR_print_errors(bio_err);
  276. goto end;
  277. }
  278. }
  279. if(pubkey) PEM_write_bio_PUBKEY(out, pkey);
  280. ret = 0;
  281. end:
  282. CONF_free(conf);
  283. NETSCAPE_SPKI_free(spki);
  284. BIO_free(in);
  285. BIO_free_all(out);
  286. BIO_free(key);
  287. EVP_PKEY_free(pkey);
  288. if(passin) OPENSSL_free(passin);
  289. EXIT(ret);
  290. }