2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
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
15 #include <openssl/bio.h>
16 #include <openssl/err.h>
17 #include <openssl/x509.h>
18 #include <openssl/pem.h>
19 #include <openssl/ssl.h>
21 typedef enum OPTION_choice {
22 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
23 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
24 OPT_TEXT, OPT_CERT, OPT_NOOUT, OPT_CONTEXT
27 const OPTIONS sess_id_options[] = {
28 OPT_SECTION("General"),
29 {"help", OPT_HELP, '-', "Display this summary"},
30 {"context", OPT_CONTEXT, 's', "Set the session ID context"},
33 {"in", OPT_IN, 's', "Input file - default stdin"},
34 {"inform", OPT_INFORM, 'F', "Input format - default PEM (DER or PEM)"},
36 OPT_SECTION("Output"),
37 {"out", OPT_OUT, '>', "Output file - default stdout"},
38 {"outform", OPT_OUTFORM, 'f',
39 "Output format - default PEM (PEM, DER or NSS)"},
40 {"text", OPT_TEXT, '-', "Print ssl session id details"},
41 {"cert", OPT_CERT, '-', "Output certificate "},
42 {"noout", OPT_NOOUT, '-', "Don't output the encoded session info"},
46 static SSL_SESSION *load_sess_id(char *file, int format);
48 int sess_id_main(int argc, char **argv)
50 SSL_SESSION *x = NULL;
53 char *infile = NULL, *outfile = NULL, *context = NULL, *prog;
54 int informat = FORMAT_PEM, outformat = FORMAT_PEM;
55 int cert = 0, noout = 0, text = 0, ret = 1, i, num = 0;
58 prog = opt_init(argc, argv, sess_id_options);
59 while ((o = opt_next()) != OPT_EOF) {
64 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
67 opt_help(sess_id_options);
71 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
75 if (!opt_format(opt_arg(), OPT_FMT_PEMDER | OPT_FMT_NSS,
99 argc = opt_num_rest();
103 x = load_sess_id(infile, informat);
107 peer = SSL_SESSION_get0_peer(x);
109 if (context != NULL) {
110 size_t ctx_len = strlen(context);
111 if (ctx_len > SSL_MAX_SID_CTX_LENGTH) {
112 BIO_printf(bio_err, "Context too long\n");
115 if (!SSL_SESSION_set1_id_context(x, (unsigned char *)context,
117 BIO_printf(bio_err, "Error setting id context\n");
122 if (!noout || text) {
123 out = bio_open_default(outfile, 'w', outformat);
129 SSL_SESSION_print(out, x);
133 BIO_puts(out, "No certificate present\n");
135 X509_print(out, peer);
139 if (!noout && !cert) {
140 if (outformat == FORMAT_ASN1) {
141 i = i2d_SSL_SESSION_bio(out, x);
142 } else if (outformat == FORMAT_PEM) {
143 i = PEM_write_bio_SSL_SESSION(out, x);
144 } else if (outformat == FORMAT_NSS) {
145 i = SSL_SESSION_print_keylog(out, x);
147 BIO_printf(bio_err, "bad output format specified for outfile\n");
151 BIO_printf(bio_err, "unable to write SSL_SESSION\n");
154 } else if (!noout && (peer != NULL)) { /* just print the certificate */
155 if (outformat == FORMAT_ASN1) {
156 i = (int)i2d_X509_bio(out, peer);
157 } else if (outformat == FORMAT_PEM) {
158 i = PEM_write_bio_X509(out, peer);
160 BIO_printf(bio_err, "bad output format specified for outfile\n");
164 BIO_printf(bio_err, "unable to write X509\n");
175 static SSL_SESSION *load_sess_id(char *infile, int format)
177 SSL_SESSION *x = NULL;
180 in = bio_open_default(infile, 'r', format);
183 if (format == FORMAT_ASN1)
184 x = d2i_SSL_SESSION_bio(in, NULL);
186 x = PEM_read_bio_SSL_SESSION(in, NULL, NULL, NULL);
188 BIO_printf(bio_err, "unable to load SSL_SESSION\n");
189 ERR_print_errors(bio_err);