os/ossrv/ssl/libcrypto/src/crypto/ocsp/ocsp_prn.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /* ocsp_prn.c */
     2 /* Written by Tom Titchener <Tom_Titchener@groove.net> for the OpenSSL
     3  * project. */
     4 
     5 /* History:
     6    This file was originally part of ocsp.c and was transfered to Richard
     7    Levitte from CertCo by Kathy Weinhold in mid-spring 2000 to be included
     8    in OpenSSL or released as a patch kit. */
     9 
    10 /* ====================================================================
    11  * Copyright (c) 1998-2000 The OpenSSL Project.  All rights reserved.
    12  *
    13  * Redistribution and use in source and binary forms, with or without
    14  * modification, are permitted provided that the following conditions
    15  * are met:
    16  *
    17  * 1. Redistributions of source code must retain the above copyright
    18  *    notice, this list of conditions and the following disclaimer. 
    19  *
    20  * 2. Redistributions in binary form must reproduce the above copyright
    21  *    notice, this list of conditions and the following disclaimer in
    22  *    the documentation and/or other materials provided with the
    23  *    distribution.
    24  *
    25  * 3. All advertising materials mentioning features or use of this
    26  *    software must display the following acknowledgment:
    27  *    "This product includes software developed by the OpenSSL Project
    28  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
    29  *
    30  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
    31  *    endorse or promote products derived from this software without
    32  *    prior written permission. For written permission, please contact
    33  *    openssl-core@openssl.org.
    34  *
    35  * 5. Products derived from this software may not be called "OpenSSL"
    36  *    nor may "OpenSSL" appear in their names without prior written
    37  *    permission of the OpenSSL Project.
    38  *
    39  * 6. Redistributions of any form whatsoever must retain the following
    40  *    acknowledgment:
    41  *    "This product includes software developed by the OpenSSL Project
    42  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
    43  *
    44  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
    45  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    46  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    47  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
    48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    50  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    51  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    52  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    53  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    54  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    55  * OF THE POSSIBILITY OF SUCH DAMAGE.
    56  * ====================================================================
    57  *
    58  * This product includes cryptographic software written by Eric Young
    59  * (eay@cryptsoft.com).  This product includes software written by Tim
    60  * Hudson (tjh@cryptsoft.com).
    61  *
    62  */
    63 /*
    64  © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
    65  */
    66 
    67 #include <openssl/bio.h>
    68 #include <openssl/err.h>
    69 #include <openssl/ocsp.h>
    70 #include <openssl/pem.h>
    71 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
    72 #include "libcrypto_wsd_macros.h"
    73 #include "libcrypto_wsd.h"
    74 #endif
    75 
    76 static int ocsp_certid_print(BIO *bp, OCSP_CERTID* a, int indent)
    77         {
    78 	BIO_printf(bp, "%*sCertificate ID:\n", indent, "");
    79 	indent += 2;
    80 	BIO_printf(bp, "%*sHash Algorithm: ", indent, "");
    81 	i2a_ASN1_OBJECT(bp, a->hashAlgorithm->algorithm);
    82 	BIO_printf(bp, "\n%*sIssuer Name Hash: ", indent, "");
    83 	i2a_ASN1_STRING(bp, a->issuerNameHash, V_ASN1_OCTET_STRING);
    84 	BIO_printf(bp, "\n%*sIssuer Key Hash: ", indent, "");
    85 	i2a_ASN1_STRING(bp, a->issuerKeyHash, V_ASN1_OCTET_STRING);
    86 	BIO_printf(bp, "\n%*sSerial Number: ", indent, "");
    87 	i2a_ASN1_INTEGER(bp, a->serialNumber);
    88 	BIO_printf(bp, "\n");
    89 	return 1;
    90 	}
    91 
    92 typedef struct
    93 	{
    94 	long t;
    95 	char *m;
    96 	} OCSP_TBLSTR;
    97 
    98 static char *table2string(long s, OCSP_TBLSTR *ts, int len)
    99 {
   100 	OCSP_TBLSTR *p;
   101 	for (p=ts; p < ts + len; p++)
   102 	        if (p->t == s)
   103 		         return p->m;
   104 	return "(UNKNOWN)";
   105 }
   106 
   107 
   108 EXPORT_C char *OCSP_response_status_str(long s)
   109         {
   110 #ifndef EMULATOR        
   111 	static OCSP_TBLSTR rstat_tbl[] = {
   112 	        { OCSP_RESPONSE_STATUS_SUCCESSFUL, "successful" },
   113 	        { OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, "malformedrequest" },
   114 	        { OCSP_RESPONSE_STATUS_INTERNALERROR, "internalerror" },
   115 	        { OCSP_RESPONSE_STATUS_TRYLATER, "trylater" },
   116 	        { OCSP_RESPONSE_STATUS_SIGREQUIRED, "sigrequired" },
   117 	        { OCSP_RESPONSE_STATUS_UNAUTHORIZED, "unauthorized" } };
   118 #else //EMULATOR
   119 	OCSP_TBLSTR rstat_tbl[] = {
   120 	        { OCSP_RESPONSE_STATUS_SUCCESSFUL, "successful" },
   121 	        { OCSP_RESPONSE_STATUS_MALFORMEDREQUEST, "malformedrequest" },
   122 	        { OCSP_RESPONSE_STATUS_INTERNALERROR, "internalerror" },
   123 	        { OCSP_RESPONSE_STATUS_TRYLATER, "trylater" },
   124 	        { OCSP_RESPONSE_STATUS_SIGREQUIRED, "sigrequired" },
   125 	        { OCSP_RESPONSE_STATUS_UNAUTHORIZED, "unauthorized" } };
   126 	        
   127 #endif	        
   128 
   129 	return table2string(s, rstat_tbl, 6);
   130 	} 
   131 
   132 EXPORT_C char *OCSP_cert_status_str(long s)
   133         {
   134 #ifndef EMULATOR                
   135 	static OCSP_TBLSTR cstat_tbl[] = {
   136 	        { V_OCSP_CERTSTATUS_GOOD, "good" },
   137 	        { V_OCSP_CERTSTATUS_REVOKED, "revoked" },
   138 	        { V_OCSP_CERTSTATUS_UNKNOWN, "unknown" } };
   139 #else
   140 	OCSP_TBLSTR cstat_tbl[] = {
   141 	        { V_OCSP_CERTSTATUS_GOOD, "good" },
   142 	        { V_OCSP_CERTSTATUS_REVOKED, "revoked" },
   143 	        { V_OCSP_CERTSTATUS_UNKNOWN, "unknown" } };
   144 	        
   145 #endif	        
   146 	return table2string(s, cstat_tbl, 3);
   147 	} 
   148 
   149 EXPORT_C char *OCSP_crl_reason_str(long s)
   150         {
   151 	OCSP_TBLSTR reason_tbl[] = {
   152 	  { OCSP_REVOKED_STATUS_UNSPECIFIED, "unspecified" },
   153           { OCSP_REVOKED_STATUS_KEYCOMPROMISE, "keyCompromise" },
   154           { OCSP_REVOKED_STATUS_CACOMPROMISE, "cACompromise" },
   155           { OCSP_REVOKED_STATUS_AFFILIATIONCHANGED, "affiliationChanged" },
   156           { OCSP_REVOKED_STATUS_SUPERSEDED, "superseded" },
   157           { OCSP_REVOKED_STATUS_CESSATIONOFOPERATION, "cessationOfOperation" },
   158           { OCSP_REVOKED_STATUS_CERTIFICATEHOLD, "certificateHold" },
   159           { OCSP_REVOKED_STATUS_REMOVEFROMCRL, "removeFromCRL" } };
   160 	return table2string(s, reason_tbl, 8);
   161 	} 
   162 
   163 EXPORT_C int OCSP_REQUEST_print(BIO *bp, OCSP_REQUEST* o, unsigned long flags)
   164         {
   165 	int i;
   166 	long l;
   167 	OCSP_CERTID* cid = NULL;
   168 	OCSP_ONEREQ *one = NULL;
   169 	OCSP_REQINFO *inf = o->tbsRequest;
   170 	OCSP_SIGNATURE *sig = o->optionalSignature;
   171 
   172 	if (BIO_write(bp,"OCSP Request Data:\n",19) <= 0) goto err;
   173 	l=ASN1_INTEGER_get(inf->version);
   174 	if (BIO_printf(bp,"    Version: %lu (0x%lx)",l+1,l) <= 0) goto err;
   175 	if (inf->requestorName != NULL)
   176 	        {
   177 		if (BIO_write(bp,"\n    Requestor Name: ",21) <= 0) 
   178 		        goto err;
   179 		GENERAL_NAME_print(bp, inf->requestorName);
   180 		}
   181 	if (BIO_write(bp,"\n    Requestor List:\n",21) <= 0) goto err;
   182 	for (i = 0; i < sk_OCSP_ONEREQ_num(inf->requestList); i++)
   183 	        {
   184 		one = sk_OCSP_ONEREQ_value(inf->requestList, i);
   185 		cid = one->reqCert;
   186 		ocsp_certid_print(bp, cid, 8);
   187 		if (!X509V3_extensions_print(bp,
   188 					"Request Single Extensions",
   189 					one->singleRequestExtensions, flags, 8))
   190 							goto err;
   191 		}
   192 	if (!X509V3_extensions_print(bp, "Request Extensions",
   193 			inf->requestExtensions, flags, 4))
   194 							goto err;
   195 	if (sig)
   196 	        {
   197 		X509_signature_print(bp, sig->signatureAlgorithm, sig->signature);
   198 		for (i=0; i<sk_X509_num(sig->certs); i++)
   199 			{
   200 			X509_print(bp, sk_X509_value(sig->certs,i));
   201 			PEM_write_bio_X509(bp,sk_X509_value(sig->certs,i));
   202 			}
   203 		}
   204 	return 1;
   205 err:
   206 	return 0;
   207 	}
   208 
   209 EXPORT_C int OCSP_RESPONSE_print(BIO *bp, OCSP_RESPONSE* o, unsigned long flags)
   210         {
   211 	int i, ret = 0;
   212 	long l;
   213 	unsigned char *p;
   214 	OCSP_CERTID *cid = NULL;
   215 	OCSP_BASICRESP *br = NULL;
   216 	OCSP_RESPID *rid = NULL;
   217 	OCSP_RESPDATA  *rd = NULL;
   218 	OCSP_CERTSTATUS *cst = NULL;
   219 	OCSP_REVOKEDINFO *rev = NULL;
   220 	OCSP_SINGLERESP *single = NULL;
   221 	OCSP_RESPBYTES *rb = o->responseBytes;
   222 
   223 	if (BIO_puts(bp,"OCSP Response Data:\n") <= 0) goto err;
   224 	l=ASN1_ENUMERATED_get(o->responseStatus);
   225 	if (BIO_printf(bp,"    OCSP Response Status: %s (0x%lx)\n",
   226 		       OCSP_response_status_str(l), l) <= 0) goto err;
   227 	if (rb == NULL) return 1;
   228         if (BIO_puts(bp,"    Response Type: ") <= 0)
   229 	        goto err;
   230 	if(i2a_ASN1_OBJECT(bp, rb->responseType) <= 0)
   231 	        goto err;
   232 	if (OBJ_obj2nid(rb->responseType) != NID_id_pkix_OCSP_basic) 
   233 	        {
   234 		BIO_puts(bp," (unknown response type)\n");
   235 		return 1;
   236 		}
   237 
   238 	p = ASN1_STRING_data(rb->response);
   239 	i = ASN1_STRING_length(rb->response);
   240 	if (!(br = OCSP_response_get1_basic(o))) goto err;
   241 	rd = br->tbsResponseData;
   242 	l=ASN1_INTEGER_get(rd->version);
   243 	if (BIO_printf(bp,"\n    Version: %lu (0x%lx)\n",
   244 		       l+1,l) <= 0) goto err;
   245 	if (BIO_puts(bp,"    Responder Id: ") <= 0) goto err;
   246 
   247 	rid =  rd->responderId;
   248 	switch (rid->type)
   249 		{
   250 		case V_OCSP_RESPID_NAME:
   251 		        X509_NAME_print_ex(bp, rid->value.byName, 0, XN_FLAG_ONELINE);
   252 		        break;
   253 		case V_OCSP_RESPID_KEY:
   254 		        i2a_ASN1_STRING(bp, rid->value.byKey, V_ASN1_OCTET_STRING);
   255 		        break;
   256 		}
   257 
   258 	if (BIO_printf(bp,"\n    Produced At: ")<=0) goto err;
   259 	if (!ASN1_GENERALIZEDTIME_print(bp, rd->producedAt)) goto err;
   260 	if (BIO_printf(bp,"\n    Responses:\n") <= 0) goto err;
   261 	for (i = 0; i < sk_OCSP_SINGLERESP_num(rd->responses); i++)
   262 	        {
   263 		if (! sk_OCSP_SINGLERESP_value(rd->responses, i)) continue;
   264 		single = sk_OCSP_SINGLERESP_value(rd->responses, i);
   265 		cid = single->certId;
   266 		if(ocsp_certid_print(bp, cid, 4) <= 0) goto err;
   267 		cst = single->certStatus;
   268 		if (BIO_printf(bp,"    Cert Status: %s",
   269 			       OCSP_cert_status_str(cst->type)) <= 0)
   270 		        goto err;
   271 		if (cst->type == V_OCSP_CERTSTATUS_REVOKED)
   272 		        {
   273 		        rev = cst->value.revoked;
   274 			if (BIO_printf(bp, "\n    Revocation Time: ") <= 0) 
   275 			        goto err;
   276 			if (!ASN1_GENERALIZEDTIME_print(bp, 
   277 							rev->revocationTime)) 
   278 				goto err;
   279 			if (rev->revocationReason) 
   280 			        {
   281 				l=ASN1_ENUMERATED_get(rev->revocationReason);
   282 				if (BIO_printf(bp, 
   283 					 "\n    Revocation Reason: %s (0x%lx)",
   284 					       OCSP_crl_reason_str(l), l) <= 0)
   285 				        goto err;
   286 				}
   287 			}
   288 		if (BIO_printf(bp,"\n    This Update: ") <= 0) goto err;
   289 		if (!ASN1_GENERALIZEDTIME_print(bp, single->thisUpdate)) 
   290 			goto err;
   291 		if (single->nextUpdate)
   292 		        {
   293 			if (BIO_printf(bp,"\n    Next Update: ") <= 0)goto err;
   294 			if (!ASN1_GENERALIZEDTIME_print(bp,single->nextUpdate))
   295 				goto err;
   296 			}
   297 		if (!BIO_write(bp,"\n",1)) goto err;
   298 		if (!X509V3_extensions_print(bp,
   299 					"Response Single Extensions",
   300 					single->singleExtensions, flags, 8))
   301 							goto err;
   302 		if (!BIO_write(bp,"\n",1)) goto err;
   303 		}
   304 	if (!X509V3_extensions_print(bp, "Response Extensions",
   305 					rd->responseExtensions, flags, 4))
   306 	if(X509_signature_print(bp, br->signatureAlgorithm, br->signature) <= 0)
   307 							goto err;
   308 
   309 	for (i=0; i<sk_X509_num(br->certs); i++)
   310 		{
   311 		X509_print(bp, sk_X509_value(br->certs,i));
   312 		PEM_write_bio_X509(bp,sk_X509_value(br->certs,i));
   313 		}
   314 
   315 	ret = 1;
   316 err:
   317 	OCSP_BASICRESP_free(br);
   318 	return ret;
   319 	}