#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "apps.h"
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/ssl.h>
#include <openssl/x509.h>
static struct {
int cert;
char *context;
char *infile;
int informat;
int noout;
char *outfile;
int outformat;
int text;
} cfg;
static const struct option sess_id_options[] = {
{
.name = "cert",
.desc = "Output certificate if present in session",
.type = OPTION_FLAG,
.opt.flag = &cfg.cert,
},
{
.name = "context",
.argname = "id",
.desc = "Set the session ID context for output",
.type = OPTION_ARG,
.opt.arg = &cfg.context,
},
{
.name = "in",
.argname = "file",
.desc = "Input file (default stdin)",
.type = OPTION_ARG,
.opt.arg = &cfg.infile,
},
{
.name = "inform",
.argname = "format",
.desc = "Input format (DER or PEM (default))",
.type = OPTION_ARG_FORMAT,
.opt.value = &cfg.informat,
},
{
.name = "noout",
.desc = "Do not output the encoded session info",
.type = OPTION_FLAG,
.opt.flag = &cfg.noout,
},
{
.name = "out",
.argname = "file",
.desc = "Output file (default stdout)",
.type = OPTION_ARG,
.opt.arg = &cfg.outfile,
},
{
.name = "outform",
.argname = "format",
.desc = "Output format (DER or PEM (default))",
.type = OPTION_ARG_FORMAT,
.opt.value = &cfg.outformat,
},
{
.name = "text",
.desc = "Print various public or private key components in"
" plain text",
.type = OPTION_FLAG,
.opt.flag = &cfg.text,
},
{ NULL }
};
static void
sess_id_usage(void)
{
fprintf(stderr,
"usage: sess_id [-cert] [-context id] [-in file] [-inform fmt] "
"[-noout]\n"
" [-out file] [-outform fmt] [-text]\n\n");
options_usage(sess_id_options);
}
static SSL_SESSION *load_sess_id(char *file, int format);
int
sess_id_main(int argc, char **argv)
{
SSL_SESSION *x = NULL;
X509 *peer = NULL;
int ret = 1, i;
BIO *out = NULL;
if (pledge("stdio cpath wpath rpath", NULL) == -1) {
perror("pledge");
exit(1);
}
memset(&cfg, 0, sizeof(cfg));
cfg.informat = FORMAT_PEM;
cfg.outformat = FORMAT_PEM;
if (options_parse(argc, argv, sess_id_options, NULL, NULL) != 0) {
sess_id_usage();
return (1);
}
x = load_sess_id(cfg.infile, cfg.informat);
if (x == NULL) {
goto end;
}
peer = SSL_SESSION_get0_peer(x);
if (cfg.context) {
size_t ctx_len = strlen(cfg.context);
if (ctx_len > SSL_MAX_SID_CTX_LENGTH) {
BIO_printf(bio_err, "Context too long\n");
goto end;
}
SSL_SESSION_set1_id_context(x,
(unsigned char *)cfg.context, ctx_len);
}
if (!cfg.noout || cfg.text) {
out = BIO_new(BIO_s_file());
if (out == NULL) {
ERR_print_errors(bio_err);
goto end;
}
if (cfg.outfile == NULL) {
BIO_set_fp(out, stdout, BIO_NOCLOSE);
} else {
if (BIO_write_filename(out, cfg.outfile)
<= 0) {
perror(cfg.outfile);
goto end;
}
}
}
if (cfg.text) {
SSL_SESSION_print(out, x);
if (cfg.cert) {
if (peer == NULL)
BIO_puts(out, "No certificate present\n");
else
X509_print(out, peer);
}
}
if (!cfg.noout && !cfg.cert) {
if (cfg.outformat == FORMAT_ASN1)
i = i2d_SSL_SESSION_bio(out, x);
else if (cfg.outformat == FORMAT_PEM)
i = PEM_write_bio_SSL_SESSION(out, x);
else {
BIO_printf(bio_err,
"bad output format specified for outfile\n");
goto end;
}
if (!i) {
BIO_printf(bio_err, "unable to write SSL_SESSION\n");
goto end;
}
} else if (!cfg.noout && (peer != NULL)) {
if (cfg.outformat == FORMAT_ASN1)
i = (int) i2d_X509_bio(out, peer);
else if (cfg.outformat == FORMAT_PEM)
i = PEM_write_bio_X509(out, peer);
else {
BIO_printf(bio_err,
"bad output format specified for outfile\n");
goto end;
}
if (!i) {
BIO_printf(bio_err, "unable to write X509\n");
goto end;
}
}
ret = 0;
end:
BIO_free_all(out);
SSL_SESSION_free(x);
return (ret);
}
static SSL_SESSION *
load_sess_id(char *infile, int format)
{
SSL_SESSION *x = NULL;
BIO *in = NULL;
in = BIO_new(BIO_s_file());
if (in == NULL) {
ERR_print_errors(bio_err);
goto end;
}
if (infile == NULL)
BIO_set_fp(in, stdin, BIO_NOCLOSE);
else {
if (BIO_read_filename(in, infile) <= 0) {
perror(infile);
goto end;
}
}
if (format == FORMAT_ASN1)
x = d2i_SSL_SESSION_bio(in, NULL);
else if (format == FORMAT_PEM)
x = PEM_read_bio_SSL_SESSION(in, NULL, NULL, NULL);
else {
BIO_printf(bio_err,
"bad input format specified for input crl\n");
goto end;
}
if (x == NULL) {
BIO_printf(bio_err, "unable to load SSL_SESSION\n");
ERR_print_errors(bio_err);
goto end;
}
end:
BIO_free(in);
return (x);
}