summaryrefslogtreecommitdiff
path: root/src/ocsptool-common.c
blob: 835ccbfc2bdfa7ac759f5745c2c28b60f653384a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * Copyright (C) 2012 Free Software Foundation, Inc.
 *
 * This file is part of GnuTLS.
 *
 * GnuTLS is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GnuTLS is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include <gnutls/gnutls.h>
#include <gnutls/ocsp.h>
#include <gnutls/x509.h>
#include <gnutls/crypto.h>

/* Gnulib portability files. */
#include <error.h>
#include <progname.h>
#include <version-etc.h>
#include <read-file.h>
#include <socket.h>

#include <ocsptool-common.h>

#define MAX_BUF 4*1024
#define HEADER_PATTERN "POST / HTTP/1.1\r\n" \
  "Host: %s\r\n" \
  "Accept: */*\r\n" \
  "Content-Type: application/ocsp-request\r\n" \
  "Content-Length: %u\r\n" \
  "Connection: close\r\n\r\n"
static char buffer[MAX_BUF + 1];

/* returns the host part of a URL */
static const char* host_from_url(const char* url, unsigned int* port)
{
static char hostname[512];
char * p;

  *port = 0;

  if ((p=strstr(url, "http://")) != NULL)
    {
      snprintf(hostname, sizeof(hostname), "%s", p+7);
      p = strchr(hostname, '/');
      if (p != NULL) *p = 0;

      p = strchr(hostname, ':');
      if (p != NULL) {
        *p = 0;
        *port = atoi(p+1);
      }
      
      return hostname;
    }
  else
    {
      return url;
    }
}

void
_generate_request (gnutls_x509_crt_t cert, gnutls_x509_crt_t issuer,
                   gnutls_datum_t * rdata, int nonce)
{
  gnutls_ocsp_req_t req;
  int ret;

  ret = gnutls_ocsp_req_init (&req);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "ocsp_req_init: %s", gnutls_strerror (ret));

  ret = gnutls_ocsp_req_add_cert (req, GNUTLS_DIG_SHA1,
  				      issuer, cert);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "ocsp_req_add_cert: %s", gnutls_strerror (ret));
    
  if (nonce)
    {
      unsigned char noncebuf[23];
      gnutls_datum_t nonce = { noncebuf, sizeof (noncebuf) };

      ret = gnutls_rnd (GNUTLS_RND_RANDOM, nonce.data, nonce.size);
      if (ret < 0)
	error (EXIT_FAILURE, 0, "gnutls_rnd: %s", gnutls_strerror (ret));

      ret = gnutls_ocsp_req_set_nonce (req, 0, &nonce);
      if (ret < 0)
	error (EXIT_FAILURE, 0, "ocsp_req_set_nonce: %s",
	       gnutls_strerror (ret));
    }

  ret = gnutls_ocsp_req_export (req, rdata);
  if (ret != 0)
    error (EXIT_FAILURE, 0, "ocsp_req_export: %s", gnutls_strerror (ret));

  gnutls_ocsp_req_deinit (req);
  return;
}

static size_t get_data(void *buffer, size_t size, size_t nmemb, void *userp)
{
gnutls_datum_t *ud = userp;
  
  size *= nmemb;

  ud->data = realloc(ud->data, size+ud->size);
  if (ud->data == NULL)
    {
      fprintf(stderr, "Not enough memory for the request\n");
      exit(1);
    }

  memcpy(&ud->data[ud->size], buffer, size);
  ud->size += size;
  
  return size;
}

/* Returns 0 on ok, and -1 on error */
int send_ocsp_request(const char* server,
                       gnutls_x509_crt_t cert, gnutls_x509_crt_t issuer,
                       gnutls_datum_t * resp_data, int nonce)
{
gnutls_datum_t ud;
int ret;
gnutls_datum_t req;
char* url = (void*)server;
char headers[1024];
char service[16];
unsigned char * p;
const char *hostname;
unsigned int headers_size = 0, port;
socket_st hd;

  sockets_init ();

  if (url == NULL)
    {
      /* try to read URL from issuer certificate */
      gnutls_datum_t data;
      
      ret = gnutls_x509_crt_get_authority_info_access(cert, 0, 
                                  GNUTLS_IA_OCSP_URI, &data, NULL);
      
      if (ret < 0)
        ret = gnutls_x509_crt_get_authority_info_access(issuer, 0, 
                                    GNUTLS_IA_OCSP_URI, &data, NULL);
      if (ret < 0)
        {
          fprintf(stderr, "Cannot find URL from issuer: %s\n", gnutls_strerror(ret));
          return -1;
        }
      
      url = malloc(data.size+1);
      memcpy(url, data.data, data.size);
      url[data.size] = 0;
      
      gnutls_free(data.data);
    }
    
  hostname = host_from_url(url, &port);
  if (port != 0)
    snprintf(service, sizeof(service), "%u", port);
  else strcpy(service, "80");
  
  fprintf(stderr, "Connecting to OCSP server: %s...\n", hostname);

  memset(&ud, 0, sizeof(ud));

  _generate_request(cert, issuer, &req, nonce);

  snprintf(headers, sizeof(headers), HEADER_PATTERN, hostname, (unsigned int)req.size);
  headers_size = strlen(headers);
  
  socket_open(&hd, hostname, service, 0);
  socket_connect (&hd);
  
  socket_send(&hd, headers, headers_size);
  socket_send(&hd, req.data, req.size);
  
  do {
    ret = socket_recv(&hd, buffer, sizeof(buffer));
    if (ret > 0) get_data(buffer, ret, 1, &ud);
  } while(ret > 0);
  
  if (ret < 0 || ud.size == 0)
    {
      perror("recv");
      return -1;
    }
  
  socket_bye(&hd);
  
  p = memmem(ud.data, ud.size, "\r\n\r\n", 4);
  if (p == NULL)
    {
      fprintf(stderr, "Cannot interpret HTTP response\n");
      return -1;
    }
  
  p += 4;
  resp_data->size = ud.size - (p - ud.data);
  resp_data->data = malloc(resp_data->size);
  if (resp_data->data == NULL)
    return -1;
  
  memcpy(resp_data->data, p, resp_data->size);

  free(ud.data);
  
  return 0;
}

