155714Skris/* p12_utl.c */
2296465Sdelphij/*
3296465Sdelphij * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
4296465Sdelphij * 1999.
555714Skris */
655714Skris/* ====================================================================
755714Skris * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
855714Skris *
955714Skris * Redistribution and use in source and binary forms, with or without
1055714Skris * modification, are permitted provided that the following conditions
1155714Skris * are met:
1255714Skris *
1355714Skris * 1. Redistributions of source code must retain the above copyright
14296465Sdelphij *    notice, this list of conditions and the following disclaimer.
1555714Skris *
1655714Skris * 2. Redistributions in binary form must reproduce the above copyright
1755714Skris *    notice, this list of conditions and the following disclaimer in
1855714Skris *    the documentation and/or other materials provided with the
1955714Skris *    distribution.
2055714Skris *
2155714Skris * 3. All advertising materials mentioning features or use of this
2255714Skris *    software must display the following acknowledgment:
2355714Skris *    "This product includes software developed by the OpenSSL Project
2455714Skris *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2555714Skris *
2655714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2755714Skris *    endorse or promote products derived from this software without
2855714Skris *    prior written permission. For written permission, please contact
2955714Skris *    licensing@OpenSSL.org.
3055714Skris *
3155714Skris * 5. Products derived from this software may not be called "OpenSSL"
3255714Skris *    nor may "OpenSSL" appear in their names without prior written
3355714Skris *    permission of the OpenSSL Project.
3455714Skris *
3555714Skris * 6. Redistributions of any form whatsoever must retain the following
3655714Skris *    acknowledgment:
3755714Skris *    "This product includes software developed by the OpenSSL Project
3855714Skris *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3955714Skris *
4055714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4155714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4255714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4355714Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4455714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4555714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4655714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4755714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4955714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
5055714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
5155714Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
5255714Skris * ====================================================================
5355714Skris *
5455714Skris * This product includes cryptographic software written by Eric Young
5555714Skris * (eay@cryptsoft.com).  This product includes software written by Tim
5655714Skris * Hudson (tjh@cryptsoft.com).
5755714Skris *
5855714Skris */
5955714Skris
6055714Skris#include <stdio.h>
6155714Skris#include "cryptlib.h"
6255714Skris#include <openssl/pkcs12.h>
6355714Skris
64205128Ssimon#ifdef OPENSSL_SYS_NETWARE
65205128Ssimon/* Rename these functions to avoid name clashes on NetWare OS */
66296465Sdelphij# define uni2asc OPENSSL_uni2asc
67296465Sdelphij# define asc2uni OPENSSL_asc2uni
68205128Ssimon#endif
69205128Ssimon
7055714Skris/* Cheap and nasty Unicode stuff */
7155714Skris
72296465Sdelphijunsigned char *asc2uni(const char *asc, int asclen, unsigned char **uni,
73296465Sdelphij                       int *unilen)
7455714Skris{
75296465Sdelphij    int ulen, i;
76296465Sdelphij    unsigned char *unitmp;
77296465Sdelphij    if (asclen == -1)
78296465Sdelphij        asclen = strlen(asc);
79296465Sdelphij    ulen = asclen * 2 + 2;
80296465Sdelphij    if (!(unitmp = OPENSSL_malloc(ulen)))
81296465Sdelphij        return NULL;
82296465Sdelphij    for (i = 0; i < ulen - 2; i += 2) {
83296465Sdelphij        unitmp[i] = 0;
84296465Sdelphij        unitmp[i + 1] = asc[i >> 1];
85296465Sdelphij    }
86296465Sdelphij    /* Make result double null terminated */
87296465Sdelphij    unitmp[ulen - 2] = 0;
88296465Sdelphij    unitmp[ulen - 1] = 0;
89296465Sdelphij    if (unilen)
90296465Sdelphij        *unilen = ulen;
91296465Sdelphij    if (uni)
92296465Sdelphij        *uni = unitmp;
93296465Sdelphij    return unitmp;
9455714Skris}
9555714Skris
9672613Skrischar *uni2asc(unsigned char *uni, int unilen)
9755714Skris{
98296465Sdelphij    int asclen, i;
99296465Sdelphij    char *asctmp;
100296465Sdelphij    asclen = unilen / 2;
101296465Sdelphij    /* If no terminating zero allow for one */
102296465Sdelphij    if (!unilen || uni[unilen - 1])
103296465Sdelphij        asclen++;
104296465Sdelphij    uni++;
105296465Sdelphij    if (!(asctmp = OPENSSL_malloc(asclen)))
106296465Sdelphij        return NULL;
107296465Sdelphij    for (i = 0; i < unilen; i += 2)
108296465Sdelphij        asctmp[i >> 1] = uni[i];
109296465Sdelphij    asctmp[asclen - 1] = 0;
110296465Sdelphij    return asctmp;
11155714Skris}
11255714Skris
11355714Skrisint i2d_PKCS12_bio(BIO *bp, PKCS12 *p12)
11455714Skris{
115296465Sdelphij    return ASN1_item_i2d_bio(ASN1_ITEM_rptr(PKCS12), bp, p12);
11655714Skris}
11755714Skris
118109998Smarkm#ifndef OPENSSL_NO_FP_API
11955714Skrisint i2d_PKCS12_fp(FILE *fp, PKCS12 *p12)
12055714Skris{
121296465Sdelphij    return ASN1_item_i2d_fp(ASN1_ITEM_rptr(PKCS12), fp, p12);
12255714Skris}
12355714Skris#endif
12455714Skris
12555714SkrisPKCS12 *d2i_PKCS12_bio(BIO *bp, PKCS12 **p12)
12655714Skris{
127296465Sdelphij    return ASN1_item_d2i_bio(ASN1_ITEM_rptr(PKCS12), bp, p12);
12855714Skris}
129296465Sdelphij
130109998Smarkm#ifndef OPENSSL_NO_FP_API
13155714SkrisPKCS12 *d2i_PKCS12_fp(FILE *fp, PKCS12 **p12)
13255714Skris{
133296465Sdelphij    return ASN1_item_d2i_fp(ASN1_ITEM_rptr(PKCS12), fp, p12);
13455714Skris}
13555714Skris#endif
13655714Skris
137109998SmarkmPKCS12_SAFEBAG *PKCS12_x5092certbag(X509 *x509)
138109998Smarkm{
139296465Sdelphij    return PKCS12_item_pack_safebag(x509, ASN1_ITEM_rptr(X509),
140296465Sdelphij                                    NID_x509Certificate, NID_certBag);
141109998Smarkm}
142109998Smarkm
143109998SmarkmPKCS12_SAFEBAG *PKCS12_x509crl2certbag(X509_CRL *crl)
144109998Smarkm{
145296465Sdelphij    return PKCS12_item_pack_safebag(crl, ASN1_ITEM_rptr(X509_CRL),
146296465Sdelphij                                    NID_x509Crl, NID_crlBag);
147109998Smarkm}
148109998Smarkm
149109998SmarkmX509 *PKCS12_certbag2x509(PKCS12_SAFEBAG *bag)
150109998Smarkm{
151296465Sdelphij    if (M_PKCS12_bag_type(bag) != NID_certBag)
152296465Sdelphij        return NULL;
153296465Sdelphij    if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate)
154296465Sdelphij        return NULL;
155296465Sdelphij    return ASN1_item_unpack(bag->value.bag->value.octet,
156296465Sdelphij                            ASN1_ITEM_rptr(X509));
157109998Smarkm}
158109998Smarkm
159109998SmarkmX509_CRL *PKCS12_certbag2x509crl(PKCS12_SAFEBAG *bag)
160109998Smarkm{
161296465Sdelphij    if (M_PKCS12_bag_type(bag) != NID_crlBag)
162296465Sdelphij        return NULL;
163296465Sdelphij    if (M_PKCS12_cert_bag_type(bag) != NID_x509Crl)
164296465Sdelphij        return NULL;
165296465Sdelphij    return ASN1_item_unpack(bag->value.bag->value.octet,
166296465Sdelphij                            ASN1_ITEM_rptr(X509_CRL));
167109998Smarkm}
168