p12_utl.c revision 256281
1193323Sed/* p12_utl.c */
2193323Sed/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3193323Sed * project 1999.
4193323Sed */
5193323Sed/* ====================================================================
6193323Sed * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7193323Sed *
8193323Sed * Redistribution and use in source and binary forms, with or without
9193323Sed * modification, are permitted provided that the following conditions
10193323Sed * are met:
11193323Sed *
12193323Sed * 1. Redistributions of source code must retain the above copyright
13193323Sed *    notice, this list of conditions and the following disclaimer.
14193323Sed *
15193323Sed * 2. Redistributions in binary form must reproduce the above copyright
16193323Sed *    notice, this list of conditions and the following disclaimer in
17193323Sed *    the documentation and/or other materials provided with the
18234353Sdim *    distribution.
19234353Sdim *
20193323Sed * 3. All advertising materials mentioning features or use of this
21207618Srdivacky *    software must display the following acknowledgment:
22234353Sdim *    "This product includes software developed by the OpenSSL Project
23207618Srdivacky *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24198090Srdivacky *
25198090Srdivacky * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26198090Srdivacky *    endorse or promote products derived from this software without
27249423Sdim *    prior written permission. For written permission, please contact
28198090Srdivacky *    licensing@OpenSSL.org.
29198090Srdivacky *
30198090Srdivacky * 5. Products derived from this software may not be called "OpenSSL"
31198090Srdivacky *    nor may "OpenSSL" appear in their names without prior written
32193323Sed *    permission of the OpenSSL Project.
33239462Sdim *
34239462Sdim * 6. Redistributions of any form whatsoever must retain the following
35239462Sdim *    acknowledgment:
36239462Sdim *    "This product includes software developed by the OpenSSL Project
37239462Sdim *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38239462Sdim *
39239462Sdim * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40239462Sdim * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41234353Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42234353Sdim * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43234353Sdim * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44234353Sdim * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45263508Sdim * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46239462Sdim * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47234353Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48263508Sdim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49234353Sdim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50234353Sdim * OF THE POSSIBILITY OF SUCH DAMAGE.
51263508Sdim * ====================================================================
52249423Sdim *
53249423Sdim * This product includes cryptographic software written by Eric Young
54249423Sdim * (eay@cryptsoft.com).  This product includes software written by Tim
55234353Sdim * Hudson (tjh@cryptsoft.com).
56193323Sed *
57234353Sdim */
58234353Sdim
59234353Sdim#include <stdio.h>
60234353Sdim#include "cryptlib.h"
61207618Srdivacky#include <openssl/pkcs12.h>
62234353Sdim
63234353Sdim/* Cheap and nasty Unicode stuff */
64234353Sdim
65234353Sdimunsigned char *OPENSSL_asc2uni(const char *asc, int asclen, unsigned char **uni, int *unilen)
66207618Srdivacky{
67234353Sdim	int ulen, i;
68234353Sdim	unsigned char *unitmp;
69234353Sdim	if (asclen == -1) asclen = strlen(asc);
70193323Sed	ulen = asclen*2  + 2;
71234353Sdim	if (!(unitmp = OPENSSL_malloc(ulen))) return NULL;
72234353Sdim	for (i = 0; i < ulen - 2; i+=2) {
73234353Sdim		unitmp[i] = 0;
74234353Sdim		unitmp[i + 1] = asc[i>>1];
75234353Sdim	}
76234353Sdim	/* Make result double null terminated */
77234353Sdim	unitmp[ulen - 2] = 0;
78193323Sed	unitmp[ulen - 1] = 0;
79234353Sdim	if (unilen) *unilen = ulen;
80234353Sdim	if (uni) *uni = unitmp;
81234353Sdim	return unitmp;
82234353Sdim}
83234353Sdim
84234353Sdimchar *OPENSSL_uni2asc(unsigned char *uni, int unilen)
85234353Sdim{
86212904Sdim	int asclen, i;
87234353Sdim	char *asctmp;
88234353Sdim	asclen = unilen / 2;
89234353Sdim	/* If no terminating zero allow for one */
90234353Sdim	if (!unilen || uni[unilen - 1]) asclen++;
91234353Sdim	uni++;
92193323Sed	if (!(asctmp = OPENSSL_malloc(asclen))) return NULL;
93234353Sdim	for (i = 0; i < unilen; i+=2) asctmp[i>>1] = uni[i];
94234353Sdim	asctmp[asclen - 1] = 0;
95234353Sdim	return asctmp;
96234353Sdim}
97234353Sdim
98194178Sedint i2d_PKCS12_bio(BIO *bp, PKCS12 *p12)
99234353Sdim{
100234353Sdim	return ASN1_item_i2d_bio(ASN1_ITEM_rptr(PKCS12), bp, p12);
101234353Sdim}
102234353Sdim
103234353Sdim#ifndef OPENSSL_NO_FP_API
104234353Sdimint i2d_PKCS12_fp(FILE *fp, PKCS12 *p12)
105234353Sdim{
106234353Sdim	return ASN1_item_i2d_fp(ASN1_ITEM_rptr(PKCS12), fp, p12);
107234353Sdim}
108193323Sed#endif
109234353Sdim
110234353SdimPKCS12 *d2i_PKCS12_bio(BIO *bp, PKCS12 **p12)
111234353Sdim{
112234353Sdim	return ASN1_item_d2i_bio(ASN1_ITEM_rptr(PKCS12), bp, p12);
113234353Sdim}
114198090Srdivacky#ifndef OPENSSL_NO_FP_API
115234353SdimPKCS12 *d2i_PKCS12_fp(FILE *fp, PKCS12 **p12)
116234353Sdim{
117234353Sdim        return ASN1_item_d2i_fp(ASN1_ITEM_rptr(PKCS12), fp, p12);
118234353Sdim}
119198090Srdivacky#endif
120234353Sdim
121234353SdimPKCS12_SAFEBAG *PKCS12_x5092certbag(X509 *x509)
122234353Sdim{
123193323Sed	return PKCS12_item_pack_safebag(x509, ASN1_ITEM_rptr(X509),
124234353Sdim			NID_x509Certificate, NID_certBag);
125234353Sdim}
126234353Sdim
127234353SdimPKCS12_SAFEBAG *PKCS12_x509crl2certbag(X509_CRL *crl)
128193323Sed{
129234353Sdim	return PKCS12_item_pack_safebag(crl, ASN1_ITEM_rptr(X509_CRL),
130234353Sdim			NID_x509Crl, NID_crlBag);
131234353Sdim}
132234353Sdim
133234353SdimX509 *PKCS12_certbag2x509(PKCS12_SAFEBAG *bag)
134234353Sdim{
135234353Sdim	if(M_PKCS12_bag_type(bag) != NID_certBag) return NULL;
136193323Sed	if(M_PKCS12_cert_bag_type(bag) != NID_x509Certificate) return NULL;
137234353Sdim	return ASN1_item_unpack(bag->value.bag->value.octet, ASN1_ITEM_rptr(X509));
138234353Sdim}
139234353Sdim
140193323SedX509_CRL *PKCS12_certbag2x509crl(PKCS12_SAFEBAG *bag)
141234353Sdim{
142234353Sdim	if(M_PKCS12_bag_type(bag) != NID_crlBag) return NULL;
143193323Sed	if(M_PKCS12_cert_bag_type(bag) != NID_x509Crl) return NULL;
144234353Sdim	return ASN1_item_unpack(bag->value.bag->value.octet,
145234353Sdim							ASN1_ITEM_rptr(X509_CRL));
146234353Sdim}
147234353Sdim