155714Skris/* p12_attr.c */ 2280297Sjkim/* 3280297Sjkim * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project 4280297Sjkim * 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 14280297Sjkim * 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/* Add a local keyid to a safebag */ 6555714Skris 66109998Smarkmint PKCS12_add_localkeyid(PKCS12_SAFEBAG *bag, unsigned char *name, 67280297Sjkim int namelen) 6855714Skris{ 69280297Sjkim if (X509at_add1_attr_by_NID(&bag->attrib, NID_localKeyID, 70280297Sjkim V_ASN1_OCTET_STRING, name, namelen)) 71280297Sjkim return 1; 72280297Sjkim else 73280297Sjkim return 0; 7455714Skris} 7555714Skris 7655714Skris/* Add key usage to PKCS#8 structure */ 7755714Skris 78109998Smarkmint PKCS8_add_keyusage(PKCS8_PRIV_KEY_INFO *p8, int usage) 7955714Skris{ 80280297Sjkim unsigned char us_val; 81280297Sjkim us_val = (unsigned char)usage; 82280297Sjkim if (X509at_add1_attr_by_NID(&p8->attributes, NID_key_usage, 83280297Sjkim V_ASN1_BIT_STRING, &us_val, 1)) 84280297Sjkim return 1; 85280297Sjkim else 86280297Sjkim return 0; 8755714Skris} 8855714Skris 8955714Skris/* Add a friendlyname to a safebag */ 9055714Skris 91109998Smarkmint PKCS12_add_friendlyname_asc(PKCS12_SAFEBAG *bag, const char *name, 92280297Sjkim int namelen) 9355714Skris{ 94280297Sjkim if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName, 95280297Sjkim MBSTRING_ASC, (unsigned char *)name, namelen)) 96280297Sjkim return 1; 97280297Sjkim else 98280297Sjkim return 0; 9955714Skris} 10055714Skris 101109998Smarkmint PKCS12_add_friendlyname_uni(PKCS12_SAFEBAG *bag, 102280297Sjkim const unsigned char *name, int namelen) 10355714Skris{ 104280297Sjkim if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName, 105280297Sjkim MBSTRING_BMP, name, namelen)) 106280297Sjkim return 1; 107280297Sjkim else 108280297Sjkim return 0; 109109998Smarkm} 110109998Smarkm 111280297Sjkimint PKCS12_add_CSPName_asc(PKCS12_SAFEBAG *bag, const char *name, int namelen) 112109998Smarkm{ 113280297Sjkim if (X509at_add1_attr_by_NID(&bag->attrib, NID_ms_csp_name, 114280297Sjkim MBSTRING_ASC, (unsigned char *)name, namelen)) 115280297Sjkim return 1; 116280297Sjkim else 117280297Sjkim return 0; 11855714Skris} 11955714Skris 120109998SmarkmASN1_TYPE *PKCS12_get_attr_gen(STACK_OF(X509_ATTRIBUTE) *attrs, int attr_nid) 12155714Skris{ 122280297Sjkim X509_ATTRIBUTE *attrib; 123280297Sjkim int i; 124280297Sjkim if (!attrs) 125280297Sjkim return NULL; 126280297Sjkim for (i = 0; i < sk_X509_ATTRIBUTE_num(attrs); i++) { 127280297Sjkim attrib = sk_X509_ATTRIBUTE_value(attrs, i); 128280297Sjkim if (OBJ_obj2nid(attrib->object) == attr_nid) { 129280297Sjkim if (sk_ASN1_TYPE_num(attrib->value.set)) 130280297Sjkim return sk_ASN1_TYPE_value(attrib->value.set, 0); 131280297Sjkim else 132280297Sjkim return NULL; 133280297Sjkim } 134280297Sjkim } 135280297Sjkim return NULL; 13655714Skris} 13755714Skris 13855714Skrischar *PKCS12_get_friendlyname(PKCS12_SAFEBAG *bag) 13955714Skris{ 140280297Sjkim ASN1_TYPE *atype; 141280297Sjkim if (!(atype = PKCS12_get_attr(bag, NID_friendlyName))) 142280297Sjkim return NULL; 143280297Sjkim if (atype->type != V_ASN1_BMPSTRING) 144280297Sjkim return NULL; 145280297Sjkim return OPENSSL_uni2asc(atype->value.bmpstring->data, 146280297Sjkim atype->value.bmpstring->length); 14755714Skris} 148