Home | History | Annotate | Line # | Download | only in ssl
      1   1.1.1.6  christos /*
      2  1.1.1.10  christos  * Copyright 1995-2022 The OpenSSL Project Authors. All Rights Reserved.
      3   1.1.1.8  christos  * Copyright 2005 Nokia. All rights reserved.
      4       1.1  christos  *
      5  1.1.1.11  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      6   1.1.1.6  christos  * this file except in compliance with the License.  You can obtain a copy
      7   1.1.1.6  christos  * in the file LICENSE in the source distribution or at
      8   1.1.1.6  christos  * https://www.openssl.org/source/license.html
      9       1.1  christos  */
     10   1.1.1.6  christos 
     11       1.1  christos #include <stdio.h>
     12       1.1  christos #include <openssl/buffer.h>
     13   1.1.1.9  christos #include "ssl_local.h"
     14       1.1  christos 
     15   1.1.1.6  christos #ifndef OPENSSL_NO_STDIO
     16       1.1  christos int SSL_SESSION_print_fp(FILE *fp, const SSL_SESSION *x)
     17   1.1.1.4       spz {
     18   1.1.1.4       spz     BIO *b;
     19   1.1.1.4       spz     int ret;
     20   1.1.1.4       spz 
     21   1.1.1.6  christos     if ((b = BIO_new(BIO_s_file())) == NULL) {
     22  1.1.1.11  christos         ERR_raise(ERR_LIB_SSL, ERR_R_BUF_LIB);
     23   1.1.1.8  christos         return 0;
     24   1.1.1.4       spz     }
     25   1.1.1.4       spz     BIO_set_fp(b, fp, BIO_NOCLOSE);
     26   1.1.1.4       spz     ret = SSL_SESSION_print(b, x);
     27   1.1.1.4       spz     BIO_free(b);
     28   1.1.1.8  christos     return ret;
     29   1.1.1.4       spz }
     30       1.1  christos #endif
     31       1.1  christos 
     32       1.1  christos int SSL_SESSION_print(BIO *bp, const SSL_SESSION *x)
     33   1.1.1.4       spz {
     34   1.1.1.8  christos     size_t i;
     35   1.1.1.4       spz     const char *s;
     36   1.1.1.8  christos     int istls13;
     37   1.1.1.4       spz 
     38   1.1.1.4       spz     if (x == NULL)
     39   1.1.1.4       spz         goto err;
     40   1.1.1.8  christos     istls13 = (x->ssl_version == TLS1_3_VERSION);
     41   1.1.1.4       spz     if (BIO_puts(bp, "SSL-Session:\n") <= 0)
     42   1.1.1.4       spz         goto err;
     43   1.1.1.6  christos     s = ssl_protocol_to_string(x->ssl_version);
     44   1.1.1.4       spz     if (BIO_printf(bp, "    Protocol  : %s\n", s) <= 0)
     45   1.1.1.4       spz         goto err;
     46   1.1.1.4       spz 
     47   1.1.1.4       spz     if (x->cipher == NULL) {
     48   1.1.1.4       spz         if (((x->cipher_id) & 0xff000000) == 0x02000000) {
     49   1.1.1.7  christos             if (BIO_printf(bp, "    Cipher    : %06lX\n",
     50   1.1.1.7  christos                            x->cipher_id & 0xffffff) <= 0)
     51   1.1.1.4       spz                 goto err;
     52   1.1.1.4       spz         } else {
     53   1.1.1.7  christos             if (BIO_printf(bp, "    Cipher    : %04lX\n",
     54   1.1.1.7  christos                            x->cipher_id & 0xffff) <= 0)
     55   1.1.1.4       spz                 goto err;
     56   1.1.1.4       spz         }
     57   1.1.1.4       spz     } else {
     58   1.1.1.7  christos         if (BIO_printf(bp, "    Cipher    : %s\n",
     59   1.1.1.7  christos                        ((x->cipher->name == NULL) ? "unknown"
     60   1.1.1.7  christos                                                   : x->cipher->name)) <= 0)
     61   1.1.1.4       spz             goto err;
     62   1.1.1.4       spz     }
     63   1.1.1.4       spz     if (BIO_puts(bp, "    Session-ID: ") <= 0)
     64   1.1.1.4       spz         goto err;
     65   1.1.1.4       spz     for (i = 0; i < x->session_id_length; i++) {
     66   1.1.1.4       spz         if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
     67   1.1.1.4       spz             goto err;
     68   1.1.1.4       spz     }
     69   1.1.1.4       spz     if (BIO_puts(bp, "\n    Session-ID-ctx: ") <= 0)
     70   1.1.1.4       spz         goto err;
     71   1.1.1.4       spz     for (i = 0; i < x->sid_ctx_length; i++) {
     72   1.1.1.4       spz         if (BIO_printf(bp, "%02X", x->sid_ctx[i]) <= 0)
     73   1.1.1.4       spz             goto err;
     74   1.1.1.4       spz     }
     75   1.1.1.8  christos     if (istls13) {
     76   1.1.1.8  christos         if (BIO_puts(bp, "\n    Resumption PSK: ") <= 0)
     77   1.1.1.8  christos             goto err;
     78   1.1.1.8  christos     } else if (BIO_puts(bp, "\n    Master-Key: ") <= 0)
     79   1.1.1.4       spz         goto err;
     80   1.1.1.8  christos     for (i = 0; i < x->master_key_length; i++) {
     81   1.1.1.4       spz         if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
     82   1.1.1.4       spz             goto err;
     83   1.1.1.4       spz     }
     84       1.1  christos #ifndef OPENSSL_NO_PSK
     85   1.1.1.4       spz     if (BIO_puts(bp, "\n    PSK identity: ") <= 0)
     86   1.1.1.4       spz         goto err;
     87   1.1.1.4       spz     if (BIO_printf(bp, "%s", x->psk_identity ? x->psk_identity : "None") <= 0)
     88   1.1.1.4       spz         goto err;
     89   1.1.1.4       spz     if (BIO_puts(bp, "\n    PSK identity hint: ") <= 0)
     90   1.1.1.4       spz         goto err;
     91   1.1.1.4       spz     if (BIO_printf
     92   1.1.1.4       spz         (bp, "%s", x->psk_identity_hint ? x->psk_identity_hint : "None") <= 0)
     93   1.1.1.4       spz         goto err;
     94       1.1  christos #endif
     95   1.1.1.3       spz #ifndef OPENSSL_NO_SRP
     96   1.1.1.4       spz     if (BIO_puts(bp, "\n    SRP username: ") <= 0)
     97   1.1.1.4       spz         goto err;
     98   1.1.1.4       spz     if (BIO_printf(bp, "%s", x->srp_username ? x->srp_username : "None") <= 0)
     99   1.1.1.4       spz         goto err;
    100   1.1.1.3       spz #endif
    101   1.1.1.8  christos     if (x->ext.tick_lifetime_hint) {
    102   1.1.1.4       spz         if (BIO_printf(bp,
    103   1.1.1.4       spz                        "\n    TLS session ticket lifetime hint: %ld (seconds)",
    104   1.1.1.8  christos                        x->ext.tick_lifetime_hint) <= 0)
    105   1.1.1.4       spz             goto err;
    106   1.1.1.4       spz     }
    107   1.1.1.8  christos     if (x->ext.tick) {
    108   1.1.1.4       spz         if (BIO_puts(bp, "\n    TLS session ticket:\n") <= 0)
    109   1.1.1.4       spz             goto err;
    110   1.1.1.6  christos         if (BIO_dump_indent
    111   1.1.1.8  christos             (bp, (const char *)x->ext.tick, (int)x->ext.ticklen, 4)
    112   1.1.1.4       spz             <= 0)
    113   1.1.1.4       spz             goto err;
    114   1.1.1.4       spz     }
    115       1.1  christos #ifndef OPENSSL_NO_COMP
    116   1.1.1.4       spz     if (x->compress_meth != 0) {
    117   1.1.1.4       spz         SSL_COMP *comp = NULL;
    118       1.1  christos 
    119  1.1.1.11  christos         if (!ssl_cipher_get_evp(NULL, x, NULL, NULL, NULL, NULL, &comp, 0))
    120   1.1.1.6  christos             goto err;
    121   1.1.1.4       spz         if (comp == NULL) {
    122   1.1.1.6  christos             if (BIO_printf(bp, "\n    Compression: %d", x->compress_meth) <= 0)
    123   1.1.1.4       spz                 goto err;
    124   1.1.1.4       spz         } else {
    125   1.1.1.6  christos             if (BIO_printf(bp, "\n    Compression: %d (%s)", comp->id,
    126   1.1.1.6  christos                            comp->name) <= 0)
    127   1.1.1.4       spz                 goto err;
    128   1.1.1.4       spz         }
    129   1.1.1.4       spz     }
    130   1.1.1.4       spz #endif
    131   1.1.1.4       spz     if (x->time != 0L) {
    132  1.1.1.10  christos         if (BIO_printf(bp, "\n    Start Time: %lld", (long long)x->time) <= 0)
    133   1.1.1.4       spz             goto err;
    134   1.1.1.4       spz     }
    135   1.1.1.4       spz     if (x->timeout != 0L) {
    136  1.1.1.10  christos         if (BIO_printf(bp, "\n    Timeout   : %lld (sec)", (long long)x->timeout) <= 0)
    137   1.1.1.4       spz             goto err;
    138   1.1.1.4       spz     }
    139   1.1.1.4       spz     if (BIO_puts(bp, "\n") <= 0)
    140   1.1.1.4       spz         goto err;
    141   1.1.1.4       spz 
    142   1.1.1.4       spz     if (BIO_puts(bp, "    Verify return code: ") <= 0)
    143   1.1.1.4       spz         goto err;
    144   1.1.1.4       spz     if (BIO_printf(bp, "%ld (%s)\n", x->verify_result,
    145   1.1.1.4       spz                    X509_verify_cert_error_string(x->verify_result)) <= 0)
    146   1.1.1.4       spz         goto err;
    147   1.1.1.4       spz 
    148   1.1.1.6  christos     if (BIO_printf(bp, "    Extended master secret: %s\n",
    149   1.1.1.6  christos                    x->flags & SSL_SESS_FLAG_EXTMS ? "yes" : "no") <= 0)
    150   1.1.1.6  christos         goto err;
    151   1.1.1.6  christos 
    152   1.1.1.8  christos     if (istls13) {
    153   1.1.1.8  christos         if (BIO_printf(bp, "    Max Early Data: %u\n",
    154   1.1.1.8  christos                        x->ext.max_early_data) <= 0)
    155   1.1.1.8  christos             goto err;
    156   1.1.1.8  christos     }
    157   1.1.1.8  christos 
    158   1.1.1.8  christos     return 1;
    159   1.1.1.6  christos  err:
    160   1.1.1.8  christos     return 0;
    161   1.1.1.6  christos }
    162   1.1.1.6  christos 
    163   1.1.1.6  christos /*
    164   1.1.1.6  christos  * print session id and master key in NSS keylog format (RSA
    165   1.1.1.6  christos  * Session-ID:<session id> Master-Key:<master key>)
    166   1.1.1.6  christos  */
    167   1.1.1.6  christos int SSL_SESSION_print_keylog(BIO *bp, const SSL_SESSION *x)
    168   1.1.1.6  christos {
    169   1.1.1.8  christos     size_t i;
    170   1.1.1.6  christos 
    171   1.1.1.6  christos     if (x == NULL)
    172   1.1.1.6  christos         goto err;
    173   1.1.1.6  christos     if (x->session_id_length == 0 || x->master_key_length == 0)
    174   1.1.1.6  christos         goto err;
    175   1.1.1.6  christos 
    176   1.1.1.6  christos     /*
    177   1.1.1.6  christos      * the RSA prefix is required by the format's definition although there's
    178   1.1.1.6  christos      * nothing RSA-specific in the output, therefore, we don't have to check if
    179   1.1.1.6  christos      * the cipher suite is based on RSA
    180   1.1.1.6  christos      */
    181   1.1.1.6  christos     if (BIO_puts(bp, "RSA ") <= 0)
    182   1.1.1.6  christos         goto err;
    183   1.1.1.6  christos 
    184   1.1.1.6  christos     if (BIO_puts(bp, "Session-ID:") <= 0)
    185   1.1.1.6  christos         goto err;
    186   1.1.1.6  christos     for (i = 0; i < x->session_id_length; i++) {
    187   1.1.1.6  christos         if (BIO_printf(bp, "%02X", x->session_id[i]) <= 0)
    188   1.1.1.6  christos             goto err;
    189   1.1.1.6  christos     }
    190   1.1.1.6  christos     if (BIO_puts(bp, " Master-Key:") <= 0)
    191   1.1.1.6  christos         goto err;
    192   1.1.1.8  christos     for (i = 0; i < x->master_key_length; i++) {
    193   1.1.1.6  christos         if (BIO_printf(bp, "%02X", x->master_key[i]) <= 0)
    194   1.1.1.6  christos             goto err;
    195   1.1.1.6  christos     }
    196   1.1.1.6  christos     if (BIO_puts(bp, "\n") <= 0)
    197   1.1.1.6  christos         goto err;
    198   1.1.1.6  christos 
    199   1.1.1.8  christos     return 1;
    200   1.1.1.4       spz  err:
    201   1.1.1.8  christos     return 0;
    202   1.1.1.4       spz }
    203