3 * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
5 * This file is part of CyaSSL.
7 * CyaSSL is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * CyaSSL is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
26 #include <cyassl/error.h>
27 #include <cyassl/ocsp.h>
28 #include <cyassl/internal.h>
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
36 #include <arpa/inet.h>
37 #include <sys/ioctl.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
44 CYASSL_API int ocsp_test(unsigned char* buf, int sz);
45 #define CYASSL_OCSP_ENABLE 0x0001 /* Enable OCSP lookups */
46 #define CYASSL_OCSP_URL_OVERRIDE 0x0002 /* Use the override URL instead of URL
49 typedef struct sockaddr_in SOCKADDR_IN_T;
50 #define AF_INET_V AF_INET
51 #define SOCKET_T unsigned int
54 int ocsp_test(unsigned char* buf, int sz)
60 CyaSSL_OCSP_Init(&ocsp);
61 InitOcspResponse(&resp, buf, sz, NULL);
64 ocsp.useOverrideUrl = 1;
65 CyaSSL_OCSP_set_override_url(&ocsp, "http://ocsp.example.com:8080/bob");
66 CyaSSL_OCSP_Lookup_Cert(&ocsp, NULL);
68 result = OcspResponseDecode(&resp);
70 FreeOcspResponse(&resp);
71 CyaSSL_OCSP_Cleanup(&ocsp);
77 int CyaSSL_OCSP_Init(CYASSL_OCSP* ocsp)
80 XMEMSET(ocsp, 0, sizeof(*ocsp));
88 void CyaSSL_OCSP_Cleanup(CYASSL_OCSP* ocsp)
94 int CyaSSL_OCSP_set_override_url(CYASSL_OCSP* ocsp, const char* url)
96 if (ocsp != NULL && url != NULL) {
99 /* need to break the url down into scheme, address, and port */
100 /* "http://example.com:8080/" */
101 if (XSTRNCMP(url, "http://", 7) == 0) {
106 while (url[cur] != 0 && url[cur] != ':' && url[cur] != '/') {
107 ocsp->overrideName[i++] = url[cur++];
109 ocsp->overrideName[i] = 0;
110 /* Need to pick out the path after the domain name */
112 if (url[cur] == ':') {
117 while (url[cur] != 0 && url[cur] != '/' && i < 6) {
118 port[i++] = url[cur++];
121 ocsp->overridePort = 0;
122 for (j = 0; j < i; j++) {
123 if (port[j] < '0' || port[j] > '9') return -1;
125 (ocsp->overridePort * 10) + (port[j] - '0');
129 ocsp->overridePort = 80;
131 if (url[cur] == '/') {
133 while (url[cur] != 0 && i < 80) {
134 ocsp->overridePath[i++] = url[cur++];
136 ocsp->overridePath[i] = 0;
139 ocsp->overridePath[0] = '/';
140 ocsp->overridePath[1] = 0;
151 static INLINE void tcp_socket(SOCKET_T* sockfd, SOCKADDR_IN_T* addr,
152 const char* peer, word16 port)
154 const char* host = peer;
156 /* peer could be in human readable form */
157 if (peer != INADDR_ANY && isalpha(peer[0])) {
158 struct hostent* entry = gethostbyname(peer);
161 struct sockaddr_in tmp;
162 memset(&tmp, 0, sizeof(struct sockaddr_in));
163 memcpy(&tmp.sin_addr.s_addr, entry->h_addr_list[0],
165 host = inet_ntoa(tmp.sin_addr);
168 CYASSL_MSG("no entry for host");
171 *sockfd = socket(AF_INET_V, SOCK_STREAM, 0);
172 memset(addr, 0, sizeof(SOCKADDR_IN_T));
174 addr->sin_family = AF_INET_V;
175 addr->sin_port = htons(port);
176 if (host == INADDR_ANY)
177 addr->sin_addr.s_addr = INADDR_ANY;
179 addr->sin_addr.s_addr = inet_addr(host);
183 static INLINE void tcp_connect(SOCKET_T* sockfd, const char* ip, word16 port)
186 tcp_socket(sockfd, &addr, ip, port);
188 if (connect(*sockfd, (const struct sockaddr*)&addr, sizeof(addr)) != 0)
189 CYASSL_MSG("tcp connect failed");
193 static int build_http_request(CYASSL_OCSP* ocsp, int ocspReqSz,
194 byte* buf, int bufSize)
196 return snprintf((char*)buf, bufSize,
197 "POST %s HTTP/1.1\r\n"
199 "Content-Length: %d\r\n"
200 "Content-Type: application/ocsp-request\r\n"
202 ocsp->overridePath, ocsp->overrideName, ocspReqSz);
206 static const char foo[] = \
207 "\x30\x81\xB7\x30\x81\xB4\x30\x81\x8C\x30\x44\x30\x42\x30\x09\x06\x05\x2B\x0E\x03" \
208 "\x02\x1A\x05\x00\x04\x14\x49\x2D\x52\x83\x4B\x40\x37\xF5\xA9\x9E\x26\xA2\x3E\x48" \
209 "\x2F\x2E\x37\x34\xC9\x54\x04\x14\x21\xA2\x25\xEE\x57\x38\x34\x5A\x24\x9D\xF3\x7C" \
210 "\x18\x60\x59\x7A\x04\x3D\xF5\x69\x02\x09\x00\x89\x5A\xA2\xBD\xFE\x26\x8B\xEE\x30" \
211 "\x44\x30\x42\x30\x09\x06\x05\x2B\x0E\x03\x02\x1A\x05\x00\x04\x14\x49\x2D\x52\x83" \
212 "\x4B\x40\x37\xF5\xA9\x9E\x26\xA2\x3E\x48\x2F\x2E\x37\x34\xC9\x54\x04\x14\x21\xA2" \
213 "\x25\xEE\x57\x38\x34\x5A\x24\x9D\xF3\x7C\x18\x60\x59\x7A\x04\x3D\xF5\x69\x02\x09" \
214 "\x00\x89\x5A\xA2\xBD\xFE\x26\x8B\xEF\xA2\x23\x30\x21\x30\x1F\x06\x09\x2B\x06\x01" \
215 "\x05\x05\x07\x30\x01\x02\x04\x12\x04\x10\x20\x56\x47\x19\x65\x33\xB6\xB5\xAD\x39" \
216 "\x1F\x21\x65\xE0\x44\x1E";
219 static int build_ocsp_request(CYASSL_OCSP* ocsp, byte* buf, int bufSz)
221 memcpy(buf, foo, sizeof(foo));
222 return sizeof(foo) - 1;
226 static byte* decode_http_response(byte* httpBuf, int httpBufSz, int* ocspRespSz)
230 byte* contentType = NULL;
231 byte* contentLength = NULL;
232 byte* content = NULL;
233 char* buf = (char*)httpBuf; /* kludge so I'm not constantly casting */
235 if (strncasecmp(buf, "HTTP/1", 6) != 0)
238 idx = 9; /* sets to the first byte after "HTTP/1.X ", which should be the
239 * HTTP result code */
241 if (strncasecmp(&buf[idx], "200 OK", 6) != 0)
246 while (idx < httpBufSz && !stop) {
247 if (buf[idx] == '\r' && buf[idx+1] == '\n') {
252 if (contentType == NULL &&
253 strncasecmp(&buf[idx], "Content-Type:", 13) == 0) {
255 if (buf[idx] == ' ') idx++;
256 if (strncasecmp(&buf[idx], "application/ocsp-response", 25) != 0)
259 } else if (contentLength == NULL &&
260 strncasecmp(&buf[idx], "Content-Length:", 15) == 0) {
263 if (buf[idx] == ' ') idx++;
264 while (buf[idx] > '0' && buf[idx] < '9' && idx < httpBufSz) {
265 len = (len * 10) + (buf[idx] - '0');
269 idx += 2; /* skip the crlf */
271 /* Advance idx past the next \r\n */
272 char* end = strstr(&buf[idx], "\r\n");
278 return &httpBuf[idx];
282 #define SCRATCH_BUFFER_SIZE 2048
284 int CyaSSL_OCSP_Lookup_Cert(CYASSL_OCSP* ocsp, DecodedCert* cert)
287 byte buf[SCRATCH_BUFFER_SIZE];
288 byte* httpBuf = &buf[0];
289 int httpBufSz = SCRATCH_BUFFER_SIZE/4;
290 byte* ocspReqBuf = &buf[httpBufSz];
291 int ocspReqSz = SCRATCH_BUFFER_SIZE - httpBufSz;
292 OcspResponse ocspResponse;
293 int result = CERT_UNKNOWN;
295 /* If OCSP lookups are disabled, return success. */
296 if (!ocsp->enabled) {
297 CYASSL_MSG("OCSP lookup disabled, assuming CERT_GOOD");
301 /* If OCSP lookups are enabled, but URL Override is disabled, return
302 ** a failure. Need to have an override URL for right now. */
303 if (!ocsp->useOverrideUrl || cert == NULL) {
304 CYASSL_MSG("OCSP lookup enabled, but URL Override disabled");
308 XMEMCPY(ocsp->status[0].issuerHash, cert->issuerHash, SHA_SIZE);
309 XMEMCPY(ocsp->status[0].issuerKeyHash, cert->issuerKeyHash, SHA_SIZE);
310 XMEMCPY(ocsp->status[0].serial, cert->serial, cert->serialSz);
311 ocsp->status[0].serialSz = cert->serialSz;
314 /*ocspReqSz = build_ocsp_request(ocsp, ocspReqBuf, ocspReqSz);*/
315 ocspReqSz = EncodeOcspRequest(cert, ocspReqBuf, ocspReqSz);
316 httpBufSz = build_http_request(ocsp, ocspReqSz, httpBuf, httpBufSz);
318 tcp_connect(&sfd, ocsp->overrideName, ocsp->overridePort);
321 written = write(sfd, httpBuf, httpBufSz);
322 if (written == httpBufSz) {
323 written = write(sfd, ocspReqBuf, ocspReqSz);
324 if (written == ocspReqSz) {
325 httpBufSz = read(sfd, buf, SCRATCH_BUFFER_SIZE);
327 ocspReqBuf = decode_http_response(buf, httpBufSz,
333 if (ocspReqBuf == NULL) {
334 CYASSL_MSG("HTTP response was not OK, no OCSP response");
338 CYASSL_MSG("OCSP Responder connection failed");
342 InitOcspResponse(&ocspResponse, ocspReqBuf, ocspReqSz, NULL);
343 OcspResponseDecode(&ocspResponse);
344 if (ocspResponse.responseStatus != OCSP_SUCCESSFUL) {
345 CYASSL_MSG("OCSP Responder failure");
347 result = ocspResponse.certStatus[0];
349 FreeOcspResponse(&ocspResponse);
355 #endif /* HAVE_OCSP */