os/ossrv/ssl/libcrypto/src/crypto/x509v3/v3_ocsp.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* v3_ocsp.c */
sl@0
     2
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
sl@0
     3
 * project 1999.
sl@0
     4
 */
sl@0
     5
/* ====================================================================
sl@0
     6
 * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
sl@0
     7
 *
sl@0
     8
 * Redistribution and use in source and binary forms, with or without
sl@0
     9
 * modification, are permitted provided that the following conditions
sl@0
    10
 * are met:
sl@0
    11
 *
sl@0
    12
 * 1. Redistributions of source code must retain the above copyright
sl@0
    13
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    14
 *
sl@0
    15
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    16
 *    notice, this list of conditions and the following disclaimer in
sl@0
    17
 *    the documentation and/or other materials provided with the
sl@0
    18
 *    distribution.
sl@0
    19
 *
sl@0
    20
 * 3. All advertising materials mentioning features or use of this
sl@0
    21
 *    software must display the following acknowledgment:
sl@0
    22
 *    "This product includes software developed by the OpenSSL Project
sl@0
    23
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
sl@0
    24
 *
sl@0
    25
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    26
 *    endorse or promote products derived from this software without
sl@0
    27
 *    prior written permission. For written permission, please contact
sl@0
    28
 *    licensing@OpenSSL.org.
sl@0
    29
 *
sl@0
    30
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    31
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    32
 *    permission of the OpenSSL Project.
sl@0
    33
 *
sl@0
    34
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    35
 *    acknowledgment:
sl@0
    36
 *    "This product includes software developed by the OpenSSL Project
sl@0
    37
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
sl@0
    38
 *
sl@0
    39
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    40
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    42
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    43
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    48
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
    49
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
    50
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
    51
 * ====================================================================
sl@0
    52
 *
sl@0
    53
 * This product includes cryptographic software written by Eric Young
sl@0
    54
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
    55
 * Hudson (tjh@cryptsoft.com).
sl@0
    56
 *
sl@0
    57
 */
sl@0
    58
 /*
sl@0
    59
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    60
 */
sl@0
    61
sl@0
    62
#ifndef OPENSSL_NO_OCSP
sl@0
    63
sl@0
    64
#include <stdio.h>
sl@0
    65
#include "cryptlib.h"
sl@0
    66
#include <openssl/conf.h>
sl@0
    67
#include <openssl/asn1.h>
sl@0
    68
#include <openssl/ocsp.h>
sl@0
    69
#include <openssl/x509v3.h>
sl@0
    70
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
    71
#include "libcrypto_wsd_macros.h"
sl@0
    72
#include "libcrypto_wsd.h"
sl@0
    73
#endif
sl@0
    74
sl@0
    75
/* OCSP extensions and a couple of CRL entry extensions
sl@0
    76
 */
sl@0
    77
sl@0
    78
static int i2r_ocsp_crlid(X509V3_EXT_METHOD *method, void *nonce, BIO *out, int indent);
sl@0
    79
static int i2r_ocsp_acutoff(X509V3_EXT_METHOD *method, void *nonce, BIO *out, int indent);
sl@0
    80
static int i2r_object(X509V3_EXT_METHOD *method, void *obj, BIO *out, int indent);
sl@0
    81
sl@0
    82
static void *ocsp_nonce_new(void);
sl@0
    83
static int i2d_ocsp_nonce(void *a, unsigned char **pp);
sl@0
    84
static void *d2i_ocsp_nonce(void *a, const unsigned char **pp, long length);
sl@0
    85
static void ocsp_nonce_free(void *a);
sl@0
    86
static int i2r_ocsp_nonce(X509V3_EXT_METHOD *method, void *nonce, BIO *out, int indent);
sl@0
    87
sl@0
    88
static int i2r_ocsp_nocheck(X509V3_EXT_METHOD *method, void *nocheck, BIO *out, int indent);
sl@0
    89
static void *s2i_ocsp_nocheck(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, const char *str);
sl@0
    90
static int i2r_ocsp_serviceloc(X509V3_EXT_METHOD *method, void *in, BIO *bp, int ind);
sl@0
    91
sl@0
    92
#ifndef EMULATOR
sl@0
    93
X509V3_EXT_METHOD v3_ocsp_crlid = {
sl@0
    94
	NID_id_pkix_OCSP_CrlID, 0, ASN1_ITEM_ref(OCSP_CRLID),
sl@0
    95
	0,0,0,0,
sl@0
    96
	0,0,
sl@0
    97
	0,0,
sl@0
    98
	i2r_ocsp_crlid,0,
sl@0
    99
	NULL
sl@0
   100
};
sl@0
   101
