2 * Functions to trace SSL protocol behavior in DEBUG builds.
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/. */
14 #if defined(DEBUG) || defined(TRACE)
15 static const char *hex
= "0123456789abcdef";
17 static const char printable
[257] = {
18 "................" /* 0x */
19 "................" /* 1x */
20 " !\"#$%&'()*+,-./" /* 2x */
21 "0123456789:;<=>?" /* 3x */
22 "@ABCDEFGHIJKLMNO" /* 4x */
23 "PQRSTUVWXYZ[\\]^_" /* 5x */
24 "`abcdefghijklmno" /* 6x */
25 "pqrstuvwxyz{|}~." /* 7x */
26 "................" /* 8x */
27 "................" /* 9x */
28 "................" /* ax */
29 "................" /* bx */
30 "................" /* cx */
31 "................" /* dx */
32 "................" /* ex */
33 "................" /* fx */
36 void ssl_PrintBuf(sslSocket
*ss
, const char *msg
, const void *vp
, int len
)
38 const unsigned char *cp
= (const unsigned char *)vp
;
44 SSL_TRACE(("%d: SSL[%d]: %s [Len: %d]", SSL_GETPID(), ss
->fd
,
47 SSL_TRACE(("%d: SSL: %s [Len: %d]", SSL_GETPID(), msg
, len
));
49 memset(buf
, ' ', sizeof buf
);
53 unsigned char ch
= *cp
++;
54 *bp
++ = hex
[(ch
>> 4) & 0xf];
55 *bp
++ = hex
[ch
& 0xf];
57 *ap
++ = printable
[ch
];
60 SSL_TRACE((" %s", buf
));
61 memset(buf
, ' ', sizeof buf
);
68 SSL_TRACE((" %s", buf
));
72 #define LEN(cp) (((cp)[0] << 8) | ((cp)[1]))
74 static void PrintType(sslSocket
*ss
, char *msg
)
77 SSL_TRACE(("%d: SSL[%d]: dump-msg: %s", SSL_GETPID(), ss
->fd
,
80 SSL_TRACE(("%d: SSL: dump-msg: %s", SSL_GETPID(), msg
));
84 static void PrintInt(sslSocket
*ss
, char *msg
, unsigned v
)
87 SSL_TRACE(("%d: SSL[%d]: %s=%u", SSL_GETPID(), ss
->fd
,
90 SSL_TRACE(("%d: SSL: %s=%u", SSL_GETPID(), msg
, v
));
94 /* PrintBuf is just like ssl_PrintBuf above, except that:
95 * a) It prefixes each line of the buffer with "XX: SSL[xxx] "
96 * b) It dumps only hex, not ASCII.
98 static void PrintBuf(sslSocket
*ss
, char *msg
, unsigned char *cp
, int len
)
104 SSL_TRACE(("%d: SSL[%d]: %s [Len: %d]",
105 SSL_GETPID(), ss
->fd
, msg
, len
));
107 SSL_TRACE(("%d: SSL: %s [Len: %d]",
108 SSL_GETPID(), msg
, len
));
112 unsigned char ch
= *cp
++;
113 *bp
++ = hex
[(ch
>> 4) & 0xf];
114 *bp
++ = hex
[ch
& 0xf];
116 if (bp
+ 4 > buf
+ 50) {
119 SSL_TRACE(("%d: SSL[%d]: %s",
120 SSL_GETPID(), ss
->fd
, buf
));
122 SSL_TRACE(("%d: SSL: %s", SSL_GETPID(), buf
));
130 SSL_TRACE(("%d: SSL[%d]: %s",
131 SSL_GETPID(), ss
->fd
, buf
));
133 SSL_TRACE(("%d: SSL: %s", SSL_GETPID(), buf
));
138 void ssl_DumpMsg(sslSocket
*ss
, unsigned char *bp
, unsigned len
)
142 PrintType(ss
, "Error");
143 PrintInt(ss
, "error", LEN(bp
+1));
146 case SSL_MT_CLIENT_HELLO
:
148 unsigned lcs
= LEN(bp
+3);
149 unsigned ls
= LEN(bp
+5);
150 unsigned lc
= LEN(bp
+7);
152 PrintType(ss
, "Client-Hello");
154 PrintInt(ss
, "version (Major)", bp
[1]);
155 PrintInt(ss
, "version (minor)", bp
[2]);
157 PrintBuf(ss
, "cipher-specs", bp
+9, lcs
);
158 PrintBuf(ss
, "session-id", bp
+9+lcs
, ls
);
159 PrintBuf(ss
, "challenge", bp
+9+lcs
+ls
, lc
);
162 case SSL_MT_CLIENT_MASTER_KEY
:
164 unsigned lck
= LEN(bp
+4);
165 unsigned lek
= LEN(bp
+6);
166 unsigned lka
= LEN(bp
+8);
168 PrintType(ss
, "Client-Master-Key");
170 PrintInt(ss
, "cipher-choice", bp
[1]);
171 PrintInt(ss
, "key-length", LEN(bp
+2));
173 PrintBuf(ss
, "clear-key", bp
+10, lck
);
174 PrintBuf(ss
, "encrypted-key", bp
+10+lck
, lek
);
175 PrintBuf(ss
, "key-arg", bp
+10+lck
+lek
, lka
);
178 case SSL_MT_CLIENT_FINISHED
:
179 PrintType(ss
, "Client-Finished");
180 PrintBuf(ss
, "connection-id", bp
+1, len
-1);
182 case SSL_MT_SERVER_HELLO
:
184 unsigned lc
= LEN(bp
+5);
185 unsigned lcs
= LEN(bp
+7);
186 unsigned lci
= LEN(bp
+9);
188 PrintType(ss
, "Server-Hello");
190 PrintInt(ss
, "session-id-hit", bp
[1]);
191 PrintInt(ss
, "certificate-type", bp
[2]);
192 PrintInt(ss
, "version (Major)", bp
[3]);
193 PrintInt(ss
, "version (minor)", bp
[3]);
194 PrintBuf(ss
, "certificate", bp
+11, lc
);
195 PrintBuf(ss
, "cipher-specs", bp
+11+lc
, lcs
);
196 PrintBuf(ss
, "connection-id", bp
+11+lc
+lcs
, lci
);
199 case SSL_MT_SERVER_VERIFY
:
200 PrintType(ss
, "Server-Verify");
201 PrintBuf(ss
, "challenge", bp
+1, len
-1);
203 case SSL_MT_SERVER_FINISHED
:
204 PrintType(ss
, "Server-Finished");
205 PrintBuf(ss
, "session-id", bp
+1, len
-1);
207 case SSL_MT_REQUEST_CERTIFICATE
:
208 PrintType(ss
, "Request-Certificate");
209 PrintInt(ss
, "authentication-type", bp
[1]);
210 PrintBuf(ss
, "certificate-challenge", bp
+2, len
-2);
212 case SSL_MT_CLIENT_CERTIFICATE
:
214 unsigned lc
= LEN(bp
+2);
215 unsigned lr
= LEN(bp
+4);
216 PrintType(ss
, "Client-Certificate");
217 PrintInt(ss
, "certificate-type", bp
[1]);
218 PrintBuf(ss
, "certificate", bp
+6, lc
);
219 PrintBuf(ss
, "response", bp
+6+lc
, lr
);
223 ssl_PrintBuf(ss
, "sending *unknown* message type", bp
, len
);
229 ssl_Trace(const char *format
, ... )
235 va_start(args
, format
);
236 PR_vsnprintf(buf
, sizeof(buf
), format
, args
);
239 fputs(buf
, ssl_trace_iob
);
240 fputs("\n", ssl_trace_iob
);