void
print_ocsp_verify_res (unsigned int output)
{
  int comma = 0;

  if (output)
    {
      printf ("Failure");
      comma = 1;
    }
  else
    {
      printf ("Success");
      comma = 1;
    }

  if (output & GNUTLS_OCSP_VERIFY_SIGNER_NOT_FOUND)
    {
      if (comma)
        printf (", ");
      printf ("Signer cert not found");
      comma = 1;
    }

  if (output & GNUTLS_OCSP_VERIFY_SIGNER_KEYUSAGE_ERROR)
    {
      if (comma)
        printf (", ");
      printf ("Signer cert keyusage error");
      comma = 1;
    }

  if (output & GNUTLS_OCSP_VERIFY_UNTRUSTED_SIGNER)
    {
      if (comma)
        printf (", ");
      printf ("Signer cert is not trusted");
      comma = 1;
    }

  if (output & GNUTLS_OCSP_VERIFY_INSECURE_ALGORITHM)
    {
      if (comma)
        printf (", ");
      printf ("Insecure algorithm");
      comma = 1;
    }

  if (output & GNUTLS_OCSP_VERIFY_SIGNATURE_FAILURE)
    {
      if (comma)
        printf (", ");
      printf ("Signature failure");
      comma = 1;
    }

  if (output & GNUTLS_OCSP_VERIFY_CERT_NOT_ACTIVATED)
    {
      if (comma)
        printf (", ");
      printf ("Signer cert not yet activated");
      comma = 1;
    }

  if (output & GNUTLS_OCSP_VERIFY_CERT_EXPIRED)
    {
      if (comma)
        printf (", ");
      printf ("Signer cert expired");
      comma = 1;
    }
}

/* three days */
#define OCSP_VALIDITY_SECS (3*60*60*24)

/* Returns:
 *  0: certificate is revoked
 *  1: certificate is ok
 *  -1: dunno
 */
int
check_ocsp_response (gnutls_x509_crt_t cert,
                     gnutls_x509_crt_t issuer,
                     gnutls_datum_t *data)
{
  gnutls_ocsp_resp_t resp;
  int ret;
  unsigned int status, cert_status;
  time_t rtime, vtime, ntime, now;
  
  now = time(0);

  ret = gnutls_ocsp_resp_init (&resp);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "ocsp_resp_init: %s", gnutls_strerror (ret));

  ret = gnutls_ocsp_resp_import (resp, data);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "importing response: %s", gnutls_strerror (ret));
  
  ret = gnutls_ocsp_resp_check_crt(resp, 0, cert);
  if (ret < 0)
    {
      printf ("*** Got OCSP response on an unrelated certificate (ignoring)\n");
      ret = -1;
      goto cleanup;
    }

  ret = gnutls_ocsp_resp_verify_direct( resp, issuer, &status, 0);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "gnutls_ocsp_resp_verify_direct: %s",
      gnutls_strerror (ret));

  if (status != 0)
    {
      printf ("*** Verifying OCSP Response: ");
      print_ocsp_verify_res (status);
      printf (".\n");
    }

  /* do not print revocation data if response was not verified */
  if (status != 0)
    {
      ret = -1;
      goto cleanup;
    }

  ret = gnutls_ocsp_resp_get_single(resp, 0, NULL, NULL, NULL, NULL,
        &cert_status, &vtime, &ntime, &rtime, NULL);
  if (ret < 0)
    error (EXIT_FAILURE, 0, "reading response: %s", gnutls_strerror (ret));
  
  if (cert_status == GNUTLS_OCSP_CERT_REVOKED)
    {
      printf("*** Certificate was revoked at %s", ctime(&rtime));
      ret = 0;
      goto cleanup;
    }
  
  if (ntime == -1)
    {
      if (now - vtime > OCSP_VALIDITY_SECS)
        {
          printf("*** The OCSP response is old (was issued at: %s) ignoring", ctime(&vtime));
          ret = -1;
          goto cleanup;
        }
    }
  else
    {
      /* there is a newer OCSP answer, don't trust this one */
      if (ntime < now)
        {
          printf("*** The OCSP response was issued at: %s, but there is a newer issue at %s", ctime(&vtime), ctime(&ntime));
          ret = -1;
          goto cleanup;
        }
    }
  
  printf("- OCSP server flags certificate not revoked as of %s", ctime(&vtime));
  ret = 1;
cleanup:
  gnutls_ocsp_resp_deinit (resp);
  
  return ret;
}