sl@0
   102
X509V3_EXT_METHOD v3_ocsp_acutoff = {
sl@0
   103
	NID_id_pkix_OCSP_archiveCutoff, 0, ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
sl@0
   104
	0,0,0,0,
sl@0
   105
	0,0,
sl@0
   106
	0,0,
sl@0
   107
	i2r_ocsp_acutoff,0,
sl@0
   108
	NULL
sl@0
   109
};
sl@0
   110
sl@0
   111
X509V3_EXT_METHOD v3_crl_invdate = {
sl@0
   112
	NID_invalidity_date, 0, ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
sl@0
   113
	0,0,0,0,
sl@0
   114
	0,0,
sl@0
   115
	0,0,
sl@0
   116
	i2r_ocsp_acutoff,0,
sl@0
   117
	NULL
sl@0
   118
};
sl@0
   119
sl@0
   120
X509V3_EXT_METHOD v3_crl_hold = {
sl@0
   121
	NID_hold_instruction_code, 0, ASN1_ITEM_ref(ASN1_OBJECT),
sl@0
   122
	0,0,0,0,
sl@0
   123
	0,0,
sl@0
   124
	0,0,
sl@0
   125
	i2r_object,0,
sl@0
   126
	NULL
sl@0
   127
};
sl@0
   128
sl@0
   129
X509V3_EXT_METHOD v3_ocsp_nonce = {
sl@0
   130
	NID_id_pkix_OCSP_Nonce, 0, NULL,
sl@0
   131
	ocsp_nonce_new,
sl@0
   132
	ocsp_nonce_free,
sl@0
   133
	d2i_ocsp_nonce,
sl@0
   134
	i2d_ocsp_nonce,
sl@0
   135
	0,0,
sl@0
   136
	0,0,
sl@0
   137
	i2r_ocsp_nonce,0,
sl@0
   138
	NULL
sl@0
   139
};
sl@0
   140
sl@0
   141
X509V3_EXT_METHOD v3_ocsp_nocheck = {
sl@0
   142
	NID_id_pkix_OCSP_noCheck, 0, ASN1_ITEM_ref(ASN1_NULL),
sl@0
   143
	0,0,0,0,
sl@0
   144
	0,s2i_ocsp_nocheck,
sl@0
   145
	0,0,
sl@0
   146
	i2r_ocsp_nocheck,0,
sl@0
   147
	NULL
sl@0
   148
};
sl@0
   149
sl@0
   150
X509V3_EXT_METHOD v3_ocsp_serviceloc = {
sl@0
   151
	NID_id_pkix_OCSP_serviceLocator, 0, ASN1_ITEM_ref(OCSP_SERVICELOC),
sl@0
   152
	0,0,0,0,
sl@0
   153
	0,0,
sl@0
   154
	0,0,
sl@0
   155
	i2r_ocsp_serviceloc,0,
sl@0
   156
	NULL
sl@0
   157
};
sl@0
   158
#else
sl@0
   159
const X509V3_EXT_METHOD v3_ocsp_crlid = {
sl@0
   160
	NID_id_pkix_OCSP_CrlID, 0, ASN1_ITEM_ref(OCSP_CRLID),
sl@0
   161
	0,0,0,0,
sl@0
   162
	0,0,
sl@0
   163
	0,0,
sl@0
   164
	i2r_ocsp_crlid,0,
sl@0
   165
	NULL
sl@0
   166
};
sl@0
   167
sl@0
   168
const X509V3_EXT_METHOD v3_ocsp_acutoff = {
sl@0
   169
	NID_id_pkix_OCSP_archiveCutoff, 0, ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
sl@0
   170
	0,0,0,0,
sl@0
   171
	0,0,
sl@0
   172
	0,0,
sl@0
   173
	i2r_ocsp_acutoff,0,
sl@0
   174
	NULL
sl@0
   175
};
sl@0
   176
sl@0
   177
const X509V3_EXT_METHOD v3_crl_invdate = {
sl@0
   178
	NID_invalidity_date, 0, ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
sl@0
   179
	0,0,0,0,
sl@0
   180
	0,0,
sl@0
   181
	0,0,
sl@0
   182
	i2r_ocsp_acutoff,0,
sl@0
   183
	NULL
sl@0
   184
};
sl@0
   185
sl@0
   186
const X509V3_EXT_METHOD v3_crl_hold = {
sl@0
   187
	NID_hold_instruction_code, 0, ASN1_ITEM_ref(ASN1_OBJECT),
sl@0
   188
	0,0,0,0,
sl@0
   189
	0,0,
sl@0
   190
	0,0,
sl@0
   191
	i2r_object,0,
sl@0
   192
	NULL
sl@0
   193
};
sl@0
   194
