sess_id.c 5.4 KB

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