p12_attr.c revision 109998
155714Skris/* p12_attr.c */
255714Skris/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
355714Skris * project 1999.
455714Skris */
555714Skris/* ====================================================================
655714Skris * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
755714Skris *
855714Skris * Redistribution and use in source and binary forms, with or without
955714Skris * modification, are permitted provided that the following conditions
1055714Skris * are met:
1155714Skris *
1255714Skris * 1. Redistributions of source code must retain the above copyright
1355714Skris *    notice, this list of conditions and the following disclaimer.
1455714Skris *
1555714Skris * 2. Redistributions in binary form must reproduce the above copyright
1655714Skris *    notice, this list of conditions and the following disclaimer in
1755714Skris *    the documentation and/or other materials provided with the
1855714Skris *    distribution.
1955714Skris *
2055714Skris * 3. All advertising materials mentioning features or use of this
2155714Skris *    software must display the following acknowledgment:
2255714Skris *    "This product includes software developed by the OpenSSL Project
2355714Skris *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2455714Skris *
2555714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2655714Skris *    endorse or promote products derived from this software without
2755714Skris *    prior written permission. For written permission, please contact
2855714Skris *    licensing@OpenSSL.org.
2955714Skris *
3055714Skris * 5. Products derived from this software may not be called "OpenSSL"
3155714Skris *    nor may "OpenSSL" appear in their names without prior written
3255714Skris *    permission of the OpenSSL Project.
3355714Skris *
3455714Skris * 6. Redistributions of any form whatsoever must retain the following
3555714Skris *    acknowledgment:
3655714Skris *    "This product includes software developed by the OpenSSL Project
3755714Skris *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3855714Skris *
3955714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4055714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4155714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4255714Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4355714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4455714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4555714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4655714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4755714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4855714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4955714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
5055714Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
5155714Skris * ====================================================================
5255714Skris *
5355714Skris * This product includes cryptographic software written by Eric Young
5455714Skris * (eay@cryptsoft.com).  This product includes software written by Tim
5555714Skris * Hudson (tjh@cryptsoft.com).
5655714Skris *
5755714Skris */
5855714Skris
5955714Skris#include <stdio.h>
6055714Skris#include "cryptlib.h"
6155714Skris#include <openssl/pkcs12.h>
6255714Skris
6355714Skris/* Add a local keyid to a safebag */
6455714Skris
65109998Smarkmint PKCS12_add_localkeyid(PKCS12_SAFEBAG *bag, unsigned char *name,
6655714Skris	     int namelen)
6755714Skris{
68109998Smarkm	if (X509at_add1_attr_by_NID(&bag->attrib, NID_localKeyID,
69109998Smarkm				V_ASN1_OCTET_STRING, name, namelen))
70109998Smarkm		return 1;
71109998Smarkm	else
7255714Skris		return 0;
7355714Skris}
7455714Skris
7555714Skris/* Add key usage to PKCS#8 structure */
7655714Skris
77109998Smarkmint PKCS8_add_keyusage(PKCS8_PRIV_KEY_INFO *p8, int usage)
7855714Skris{
7955714Skris	unsigned char us_val;
8055714Skris	us_val = (unsigned char) usage;
81109998Smarkm	if (X509at_add1_attr_by_NID(&p8->attributes, NID_key_usage,
82109998Smarkm				V_ASN1_BIT_STRING, &us_val, 1))
83109998Smarkm		return 1;
84109998Smarkm	else
8555714Skris		return 0;
8655714Skris}
8755714Skris
8855714Skris/* Add a friendlyname to a safebag */
8955714Skris
90109998Smarkmint PKCS12_add_friendlyname_asc(PKCS12_SAFEBAG *bag, const char *name,
9155714Skris				 int namelen)
9255714Skris{
93109998Smarkm	if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName,
94109998Smarkm				MBSTRING_ASC, (unsigned char *)name, namelen))
95109998Smarkm		return 1;
96109998Smarkm	else
9755714Skris		return 0;
9855714Skris}
9955714Skris
100109998Smarkm
101109998Smarkmint PKCS12_add_friendlyname_uni(PKCS12_SAFEBAG *bag,
10255714Skris				 const unsigned char *name, int namelen)
10355714Skris{
104109998Smarkm	if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName,
105109998Smarkm				MBSTRING_BMP, name, namelen))
106109998Smarkm		return 1;
107109998Smarkm	else
10855714Skris		return 0;
109109998Smarkm}
110109998Smarkm
111109998Smarkmint PKCS12_add_CSPName_asc(PKCS12_SAFEBAG *bag, const char *name,
112109998Smarkm				 int namelen)
113109998Smarkm{
114109998Smarkm	if (X509at_add1_attr_by_NID(&bag->attrib, NID_ms_csp_name,
115109998Smarkm				MBSTRING_ASC, (unsigned char *)name, namelen))
116109998Smarkm		return 1;
117109998Smarkm	else
11855714Skris		return 0;
11955714Skris}
12055714Skris
121109998SmarkmASN1_TYPE *PKCS12_get_attr_gen(STACK_OF(X509_ATTRIBUTE) *attrs, int attr_nid)
12255714Skris{
12355714Skris	X509_ATTRIBUTE *attrib;
12455714Skris	int i;
12555714Skris	if (!attrs) return NULL;
12655714Skris	for (i = 0; i < sk_X509_ATTRIBUTE_num (attrs); i++) {
12755714Skris		attrib = sk_X509_ATTRIBUTE_value (attrs, i);
12855714Skris		if (OBJ_obj2nid (attrib->object) == attr_nid) {
12955714Skris			if (sk_ASN1_TYPE_num (attrib->value.set))
13055714Skris			    return sk_ASN1_TYPE_value(attrib->value.set, 0);
13155714Skris			else return NULL;
13255714Skris		}
13355714Skris	}
13455714Skris	return NULL;
13555714Skris}
13655714Skris
13755714Skrischar *PKCS12_get_friendlyname(PKCS12_SAFEBAG *bag)
13855714Skris{
13955714Skris	ASN1_TYPE *atype;
14055714Skris	if (!(atype = PKCS12_get_attr(bag, NID_friendlyName))) return NULL;
14155714Skris	if (atype->type != V_ASN1_BMPSTRING) return NULL;
14255714Skris	return uni2asc(atype->value.bmpstring->data,
14355714Skris				 atype->value.bmpstring->length);
14455714Skris}
14555714Skris
146