sess_id.c 5.3 KB

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