os/ossrv/ssl/libcrypto/src/crypto/ocsp/ocsp_cl.c
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/* ocsp_cl.c */
sl@0
     2
/* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
sl@0
     3
 * project. */
sl@0
     4
sl@0
     5
/* History:
sl@0
     6
   This file was transfered to Richard Levitte from CertCo by Kathy
sl@0
     7
   Weinhold in mid-spring 2000 to be included in OpenSSL or released
sl@0
     8
   as a patch kit. */
sl@0
     9
sl@0
    10
/* ====================================================================
sl@0
    11
 * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
sl@0
    12
 *
sl@0
    13
 * Redistribution and use in source and binary forms, with or without
sl@0
    14
 * modification, are permitted provided that the following conditions
sl@0
    15
 * are met:
sl@0
    16
 *
sl@0
    17
 * 1. Redistributions of source code must retain the above copyright
sl@0
    18
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    19
 *
sl@0
    20
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    21
 *    notice, this list of conditions and the following disclaimer in
sl@0
    22
 *    the documentation and/or other materials provided with the
sl@0
    23
 *    distribution.
sl@0
    24
 *
sl@0
    25
 * 3. All advertising materials mentioning features or use of this
sl@0
    26
 *    software must display the following acknowledgment:
sl@0
    27
 *    "This product includes software developed by the OpenSSL Project
sl@0
    28
 *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
sl@0
    29
 *
sl@0
    30
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    31
 *    endorse or promote products derived from this software without
sl@0
    32
 *    prior written permission. For written permission, please contact
sl@0
    33
 *    openssl-core@openssl.org.
sl@0
    34
 *
sl@0
    35
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    36
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    37
 *    permission of the OpenSSL Project.
sl@0
    38
 *
sl@0
    39
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    40
 *    acknowledgment:
sl@0
    41
 *    "This product includes software developed by the OpenSSL Project
sl@0
    42
 *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
sl@0
    43
 *
sl@0
    44
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    45
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    46
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    47
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    48
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    49
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    50
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    51
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    52
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    53
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
    54
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
    55
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
    56
 * ====================================================================
sl@0
    57
 *
sl@0
    58
 * This product includes cryptographic software written by Eric Young
sl@0
    59
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
    60
 * Hudson (tjh@cryptsoft.com).
sl@0
    61
 *
sl@0
    62
 */
sl@0
    63
sl@0
    64
#include <stdio.h>
sl@0
    65
#include <time.h>
sl@0
    66
#include <cryptlib.h>
sl@0
    67
#include <openssl/objects.h>
sl@0
    68
#include <openssl/rand.h>
sl@0
    69
#include <openssl/x509.h>
sl@0
    70
#include <openssl/pem.h>
sl@0
    71
#include <openssl/x509v3.h>
sl@0
    72
#include <openssl/ocsp.h>
sl@0
    73
sl@0
    74
/* Utility functions related to sending OCSP requests and extracting
sl@0
    75
 * relevant information from the response.
sl@0
    76
 */
sl@0
    77
sl@0
    78
/* Add an OCSP_CERTID to an OCSP request. Return new OCSP_ONEREQ 
sl@0
    79
 * pointer: useful if we want to add extensions.
sl@0
    80
 */
sl@0
    81
sl@0
    82
EXPORT_C OCSP_ONEREQ *OCSP_request_add0_id(OCSP_REQUEST *req, OCSP_CERTID *cid)
sl@0
    83
        {
sl@0
    84
	OCSP_ONEREQ *one = NULL;
sl@0
    85
sl@0
    86
	if (!(one = OCSP_ONEREQ_new())) goto err;
sl@0
    87
	if (one->reqCert) OCSP_CERTID_free(one->reqCert);
sl@0
    88
	one->reqCert = cid;
sl@0
    89
	if (req &&
sl@0
    90
		!sk_OCSP_ONEREQ_push(req->tbsRequest->requestList, one))
sl@0
    91
				goto err;
sl@0
    92
	return one;
sl@0
    93
err:
sl@0
    94
	OCSP_ONEREQ_free(one);
sl@0
    95
	return NULL;
sl@0
    96
        }
sl@0
    97
sl@0
    98
/* Set requestorName from an X509_NAME structure */
sl@0
    99
sl@0
   100
EXPORT_C int OCSP_request_set1_name(OCSP_REQUEST *req, X509_NAME *nm)
sl@0
   101
	{
sl@0
   102
	GENERAL_NAME *gen;
sl@0
   103
	gen = GENERAL_NAME_new();
sl@0
   104
	if (gen == NULL)
sl@0
   105
		return 0;
sl@0
   106
	if (!X509_NAME_set(&gen->d.directoryName, nm))
sl@0
   107
		{
sl@0
   108
		GENERAL_NAME_free(gen);
sl@0
   109
		return 0;
sl@0
   110
		}
sl@0
   111
	gen->type = GEN_DIRNAME;
sl@0
   112
	if (req->tbsRequest->requestorName)
sl@0
   113
		GENERAL_NAME_free(req->tbsRequest->requestorName);
sl@0
   114
	req->tbsRequest->requestorName = gen;
sl@0
   115
	return 1;
sl@0
   116
	}
