2
0

spkac.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright 1999-2018 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. } OPTION_CHOICE;
  27. const OPTIONS spkac_options[] = {
  28. {"help", OPT_HELP, '-', "Display this summary"},
  29. {"in", OPT_IN, '<', "Input file"},
  30. {"out", OPT_OUT, '>', "Output file"},
  31. {"key", OPT_KEY, '<', "Create SPKAC using private key"},
  32. {"keyform", OPT_KEYFORM, 'f', "Private key file format - default PEM (PEM, DER, or ENGINE)"},
  33. {"passin", OPT_PASSIN, 's', "Input file pass phrase source"},
  34. {"challenge", OPT_CHALLENGE, 's', "Challenge string"},
  35. {"spkac", OPT_SPKAC, 's', "Alternative SPKAC name"},
  36. {"noout", OPT_NOOUT, '-', "Don't print SPKAC"},
  37. {"pubkey", OPT_PUBKEY, '-', "Output public key"},
  38. {"verify", OPT_VERIFY, '-', "Verify SPKAC signature"},
  39. {"spksect", OPT_SPKSECT, 's',
  40. "Specify the name of an SPKAC-dedicated section of configuration"},
  41. #ifndef OPENSSL_NO_ENGINE
  42. {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
  43. #endif
  44. {NULL}
  45. };
  46. int spkac_main(int argc, char **argv)
  47. {
  48. BIO *out = NULL;
  49. CONF *conf = NULL;
  50. ENGINE *e = NULL;
  51. EVP_PKEY *pkey = NULL;
  52. NETSCAPE_SPKI *spki = NULL;
  53. char *challenge = NULL, *keyfile = NULL;
  54. char *infile = NULL, *outfile = NULL, *passinarg = NULL, *passin = NULL;
  55. char *spkstr = NULL, *prog;
  56. const char *spkac = "SPKAC", *spksect = "default";
  57. int i, ret = 1, verify = 0, noout = 0, pubkey = 0;
  58. int keyformat = FORMAT_PEM;
  59. OPTION_CHOICE o;
  60. prog = opt_init(argc, argv, spkac_options);
  61. while ((o = opt_next()) != OPT_EOF) {
  62. switch (o) {
  63. case OPT_EOF:
  64. case OPT_ERR:
  65. opthelp:
  66. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  67. goto end;
  68. case OPT_HELP:
  69. opt_help(spkac_options);
  70. ret = 0;
  71. goto end;
  72. case OPT_IN:
  73. infile = opt_arg();
  74. break;
  75. case OPT_OUT:
  76. outfile = opt_arg();
  77. break;
  78. case OPT_NOOUT:
  79. noout = 1;
  80. break;
  81. case OPT_PUBKEY:
  82. pubkey = 1;
  83. break;
  84. case OPT_VERIFY:
  85. verify = 1;
  86. break;
  87. case OPT_PASSIN:
  88. passinarg = opt_arg();
  89. break;
  90. case OPT_KEY:
  91. keyfile = opt_arg();
  92. break;
  93. case OPT_KEYFORM:
  94. if (!opt_format(opt_arg(), OPT_FMT_ANY, &keyformat))
  95. goto opthelp;
  96. break;
  97. case OPT_CHALLENGE:
  98. challenge = opt_arg();
  99. break;
  100. case OPT_SPKAC:
  101. spkac = opt_arg();
  102. break;
  103. case OPT_SPKSECT:
  104. spksect = opt_arg();
  105. break;
  106. case OPT_ENGINE:
  107. e = setup_engine(opt_arg(), 0);
  108. break;
  109. }
  110. }
  111. argc = opt_num_rest();
  112. if (argc != 0)
  113. goto opthelp;
  114. if (!app_passwd(passinarg, NULL, &passin, NULL)) {
  115. BIO_printf(bio_err, "Error getting password\n");
  116. goto end;
  117. }
  118. if (keyfile != NULL) {
  119. pkey = load_key(strcmp(keyfile, "-") ? keyfile : NULL,
  120. keyformat, 1, passin, e, "private key");
  121. if (pkey == NULL)
  122. goto end;
  123. spki = NETSCAPE_SPKI_new();
  124. if (spki == NULL)
  125. goto end;
  126. if (challenge != NULL)
  127. ASN1_STRING_set(spki->spkac->challenge,
  128. challenge, (int)strlen(challenge));
  129. NETSCAPE_SPKI_set_pubkey(spki, pkey);
  130. NETSCAPE_SPKI_sign(spki, pkey, EVP_md5());
  131. spkstr = NETSCAPE_SPKI_b64_encode(spki);
  132. if (spkstr == NULL)
  133. goto end;
  134. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  135. if (out == NULL) {
  136. OPENSSL_free(spkstr);
  137. goto end;
  138. }
  139. BIO_printf(out, "SPKAC=%s\n", spkstr);
  140. OPENSSL_free(spkstr);
  141. ret = 0;
  142. goto end;
  143. }
  144. if ((conf = app_load_config(infile)) == NULL)
  145. goto end;
  146. spkstr = NCONF_get_string(conf, spksect, spkac);
  147. if (spkstr == NULL) {
  148. BIO_printf(bio_err, "Can't find SPKAC called \"%s\"\n", spkac);
  149. ERR_print_errors(bio_err);
  150. goto end;
  151. }
  152. spki = NETSCAPE_SPKI_b64_decode(spkstr, -1);
  153. if (spki == NULL) {
  154. BIO_printf(bio_err, "Error loading SPKAC\n");
  155. ERR_print_errors(bio_err);
  156. goto end;
  157. }
  158. out = bio_open_default(outfile, 'w', FORMAT_TEXT);
  159. if (out == NULL)
  160. goto end;
  161. if (!noout)
  162. NETSCAPE_SPKI_print(out, spki);
  163. pkey = NETSCAPE_SPKI_get_pubkey(spki);
  164. if (verify) {
  165. i = NETSCAPE_SPKI_verify(spki, pkey);
  166. if (i > 0) {
  167. BIO_printf(bio_err, "Signature OK\n");
  168. } else {
  169. BIO_printf(bio_err, "Signature Failure\n");
  170. ERR_print_errors(bio_err);
  171. goto end;
  172. }
  173. }
  174. if (pubkey)
  175. PEM_write_bio_PUBKEY(out, pkey);
  176. ret = 0;
  177. end:
  178. NCONF_free(conf);
  179. NETSCAPE_SPKI_free(spki);
  180. BIO_free_all(out);
  181. EVP_PKEY_free(pkey);
  182. release_engine(e);
  183. OPENSSL_free(passin);
  184. return ret;
  185. }