/src/nss/lib/ssl/ssltrace.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Functions to trace SSL protocol behavior in DEBUG builds. |
3 | | * |
4 | | * This Source Code Form is subject to the terms of the Mozilla Public |
5 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
6 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
7 | | #include <stdarg.h> |
8 | | #include "cert.h" |
9 | | #include "pk11func.h" |
10 | | #include "ssl.h" |
11 | | #include "sslimpl.h" |
12 | | #include "sslproto.h" |
13 | | #include "prprf.h" |
14 | | |
15 | | #if defined(DEBUG) || defined(TRACE) |
16 | | static const char *hex = "0123456789abcdef"; |
17 | | |
18 | | static const char printable[257] = { |
19 | | "................" /* 0x */ |
20 | | "................" /* 1x */ |
21 | | " !\"#$%&'()*+,-./" /* 2x */ |
22 | | "0123456789:;<=>?" /* 3x */ |
23 | | "@ABCDEFGHIJKLMNO" /* 4x */ |
24 | | "PQRSTUVWXYZ[\\]^_" /* 5x */ |
25 | | "`abcdefghijklmno" /* 6x */ |
26 | | "pqrstuvwxyz{|}~." /* 7x */ |
27 | | "................" /* 8x */ |
28 | | "................" /* 9x */ |
29 | | "................" /* ax */ |
30 | | "................" /* bx */ |
31 | | "................" /* cx */ |
32 | | "................" /* dx */ |
33 | | "................" /* ex */ |
34 | | "................" /* fx */ |
35 | | }; |
36 | | |
37 | | void |
38 | | ssl_PrintBuf(const sslSocket *ss, const char *msg, const void *vp, int len) |
39 | 0 | { |
40 | 0 | const unsigned char *cp = (const unsigned char *)vp; |
41 | 0 | char buf[80]; |
42 | 0 | char *bp; |
43 | 0 | char *ap; |
44 | |
|
45 | 0 | if (ss) { |
46 | 0 | SSL_TRACE(("%d: SSL[%d]: %s [Len: %d]", SSL_GETPID(), ss->fd, |
47 | 0 | msg, len)); |
48 | 0 | } else { |
49 | 0 | SSL_TRACE(("%d: SSL: %s [Len: %d]", SSL_GETPID(), msg, len)); |
50 | 0 | } |
51 | |
|
52 | 0 | if (!cp) { |
53 | 0 | SSL_TRACE((" <NULL>")); |
54 | 0 | return; |
55 | 0 | } |
56 | | |
57 | 0 | memset(buf, ' ', sizeof buf); |
58 | 0 | bp = buf; |
59 | 0 | ap = buf + 50; |
60 | 0 | while (--len >= 0) { |
61 | 0 | unsigned char ch = *cp++; |
62 | 0 | *bp++ = hex[(ch >> 4) & 0xf]; |
63 | 0 | *bp++ = hex[ch & 0xf]; |
64 | 0 | *bp++ = ' '; |
65 | 0 | *ap++ = printable[ch]; |
66 | 0 | if (ap - buf >= 66) { |
67 | 0 | *ap = 0; |
68 | 0 | SSL_TRACE((" %s", buf)); |
69 | 0 | memset(buf, ' ', sizeof buf); |
70 | 0 | bp = buf; |
71 | 0 | ap = buf + 50; |
72 | 0 | } |
73 | 0 | } |
74 | 0 | if (bp > buf) { |
75 | 0 | *ap = 0; |
76 | 0 | SSL_TRACE((" %s", buf)); |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | | void |
81 | | ssl_Trace(const char *format, ...) |
82 | 0 | { |
83 | 0 | char buf[2000]; |
84 | 0 | va_list args; |
85 | |
|
86 | 0 | if (ssl_trace_iob) { |
87 | 0 | va_start(args, format); |
88 | 0 | PR_vsnprintf(buf, sizeof(buf), format, args); |
89 | 0 | va_end(args); |
90 | |
|
91 | 0 | fputs(buf, ssl_trace_iob); |
92 | 0 | fputs("\n", ssl_trace_iob); |
93 | 0 | } |
94 | 0 | } |
95 | | |
96 | | void |
97 | | ssl_PrintKey(const sslSocket *ss, const char *msg, PK11SymKey *key) |
98 | 0 | { |
99 | 0 | SECStatus rv; |
100 | 0 | SECItem *rawkey; |
101 | |
|
102 | 0 | rv = PK11_ExtractKeyValue(key); |
103 | 0 | if (rv != SECSuccess) { |
104 | 0 | ssl_Trace("Could not extract key for %s", msg); |
105 | 0 | return; |
106 | 0 | } |
107 | 0 | rawkey = PK11_GetKeyData(key); |
108 | 0 | if (!rawkey) { |
109 | 0 | ssl_Trace("Could not extract key for %s", msg); |
110 | 0 | return; |
111 | 0 | } |
112 | 0 | ssl_PrintBuf(ss, msg, rawkey->data, rawkey->len); |
113 | 0 | } |
114 | | #endif |