2
0

spkac.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* apps/spkac.c */
  2. /* Written by Dr Stephen N Henson (steve@openssl.org) 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. #undef PROG
  72. #define PROG spkac_main
  73. /* -in arg - input file - default stdin
  74. * -out arg - output file - default stdout
  75. */
  76. int MAIN(int, char **);
  77. int MAIN(int argc, char **argv)
  78. {
  79. ENGINE *e = NULL;
  80. int i,badops=0, ret = 1;
  81. BIO *in = NULL,*out = NULL;
  82. int verify=0,noout=0,pubkey=0;
  83. char *infile = NULL,*outfile = NULL,*prog;
  84. char *passargin = NULL, *passin = NULL;
  85. const char *spkac = "SPKAC", *spksect = "default";
  86. char *spkstr = NULL;
  87. char *challenge = NULL, *keyfile = NULL;
  88. CONF *conf = NULL;
  89. NETSCAPE_SPKI *spki = NULL;
  90. EVP_PKEY *pkey = NULL;
  91. #ifndef OPENSSL_NO_ENGINE
  92. char *engine=NULL;
  93. #endif
  94. apps_startup();
  95. if (!bio_err) bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  96. if (!load_config(bio_err, NULL))
  97. goto end;
  98. prog=argv[0];
  99. argc--;
  100. argv++;
  101. while (argc >= 1)
  102. {
  103. if (strcmp(*argv,"-in") == 0)
  104. {
  105. if (--argc < 1) goto bad;
  106. infile= *(++argv);
  107. }
  108. else if (strcmp(*argv,"-out") == 0)
  109. {
  110. if (--argc < 1) goto bad;
  111. outfile= *(++argv);
  112. }
  113. else if (strcmp(*argv,"-passin") == 0)
  114. {
  115. if (--argc < 1) goto bad;
  116. passargin= *(++argv);
  117. }
  118. else if (strcmp(*argv,"-key") == 0)
  119. {
  120. if (--argc < 1) goto bad;
  121. keyfile= *(++argv);
  122. }
  123. else if (strcmp(*argv,"-challenge") == 0)
  124. {
  125. if (--argc < 1) goto bad;
  126. challenge= *(++argv);
  127. }
  128. else if (strcmp(*argv,"-spkac") == 0)
  129. {
  130. if (--argc < 1) goto bad;
  131. spkac= *(++argv);
  132. }
  133. else if (strcmp(*argv,"-spksect") == 0)
  134. {
  135. if (--argc < 1) goto bad;
  136. spksect= *(++argv);
  137. }
  138. #ifndef OPENSSL_NO_ENGINE
  139. else if (strcmp(*argv,"-engine") == 0)
  140. {
  141. if (--argc < 1) goto bad;
  142. engine= *(++argv);
  143. }
  144. #endif
  145. else if (strcmp(*argv,"-noout") == 0)
  146. noout=1;
  147. else if (strcmp(*argv,"-pubkey") == 0)
  148. pubkey=1;
  149. else if (strcmp(*argv,"-verify") == 0)
  150. verify=1;
  151. else badops = 1;
  152. argc--;
  153. argv++;
  154. }
  155. if (badops)
  156. {
  157. bad:
  158. BIO_printf(bio_err,"%s [options]\n",prog);
  159. BIO_printf(bio_err,"where options are\n");
  160. BIO_printf(bio_err," -in arg input file\n");
  161. BIO_printf(bio_err," -out arg output file\n");
  162. BIO_printf(bio_err," -key arg create SPKAC using private key\n");
  163. BIO_printf(bio_err," -passin arg input file pass phrase source\n");
  164. BIO_printf(bio_err," -challenge arg challenge string\n");
  165. BIO_printf(bio_err," -spkac arg alternative SPKAC name\n");
  166. BIO_printf(bio_err," -noout don't print SPKAC\n");
  167. BIO_printf(bio_err," -pubkey output public key\n");
  168. BIO_printf(bio_err," -verify verify SPKAC signature\n");
  169. #ifndef OPENSSL_NO_ENGINE
  170. BIO_printf(bio_err," -engine e use engine e, possibly a hardware device.\n");
  171. #endif
  172. goto end;
  173. }
  174. ERR_load_crypto_strings();
  175. if(!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
  176. BIO_printf(bio_err, "Error getting password\n");
  177. goto end;
  178. }
  179. #ifndef OPENSSL_NO_ENGINE
  180. e = setup_engine(bio_err, engine, 0);
  181. #endif
  182. if(keyfile) {
  183. pkey = load_key(bio_err,
  184. strcmp(keyfile, "-") ? keyfile : NULL,
  185. FORMAT_PEM, 1, passin, e, "private key");
  186. if(!pkey) {
  187. goto end;
  188. }
  189. spki = NETSCAPE_SPKI_new();
  190. if(challenge) ASN1_STRING_set(spki->spkac->challenge,
  191. challenge, (int)strlen(challenge));
  192. NETSCAPE_SPKI_set_pubkey(spki, pkey);
  193. NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
  194. spkstr = NETSCAPE_SPKI_b64_encode(spki);
  195. if (outfile) 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) in = BIO_new_file(infile, "r");
  216. else in = BIO_new_fp(stdin, BIO_NOCLOSE);
  217. if(!in) {
  218. BIO_printf(bio_err, "Error opening input file\n");
  219. ERR_print_errors(bio_err);
  220. goto end;
  221. }
  222. conf = NCONF_new(NULL);
  223. i = NCONF_load_bio(conf, in, NULL);
  224. if(!i) {
  225. BIO_printf(bio_err, "Error parsing config file\n");
  226. ERR_print_errors(bio_err);
  227. goto end;
  228. }
  229. spkstr = NCONF_get_string(conf, spksect, spkac);
  230. if(!spkstr) {
  231. BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
  232. ERR_print_errors(bio_err);
  233. goto end;
  234. }
  235. spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
  236. if(!spki) {
  237. BIO_printf(bio_err, "Error loading SPKAC\n");
  238. ERR_print_errors(bio_err);
  239. goto end;
  240. }
  241. if (outfile) out = BIO_new_file(outfile, "w");
  242. else {
  243. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  244. #ifdef OPENSSL_SYS_VMS
  245. {
  246. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  247. out = BIO_push(tmpbio, out);
  248. }
  249. #endif
  250. }
  251. if(!out) {
  252. BIO_printf(bio_err, "Error opening output file\n");
  253. ERR_print_errors(bio_err);
  254. goto end;
  255. }
  256. if(!noout) NETSCAPE_SPKI_print(out, spki);
  257. pkey = NETSCAPE_SPKI_get_pubkey(spki);
  258. if(verify) {
  259. i = NETSCAPE_SPKI_verify(spki, pkey);
  260. if (i > 0) BIO_printf(bio_err, "Signature OK\n");
  261. else {
  262. BIO_printf(bio_err, "Signature Failure\n");
  263. ERR_print_errors(bio_err);
  264. goto end;
  265. }
  266. }
  267. if(pubkey) PEM_write_bio_PUBKEY(out, pkey);
  268. ret = 0;
  269. end:
  270. NCONF_free(conf);
  271. NETSCAPE_SPKI_free(spki);
  272. BIO_free(in);
  273. BIO_free_all(out);
  274. EVP_PKEY_free(pkey);
  275. if(passin) OPENSSL_free(passin);
  276. apps_shutdown();
  277. OPENSSL_EXIT(ret);
  278. }