os/ossrv/ssl/libcrypto/src/crypto/pkcs12/p12_utl.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/pkcs12/p12_utl.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,146 @@
     1.4 +/* p12_utl.c */
     1.5 +/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
     1.6 + * project 1999.
     1.7 + */
     1.8 +/* ====================================================================
     1.9 + * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
    1.10 + *
    1.11 + * Redistribution and use in source and binary forms, with or without
    1.12 + * modification, are permitted provided that the following conditions
    1.13 + * are met:
    1.14 + *
    1.15 + * 1. Redistributions of source code must retain the above copyright
    1.16 + *    notice, this list of conditions and the following disclaimer. 
    1.17 + *
    1.18 + * 2. Redistributions in binary form must reproduce the above copyright
    1.19 + *    notice, this list of conditions and the following disclaimer in
    1.20 + *    the documentation and/or other materials provided with the
    1.21 + *    distribution.
    1.22 + *
    1.23 + * 3. All advertising materials mentioning features or use of this
    1.24 + *    software must display the following acknowledgment:
    1.25 + *    "This product includes software developed by the OpenSSL Project
    1.26 + *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
    1.27 + *
    1.28 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
    1.29 + *    endorse or promote products derived from this software without
    1.30 + *    prior written permission. For written permission, please contact
    1.31 + *    licensing@OpenSSL.org.
    1.32 + *
    1.33 + * 5. Products derived from this software may not be called "OpenSSL"
    1.34 + *    nor may "OpenSSL" appear in their names without prior written
    1.35 + *    permission of the OpenSSL Project.
    1.36 + *
    1.37 + * 6. Redistributions of any form whatsoever must retain the following
    1.38 + *    acknowledgment:
    1.39 + *    "This product includes software developed by the OpenSSL Project
    1.40 + *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
    1.41 + *
    1.42 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
    1.43 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.44 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
    1.45 + * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
    1.46 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.47 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    1.48 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    1.49 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    1.50 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    1.51 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.52 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    1.53 + * OF THE POSSIBILITY OF SUCH DAMAGE.
    1.54 + * ====================================================================
    1.55 + *
    1.56 + * This product includes cryptographic software written by Eric Young
    1.57 + * (eay@cryptsoft.com).  This product includes software written by Tim
    1.58 + * Hudson (tjh@cryptsoft.com).
    1.59 + *
    1.60 + */
    1.61 +
    1.62 +#include <stdio.h>
    1.63 +#include "cryptlib.h"
    1.64 +#include <openssl/pkcs12.h>
    1.65 +
    1.66 +/* Cheap and nasty Unicode stuff */
    1.67 +
    1.68 +EXPORT_C unsigned char *asc2uni(const char *asc, int asclen, unsigned char **uni, int *unilen)
    1.69 +{
    1.70 +	int ulen, i;
    1.71 +	unsigned char *unitmp;
    1.72 +	if (asclen == -1) asclen = strlen(asc);
    1.73 +	ulen = asclen*2  + 2;
    1.74 +	if (!(unitmp = OPENSSL_malloc(ulen))) return NULL;
    1.75 +	for (i = 0; i < ulen - 2; i+=2) {
    1.76 +		unitmp[i] = 0;
    1.77 +		unitmp[i + 1] = asc[i>>1];
    1.78 +	}
    1.79 +	/* Make result double null terminated */
    1.80 +	unitmp[ulen - 2] = 0;
    1.81 +	unitmp[ulen - 1] = 0;
    1.82 +	if (unilen) *unilen = ulen;
    1.83 +	if (uni) *uni = unitmp;
    1.84 +	return unitmp;
    1.85 +}
    1.86 +
    1.87 +EXPORT_C char *uni2asc(unsigned char *uni, int unilen)
    1.88 +{
    1.89 +	int asclen, i;
    1.90 +	char *asctmp;
    1.91 +	asclen = unilen / 2;
    1.92 +	/* If no terminating zero allow for one */
    1.93 +	if (!unilen || uni[unilen - 1]) asclen++;
    1.94 +	uni++;
    1.95 +	if (!(asctmp = OPENSSL_malloc(asclen))) return NULL;
    1.96 +	for (i = 0; i < unilen; i+=2) asctmp[i>>1] = uni[i];
    1.97 +	asctmp[asclen - 1] = 0;
    1.98 +	return asctmp;
    1.99 +}
   1.100 +
   1.101 +EXPORT_C int i2d_PKCS12_bio(BIO *bp, PKCS12 *p12)
   1.102 +{
   1.103 +	return ASN1_item_i2d_bio(ASN1_ITEM_rptr(PKCS12), bp, p12);
   1.104 +}
   1.105 +
   1.106 +#ifndef OPENSSL_NO_FP_API
   1.107 +EXPORT_C int i2d_PKCS12_fp(FILE *fp, PKCS12 *p12)
   1.108 +{
   1.109 +	return ASN1_item_i2d_fp(ASN1_ITEM_rptr(PKCS12), fp, p12);
   1.110 +}
   1.111 +#endif
   1.112 +
   1.113 +EXPORT_C PKCS12 *d2i_PKCS12_bio(BIO *bp, PKCS12 **p12)
   1.114 +{
   1.115 +	return ASN1_item_d2i_bio(ASN1_ITEM_rptr(PKCS12), bp, p12);
   1.116 +}
   1.117 +#ifndef OPENSSL_NO_FP_API
   1.118 +EXPORT_C PKCS12 *d2i_PKCS12_fp(FILE *fp, PKCS12 **p12)
   1.119 +{
   1.120 +        return ASN1_item_d2i_fp(ASN1_ITEM_rptr(PKCS12), fp, p12);
   1.121 +}
   1.122 +#endif
   1.123 +
   1.124 +EXPORT_C PKCS12_SAFEBAG *PKCS12_x5092certbag(X509 *x509)
   1.125 +{
   1.126 +	return PKCS12_item_pack_safebag(x509, ASN1_ITEM_rptr(X509),
   1.127 +			NID_x509Certificate, NID_certBag);
   1.128 +}
   1.129 +
   1.130 +EXPORT_C PKCS12_SAFEBAG *PKCS12_x509crl2certbag(X509_CRL *crl)
   1.131 +{
   1.132 +	return PKCS12_item_pack_safebag(crl, ASN1_ITEM_rptr(X509_CRL),
   1.133 +			NID_x509Crl, NID_crlBag);
   1.134 +}
   1.135 +
   1.136 +EXPORT_C X509 *PKCS12_certbag2x509(PKCS12_SAFEBAG *bag)
   1.137 +{
   1.138 +	if(M_PKCS12_bag_type(bag) != NID_certBag) return NULL;
   1.139 +	if(M_PKCS12_cert_bag_type(bag) != NID_x509Certificate) return NULL;
   1.140 +	return ASN1_item_unpack(bag->value.bag->value.octet, ASN1_ITEM_rptr(X509));
   1.141 +}
   1.142 +
   1.143 +EXPORT_C X509_CRL *PKCS12_certbag2x509crl(PKCS12_SAFEBAG *bag)
   1.144 +{
   1.145 +	if(M_PKCS12_bag_type(bag) != NID_crlBag) return NULL;
   1.146 +	if(M_PKCS12_cert_bag_type(bag) != NID_x509Crl) return NULL;
   1.147 +	return ASN1_item_unpack(bag->value.bag->value.octet,
   1.148 +							ASN1_ITEM_rptr(X509_CRL));
   1.149 +}