spkac.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright 1999-2021 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_COMMON,
  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, OPT_DIGEST,
  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. {"digest", OPT_DIGEST, 's', "Sign new SPKAC with the specified digest (default: MD5)" },
  45. {"out", OPT_OUT, '>', "Output file"},
  46. {"noout", OPT_NOOUT, '-', "Don't print SPKAC"},
  47. {"pubkey", OPT_PUBKEY, '-', "Output public key"},
  48. {"verify", OPT_VERIFY, '-', "Verify SPKAC signature"},
  49. OPT_PROV_OPTIONS,
  50. {NULL}
  51. };
  52. int spkac_main(int argc, char **argv)
  53. {
  54. BIO *out = NULL;
  55. CONF *conf = NULL;
  56. ENGINE *e = NULL;
  57. EVP_PKEY *pkey = NULL;
  58. NETSCAPE_SPKI *spki = NULL;
  59. char *challenge = NULL, *keyfile = NULL;
  60. char *infile = NULL, *outfile = NULL, *passinarg = NULL, *passin = NULL;
  61. char *spkstr = NULL, *prog;
  62. const char *spkac = "SPKAC", *spksect = "default";
  63. const char *digest = "MD5";
  64. EVP_MD *md = NULL;
  65. int i, ret = 1, verify = 0, noout = 0, pubkey = 0;
  66. int keyformat = FORMAT_UNDEF;
  67. OPTION_CHOICE o;
  68. prog = opt_init(argc, argv, spkac_options);
  69. while ((o = opt_next()) != OPT_EOF) {
  70. switch (o) {
  71. case OPT_EOF:
  72. case OPT_ERR:
  73. opthelp:
  74. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  75. goto end;
  76. case OPT_HELP:
  77. opt_help(spkac_options);
  78. ret = 0;
  79. goto end;
  80. case OPT_IN:
  81. infile = opt_arg();
  82. break;
  83. case OPT_OUT:
  84. outfile = opt_arg();
  85. break;
  86. case OPT_NOOUT:
  87. noout = 1;
  88. break;
  89. case OPT_PUBKEY:
  90. pubkey = 1;
  91. break;
  92. case OPT_VERIFY:
  93. verify = 1;
  94. break;
  95. case OPT_PASSIN:
  96. passinarg = opt_arg();
  97. break;
  98. case OPT_KEY:
  99. keyfile = opt_arg();
  100. break;
  101. case OPT_KEYFORM:
  102. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
  103. goto opthelp;
  104. break;
  105. case OPT_CHALLENGE:
  106. challenge = opt_arg();
  107. break;
  108. case OPT_SPKAC:
  109. spkac = opt_arg();
  110. break;
  111. case OPT_SPKSECT:
  112. spksect = opt_arg();
  113. break;
  114. case OPT_DIGEST:
  115. digest = opt_arg();
  116. break;
  117. case OPT_ENGINE:
  118. e = setup_engine(opt_arg(), 0);
  119. break;
  120. case OPT_PROV_CASES:
  121. if (!opt_provider(o))
  122. goto end;
  123. break;
  124. }
  125. }
  126. /* No extra arguments. */
  127. if (!opt_check_rest_arg(NULL))
  128. goto opthelp;
  129. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  130. BIO_printf(bio_err, "Error getting password\n");
  131. goto end;
  132. }
  133. if (keyfile != NULL) {
  134. if (!opt_md(digest, &md))
  135. goto end;
  136. pkey = load_key(strcmp(keyfile, "-") ? keyfile : NULL,
  137. keyformat, 1, passin, e, "private key");
  138. if (pkey == NULL)
  139. goto end;
  140. spki = NETSCAPE_SPKI_new();
  141. if (spki == NULL)
  142. goto end;
  143. if (challenge != NULL)
  144. ASN1_STRING_set(spki->spkac->challenge,
  145. challenge, (int)strlen(challenge));
  146. if (!NETSCAPE_SPKI_set_pubkey(spki, pkey)) {
  147. BIO_printf(bio_err, "Error setting public key\n");
  148. goto end;
  149. }
  150. i = NETSCAPE_SPKI_sign(spki, pkey, md);
  151. if (i <= 0) {
  152. BIO_printf(bio_err, "Error signing SPKAC\n");
  153. goto end;
  154. }
  155. spkstr = NETSCAPE_SPKI_b64_encode(spki);
  156. if (spkstr == NULL)
  157. goto end;
  158. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  159. if (out == NULL) {
  160. OPENSSL_free(spkstr);
  161. goto end;
  162. }
  163. BIO_printf(out, "SPKAC=%s\n", spkstr);
  164. OPENSSL_free(spkstr);
  165. ret = 0;
  166. goto end;
  167. }
  168. if ((conf = app_load_config(infile)) == NULL)
  169. goto end;
  170. spkstr = NCONF_get_string(conf, spksect, spkac);
  171. if (spkstr == NULL) {
  172. BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
  173. ERR_print_errors(bio_err);
  174. goto end;
  175. }
  176. spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
  177. if (spki == NULL) {
  178. BIO_printf(bio_err, "Error loading SPKAC\n");
  179. ERR_print_errors(bio_err);
  180. goto end;
  181. }
  182. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  183. if (out == NULL)
  184. goto end;
  185. if (!noout)
  186. NETSCAPE_SPKI_print(out, spki);
  187. pkey = NETSCAPE_SPKI_get_pubkey(spki);
  188. if (verify) {
  189. i = NETSCAPE_SPKI_verify(spki, pkey);
  190. if (i > 0) {
  191. BIO_printf(bio_err, "Signature OK\n");
  192. } else {
  193. BIO_printf(bio_err, "Signature Failure\n");
  194. ERR_print_errors(bio_err);
  195. goto end;
  196. }
  197. }
  198. if (pubkey)
  199. PEM_write_bio_PUBKEY(out, pkey);
  200. ret = 0;
  201. end:
  202. EVP_MD_free(md);
  203. NCONF_free(conf);
  204. NETSCAPE_SPKI_free(spki);
  205. BIO_free_all(out);
  206. EVP_PKEY_free(pkey);
  207. release_engine(e);
  208. OPENSSL_free(passin);
  209. return ret;
  210. }