sl@0
   195
const X509V3_EXT_METHOD v3_ocsp_nonce = {
sl@0
   196
	NID_id_pkix_OCSP_Nonce, 0, NULL,
sl@0
   197
	ocsp_nonce_new,
sl@0
   198
	ocsp_nonce_free,
sl@0
   199
	d2i_ocsp_nonce,
sl@0
   200
	i2d_ocsp_nonce,
sl@0
   201
	0,0,
sl@0
   202
	0,0,
sl@0
   203
	i2r_ocsp_nonce,0,
sl@0
   204
	NULL
sl@0
   205
};
sl@0
   206
sl@0
   207
const X509V3_EXT_METHOD v3_ocsp_nocheck = {
sl@0
   208
	NID_id_pkix_OCSP_noCheck, 0, ASN1_ITEM_ref(ASN1_NULL),
sl@0
   209
	0,0,0,0,
sl@0
   210
	0,s2i_ocsp_nocheck,
sl@0
   211
	0,0,
sl@0
   212
	i2r_ocsp_nocheck,0,
sl@0
   213
	NULL
sl@0
   214
};
sl@0
   215
sl@0
   216
const X509V3_EXT_METHOD v3_ocsp_serviceloc = {
sl@0
   217
	NID_id_pkix_OCSP_serviceLocator, 0, ASN1_ITEM_ref(OCSP_SERVICELOC),
sl@0
   218
	0,0,0,0,
sl@0
   219
	0,0,
sl@0
   220
	0,0,
sl@0
   221
	i2r_ocsp_serviceloc,0,
sl@0
   222
	NULL
sl@0
   223
};
sl@0
   224
sl@0
   225
#endif
sl@0
   226
static int i2r_ocsp_crlid(X509V3_EXT_METHOD *method, void *in, BIO *bp, int ind)
sl@0
   227
{
sl@0
   228
	OCSP_CRLID *a = in;
sl@0
   229
	if (a->crlUrl)
sl@0
   230
	        {
sl@0
   231
		if (!BIO_printf(bp, "%*scrlUrl: ", ind, "")) goto err;
sl@0
   232
		if (!ASN1_STRING_print(bp, (ASN1_STRING*)a->crlUrl)) goto err;
sl@0
   233
		if (!BIO_write(bp, "\n", 1)) goto err;
sl@0
   234
		}
sl@0
   235
	if (a->crlNum)
sl@0
   236
	        {
sl@0
   237
		if (!BIO_printf(bp, "%*scrlNum: ", ind, "")) goto err;
sl@0
   238
		if (!i2a_ASN1_INTEGER(bp, a->crlNum)) goto err;
sl@0
   239
		if (!BIO_write(bp, "\n", 1)) goto err;
sl@0
   240
		}
sl@0
   241
	if (a->crlTime)
sl@0
   242
	        {
sl@0
   243
		if (!BIO_printf(bp, "%*scrlTime: ", ind, "")) goto err;
sl@0
   244
		if (!ASN1_GENERALIZEDTIME_print(bp, a->crlTime)) goto err;
sl@0
   245
		if (!BIO_write(bp, "\n", 1)) goto err;
sl@0
   246
		}
sl@0
   247
	return 1;
sl@0
   248
	err:
sl@0
   249
	return 0;
sl@0
   250
}
sl@0
   251
sl@0
   252
static int i2r_ocsp_acutoff(X509V3_EXT_METHOD *method, void *cutoff, BIO *bp, int ind)
sl@0
   253
{
sl@0
   254
	if (!BIO_printf(bp, "%*s", ind, "")) return 0;
sl@0
   255
	if(!ASN1_GENERALIZEDTIME_print(bp, cutoff)) return 0;
sl@0
   256
	return 1;
sl@0
   257
}
sl@0
   258
sl@0
   259
sl@0
   260
static int i2r_object(X509V3_EXT_METHOD *method, void *oid, BIO *bp, int ind)
sl@0
   261
{
sl@0
   262
	if (!BIO_printf(bp, "%*s", ind, "")) return 0;
sl@0
   263
	if(!i2a_ASN1_OBJECT(bp, oid)) return 0;
sl@0
   264
	return 1;
sl@0
   265
}
sl@0
   266
sl@0
   267
/* OCSP nonce. This is needs special treatment because it doesn't have
sl@0
   268
 * an ASN1 encoding at all: it just contains arbitrary data.
sl@0
   269
 */
