spkac.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include "apps.h"
  14. #include "progs.h"
  15. #include <openssl/bio.h>
  16. #include <openssl/conf.h>
  17. #include <openssl/err.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/x509.h>
  20. #include <openssl/pem.h>
  21. typedef enum OPTION_choice {
  22. OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
  23. OPT_NOOUT, OPT_PUBKEY, OPT_VERIFY, OPT_IN, OPT_OUT,
  24. OPT_ENGINE, OPT_KEY, OPT_CHALLENGE, OPT_PASSIN, OPT_SPKAC,
  25. OPT_SPKSECT, OPT_KEYFORM,
  26. OPT_PROV_ENUM
  27. } OPTION_CHOICE;
  28. const OPTIONS spkac_options[] = {
  29. OPT_SECTION("General"),
  30. {"help", OPT_HELP, '-', "Display this summary"},
  31. {"spksect", OPT_SPKSECT, 's',
  32. "Specify the name of an SPKAC-dedicated section of configuration"},
  33. #ifndef OPENSSL_NO_ENGINE
  34. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  35. #endif
  36. OPT_SECTION("Input"),
  37. {"in", OPT_IN, '<', "Input file"},
  38. {"key", OPT_KEY, '<', "Create SPKAC using private key"},
  39. {"keyform", OPT_KEYFORM, 'f', "Private key file format (ENGINE, other values ignored)"},
  40. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  41. {"challenge", OPT_CHALLENGE, 's', "Challenge string"},
  42. {"spkac", OPT_SPKAC, 's', "Alternative SPKAC name"},
  43. OPT_SECTION("Output"),
  44. {"out", OPT_OUT, '>', "Output file"},
  45. {"noout", OPT_NOOUT, '-', "Don't print SPKAC"},
  46. {"pubkey", OPT_PUBKEY, '-', "Output public key"},
  47. {"verify", OPT_VERIFY, '-', "Verify SPKAC signature"},
  48. OPT_PROV_OPTIONS,
  49. {NULL}
  50. };
  51. int spkac_main(int argc, char **argv)
  52. {
  53. BIO *out = NULL;
  54. CONF *conf = NULL;
  55. ENGINE *e = NULL;
  56. EVP_PKEY *pkey = NULL;
  57. NETSCAPE_SPKI *spki = NULL;
  58. char *challenge = NULL, *keyfile = NULL;
  59. char *infile = NULL, *outfile = NULL, *passinarg = NULL, *passin = NULL;
  60. char *spkstr = NULL, *prog;
  61. const char *spkac = "SPKAC", *spksect = "default";
  62. int i, ret = 1, verify = 0, noout = 0, pubkey = 0;
  63. int keyformat = FORMAT_PEM;
  64. OPTION_CHOICE o;
  65. prog = opt_init(argc, argv, spkac_options);
  66. while ((o = opt_next()) != OPT_EOF) {
  67. switch (o) {
  68. case OPT_EOF:
  69. case OPT_ERR:
  70. opthelp:
  71. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  72. goto end;
  73. case OPT_HELP:
  74. opt_help(spkac_options);
  75. ret = 0;
  76. goto end;
  77. case OPT_IN:
  78. infile = opt_arg();
  79. break;
  80. case OPT_OUT:
  81. outfile = opt_arg();
  82. break;
  83. case OPT_NOOUT:
  84. noout = 1;
  85. break;
  86. case OPT_PUBKEY:
  87. pubkey = 1;
  88. break;
  89. case OPT_VERIFY:
  90. verify = 1;
  91. break;
  92. case OPT_PASSIN:
  93. passinarg = opt_arg();
  94. break;
  95. case OPT_KEY:
  96. keyfile = opt_arg();
  97. break;
  98. case OPT_KEYFORM:
  99. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
  100. goto opthelp;
  101. break;
  102. case OPT_CHALLENGE:
  103. challenge = opt_arg();
  104. break;
  105. case OPT_SPKAC:
  106. spkac = opt_arg();
  107. break;
  108. case OPT_SPKSECT:
  109. spksect = opt_arg();
  110. break;
  111. case OPT_ENGINE:
  112. e = setup_engine(opt_arg(), 0);
  113. break;
  114. case OPT_PROV_CASES:
  115. if (!opt_provider(o))
  116. goto end;
  117. break;
  118. }
  119. }
  120. /* No extra arguments. */
  121. argc = opt_num_rest();
  122. if (argc != 0)
  123. goto opthelp;
  124. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  125. BIO_printf(bio_err, "Error getting password\n");
  126. goto end;
  127. }
  128. if (keyfile != NULL) {
  129. pkey = load_key(strcmp(keyfile, "-") ? keyfile : NULL,
  130. keyformat, 1, passin, e, "private key");
  131. if (pkey == NULL)
  132. goto end;
  133. spki = NETSCAPE_SPKI_new();
  134. if (spki == NULL)
  135. goto end;
  136. if (challenge != NULL)
  137. ASN1_STRING_set(spki->spkac->challenge,
  138. challenge, (int)strlen(challenge));
  139. if (!NETSCAPE_SPKI_set_pubkey(spki, pkey)) {
  140. BIO_printf(bio_err, "Error setting public key\n");
  141. goto end;
  142. }
  143. i = NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
  144. if (i <= 0) {
  145. BIO_printf(bio_err, "Error signing SPKAC\n");
  146. goto end;
  147. }
  148. spkstr = NETSCAPE_SPKI_b64_encode(spki);
  149. if (spkstr == NULL)
  150. goto end;
  151. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  152. if (out == NULL) {
  153. OPENSSL_free(spkstr);
  154. goto end;
  155. }
  156. BIO_printf(out, "SPKAC=%s\n", spkstr);
  157. OPENSSL_free(spkstr);
  158. ret = 0;
  159. goto end;
  160. }
  161. if ((conf = app_load_config(infile)) == NULL)
  162. goto end;
  163. spkstr = NCONF_get_string(conf, spksect, spkac);
  164. if (spkstr == NULL) {
  165. BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
  166. ERR_print_errors(bio_err);
  167. goto end;
  168. }
  169. spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
  170. if (spki == NULL) {
  171. BIO_printf(bio_err, "Error loading SPKAC\n");
  172. ERR_print_errors(bio_err);
  173. goto end;
  174. }
  175. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  176. if (out == NULL)
  177. goto end;
  178. if (!noout)
  179. NETSCAPE_SPKI_print(out, spki);
  180. pkey = NETSCAPE_SPKI_get_pubkey(spki);
  181. if (verify) {
  182. i = NETSCAPE_SPKI_verify(spki, pkey);
  183. if (i > 0) {
  184. BIO_printf(bio_err, "Signature OK\n");
  185. } else {
  186. BIO_printf(bio_err, "Signature Failure\n");
  187. ERR_print_errors(bio_err);
  188. goto end;
  189. }
  190. }
  191. if (pubkey)
  192. PEM_write_bio_PUBKEY(out, pkey);
  193. ret = 0;
  194. end:
  195. NCONF_free(conf);
  196. NETSCAPE_SPKI_free(spki);
  197. BIO_free_all(out);
  198. EVP_PKEY_free(pkey);
  199. release_engine(e);
  200. OPENSSL_free(passin);
  201. return ret;
  202. }