2
0

sess_id.c 5.5 KB

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