ocsp.c

来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 2,187 行 · 第 1/5 页

C
2,187 行
字号
    if (sock != NULL)	PR_Close(sock);    if (path != NULL)	PORT_Free(path);    if (hostname != NULL)	PORT_Free(hostname);    return returnSock;}/* * Read from "fd" into "buf" -- expect/attempt to read a "minimum" number * of bytes, but do not exceed "maximum".  (Obviously, stop at less than * "minimum" if hit end-of-stream.)  Only problems are low-level i/o errors. */static intocsp_MinMaxRead(PRFileDesc *fd, unsigned char *buf, int minimum, int maximum){    int total = 0;    while (total < minimum) {	PRInt32 got;	got = PR_Read(fd, buf + total, (PRInt32) (maximum - total));	if (got < 0) {	    if (PR_GetError() != PR_WOULD_BLOCK_ERROR) {		break;	    }	    continue;	} else if (got == 0) {			/* EOS */	    break;	}	total += got;    }    return total;}#ifdef BUFSIZ#define OCSP_BUFSIZE BUFSIZ#else#define OCSP_BUFSIZE 1024#endif/* * Listens on the given socket and returns an encoded response when received. * Properly formatted HTTP response headers are expected to be read from the * socket, preceding a binary-encoded OCSP response.  Problems with parsing * cause the error SEC_ERROR_OCSP_BAD_HTTP_RESPONSE to be set; any other * problems are likely low-level i/o or memory allocation errors. */static SECItem *ocsp_GetEncodedResponse(PRArenaPool *arena, PRFileDesc *sock){    unsigned char *buf = NULL;    int bufsize, bytesRead, len;    const unsigned char *bufEnd;    const char *s;    const char *newline = NULL;    PRBool headersDone = PR_FALSE;    PRBool pendingCR = PR_FALSE;    PRBool contentTypeOK = PR_FALSE;    unsigned int contentLength = 0;    void *mark = NULL;    SECItem *result = NULL;    bufsize = OCSP_BUFSIZE;    buf = PORT_Alloc(bufsize);    if (buf == NULL) {	goto loser;    }    /*     * I picked 128 because:     *	- It is a nice "round" number. ;-)     *  - I am sure it should cover at least the first line of the http     *	  response (on the order of 20 should be enough but I am allowing     *	  for excessive leading whitespace) so that I don't have to keep     *	  checking for going past the end of the buffer until after that.     *	- I am sure that any interesting response will be bigger than that;     *    the exception is one that is status-only, like "tryLater".     *  - Given a trim response (no extra whitespace), it should cover     *	  all of the http headers, and just a bit of the content.     *	  This is what I was aiming for, to minimize data copying.     */    bytesRead = ocsp_MinMaxRead(sock, buf, 128, bufsize);    if (bytesRead <= 0) {	if (bytesRead == 0)	    PORT_SetError(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);	goto loser;    }    bufEnd = buf + bytesRead;    /*     * Skip leading whitespace.     */    for (s = (char *)buf; s < (char *)bufEnd; s++) {	if (*s != ' ' && *s != '\t') {	    break;	}    }    /*     * Here we expect to find something like "HTTP/1.x 200 OK\r\n".     * (The status code may be anything, but should be 3 digits.     * The comment (e.g. "OK") may be anything as well.)  I figure that     * the minimal line would be "HTTP/x.y zzz\n", which is 13 chars.     * That may not even meet the spec, but we will accept it -- anything     * less than that, though, we will not.     */    if (((char *)bufEnd - s) < 13 || PORT_Strncasecmp(s, "http/", 5) != 0) {	PORT_SetError(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);	goto loser;    }    s += 5;			/* skip "http/" */    /*     * I don't see a reason to look at the version number.  Assuming that     * whatever version it is, it supports the one or two headers we are     * looking for, and doesn't completely change the interpretation of     * status values, it shouldn't matter.  We shouldn't reject something     * that would otherwise work just because the code was picky about     * version number.  So we just skip it, hoping real incompatibility     * will manifest itself in the parsing.     */    while (*s++ != ' ') {	/* skip "x.y ", without interpretation */	/*	 * We expect the version number to be only a few characters,	 * and that we should easily have enough in the buffer to cover.	 * So, if we go past the end, just bail.	 */	if (s >= (char *)bufEnd) {	    PORT_SetError(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);	    goto loser;	}    }    /*     * Now get, and check, the status.  Accept 200-299; reject all else.     * There are some 200s that basically have no content, but that will     * fall out as bad below, and it's just easier to handle it that way     * than to try to know all the different status values.     */    {	int statusCode = PORT_Atoi(s);	if (statusCode < 200 || statusCode >= 300) {	    PORT_SetError(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);	    goto loser;	}    }    s += 3;			/* skip 3-digit status code */    /*     * The rest of the line is a comment, which we just want to skip.     * The next thing to do is continue through all of the HTTP headers,     * watching for the couple we are interested in.  The end of the     * headers (and thus the beginning of the content) is marked by     * two consecutive newlines.  (According to spec that should be     * CRLF followed by CRLF, but we are being generous and allowing     * for people who send only CRs or only LFs, too.)     *     * XXX Explain better about how the following code works.     * Especially what the potential values of "newline" are and what     * each one means.     */    while (1) {	/*	 * Move forward to the next line, stopping when we find a CRLF,	 * CR, or LF, or the end of the buffer.  We have to be careful	 * when we find a lone CR at the end of the buffer, because if	 * the next character read is an LF we want to treat it as one	 * newline altogether.	 *	 * We also need to detect two newlines in a row, which is what	 * marks the end of the headers and time for us to quit out of	 * the outer loop.	 */	for (; s < (char *)bufEnd; s++) {	    if (*s == '\r' || *s == '\n') {		if (s == newline) {		    headersDone = PR_TRUE;	/* 2nd consecutive newline */		}		newline = s;		if (newline[0] == '\r') {		    if (s == (char *)bufEnd - 1) {			/* CR at end - wait for the next character */			newline = NULL;			pendingCR = PR_TRUE;			break;		    } else if (newline[1] == '\n') {			/* CRLF seen; swallow both */			newline++;		    }		}		newline++;		break;	    }	    /*	     * After the first time through, we no longer need to remember	     * where the previous line started.  We needed it for checking	     * for consecutive newlines, but now we need it to be clear in	     * case we finish the loop by finishing off the buffer.	     */	    newline = NULL;	}	if (headersDone) {	    s = newline;	    break;	}	/*	 * When we are here, either:	 *	- newline is NULL, meaning we're at the end of the buffer	 *	  and we need to refill it, or	 *	- newline points to the first character on the new line	 */	if (newline == NULL) {	    /* So, we need to refill the buffer. */	    len = pendingCR ? 1 : 0;	    bytesRead = ocsp_MinMaxRead(sock, buf + len, 64 - len,					bufsize - len);	    if (bytesRead <= 0) {		if (bytesRead == 0)		    PORT_SetError(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);		goto loser;	    }	    s = (char *)buf;	    bufEnd = buf + len + bytesRead;	    if (pendingCR) {		buf[0] = '\r';		pendingCR = PR_FALSE;	    }	    continue;	}	/*	 * So, we have a good newline pointer (just past a CR, LF or CRLF),	 * but now we want to make sure that what it points to is long	 * enough to be something we are looking for.  If it isn't, add	 * more to the buffer after first copying what's left to the	 * beginning.	 */	if (((char *)bufEnd - newline) < 40) {	    len = (char *)bufEnd - newline;	    PORT_Memmove(buf, newline, len);	    bytesRead = ocsp_MinMaxRead(sock, buf + len, 40 - len,					bufsize - len);	    if (bytesRead <= 0) {		if (bytesRead == 0)		    PORT_SetError(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);		goto loser;	    }	    newline = (char *)buf;	    bufEnd = buf + len + bytesRead;	}	/*	 * Okay, now we know that we are looking at an HTTP header line	 * with enough length to be safe for our comparisons.  See if it is	 * one of the ones we are interested in.  (That is, "Content-Length"	 * or "Content-Type".)	 */	if (PORT_Strncasecmp(newline, "content-", 8) == 0) {	    s = newline + 8;	    if (PORT_Strncasecmp(s, "type: ", 6) == 0) {		s += 6;		if (PORT_Strncasecmp(s, "application/ocsp-response",				     25) != 0) {		    break;		}		contentTypeOK = PR_TRUE;		s += 25;	    } else if (PORT_Strncasecmp(s, "length: ", 8) == 0) {		s += 8;		contentLength = PORT_Atoi(s);	    }	    newline = NULL;	} else {	    /*	     * Not an interesting header.  We will go around the loop	     * again to find the next line.	     */	    s = newline;	}    }    /*     * Check that we got the correct content type.  (If !contentTypeOK,     * the content-type header was either bad or missing.)     */    if (!contentTypeOK) {	PORT_SetError(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);	goto loser;    }    /*     * Now we need to handle the actual content.  We may not have a     * good content-length; things are harder if we do not -- in that     * case we will just read until we get EOS.     */    /*     * We are finally about the allocate the return area; before that     * mark the arena so we can release-back on error.     */    if (arena != NULL) {	mark = PORT_ArenaMark(arena);    }    /*     * How much of the content is already in the buffer?     */     if (s == NULL) {	len = 0;    } else {	len = (char *)bufEnd - s;	if (contentLength && len > contentLength) {	    len = contentLength;	}    }    /*     * Now allocate the item to hold the data.     */    result = SECITEM_AllocItem(arena, NULL,			       contentLength ? contentLength : len);    if (result == NULL) {	goto loser;    }    /*     * And copy the data left in the buffer.     */    if (len) {	PORT_Memcpy(result->data, s, len);    }    /*     * Now we need to read all of the (rest of the) content.  If we know     * the length, it's easy, just one big read.  If not, we need to loop,     * reading until there is nothing left to read.     */    if (contentLength) {	int remaining;	/*	 * It may be the case that our last buffer ended exactly on the	 * second CR of the CRLF pair which denotes the end of the headers.	 * If so, we have to be prepared to read, and skip over, an LF.	 * Since we want to read the rest of the content directly into	 * the buffer we are returning, we read one byte specifically and	 * see if it is an LF.  If not, we just copy that byte into the	 * first byte of our return value.	 */	if (pendingCR) {	    PORT_Assert(len == 0);	    bytesRead = ocsp_MinMaxRead(sock, buf, 1, 1);	    if (bytesRead < 1) {		goto loser;	    }	    if (buf[0] != '\n') {		result->data[0] = buf[0];		len = 1;	    }	}	remaining = contentLength - len;	if (remaining) {	    bytesRead = ocsp_MinMaxRead(sock, result->data + len,					remaining, remaining);	    if (bytesRead < remaining) {		if (bytesRead > 0) {		    PORT_SetError(SEC_ERROR_OCSP_BAD_HTTP_RESPONSE);		}		goto loser;	    }	}    } else while (1) {	/*	 * Read as much as we can.  There is no real minimum here,	 * we will just take whatever we can get and copy it over	 * into our result area.	 */	bytesRead = ocsp_MinMaxRead(sock, buf, 1, bufsize);	if (bytesRead < 0) {	    goto loser;	}	if (bytesRead == 0) {		/* EOS: all done reading */	    break;	}	/*	 * Now make room for that many more bytes in our result area.	 */	if (SECITEM_ReallocItem(arena, result, len,				len + bytesRead) != SECSuccess) {	    goto loser;	}	/*	 * The first time through this loop, it may be the case that	 * we have the final LF of our CRLF pair that ended the headers.	 * If so, we need to skip over it.	 */	if (pendingCR && buf[0] == '\n') {	    PORT_Memcpy(result->data + len, buf + 1, bytesRead - 1);	    len += bytesRead - 1;	} else {	    PORT_Memcpy(result->data + len, buf, bytesRead);	    len += bytesRead;	}	/*	 * In any case, after the first time through, we are done	 * looking for that LF.	 */	pendingCR = PR_FALSE;	/*	 * If we are reading "slowly", get ourselves a bigger buffer.	 */	if (bytesRead == bufsize) {	    bufsize *= 2;	    PORT_Free(buf);	    buf = PORT_Alloc(bufsize);	    if (buf == NULL) {		goto loser;	    }	}    }    /*     * Finally, we are done!  The encoded response (the content of the     * HTTP response) is now in "result".  We just have a little cleaning     * up to do before we return.     */    if (mark != NULL) {	PORT_ArenaUnmark(arena, mark);    }    PORT_Free(buf);    return result;loser:    if (mark != NULL) {	PORT_ArenaRelease(arena, mark);    } else if (result != NULL) {	SECITEM_FreeItem(result, PR_TRUE);    }    i

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?