Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Functions to trace SSL protocol behavior in DEBUG builds. |
michael@0 | 3 | * |
michael@0 | 4 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 5 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 6 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 7 | #include <stdarg.h> |
michael@0 | 8 | #include "cert.h" |
michael@0 | 9 | #include "ssl.h" |
michael@0 | 10 | #include "sslimpl.h" |
michael@0 | 11 | #include "sslproto.h" |
michael@0 | 12 | #include "prprf.h" |
michael@0 | 13 | |
michael@0 | 14 | #if defined(DEBUG) || defined(TRACE) |
michael@0 | 15 | static const char *hex = "0123456789abcdef"; |
michael@0 | 16 | |
michael@0 | 17 | static const char printable[257] = { |
michael@0 | 18 | "................" /* 0x */ |
michael@0 | 19 | "................" /* 1x */ |
michael@0 | 20 | " !\"#$%&'()*+,-./" /* 2x */ |
michael@0 | 21 | "0123456789:;<=>?" /* 3x */ |
michael@0 | 22 | "@ABCDEFGHIJKLMNO" /* 4x */ |
michael@0 | 23 | "PQRSTUVWXYZ[\\]^_" /* 5x */ |
michael@0 | 24 | "`abcdefghijklmno" /* 6x */ |
michael@0 | 25 | "pqrstuvwxyz{|}~." /* 7x */ |
michael@0 | 26 | "................" /* 8x */ |
michael@0 | 27 | "................" /* 9x */ |
michael@0 | 28 | "................" /* ax */ |
michael@0 | 29 | "................" /* bx */ |
michael@0 | 30 | "................" /* cx */ |
michael@0 | 31 | "................" /* dx */ |
michael@0 | 32 | "................" /* ex */ |
michael@0 | 33 | "................" /* fx */ |
michael@0 | 34 | }; |
michael@0 | 35 | |
michael@0 | 36 | void ssl_PrintBuf(sslSocket *ss, const char *msg, const void *vp, int len) |
michael@0 | 37 | { |
michael@0 | 38 | const unsigned char *cp = (const unsigned char *)vp; |
michael@0 | 39 | char buf[80]; |
michael@0 | 40 | char *bp; |
michael@0 | 41 | char *ap; |
michael@0 | 42 | |
michael@0 | 43 | if (ss) { |
michael@0 | 44 | SSL_TRACE(("%d: SSL[%d]: %s [Len: %d]", SSL_GETPID(), ss->fd, |
michael@0 | 45 | msg, len)); |
michael@0 | 46 | } else { |
michael@0 | 47 | SSL_TRACE(("%d: SSL: %s [Len: %d]", SSL_GETPID(), msg, len)); |
michael@0 | 48 | } |
michael@0 | 49 | memset(buf, ' ', sizeof buf); |
michael@0 | 50 | bp = buf; |
michael@0 | 51 | ap = buf + 50; |
michael@0 | 52 | while (--len >= 0) { |
michael@0 | 53 | unsigned char ch = *cp++; |
michael@0 | 54 | *bp++ = hex[(ch >> 4) & 0xf]; |
michael@0 | 55 | *bp++ = hex[ch & 0xf]; |
michael@0 | 56 | *bp++ = ' '; |
michael@0 | 57 | *ap++ = printable[ch]; |
michael@0 | 58 | if (ap - buf >= 66) { |
michael@0 | 59 | *ap = 0; |
michael@0 | 60 | SSL_TRACE((" %s", buf)); |
michael@0 | 61 | memset(buf, ' ', sizeof buf); |
michael@0 | 62 | bp = buf; |
michael@0 | 63 | ap = buf + 50; |
michael@0 | 64 | } |
michael@0 | 65 | } |
michael@0 | 66 | if (bp > buf) { |
michael@0 | 67 | *ap = 0; |
michael@0 | 68 | SSL_TRACE((" %s", buf)); |
michael@0 | 69 | } |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | #define LEN(cp) (((cp)[0] << 8) | ((cp)[1])) |
michael@0 | 73 | |
michael@0 | 74 | static void PrintType(sslSocket *ss, char *msg) |
michael@0 | 75 | { |
michael@0 | 76 | if (ss) { |
michael@0 | 77 | SSL_TRACE(("%d: SSL[%d]: dump-msg: %s", SSL_GETPID(), ss->fd, |
michael@0 | 78 | msg)); |
michael@0 | 79 | } else { |
michael@0 | 80 | SSL_TRACE(("%d: SSL: dump-msg: %s", SSL_GETPID(), msg)); |
michael@0 | 81 | } |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | static void PrintInt(sslSocket *ss, char *msg, unsigned v) |
michael@0 | 85 | { |
michael@0 | 86 | if (ss) { |
michael@0 | 87 | SSL_TRACE(("%d: SSL[%d]: %s=%u", SSL_GETPID(), ss->fd, |
michael@0 | 88 | msg, v)); |
michael@0 | 89 | } else { |
michael@0 | 90 | SSL_TRACE(("%d: SSL: %s=%u", SSL_GETPID(), msg, v)); |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | /* PrintBuf is just like ssl_PrintBuf above, except that: |
michael@0 | 95 | * a) It prefixes each line of the buffer with "XX: SSL[xxx] " |
michael@0 | 96 | * b) It dumps only hex, not ASCII. |
michael@0 | 97 | */ |
michael@0 | 98 | static void PrintBuf(sslSocket *ss, char *msg, unsigned char *cp, int len) |
michael@0 | 99 | { |
michael@0 | 100 | char buf[80]; |
michael@0 | 101 | char *bp; |
michael@0 | 102 | |
michael@0 | 103 | if (ss) { |
michael@0 | 104 | SSL_TRACE(("%d: SSL[%d]: %s [Len: %d]", |
michael@0 | 105 | SSL_GETPID(), ss->fd, msg, len)); |
michael@0 | 106 | } else { |
michael@0 | 107 | SSL_TRACE(("%d: SSL: %s [Len: %d]", |
michael@0 | 108 | SSL_GETPID(), msg, len)); |
michael@0 | 109 | } |
michael@0 | 110 | bp = buf; |
michael@0 | 111 | while (--len >= 0) { |
michael@0 | 112 | unsigned char ch = *cp++; |
michael@0 | 113 | *bp++ = hex[(ch >> 4) & 0xf]; |
michael@0 | 114 | *bp++ = hex[ch & 0xf]; |
michael@0 | 115 | *bp++ = ' '; |
michael@0 | 116 | if (bp + 4 > buf + 50) { |
michael@0 | 117 | *bp = 0; |
michael@0 | 118 | if (ss) { |
michael@0 | 119 | SSL_TRACE(("%d: SSL[%d]: %s", |
michael@0 | 120 | SSL_GETPID(), ss->fd, buf)); |
michael@0 | 121 | } else { |
michael@0 | 122 | SSL_TRACE(("%d: SSL: %s", SSL_GETPID(), buf)); |
michael@0 | 123 | } |
michael@0 | 124 | bp = buf; |
michael@0 | 125 | } |
michael@0 | 126 | } |
michael@0 | 127 | if (bp > buf) { |
michael@0 | 128 | *bp = 0; |
michael@0 | 129 | if (ss) { |
michael@0 | 130 | SSL_TRACE(("%d: SSL[%d]: %s", |
michael@0 | 131 | SSL_GETPID(), ss->fd, buf)); |
michael@0 | 132 | } else { |
michael@0 | 133 | SSL_TRACE(("%d: SSL: %s", SSL_GETPID(), buf)); |
michael@0 | 134 | } |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | void ssl_DumpMsg(sslSocket *ss, unsigned char *bp, unsigned len) |
michael@0 | 139 | { |
michael@0 | 140 | switch (bp[0]) { |
michael@0 | 141 | case SSL_MT_ERROR: |
michael@0 | 142 | PrintType(ss, "Error"); |
michael@0 | 143 | PrintInt(ss, "error", LEN(bp+1)); |
michael@0 | 144 | break; |
michael@0 | 145 | |
michael@0 | 146 | case SSL_MT_CLIENT_HELLO: |
michael@0 | 147 | { |
michael@0 | 148 | unsigned lcs = LEN(bp+3); |
michael@0 | 149 | unsigned ls = LEN(bp+5); |
michael@0 | 150 | unsigned lc = LEN(bp+7); |
michael@0 | 151 | |
michael@0 | 152 | PrintType(ss, "Client-Hello"); |
michael@0 | 153 | |
michael@0 | 154 | PrintInt(ss, "version (Major)", bp[1]); |
michael@0 | 155 | PrintInt(ss, "version (minor)", bp[2]); |
michael@0 | 156 | |
michael@0 | 157 | PrintBuf(ss, "cipher-specs", bp+9, lcs); |
michael@0 | 158 | PrintBuf(ss, "session-id", bp+9+lcs, ls); |
michael@0 | 159 | PrintBuf(ss, "challenge", bp+9+lcs+ls, lc); |
michael@0 | 160 | } |
michael@0 | 161 | break; |
michael@0 | 162 | case SSL_MT_CLIENT_MASTER_KEY: |
michael@0 | 163 | { |
michael@0 | 164 | unsigned lck = LEN(bp+4); |
michael@0 | 165 | unsigned lek = LEN(bp+6); |
michael@0 | 166 | unsigned lka = LEN(bp+8); |
michael@0 | 167 | |
michael@0 | 168 | PrintType(ss, "Client-Master-Key"); |
michael@0 | 169 | |
michael@0 | 170 | PrintInt(ss, "cipher-choice", bp[1]); |
michael@0 | 171 | PrintInt(ss, "key-length", LEN(bp+2)); |
michael@0 | 172 | |
michael@0 | 173 | PrintBuf(ss, "clear-key", bp+10, lck); |
michael@0 | 174 | PrintBuf(ss, "encrypted-key", bp+10+lck, lek); |
michael@0 | 175 | PrintBuf(ss, "key-arg", bp+10+lck+lek, lka); |
michael@0 | 176 | } |
michael@0 | 177 | break; |
michael@0 | 178 | case SSL_MT_CLIENT_FINISHED: |
michael@0 | 179 | PrintType(ss, "Client-Finished"); |
michael@0 | 180 | PrintBuf(ss, "connection-id", bp+1, len-1); |
michael@0 | 181 | break; |
michael@0 | 182 | case SSL_MT_SERVER_HELLO: |
michael@0 | 183 | { |
michael@0 | 184 | unsigned lc = LEN(bp+5); |
michael@0 | 185 | unsigned lcs = LEN(bp+7); |
michael@0 | 186 | unsigned lci = LEN(bp+9); |
michael@0 | 187 | |
michael@0 | 188 | PrintType(ss, "Server-Hello"); |
michael@0 | 189 | |
michael@0 | 190 | PrintInt(ss, "session-id-hit", bp[1]); |
michael@0 | 191 | PrintInt(ss, "certificate-type", bp[2]); |
michael@0 | 192 | PrintInt(ss, "version (Major)", bp[3]); |
michael@0 | 193 | PrintInt(ss, "version (minor)", bp[3]); |
michael@0 | 194 | PrintBuf(ss, "certificate", bp+11, lc); |
michael@0 | 195 | PrintBuf(ss, "cipher-specs", bp+11+lc, lcs); |
michael@0 | 196 | PrintBuf(ss, "connection-id", bp+11+lc+lcs, lci); |
michael@0 | 197 | } |
michael@0 | 198 | break; |
michael@0 | 199 | case SSL_MT_SERVER_VERIFY: |
michael@0 | 200 | PrintType(ss, "Server-Verify"); |
michael@0 | 201 | PrintBuf(ss, "challenge", bp+1, len-1); |
michael@0 | 202 | break; |
michael@0 | 203 | case SSL_MT_SERVER_FINISHED: |
michael@0 | 204 | PrintType(ss, "Server-Finished"); |
michael@0 | 205 | PrintBuf(ss, "session-id", bp+1, len-1); |
michael@0 | 206 | break; |
michael@0 | 207 | case SSL_MT_REQUEST_CERTIFICATE: |
michael@0 | 208 | PrintType(ss, "Request-Certificate"); |
michael@0 | 209 | PrintInt(ss, "authentication-type", bp[1]); |
michael@0 | 210 | PrintBuf(ss, "certificate-challenge", bp+2, len-2); |
michael@0 | 211 | break; |
michael@0 | 212 | case SSL_MT_CLIENT_CERTIFICATE: |
michael@0 | 213 | { |
michael@0 | 214 | unsigned lc = LEN(bp+2); |
michael@0 | 215 | unsigned lr = LEN(bp+4); |
michael@0 | 216 | PrintType(ss, "Client-Certificate"); |
michael@0 | 217 | PrintInt(ss, "certificate-type", bp[1]); |
michael@0 | 218 | PrintBuf(ss, "certificate", bp+6, lc); |
michael@0 | 219 | PrintBuf(ss, "response", bp+6+lc, lr); |
michael@0 | 220 | } |
michael@0 | 221 | break; |
michael@0 | 222 | default: |
michael@0 | 223 | ssl_PrintBuf(ss, "sending *unknown* message type", bp, len); |
michael@0 | 224 | return; |
michael@0 | 225 | } |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | void |
michael@0 | 229 | ssl_Trace(const char *format, ... ) |
michael@0 | 230 | { |
michael@0 | 231 | char buf[2000]; |
michael@0 | 232 | va_list args; |
michael@0 | 233 | |
michael@0 | 234 | if (ssl_trace_iob) { |
michael@0 | 235 | va_start(args, format); |
michael@0 | 236 | PR_vsnprintf(buf, sizeof(buf), format, args); |
michael@0 | 237 | va_end(args); |
michael@0 | 238 | |
michael@0 | 239 | fputs(buf, ssl_trace_iob); |
michael@0 | 240 | fputs("\n", ssl_trace_iob); |
michael@0 | 241 | } |
michael@0 | 242 | } |
michael@0 | 243 | #endif |