/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* * ssltap.c * * Version 1.0 : Frederick Roeber : 11 June 1997 * Version 2.0 : Steve Parkinson : 13 November 1997 * Version 3.0 : Nelson Bolyard : 22 July 1998 * Version 3.1 : Nelson Bolyard : 24 May 1999 * * changes in version 2.0: * Uses NSPR20 * Shows structure of SSL negotiation, if enabled. * * This "proxies" a socket connection (like a socks tunnel), but displays the * data is it flies by. * * In the code, the 'client' socket is the one on the client side of the * proxy, and the server socket is on the server side. *
*/
#include"nspr.h" #include"plstr.h" #include"secutil.h" #include <memory.h> /* for memcpy, etc. */ #include <string.h> #include <time.h>
constchar *progName; int hexparse = 0; int sslparse = 0; int sslhexparse = 0; int looparound = 0; int fancy = 0; int isV2Session = 0; int currentcipher = 0;
DataBufferList clientstream, serverstream;
constchar *
get_error_text(int error)
{ switch (error) { case PR_IO_TIMEOUT_ERROR: return"Timeout"; break; case PR_CONNECT_REFUSED_ERROR: return"Connection refused"; break; case PR_NETWORK_UNREACHABLE_ERROR: return"Network unreachable"; break; case PR_BAD_ADDRESS_ERROR: return"Bad address"; break; case PR_CONNECT_RESET_ERROR: return"Connection reset"; break; case PR_PIPE_ERROR: return"Pipe error"; break;
}
return"";
}
void
check_integrity(DataBufferList *dbl)
{
DataBuffer *db; int i;
db = dbl->first;
i = 0; while (db) {
i += db->length - db->offset;
db = db->next;
} if (i != dbl->size) {
myhalt(dbl->size, i);
}
}
/* Free's the DataBuffer at the head of the list and returns the pointer * to the new head of the list.
*/
DataBuffer *
free_head(DataBufferList *dbl)
{
DataBuffer *db = dbl->first;
PR_ASSERT(db->offset >= db->length); if (db->offset >= db->length) {
dbl->first = db->next; if (dbl->first == NULL) {
dbl->last = NULL;
}
PORT_Free(db->buffer);
PORT_Free(db);
db = dbl->first;
} return db;
}
void
read_stream_bytes(unsignedchar *d, DataBufferList *dbl, int length)
{ int copied = 0;
DataBuffer *db = dbl->first;
if (!db) {
PR_fprintf(PR_STDERR, "assert failed - dbl->first is null\n"); exit(8);
} while (length) { int toCopy; /* find the number of bytes to copy from the head buffer */ /* if there's too many in this buffer, then only copy 'length' */
toCopy = PR_MIN(db->length - db->offset, length);
/* * Note this must match (exactly) the enumeration ocspResponseStatus.
*/ staticchar *responseStatusNames[] = { "successful (Response has valid confirmations)", "malformedRequest (Illegal confirmation request)", "internalError (Internal error in issuer)", "tryLater (Try again later)", "unused ((4) is not used)", "sigRequired (Must sign the request)", "unauthorized (Request unauthorized)",
};
switch (status->certStatusType) { case ocspCertStatus_good:
fprintf(out_file, "Cert is good.\n"); break; case ocspCertStatus_revoked:
fprintf(out_file, "Cert has been revoked.\n");
print_revoked_info(out_file, status->certStatusInfo.revokedInfo,
level + 1); break; case ocspCertStatus_unknown:
fprintf(out_file, "Cert is unknown to responder.\n"); break; default:
fprintf(out_file, "Unrecognized status.\n"); break;
}
}
/* In the case of renegotiation, handshakes that occur in an already MAC'ed * channel, by the time of this call, the caller has already removed the MAC * from input recordLen. The only MAC'ed record that will get here with its * MAC intact (not removed) is the first Finished message on the connection.
*/ void
print_ssl3_handshake(unsignedchar *recordBuf, unsignedint recordLen,
SSLRecord *sr,
DataBufferList *s)
{ struct sslhandshake sslh; unsignedchar *hsdata; unsignedint offset = 0;
PR_fprintf(PR_STDOUT, " handshake {\n");
if (s->msgBufOffset && s->msgBuf) { /* append recordBuf to msgBuf, then use msgBuf */ if (s->msgBufOffset + recordLen > s->msgBufSize) { int newSize = s->msgBufOffset + recordLen; unsignedchar *newBuf = PORT_Realloc(s->msgBuf, newSize); if (!newBuf) {
PR_ASSERT(newBuf);
showErr("Realloc failed"); exit(10);
}
s->msgBuf = newBuf;
s->msgBufSize = newSize;
}
memcpy(s->msgBuf + s->msgBufOffset, recordBuf, recordLen);
s->msgBufOffset += recordLen;
recordLen = s->msgBufOffset;
recordBuf = s->msgBuf;
} while (offset + 4 <= recordLen) {
sslh.type = recordBuf[offset];
sslh.length = GET_24(recordBuf + offset + 1); if (offset + 4 + sslh.length > recordLen) break; /* finally have a complete message */ if (sslhexparse)
print_hex(4, recordBuf + offset);
hsdata = &recordBuf[offset + 4];
PR_fprintf(PR_STDOUT, " type = %d (", sslh.type); switch (sslh.type) { case 0:
PR_FPUTS("hello_request)\n"); break; case 1:
PR_FPUTS("client_hello)\n"); break; case 2:
PR_FPUTS("server_hello)\n"); break; case 4:
PR_FPUTS("new_session_ticket)\n"); break; case 11:
PR_FPUTS("certificate)\n"); break; case 12:
PR_FPUTS("server_key_exchange)\n"); break; case 13:
PR_FPUTS("certificate_request)\n"); break; case 14:
PR_FPUTS("server_hello_done)\n"); break; case 15:
PR_FPUTS("certificate_verify)\n"); break; case 16:
PR_FPUTS("client_key_exchange)\n"); break; case 20:
PR_FPUTS("finished)\n"); break; case 22:
PR_FPUTS("certificate_status)\n"); break; default:
PR_FPUTS("unknown)\n"); break;
}
/* pretty print extensions, if any */
pos =
print_hello_extension(hsdata, sslh.length, pos);
PR_fprintf(PR_STDOUT, " }\n");
} /* end of ssl version 3 */ break; default:
PR_fprintf(PR_STDOUT, " UNDEFINED VERSION %d.%d {...}\n",
sr->ver_maj, sr->ver_min); if (sslhexparse)
print_hex(sslh.length, hsdata); break;
} /* end of switch sr->ver_maj */ break;
case 2: /* server hello */
{ unsignedint sidlength, pos;
/* pretty print extensions, if any */
pos = print_hello_extension(hsdata, sslh.length, pos);
PR_fprintf(PR_STDOUT, " }\n");
} break;
case 4: /* new session ticket */
{
PRUint32 lifetimehint;
PRUint16 ticketlength; char lifetime[32];
lifetimehint = GET_32(hsdata); if (lifetimehint) {
PRExplodedTime et;
PRTime t =
lifetimehint;
t *=
PR_USEC_PER_SEC;
PR_ExplodeTime(t, PR_GMTParameters, &et); /* use HTTP Cookie header's date format */
PR_FormatTimeUSEnglish(lifetime, sizeof lifetime, "%a, %d-%b-%Y %H:%M:%S GMT", &et);
} else { /* 0 means the lifetime of the ticket is unspecified */
strcpy(lifetime, "unspecified");
}
ticketlength = GET_SHORT((hsdata +
4));
PR_fprintf(PR_STDOUT, " NewSessionTicket {\n");
PR_fprintf(PR_STDOUT, " ticket_lifetime_hint = %s\n",
lifetime);
PR_fprintf(PR_STDOUT, " ticket = {\n");
PR_fprintf(PR_STDOUT, " length = %d\n", ticketlength);
PR_fprintf(PR_STDOUT, " contents = {...}\n"); if (sslhexparse)
print_hex(ticketlength, &hsdata[4 + 2]);
PR_fprintf(PR_STDOUT, " }\n");
PR_fprintf(PR_STDOUT, " }\n");
} break;
case 11: /* certificate */
{
PRFileDesc *cfd; int pos; int certslength; int certlength; int certbytesread = 0; staticint certFileNumber; char certFileName[20];
if (!isNULLmac(currentcipher) &&
!s->hMACsize) { /* To calculate the size of MAC, we subtract the number of known * bytes of message from the number of remaining bytes in the * record. This assumes that this is the first record on the * connection to have a MAC, and that the sender has not put another * message after the finished message in the handshake record. * This is only correct for the first transition from unMACed to * MACed. If the connection switches from one cipher suite to * another one with a different MAC, this logic will not track that * change correctly.
*/
s->hMACsize =
recordLen - (sslh.length + 4);
sslh.length +=
s->hMACsize; /* skip over the MAC data */
} break;
void
print_ssl(DataBufferList *s, int length, unsignedchar *buffer)
{ /* -------------------------------------------------------- */ /* first, create a new buffer object for this piece of data. */
DataBuffer *db;
if (s->size == 0 && length > 0 && buffer[0] >= 32 && buffer[0] < 128) { /* Not an SSL record, treat entire buffer as plaintext */
PR_Write(PR_STDOUT, buffer, length); return;
}
if (s->first == NULL) {
PR_fprintf(PR_STDOUT, "ERROR: s->first is null\n"); exit(9);
}
/* in the case of an SSL 2 client-hello */ /* will have the high-bit set, whereas an SSL 3 client-hello will not */ /* SSL2 can also send records that begin with the high bit clear. * This code will incorrectly handle them. XXX
*/ if (isV2Session || s->first->buffer[s->first->offset] & 0x80) { /* it's an SSL 2 packet */ unsignedchar lenbuf[3];
/* first, we check if there's enough data for it to be an SSL2-type
* record. What a pain.*/ if (s->size < sizeof lenbuf) {
partial_packet(length, s->size, sizeof lenbuf); return;
}
/* read the first two bytes off the stream. */
read_stream_bytes(lenbuf, s, sizeof(lenbuf));
recordLen = ((unsignedint)(lenbuf[0] & 0x7f) << 8) + lenbuf[1] +
((lenbuf[0] & 0x80) ? 2 : 3);
PR_fprintf(PR_STDOUT, "recordLen = %u bytes\n", recordLen);
/* put 'em back on the head of the stream. */
db = PR_NEW(struct _DataBuffer);
/* if there wasn't enough, go back for more. */ if (s->size < recordLen) {
check_integrity(s);
partial_packet(length, s->size, recordLen); return;
}
partial_packet(length, s->size, recordLen);
/* read in the whole record. */
recordBuf = PORT_Alloc(recordLen);
read_stream_bytes(recordBuf, s, recordLen);
/* we have read the stream bytes. Look at the length of the ssl record. If we don't have enough data to satisfy this request, then put the bytes we just took back at the head
of the queue */
recordsize = GET_SHORT(sr.length);
if (recordsize > s->size) {
db = PR_NEW(struct _DataBuffer);
if (s->isEncrypted) {
PR_fprintf(PR_STDOUT, " < encrypted >\n");
} else { /* not encrypted */
switch (sr.type) { case 20: /* change_cipher_spec */ if (sslhexparse)
print_hex(recordLen - s->hMACsize, recordBuf); /* mark to say we can only dump hex form now on
* if it is not one on a null cipher */
s->isEncrypted =
isNULLcipher(currentcipher) ? 0 : 1; break;
switch (recordBuf[1]) { case 0:
PR_FPUTS("close_notify\n"); break; case 10:
PR_FPUTS("unexpected_message\n"); break; case 20:
PR_FPUTS("bad_record_mac\n"); break; case 21:
PR_FPUTS("decryption_failed\n"); break; case 22:
PR_FPUTS("record_overflow\n"); break; case 30:
PR_FPUTS("decompression_failure\n"); break; case 40:
PR_FPUTS("handshake_failure\n"); break; case 41:
PR_FPUTS("no_certificate\n"); break; case 42:
PR_FPUTS("bad_certificate\n"); break; case 43:
PR_FPUTS("unsupported_certificate\n"); break; case 44:
PR_FPUTS("certificate_revoked\n"); break; case 45:
PR_FPUTS("certificate_expired\n"); break; case 46:
PR_FPUTS("certificate_unknown\n"); break; case 47:
PR_FPUTS("illegal_parameter\n"); break; case 48:
PR_FPUTS("unknown_ca\n"); break; case 49:
PR_FPUTS("access_denied\n"); break; case 50:
PR_FPUTS("decode_error\n"); break; case 51:
PR_FPUTS("decrypt_error\n"); break; case 60:
PR_FPUTS("export_restriction\n"); break; case 70:
PR_FPUTS("protocol_version\n"); break; case 71:
PR_FPUTS("insufficient_security\n"); break; case 80:
PR_FPUTS("internal_error\n"); break; case 90:
PR_FPUTS("user_canceled\n"); break; case 100:
PR_FPUTS("no_renegotiation\n"); break; case 110:
PR_FPUTS("unsupported_extension\n"); break; case 111:
PR_FPUTS("certificate_unobtainable\n"); break; case 112:
PR_FPUTS("unrecognized_name\n"); break; case 113:
PR_FPUTS("bad_certificate_status_response\n"); break; case 114:
PR_FPUTS("bad_certificate_hash_value\n"); break;
/* if we've reached the end of the line - add the string */ if (i % 16 == 15)
PR_fprintf(PR_STDOUT, " | %s\n", string);
} /* we reached the end of the buffer,*/ /* do we have buffer left over? */
j = i % 16; if (j > 0) { for (k = 0; k < (16 -
j);
k++) { /* print additional space after every four bytes */ if ((k + j) % 4 == 0) {
PR_fprintf(PR_STDOUT, " ");
}
PR_fprintf(PR_STDOUT, " ");
}
PR_fprintf(PR_STDOUT, " | %s\n", string);
}
}
void
Usage(void)
{
PR_fprintf(PR_STDERR, "Usage: ssltap [-vhfsxl] [-p port] hostname:port\n");
PR_fprintf(PR_STDERR, " -v [prints version string]\n");
PR_fprintf(PR_STDERR, " -h [outputs hex instead of ASCII]\n");
PR_fprintf(PR_STDERR, " -f [turn on Fancy HTML coloring]\n");
PR_fprintf(PR_STDERR, " -s [turn on SSL decoding]\n");
PR_fprintf(PR_STDERR, " -x [turn on extra SSL hex dumps]\n");
PR_fprintf(PR_STDERR, " -p port [specify rendezvous port (default 1924)]\n");
PR_fprintf(PR_STDERR, " -l [loop - continue to wait for more connections]\n");
}
if (fancy) { if (!hexparse && !sslparse) {
PR_fprintf(PR_STDERR, "Note: use of -f without -s or -h not recommended, \n" "as the output looks a little strange. It may be useful, however\n");
}
}
if (!hostname)
Usage(), exit(2);
{ char *colon = (char *)strchr(hostname, ':'); if (!colon) {
PR_fprintf(PR_STDERR, "You must specify the host AND port you wish to connect to\n");
Usage(), exit(3);
}
port = atoi(&colon[1]);
*colon = '\0';
if (port == 0) {
PR_fprintf(PR_STDERR, "Port must be a nonzero number.\n"); exit(4);
}
}
/* find the 'server' IP address so we don't have to look it up later */
if (fancy) {
PR_fprintf(PR_STDOUT, "<HTML><HEAD><TITLE>SSLTAP output</TITLE></HEAD>\n");
PR_fprintf(PR_STDOUT, "<BODY><PRE>\n");
}
PR_fprintf(PR_STDERR, "Looking up \"%s\"...\n", hostname);
ai = PR_GetAddrInfoByName(hostname, PR_AF_UNSPEC, PR_AI_ADDRCONFIG); if (!ai) {
showErr("Host Name lookup failed\n"); exit(5);
}
iter = NULL;
iter = PR_EnumerateAddrInfo(iter, ai, port, &na_server); /* set up the port which the client will connect to */
r = PR_InitializeNetAddr(PR_IpAddrAny, rendport, &na_rend); if (r == PR_FAILURE) {
PR_fprintf(PR_STDERR, "PR_InitializeNetAddr(,%d,) failed with error %d\n", PR_GetError()); exit(0);
}
rv = NSS_NoDB_Init(""); if (rv != SECSuccess) {
PR_fprintf(PR_STDERR, "NSS_NoDB_Init() failed with error %d\n", PR_GetError()); exit(5);
}
if (amt == 0) {
PR_fprintf(PR_STDOUT, "Read EOF on Client socket. [%s]\n",
get_time_string());
pds[PD_C].in_flags &= ~PR_POLL_READ;
PR_Shutdown(s_server, PR_SHUTDOWN_SEND); continue;
}
PR_fprintf(PR_STDOUT, "--> [\n"); if (fancy)
PR_fprintf(PR_STDOUT, "<font color=blue>");
if (hexparse)
print_hex(amt, buffer); if (sslparse)
print_ssl(&clientstream, amt, buffer); if (!hexparse && !sslparse)
PR_Write(PR_STDOUT, buffer, amt); if (fancy)
PR_fprintf(PR_STDOUT, "</font>");
PR_fprintf(PR_STDOUT, "]\n");
wrote = PR_Write(s_server, buffer, amt); if (wrote != amt) { if (wrote < 0) {
showErr("Write to server socket failed.\n"); break;
} else {
PR_fprintf(PR_STDERR, "Short write to server socket!\n");
}
}
} /* end of read from client socket. */
/* read data, copy it to stdout, and write to other socket */ if ((pds[PD_S].in_flags & PR_POLL_READ) != 0 &&
(pds[PD_S].out_flags & PR_POLL_READ) != 0) {
amt = PR_Read(s_server, buffer, sizeof(buffer));
if (amt < 0) {
showErr("error on server-side socket.\n"); break;
}
if (amt == 0) {
PR_fprintf(PR_STDOUT, "Read EOF on Server socket. [%s]\n",
get_time_string());
pds[PD_S].in_flags &= ~PR_POLL_READ;
PR_Shutdown(s_client, PR_SHUTDOWN_SEND); continue;
}
PR_fprintf(PR_STDOUT, "<-- [\n"); if (fancy)
PR_fprintf(PR_STDOUT, "<font color=red>"); if (hexparse)
print_hex(amt, (unsignedchar *)buffer); if (sslparse)
print_ssl(&serverstream, amt, (unsignedchar *)buffer); if (!hexparse && !sslparse)
PR_Write(PR_STDOUT, buffer, amt); if (fancy)
PR_fprintf(PR_STDOUT, "</font>");
PR_fprintf(PR_STDOUT, "]\n");
wrote = PR_Write(s_client, buffer, amt); if (wrote != amt) { if (wrote < 0) {
showErr("Write to client socket failed.\n"); break;
} else {
PR_fprintf(PR_STDERR, "Short write to client socket!\n");
}
}
} /* end of read from server socket. */
/* Loop, handle next message. */
} /* handle messages during a connection loop */
PR_Close(s_client);
PR_Close(s_server);
flush_stream(&clientstream);
flush_stream(&serverstream); /* Connection is closed, so reset the current cipher */
currentcipher = 0;
c_count++;
PR_fprintf(PR_STDERR, "Connection %d Complete [%s]\n", c_count,
get_time_string());
} while (looparound); /* accept connection and process it. */
PR_Close(s_rend); if (NSS_Shutdown() != SECSuccess) { return 1;
} return 0;
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.38 Sekunden
(vorverarbeitet am 2026-04-27)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.