sess_id.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright 1995-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 "apps.h"
  13. #include "progs.h"
  14. #include <openssl/bio.h>
  15. #include <openssl/err.h>
  16. #include <openssl/x509.h>
  17. #include <openssl/pem.h>
  18. #include <openssl/ssl.h>
  19. typedef enum OPTION_choice {
  20. OPT_COMMON,
  21. OPT_INFORM,
  22. OPT_OUTFORM,
  23. OPT_IN,
  24. OPT_OUT,
  25. OPT_TEXT,
  26. OPT_CERT,
  27. OPT_NOOUT,
  28. OPT_CONTEXT
  29. } OPTION_CHOICE;
  30. const OPTIONS sess_id_options[] = {
  31. OPT_SECTION("General"),
  32. { "help", OPT_HELP, '-', "Display this summary" },
  33. { "context", OPT_CONTEXT, 's', "Set the session ID context" },
  34. OPT_SECTION("Input"),
  35. { "in", OPT_IN, 's', "Input file - default stdin" },
  36. { "inform", OPT_INFORM, 'F', "Input format - default PEM (DER or PEM)" },
  37. OPT_SECTION("Output"),
  38. { "out", OPT_OUT, '>', "Output file - default stdout" },
  39. { "outform", OPT_OUTFORM, 'f',
  40. "Output format - default PEM (PEM, DER or NSS)" },
  41. { "text", OPT_TEXT, '-', "Print ssl session id details" },
  42. { "cert", OPT_CERT, '-', "Output certificate " },
  43. { "noout", OPT_NOOUT, '-', "Don't output the encoded session info" },
  44. { NULL }
  45. };
  46. static SSL_SESSION *load_sess_id(char *file, int format);
  47. int sess_id_main(int argc, char **argv)
  48. {
  49. SSL_SESSION *x = NULL;
  50. X509 *peer = NULL;
  51. BIO *out = NULL;
  52. char *infile = NULL, *outfile = NULL, *context = NULL, *prog;
  53. int informat = FORMAT_PEM, outformat = FORMAT_PEM;
  54. int cert = 0, noout = 0, text = 0, ret = 1, i, num = 0;
  55. OPTION_CHOICE o;
  56. prog = opt_init(argc, argv, sess_id_options);
  57. while ((o = opt_next()) != OPT_EOF) {
  58. switch (o) {
  59. case OPT_EOF:
  60. case OPT_ERR:
  61. opthelp:
  62. BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
  63. goto end;
  64. case OPT_HELP:
  65. opt_help(sess_id_options);
  66. ret = 0;
  67. goto end;
  68. case OPT_INFORM:
  69. if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
  70. goto opthelp;
  71. break;
  72. case OPT_OUTFORM:
  73. if (!opt_format(opt_arg(), OPT_FMT_PEMDER | OPT_FMT_NSS,
  74. &outformat))
  75. goto opthelp;
  76. break;
  77. case OPT_IN:
  78. infile = opt_arg();
  79. break;
  80. case OPT_OUT:
  81. outfile = opt_arg();
  82. break;
  83. case OPT_TEXT:
  84. text = ++num;
  85. break;
  86. case OPT_CERT:
  87. cert = ++num;
  88. break;
  89. case OPT_NOOUT:
  90. noout = ++num;
  91. break;
  92. case OPT_CONTEXT:
  93. context = opt_arg();
  94. break;
  95. }
  96. }
  97. /* No extra arguments. */
  98. if (!opt_check_rest_arg(NULL))
  99. goto opthelp;
  100. x = load_sess_id(infile, informat);
  101. if (x == NULL) {
  102. goto end;
  103. }
  104. peer = SSL_SESSION_get0_peer(x);
  105. if (context != NULL) {
  106. size_t ctx_len = strlen(context);
  107. if (ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  108. BIO_printf(bio_err, "Context too long\n");
  109. goto end;
  110. }
  111. if (!SSL_SESSION_set1_id_context(x, (unsigned char *)context,
  112. ctx_len)) {
  113. BIO_printf(bio_err, "Error setting id context\n");
  114. goto end;
  115. }
  116. }
  117. if (!noout || text) {
  118. out = bio_open_default(outfile, 'w', outformat);
  119. if (out == NULL)
  120. goto end;
  121. }
  122. if (text) {
  123. SSL_SESSION_print(out, x);
  124. if (cert) {
  125. if (peer == NULL)
  126. BIO_puts(out, "No certificate present\n");
  127. else
  128. X509_print(out, peer);
  129. }
  130. }
  131. if (!noout && !cert) {
  132. if (outformat == FORMAT_ASN1) {
  133. i = i2d_SSL_SESSION_bio(out, x);
  134. } else if (outformat == FORMAT_PEM) {
  135. i = PEM_write_bio_SSL_SESSION(out, x);
  136. } else if (outformat == FORMAT_NSS) {
  137. i = SSL_SESSION_print_keylog(out, x);
  138. } else {
  139. BIO_printf(bio_err, "bad output format specified for outfile\n");
  140. goto end;
  141. }
  142. if (!i) {
  143. BIO_printf(bio_err, "unable to write SSL_SESSION\n");
  144. goto end;
  145. }
  146. } else if (!noout && (peer != NULL)) { /* just print the certificate */
  147. if (outformat == FORMAT_ASN1) {
  148. i = (int)i2d_X509_bio(out, peer);
  149. } else if (outformat == FORMAT_PEM) {
  150. i = PEM_write_bio_X509(out, peer);
  151. } else {
  152. BIO_printf(bio_err, "bad output format specified for outfile\n");
  153. goto end;
  154. }
  155. if (!i) {
  156. BIO_printf(bio_err, "unable to write X509\n");
  157. goto end;
  158. }
  159. }
  160. ret = 0;
  161. end:
  162. BIO_free_all(out);
  163. SSL_SESSION_free(x);
  164. return ret;
  165. }
  166. static SSL_SESSION *load_sess_id(char *infile, int format)
  167. {
  168. SSL_SESSION *x = NULL;
  169. BIO *in = NULL;
  170. in = bio_open_default(infile, 'r', format);
  171. if (in == NULL)
  172. goto end;
  173. if (format == FORMAT_ASN1)
  174. x = d2i_SSL_SESSION_bio(in, NULL);
  175. else
  176. x = PEM_read_bio_SSL_SESSION(in, NULL, NULL, NULL);
  177. if (x == NULL) {
  178. BIO_printf(bio_err, "unable to load SSL_SESSION\n");
  179. ERR_print_errors(bio_err);
  180. goto end;
  181. }
  182. end:
  183. BIO_free(in);
  184. return x;
  185. }