sl@0
   117
	
sl@0
   118
sl@0
   119
/* Add a certificate to an OCSP request */
sl@0
   120
sl@0
   121
EXPORT_C int OCSP_request_add1_cert(OCSP_REQUEST *req, X509 *cert)
sl@0
   122
	{
sl@0
   123
	OCSP_SIGNATURE *sig;
sl@0
   124
	if (!req->optionalSignature)
sl@0
   125
		req->optionalSignature = OCSP_SIGNATURE_new();
sl@0
   126
	sig = req->optionalSignature;
sl@0
   127
	if (!sig) return 0;
sl@0
   128
	if (!cert) return 1;
sl@0
   129
	if (!sig->certs && !(sig->certs = sk_X509_new_null()))
sl@0
   130
		return 0;
sl@0
   131
sl@0
   132
	if(!sk_X509_push(sig->certs, cert)) return 0;
sl@0
   133
	CRYPTO_add(&cert->references, 1, CRYPTO_LOCK_X509);
sl@0
   134
	return 1;
sl@0
   135
	}
sl@0
   136
sl@0
   137
/* Sign an OCSP request set the requestorName to the subjec
sl@0
   138
 * name of an optional signers certificate and include one
sl@0
   139
 * or more optional certificates in the request. Behaves
sl@0
   140
 * like PKCS7_sign().
sl@0
   141
 */
sl@0
   142
sl@0
   143
EXPORT_C int OCSP_request_sign(OCSP_REQUEST   *req,
sl@0
   144
		      X509           *signer,
sl@0
   145
		      EVP_PKEY       *key,
sl@0
   146
		      const EVP_MD   *dgst,
sl@0
   147
		      STACK_OF(X509) *certs,
sl@0
   148
		      unsigned long flags)
sl@0
   149
        {
sl@0
   150
	int i;
sl@0
   151
	OCSP_SIGNATURE *sig;
sl@0
   152
	X509 *x;
sl@0
   153
sl@0
   154
	if (!OCSP_request_set1_name(req, X509_get_subject_name(signer)))
sl@0
   155
			goto err;
sl@0
   156
sl@0
   157
	if (!(req->optionalSignature = sig = OCSP_SIGNATURE_new())) goto err;
sl@0
   158
	if (!dgst) dgst = EVP_sha1();
sl@0
   159
	if (key)
sl@0
   160
		{
sl@0
   161
		if (!X509_check_private_key(signer, key))
sl@0
   162
			{
sl@0
   163
			OCSPerr(OCSP_F_OCSP_REQUEST_SIGN, OCSP_R_PRIVATE_KEY_DOES_NOT_MATCH_CERTIFICATE);
sl@0
   164
			goto err;
sl@0
   165
			}
sl@0
   166
		if (!OCSP_REQUEST_sign(req, key, dgst)) goto err;
sl@0
   167
		}
sl@0
   168
sl@0
   169
	if (!(flags & OCSP_NOCERTS))
sl@0
   170
		{
sl@0
   171
		if(!OCSP_request_add1_cert(req, signer)) goto err;
sl@0
   172
		for (i = 0; i < sk_X509_num(certs); i++)
sl@0
   173
			{
sl@0
   174
			x = sk_X509_value(certs, i);
sl@0
   175
			if (!OCSP_request_add1_cert(req, x)) goto err;
sl@0
   176
			}
sl@0
   177
		}
sl@0
   178
sl@0
   179
	return 1;
sl@0
   180
err:
sl@0
   181
	OCSP_SIGNATURE_free(req->optionalSignature);
sl@0
   182
	req->optionalSignature = NULL;
sl@0
   183
	return 0;
sl@0
   184
	}
sl@0
   185
sl@0
   186
/* Get response status */
sl@0
   187
sl@0
   188
EXPORT_C int OCSP_response_status(OCSP_RESPONSE *resp)
sl@0
   189
	{
sl@0
   190
	return ASN1_ENUMERATED_get(resp->responseStatus);
sl@0
   191
	}
sl@0
   192
sl@0
   193
/* Extract basic response from OCSP_RESPONSE or NULL if
sl@0
   194
 * no basic response present.
sl@0
   195
 */
sl@0
   196
 
sl@0
   197
sl@0
   198
