155714Skris/* p12_utl.c */ 2280304Sjkim/* 3280304Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 4280304Sjkim * 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 14280304Sjkim * 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 6455714Skris/* Cheap and nasty Unicode stuff */ 6555714Skris 66280304Sjkimunsigned char *OPENSSL_asc2uni(const char *asc, int asclen, 67280304Sjkim unsigned char **uni, int *unilen) 6855714Skris{ 69280304Sjkim int ulen, i; 70280304Sjkim unsigned char *unitmp; 71280304Sjkim if (asclen == -1) 72280304Sjkim asclen = strlen(asc); 73280304Sjkim ulen = asclen * 2 + 2; 74280304Sjkim if (!(unitmp = OPENSSL_malloc(ulen))) 75280304Sjkim return NULL; 76280304Sjkim for (i = 0; i < ulen - 2; i += 2) { 77280304Sjkim unitmp[i] = 0; 78280304Sjkim unitmp[i + 1] = asc[i >> 1]; 79280304Sjkim } 80280304Sjkim /* Make result double null terminated */ 81280304Sjkim unitmp[ulen - 2] = 0; 82280304Sjkim unitmp[ulen - 1] = 0; 83280304Sjkim if (unilen) 84280304Sjkim *unilen = ulen; 85280304Sjkim if (uni) 86280304Sjkim *uni = unitmp; 87280304Sjkim return unitmp; 8855714Skris} 8955714Skris 90238405Sjkimchar *OPENSSL_uni2asc(unsigned char *uni, int unilen) 9155714Skris{ 92280304Sjkim int asclen, i; 93280304Sjkim char *asctmp; 94306196Sjkim 95306196Sjkim /* string must contain an even number of bytes */ 96306196Sjkim if (unilen & 1) 97306196Sjkim return NULL; 98280304Sjkim asclen = unilen / 2; 99280304Sjkim /* If no terminating zero allow for one */ 100280304Sjkim if (!unilen || uni[unilen - 1]) 101280304Sjkim asclen++; 102280304Sjkim uni++; 103280304Sjkim if (!(asctmp = OPENSSL_malloc(asclen))) 104280304Sjkim return NULL; 105280304Sjkim for (i = 0; i < unilen; i += 2) 106280304Sjkim asctmp[i >> 1] = uni[i]; 107280304Sjkim asctmp[asclen - 1] = 0; 108280304Sjkim return asctmp; 10955714Skris} 11055714Skris 11155714Skrisint i2d_PKCS12_bio(BIO *bp, PKCS12 *p12) 11255714Skris{ 113280304Sjkim return ASN1_item_i2d_bio(ASN1_ITEM_rptr(PKCS12), bp, p12); 11455714Skris} 11555714Skris 116109998Smarkm#ifndef OPENSSL_NO_FP_API 11755714Skrisint i2d_PKCS12_fp(FILE *fp, PKCS12 *p12) 11855714Skris{ 119280304Sjkim return ASN1_item_i2d_fp(ASN1_ITEM_rptr(PKCS12), fp, p12); 12055714Skris} 12155714Skris#endif 12255714Skris 12355714SkrisPKCS12 *d2i_PKCS12_bio(BIO *bp, PKCS12 **p12) 12455714Skris{ 125280304Sjkim return ASN1_item_d2i_bio(ASN1_ITEM_rptr(PKCS12), bp, p12); 12655714Skris} 127280304Sjkim 128109998Smarkm#ifndef OPENSSL_NO_FP_API 12955714SkrisPKCS12 *d2i_PKCS12_fp(FILE *fp, PKCS12 **p12) 13055714Skris{ 131280304Sjkim return ASN1_item_d2i_fp(ASN1_ITEM_rptr(PKCS12), fp, p12); 13255714Skris} 13355714Skris#endif 13455714Skris 135109998SmarkmPKCS12_SAFEBAG *PKCS12_x5092certbag(X509 *x509) 136109998Smarkm{ 137280304Sjkim return PKCS12_item_pack_safebag(x509, ASN1_ITEM_rptr(X509), 138280304Sjkim NID_x509Certificate, NID_certBag); 139109998Smarkm} 140109998Smarkm 141109998SmarkmPKCS12_SAFEBAG *PKCS12_x509crl2certbag(X509_CRL *crl) 142109998Smarkm{ 143280304Sjkim return PKCS12_item_pack_safebag(crl, ASN1_ITEM_rptr(X509_CRL), 144280304Sjkim NID_x509Crl, NID_crlBag); 145109998Smarkm} 146109998Smarkm 147109998SmarkmX509 *PKCS12_certbag2x509(PKCS12_SAFEBAG *bag) 148109998Smarkm{ 149280304Sjkim if (M_PKCS12_bag_type(bag) != NID_certBag) 150280304Sjkim return NULL; 151280304Sjkim if (M_PKCS12_cert_bag_type(bag) != NID_x509Certificate) 152280304Sjkim return NULL; 153280304Sjkim return ASN1_item_unpack(bag->value.bag->value.octet, 154280304Sjkim ASN1_ITEM_rptr(X509)); 155109998Smarkm} 156109998Smarkm 157109998SmarkmX509_CRL *PKCS12_certbag2x509crl(PKCS12_SAFEBAG *bag) 158109998Smarkm{ 159280304Sjkim if (M_PKCS12_bag_type(bag) != NID_crlBag) 160280304Sjkim return NULL; 161280304Sjkim if (M_PKCS12_cert_bag_type(bag) != NID_x509Crl) 162280304Sjkim return NULL; 163280304Sjkim return ASN1_item_unpack(bag->value.bag->value.octet, 164280304Sjkim ASN1_ITEM_rptr(X509_CRL)); 165109998Smarkm} 166