sl@0
   270
sl@0
   271
static void *ocsp_nonce_new(void)
sl@0
   272
{
sl@0
   273
	return ASN1_OCTET_STRING_new();
sl@0
   274
}
sl@0
   275
sl@0
   276
static int i2d_ocsp_nonce(void *a, unsigned char **pp)
sl@0
   277
{
sl@0
   278
	ASN1_OCTET_STRING *os = a;
sl@0
   279
	if(pp) {
sl@0
   280
		memcpy(*pp, os->data, os->length);
sl@0
   281
		*pp += os->length;
sl@0
   282
	}
sl@0
   283
	return os->length;
sl@0
   284
}
sl@0
   285
sl@0
   286
static void *d2i_ocsp_nonce(void *a, const unsigned char **pp, long length)
sl@0
   287
{
sl@0
   288
	ASN1_OCTET_STRING *os, **pos;
sl@0
   289
	pos = a;
sl@0
   290
	if(!pos || !*pos) os = ASN1_OCTET_STRING_new();
sl@0
   291
	else os = *pos;
sl@0
   292
	if(!ASN1_OCTET_STRING_set(os, *pp, length)) goto err;
sl@0
   293
sl@0
   294
	*pp += length;
sl@0
   295
sl@0
   296
	if(pos) *pos = os;
sl@0
   297
	return os;
sl@0
   298
sl@0
   299
	err:
sl@0
   300
	if(os && (!pos || (*pos != os))) M_ASN1_OCTET_STRING_free(os);
sl@0
   301
	OCSPerr(OCSP_F_D2I_OCSP_NONCE, ERR_R_MALLOC_FAILURE);
sl@0
   302
	return NULL;
sl@0
   303
}
sl@0
   304
sl@0
   305
static void ocsp_nonce_free(void *a)
sl@0
   306
{
sl@0
   307
	M_ASN1_OCTET_STRING_free(a);
sl@0
   308
}
sl@0
   309
sl@0
   310
static int i2r_ocsp_nonce(X509V3_EXT_METHOD *method, void *nonce, BIO *out, int indent)
sl@0
   311
{
sl@0
   312
	if(BIO_printf(out, "%*s", indent, "") <= 0) return 0;
sl@0
   313
	if(i2a_ASN1_STRING(out, nonce, V_ASN1_OCTET_STRING) <= 0) return 0;
sl@0
   314
	return 1;
sl@0
   315
}
sl@0
   316
sl@0
   317
/* Nocheck is just a single NULL. Don't print anything and always set it */
sl@0
   318
sl@0
   319
static int i2r_ocsp_nocheck(X509V3_EXT_METHOD *method, void *nocheck, BIO *out, int indent)
sl@0
   320
{
sl@0
   321
	return 1;
sl@0
   322
}
sl@0
   323
sl@0
   324
static void *s2i_ocsp_nocheck(X509V3_EXT_METHOD *method, X509V3_CTX *ctx, const char *str)
sl@0
   325
{
sl@0
   326
	return ASN1_NULL_new();
sl@0
   327
}
sl@0
   328
sl@0
   329
static int i2r_ocsp_serviceloc(X509V3_EXT_METHOD *method, void *in, BIO *bp, int ind)
sl@0
   330
        {
sl@0
   331
	int i;
sl@0
   332
	OCSP_SERVICELOC *a = in;
sl@0
   333
	ACCESS_DESCRIPTION *ad;
sl@0
   334
sl@0
   335
        if (BIO_printf(bp, "%*sIssuer: ", ind, "") <= 0) goto err;
sl@0
   336
        if (X509_NAME_print_ex(bp, a->issuer, 0, XN_FLAG_ONELINE) <= 0) goto err;
sl@0
   337
	for (i = 0; i < sk_ACCESS_DESCRIPTION_num(a->locator); i++)
sl@0
   338
	        {
sl@0
   339
				ad = sk_ACCESS_DESCRIPTION_value(a->locator,i);
sl@0
   340
				if (BIO_printf(bp, "\n%*s", (2*ind), "") <= 0) 
sl@0
   341
					goto err;
sl@0
   342
				if(i2a_ASN1_OBJECT(bp, ad->method) <= 0) goto err;
sl@0
   343
				if(BIO_puts(bp, " - ") <= 0) goto err;
sl@0
   344
				if(GENERAL_NAME_print(bp, ad->location) <= 0) goto err;
sl@0
   345
		}
sl@0
   346
	return 1;
sl@0
   347
err:
sl@0
   348
	return 0;
sl@0
   349
	}
sl@0
   350
#endif