EXPORT_C OCSP_BASICRESP *OCSP_response_get1_basic(OCSP_RESPONSE *resp)
sl@0
   199
	{
sl@0
   200
	OCSP_RESPBYTES *rb;
sl@0
   201
	rb = resp->responseBytes;
sl@0
   202
	if (!rb)
sl@0
   203
		{
sl@0
   204
		OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NO_RESPONSE_DATA);
sl@0
   205
		return NULL;
sl@0
   206
		}
sl@0
   207
	if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic)
sl@0
   208
		{
sl@0
   209
		OCSPerr(OCSP_F_OCSP_RESPONSE_GET1_BASIC, OCSP_R_NOT_BASIC_RESPONSE);
sl@0
   210
		return NULL;
sl@0
   211
		}
sl@0
   212
sl@0
   213
	return ASN1_item_unpack(rb->response, ASN1_ITEM_rptr(OCSP_BASICRESP));
sl@0
   214
	}
sl@0
   215
sl@0
   216
/* Return number of OCSP_SINGLERESP reponses present in
sl@0
   217
 * a basic response.
sl@0
   218
 */
sl@0
   219
sl@0
   220
EXPORT_C int OCSP_resp_count(OCSP_BASICRESP *bs)
sl@0
   221
	{
sl@0
   222
	if (!bs) return -1;
sl@0
   223
	return sk_OCSP_SINGLERESP_num(bs->tbsResponseData->responses);
sl@0
   224
	}
sl@0
   225
sl@0
   226
/* Extract an OCSP_SINGLERESP response with a given index */
sl@0
   227
sl@0
   228
EXPORT_C OCSP_SINGLERESP *OCSP_resp_get0(OCSP_BASICRESP *bs, int idx)
sl@0
   229
	{
sl@0
   230
	if (!bs) return NULL;
sl@0
   231
	return sk_OCSP_SINGLERESP_value(bs->tbsResponseData->responses, idx);
sl@0
   232
	}
sl@0
   233
sl@0
   234
/* Look single response matching a given certificate ID */
sl@0
   235
sl@0
   236
EXPORT_C int OCSP_resp_find(OCSP_BASICRESP *bs, OCSP_CERTID *id, int last)
sl@0
   237
	{
sl@0
   238
	int i;
sl@0
   239
	STACK_OF(OCSP_SINGLERESP) *sresp;
sl@0
   240
	OCSP_SINGLERESP *single;
sl@0
   241
	if (!bs) return -1;
sl@0
   242
	if (last < 0) last = 0;
sl@0
   243
	else last++;
sl@0
   244
	sresp = bs->tbsResponseData->responses;
sl@0
   245
	for (i = last; i < sk_OCSP_SINGLERESP_num(sresp); i++)
sl@0
   246
		{
sl@0
   247
		single = sk_OCSP_SINGLERESP_value(sresp, i);
sl@0
   248
		if (!OCSP_id_cmp(id, single->certId)) return i;
sl@0
   249
		}
sl@0
   250
	return -1;
sl@0
   251
	}
sl@0
   252
sl@0
   253
/* Extract status information from an OCSP_SINGLERESP structure.
sl@0
   254
 * Note: the revtime and reason values are only set if the 
sl@0
   255
 * certificate status is revoked. Returns numerical value of
sl@0
   256
 * status.
sl@0
   257
 */
sl@0
   258
sl@0
   259
EXPORT_C int OCSP_single_get0_status(OCSP_SINGLERESP *single, int *reason,
sl@0
   260
				ASN1_GENERALIZEDTIME **revtime,
sl@0
   261
				ASN1_GENERALIZEDTIME **thisupd,
sl@0
   262
				ASN1_GENERALIZEDTIME **nextupd)
sl@0
   263
	{
sl@0
   264
	int ret;
sl@0
   265
	OCSP_CERTSTATUS *cst;
sl@0
   266
	if(!single) return -1;
sl@0
   267
	cst = single->certStatus;
sl@0
   268
	ret = cst->type;
sl@0
   269
	if (ret == V_OCSP_CERTSTATUS_REVOKED)
sl@0
   270
		{
sl@0
   271
		OCSP_REVOKEDINFO *rev = cst->value.revoked;
sl@0
   272
		if (revtime) *revtime = rev->revocationTime;
sl@0
   273
		if (reason) 
sl@0
   274
			{
sl@0
   275
			if(rev->revocationReason)
sl@0
   276
				*reason = ASN1_ENUMERATED_get(rev->revocationReason);
sl@0
   277
			else *reason = -1;
sl@0
   278
			}
sl@0
   279
		}
sl@0
   280
	if(thisupd) *thisupd = single->thisUpdate;
sl@0
   281
	if(nextupd) *nextupd = single->nextUpdate;
sl@0
   282
	return ret;
sl@0
   283
	}
sl@0
   284
sl@0
   285
