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-2017 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. char *engine = NULL;
  93. apps_startup();
  94. if (!bio_err)
  95. 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. if (strcmp(*argv, "-in") == 0) {
  103. if (--argc < 1)
  104. goto bad;
  105. infile = *(++argv);
  106. } else if (strcmp(*argv, "-out") == 0) {
  107. if (--argc < 1)
  108. goto bad;
  109. outfile = *(++argv);
  110. } else if (strcmp(*argv, "-passin") == 0) {
  111. if (--argc < 1)
  112. goto bad;
  113. passargin = *(++argv);
  114. } else if (strcmp(*argv, "-key") == 0) {
  115. if (--argc < 1)
  116. goto bad;
  117. keyfile = *(++argv);
  118. } else if (strcmp(*argv, "-challenge") == 0) {
  119. if (--argc < 1)
  120. goto bad;
  121. challenge = *(++argv);
  122. } else if (strcmp(*argv, "-spkac") == 0) {
  123. if (--argc < 1)
  124. goto bad;
  125. spkac = *(++argv);
  126. } else if (strcmp(*argv, "-spksect") == 0) {
  127. if (--argc < 1)
  128. goto bad;
  129. spksect = *(++argv);
  130. }
  131. #ifndef OPENSSL_NO_ENGINE
  132. else if (strcmp(*argv, "-engine") == 0) {
  133. if (--argc < 1)
  134. goto bad;
  135. engine = *(++argv);
  136. }
  137. #endif
  138. else if (strcmp(*argv, "-noout") == 0)
  139. noout = 1;
  140. else if (strcmp(*argv, "-pubkey") == 0)
  141. pubkey = 1;
  142. else if (strcmp(*argv, "-verify") == 0)
  143. verify = 1;
  144. else
  145. badops = 1;
  146. argc--;
  147. argv++;
  148. }
  149. if (badops) {
  150. bad:
  151. BIO_printf(bio_err, "%s [options]\n", prog);
  152. BIO_printf(bio_err, "where options are\n");
  153. BIO_printf(bio_err, " -in arg input file\n");
  154. BIO_printf(bio_err, " -out arg output file\n");
  155. BIO_printf(bio_err,
  156. " -key arg create SPKAC using private key\n");
  157. BIO_printf(bio_err,
  158. " -passin arg input file pass phrase source\n");
  159. BIO_printf(bio_err, " -challenge arg challenge string\n");
  160. BIO_printf(bio_err, " -spkac arg alternative SPKAC name\n");
  161. BIO_printf(bio_err, " -noout don't print SPKAC\n");
  162. BIO_printf(bio_err, " -pubkey output public key\n");
  163. BIO_printf(bio_err, " -verify verify SPKAC signature\n");
  164. #ifndef OPENSSL_NO_ENGINE
  165. BIO_printf(bio_err,
  166. " -engine e use engine e, possibly a hardware device.\n");
  167. #endif
  168. goto end;
  169. }
  170. ERR_load_crypto_strings();
  171. if (!app_passwd(bio_err, passargin, NULL, &passin, NULL)) {
  172. BIO_printf(bio_err, "Error getting password\n");
  173. goto end;
  174. }
  175. e = setup_engine(bio_err, engine, 0);
  176. if (keyfile != NULL) {
  177. pkey = load_key(bio_err,
  178. strcmp(keyfile, "-") ? keyfile : NULL,
  179. FORMAT_PEM, 1, passin, e, "private key");
  180. if (pkey == NULL)
  181. goto end;
  182. spki = NETSCAPE_SPKI_new();
  183. if (spki == NULL)
  184. goto end;
  185. if (challenge != NULL)
  186. ASN1_STRING_set(spki->spkac->challenge,
  187. challenge, (int)strlen(challenge));
  188. NETSCAPE_SPKI_set_pubkey(spki, pkey);
  189. NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
  190. spkstr = NETSCAPE_SPKI_b64_encode(spki);
  191. if (spkstr == NULL)
  192. goto end;
  193. if (outfile)
  194. out = BIO_new_file(outfile, "w");
  195. else {
  196. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  197. #ifdef OPENSSL_SYS_VMS
  198. {
  199. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  200. out = BIO_push(tmpbio, out);
  201. }
  202. #endif
  203. }
  204. if (!out) {
  205. BIO_printf(bio_err, "Error opening output file\n");
  206. ERR_print_errors(bio_err);
  207. goto end;
  208. }
  209. BIO_printf(out, "SPKAC=%s\n", spkstr);
  210. OPENSSL_free(spkstr);
  211. ret = 0;
  212. goto end;
  213. }
  214. if (infile)
  215. in = BIO_new_file(infile, "r");
  216. else
  217. in = BIO_new_fp(stdin, BIO_NOCLOSE);
  218. if (!in) {
  219. BIO_printf(bio_err, "Error opening input file\n");
  220. ERR_print_errors(bio_err);
  221. goto end;
  222. }
  223. conf = NCONF_new(NULL);
  224. i = NCONF_load_bio(conf, in, NULL);
  225. if (!i) {
  226. BIO_printf(bio_err, "Error parsing config file\n");
  227. ERR_print_errors(bio_err);
  228. goto end;
  229. }
  230. spkstr = NCONF_get_string(conf, spksect, spkac);
  231. if (!spkstr) {
  232. BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
  233. ERR_print_errors(bio_err);
  234. goto end;
  235. }
  236. spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
  237. if (spki == NULL) {
  238. BIO_printf(bio_err, "Error loading SPKAC\n");
  239. ERR_print_errors(bio_err);
  240. goto end;
  241. }
  242. if (outfile)
  243. out = BIO_new_file(outfile, "w");
  244. else {
  245. out = BIO_new_fp(stdout, BIO_NOCLOSE);
  246. #ifdef OPENSSL_SYS_VMS
  247. {
  248. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  249. out = BIO_push(tmpbio, out);
  250. }
  251. #endif
  252. }
  253. if (!out) {
  254. BIO_printf(bio_err, "Error opening output file\n");
  255. ERR_print_errors(bio_err);
  256. goto end;
  257. }
  258. if (!noout)
  259. NETSCAPE_SPKI_print(out, spki);
  260. pkey = NETSCAPE_SPKI_get_pubkey(spki);
  261. if (verify) {
  262. i = NETSCAPE_SPKI_verify(spki, pkey);
  263. if (i > 0) {
  264. BIO_printf(bio_err, "Signature OK\n");
  265. } else {
  266. BIO_printf(bio_err, "Signature Failure\n");
  267. ERR_print_errors(bio_err);
  268. goto end;
  269. }
  270. }
  271. if (pubkey)
  272. PEM_write_bio_PUBKEY(out, pkey);
  273. ret = 0;
  274. end:
  275. NCONF_free(conf);
  276. NETSCAPE_SPKI_free(spki);
  277. BIO_free(in);
  278. BIO_free_all(out);
  279. EVP_PKEY_free(pkey);
  280. release_engine(e);
  281. if (passin)
  282. OPENSSL_free(passin);
  283. apps_shutdown();
  284. OPENSSL_EXIT(ret);
  285. }