spkac.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. argc = opt_num_rest();
  121. if (argc != 0)
  122. goto opthelp;
  123. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  124. BIO_printf(bio_err, "Error getting password\n");
  125. goto end;
  126. }
  127. if (keyfile != NULL) {
  128. pkey = load_key(strcmp(keyfile, "-") ? keyfile : NULL,
  129. keyformat, 1, passin, e, "private key");
  130. if (pkey == NULL)
  131. goto end;
  132. spki = NETSCAPE_SPKI_new();
  133. if (spki == NULL)
  134. goto end;
  135. if (challenge != NULL)
  136. ASN1_STRING_set(spki->spkac->challenge,
  137. challenge, (int)strlen(challenge));
  138. if (!NETSCAPE_SPKI_set_pubkey(spki, pkey)) {
  139. BIO_printf(bio_err, "Error setting public key\n");
  140. goto end;
  141. }
  142. i = NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
  143. if (i <= 0) {
  144. BIO_printf(bio_err, "Error signing SPKAC\n");
  145. goto end;
  146. }
  147. spkstr = NETSCAPE_SPKI_b64_encode(spki);
  148. if (spkstr == NULL)
  149. goto end;
  150. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  151. if (out == NULL) {
  152. OPENSSL_free(spkstr);
  153. goto end;
  154. }
  155. BIO_printf(out, "SPKAC=%s\n", spkstr);
  156. OPENSSL_free(spkstr);
  157. ret = 0;
  158. goto end;
  159. }
  160. if ((conf = app_load_config(infile)) == NULL)
  161. goto end;
  162. spkstr = NCONF_get_string(conf, spksect, spkac);
  163. if (spkstr == NULL) {
  164. BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
  165. ERR_print_errors(bio_err);
  166. goto end;
  167. }
  168. spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
  169. if (spki == NULL) {
  170. BIO_printf(bio_err, "Error loading SPKAC\n");
  171. ERR_print_errors(bio_err);
  172. goto end;
  173. }
  174. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  175. if (out == NULL)
  176. goto end;
  177. if (!noout)
  178. NETSCAPE_SPKI_print(out, spki);
  179. pkey = NETSCAPE_SPKI_get_pubkey(spki);
  180. if (verify) {
  181. i = NETSCAPE_SPKI_verify(spki, pkey);
  182. if (i > 0) {
  183. BIO_printf(bio_err, "Signature OK\n");
  184. } else {
  185. BIO_printf(bio_err, "Signature Failure\n");
  186. ERR_print_errors(bio_err);
  187. goto end;
  188. }
  189. }
  190. if (pubkey)
  191. PEM_write_bio_PUBKEY(out, pkey);
  192. ret = 0;
  193. end:
  194. NCONF_free(conf);
  195. NETSCAPE_SPKI_free(spki);
  196. BIO_free_all(out);
  197. EVP_PKEY_free(pkey);
  198. release_engine(e);
  199. OPENSSL_free(passin);
  200. return ret;
  201. }