sess_id.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright 1995-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 "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_ERR = -1, OPT_EOF = 0, OPT_HELP,
  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. argc = opt_num_rest();
  93. if (argc != 0)
  94. goto opthelp;
  95. x = load_sess_id(infile, informat);
  96. if (x == NULL) {
  97. goto end;
  98. }
  99. peer = SSL_SESSION_get0_peer(x);
  100. if (context != NULL) {
  101. size_t ctx_len = strlen(context);
  102. if (ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  103. BIO_printf(bio_err, "Context too long\n");
  104. goto end;
  105. }
  106. if (!SSL_SESSION_set1_id_context(x, (unsigned char *)context,
  107. ctx_len)) {
  108. BIO_printf(bio_err, "Error setting id context\n");
  109. goto end;
  110. }
  111. }
  112. if (!noout || text) {
  113. out = bio_open_default(outfile, 'w', outformat);
  114. if (out == NULL)
  115. goto end;
  116. }
  117. if (text) {
  118. SSL_SESSION_print(out, x);
  119. if (cert) {
  120. if (peer == NULL)
  121. BIO_puts(out, "No certificate present\n");
  122. else
  123. X509_print(out, peer);
  124. }
  125. }
  126. if (!noout && !cert) {
  127. if (outformat == FORMAT_ASN1) {
  128. i = i2d_SSL_SESSION_bio(out, x);
  129. } else if (outformat == FORMAT_PEM) {
  130. i = PEM_write_bio_SSL_SESSION(out, x);
  131. } else if (outformat == FORMAT_NSS) {
  132. i = SSL_SESSION_print_keylog(out, x);
  133. } else {
  134. BIO_printf(bio_err, "bad output format specified for outfile\n");
  135. goto end;
  136. }
  137. if (!i) {
  138. BIO_printf(bio_err, "unable to write SSL_SESSION\n");
  139. goto end;
  140. }
  141. } else if (!noout && (peer != NULL)) { /* just print the certificate */
  142. if (outformat == FORMAT_ASN1) {
  143. i = (int)i2d_X509_bio(out, peer);
  144. } else if (outformat == FORMAT_PEM) {
  145. i = PEM_write_bio_X509(out, peer);
  146. } else {
  147. BIO_printf(bio_err, "bad output format specified for outfile\n");
  148. goto end;
  149. }
  150. if (!i) {
  151. BIO_printf(bio_err, "unable to write X509\n");
  152. goto end;
  153. }
  154. }
  155. ret = 0;
  156. end:
  157. BIO_free_all(out);
  158. SSL_SESSION_free(x);
  159. return ret;
  160. }
  161. static SSL_SESSION *load_sess_id(char *infile, int format)
  162. {
  163. SSL_SESSION *x = NULL;
  164. BIO *in = NULL;
  165. in = bio_open_default(infile, 'r', format);
  166. if (in == NULL)
  167. goto end;
  168. if (format == FORMAT_ASN1)
  169. x = d2i_SSL_SESSION_bio(in, NULL);
  170. else
  171. x = PEM_read_bio_SSL_SESSION(in, NULL, NULL, NULL);
  172. if (x == NULL) {
  173. BIO_printf(bio_err, "unable to load SSL_SESSION\n");
  174. ERR_print_errors(bio_err);
  175. goto end;
  176. }
  177. end:
  178. BIO_free(in);
  179. return x;
  180. }