/* This function combines the previous ones: look up a certificate ID and
sl@0
   286
 * if found extract status information. Return 0 is successful.
sl@0
   287
 */
sl@0
   288
sl@0
   289
EXPORT_C int OCSP_resp_find_status(OCSP_BASICRESP *bs, OCSP_CERTID *id, int *status,
sl@0
   290
				int *reason,
sl@0
   291
				ASN1_GENERALIZEDTIME **revtime,
sl@0
   292
				ASN1_GENERALIZEDTIME **thisupd,
sl@0
   293
				ASN1_GENERALIZEDTIME **nextupd)
sl@0
   294
	{
sl@0
   295
	int i;
sl@0
   296
	OCSP_SINGLERESP *single;
sl@0
   297
	i = OCSP_resp_find(bs, id, -1);
sl@0
   298
	/* Maybe check for multiple responses and give an error? */
sl@0
   299
	if(i < 0) return 0;
sl@0
   300
	single = OCSP_resp_get0(bs, i);
sl@0
   301
	i = OCSP_single_get0_status(single, reason, revtime, thisupd, nextupd);
sl@0
   302
	if(status) *status = i;
sl@0
   303
	return 1;
sl@0
   304
	}
sl@0
   305
sl@0
   306
/* Check validity of thisUpdate and nextUpdate fields. It is possible that the request will
sl@0
   307
 * take a few seconds to process and/or the time wont be totally accurate. Therefore to avoid
sl@0
   308
 * rejecting otherwise valid time we allow the times to be within 'nsec' of the current time.
sl@0
   309
 * Also to avoid accepting very old responses without a nextUpdate field an optional maxage
sl@0
   310
 * parameter specifies the maximum age the thisUpdate field can be.
sl@0
   311
 */
sl@0
   312
sl@0
   313
EXPORT_C int OCSP_check_validity(ASN1_GENERALIZEDTIME *thisupd, ASN1_GENERALIZEDTIME *nextupd, long nsec, long maxsec)
sl@0
   314
	{
sl@0
   315
	int ret = 1;
sl@0
   316
	time_t t_now, t_tmp;
sl@0
   317
	time(&t_now);
sl@0
   318
	/* Check thisUpdate is valid and not more than nsec in the future */
sl@0
   319
	if (!ASN1_GENERALIZEDTIME_check(thisupd))
sl@0
   320
		{
sl@0
   321
		OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_THISUPDATE_FIELD);
sl@0
   322
		ret = 0;
sl@0
   323
		}
sl@0
   324
	else 
sl@0
   325
		{
sl@0
   326
			t_tmp = t_now + nsec;
sl@0
   327
			if (X509_cmp_time(thisupd, &t_tmp) > 0)
sl@0
   328
			{
sl@0
   329
			OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_NOT_YET_VALID);
sl@0
   330
			ret = 0;
sl@0
   331
			}
sl@0
   332
sl@0
   333
		/* If maxsec specified check thisUpdate is not more than maxsec in the past */
sl@0
   334
		if (maxsec >= 0)
sl@0
   335
			{
sl@0
   336
			t_tmp = t_now - maxsec;
sl@0
   337
			if (X509_cmp_time(thisupd, &t_tmp) < 0)
sl@0
   338
				{
sl@0
   339
				OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_TOO_OLD);
sl@0
   340
				ret = 0;
sl@0
   341
				}
sl@0
   342
			}
sl@0
   343
		}
sl@0
   344
		
sl@0
   345
sl@0
   346
	if (!nextupd) return ret;
sl@0
   347
sl@0
   348
	/* Check nextUpdate is valid and not more than nsec in the past */
sl@0
   349
	if (!ASN1_GENERALIZEDTIME_check(nextupd))
sl@0
   350
		{
sl@0
   351
		OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_ERROR_IN_NEXTUPDATE_FIELD);
sl@0
   352
		ret = 0;
sl@0
   353
		}
sl@0
   354
	else 
sl@0
   355
		{
sl@0
   356
		t_tmp = t_now - nsec;
sl@0
   357
		if (X509_cmp_time(nextupd, &t_tmp) < 0)
sl@0
   358
			{
sl@0
   359
			OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_STATUS_EXPIRED);
sl@0
   360
			ret = 0;
sl@0
   361
			}
sl@0
   362
		}
sl@0
   363
sl@0
   364
	/* Also don't allow nextUpdate to precede thisUpdate */
sl@0
   365
	if (ASN1_STRING_cmp(nextupd, thisupd) < 0)
sl@0
   366
		{
sl@0
   367
		OCSPerr(OCSP_F_OCSP_CHECK_VALIDITY, OCSP_R_NEXTUPDATE_BEFORE_THISUPDATE);
sl@0
   368
		ret = 0;
sl@0
   369
		}
sl@0
   370
sl@0
   371
	return ret;
sl@0
   372
	}