2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
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
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>
20 typedef enum OPTION_choice {
21 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
22 OPT_INFORM, OPT_OUTFORM, OPT_IN, OPT_OUT,
23 OPT_TEXT, OPT_CERT, OPT_NOOUT, OPT_CONTEXT
26 const OPTIONS sess_id_options[] = {
27 {"help", OPT_HELP, '-', "Display this summary"},
28 {"inform", OPT_INFORM, 'F', "Input format - default PEM (DER or PEM)"},
29 {"outform", OPT_OUTFORM, 'f',
30 "Output format - default PEM (PEM, DER or NSS)"},
31 {"in", OPT_IN, 's', "Input file - default stdin"},
32 {"out", OPT_OUT, 's', "Output file - default stdout"},
33 {"text", OPT_TEXT, '-', "Print ssl session id details"},
34 {"cert", OPT_CERT, '-', "Output certificate "},
35 {"noout", OPT_NOOUT, '-', "Don't output the encoded session info"},
36 {"context", OPT_CONTEXT, 's', "Set the session ID context"},
40 static SSL_SESSION *load_sess_id(char *file, int format);
42 int sess_id_main(int argc, char **argv)
44 SSL_SESSION *x = NULL;
47 char *infile = NULL, *outfile = NULL, *context = NULL, *prog;
48 int informat = FORMAT_PEM, outformat = FORMAT_PEM;
49 int cert = 0, noout = 0, text = 0, ret = 1, i, num = 0;
52 prog = opt_init(argc, argv, sess_id_options);
53 while ((o = opt_next()) != OPT_EOF) {
58 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
61 opt_help(sess_id_options);
65 if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
69 if (!opt_format(opt_arg(), OPT_FMT_PEMDER | OPT_FMT_NSS,
93 argc = opt_num_rest();
97 x = load_sess_id(infile, informat);
101 peer = SSL_SESSION_get0_peer(x);
103 if (context != NULL) {
104 size_t ctx_len = strlen(context);
105 if (ctx_len > SSL_MAX_SID_CTX_LENGTH) {
106 BIO_printf(bio_err, "Context too long\n");
109 if (!SSL_SESSION_set1_id_context(x, (unsigned char *)context,
111 BIO_printf(bio_err, "Error setting id context\n");
116 if (!noout || text) {
117 out = bio_open_default(outfile, 'w', outformat);
123 SSL_SESSION_print(out, x);
127 BIO_puts(out, "No certificate present\n");
129 X509_print(out, peer);
133 if (!noout && !cert) {
134 if (outformat == FORMAT_ASN1) {
135 i = i2d_SSL_SESSION_bio(out, x);
136 } else if (outformat == FORMAT_PEM) {
137 i = PEM_write_bio_SSL_SESSION(out, x);
138 } else if (outformat == FORMAT_NSS) {
139 i = SSL_SESSION_print_keylog(out, x);
141 BIO_printf(bio_err, "bad output format specified for outfile\n");
145 BIO_printf(bio_err, "unable to write SSL_SESSION\n");
148 } else if (!noout && (peer != NULL)) { /* just print the certificate */
149 if (outformat == FORMAT_ASN1) {
150 i = (int)i2d_X509_bio(out, peer);
151 } else if (outformat == FORMAT_PEM) {
152 i = PEM_write_bio_X509(out, peer);
154 BIO_printf(bio_err, "bad output format specified for outfile\n");
158 BIO_printf(bio_err, "unable to write X509\n");
169 static SSL_SESSION *load_sess_id(char *infile, int format)
171 SSL_SESSION *x = NULL;
174 in = bio_open_default(infile, 'r', format);
177 if (format == FORMAT_ASN1)
178 x = d2i_SSL_SESSION_bio(in, NULL);
180 x = PEM_read_bio_SSL_SESSION(in, NULL, NULL, NULL);
182 BIO_printf(bio_err, "unable to load SSL_SESSION\n");
183 ERR_print_errors(bio_err);