spkac.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright 1999-2023 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. goto end;
  147. if (!NETSCAPE_SPKI_set_pubkey(spki, pkey)) {
  148. BIO_printf(bio_err, "Error setting public key\n");
  149. goto end;
  150. }
  151. i = NETSCAPE_SPKI_sign(spki, pkey, md);
  152. if (i <= 0) {
  153. BIO_printf(bio_err, "Error signing SPKAC\n");
  154. goto end;
  155. }
  156. spkstr = NETSCAPE_SPKI_b64_encode(spki);
  157. if (spkstr == NULL)
  158. goto end;
  159. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  160. if (out == NULL) {
  161. OPENSSL_free(spkstr);
  162. goto end;
  163. }
  164. BIO_printf(out, "SPKAC=%s\n", spkstr);
  165. OPENSSL_free(spkstr);
  166. ret = 0;
  167. goto end;
  168. }
  169. if ((conf = app_load_config(infile)) == NULL)
  170. goto end;
  171. spkstr = NCONF_get_string(conf, spksect, spkac);
  172. if (spkstr == NULL) {
  173. BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
  174. ERR_print_errors(bio_err);
  175. goto end;
  176. }
  177. spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
  178. if (spki == NULL) {
  179. BIO_printf(bio_err, "Error loading SPKAC\n");
  180. ERR_print_errors(bio_err);
  181. goto end;
  182. }
  183. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  184. if (out == NULL)
  185. goto end;
  186. if (!noout)
  187. NETSCAPE_SPKI_print(out, spki);
  188. pkey = NETSCAPE_SPKI_get_pubkey(spki);
  189. if (verify) {
  190. i = NETSCAPE_SPKI_verify(spki, pkey);
  191. if (i > 0) {
  192. BIO_printf(bio_err, "Signature OK\n");
  193. } else {
  194. BIO_printf(bio_err, "Signature Failure\n");
  195. ERR_print_errors(bio_err);
  196. goto end;
  197. }
  198. }
  199. if (pubkey)
  200. PEM_write_bio_PUBKEY(out, pkey);
  201. ret = 0;
  202. end:
  203. EVP_MD_free(md);
  204. NCONF_free(conf);
  205. NETSCAPE_SPKI_free(spki);
  206. BIO_free_all(out);
  207. EVP_PKEY_free(pkey);
  208. release_engine(e);
  209. OPENSSL_free(passin);
  210. return ret;
  211. }