xref: /freebsd/crypto/openssl/crypto/pkcs12/p12_attr.c (revision f25b8c9fb4f58cf61adb47d7570abe7caa6d385d)
1 /*
2  * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include <stdio.h>
11 #include "internal/cryptlib.h"
12 #include <openssl/pkcs12.h>
13 #include "p12_local.h"
14 
15 /* Add a local keyid to a safebag */
16 
PKCS12_add_localkeyid(PKCS12_SAFEBAG * bag,unsigned char * name,int namelen)17 int PKCS12_add_localkeyid(PKCS12_SAFEBAG *bag, unsigned char *name,
18     int namelen)
19 {
20     if (X509at_add1_attr_by_NID(&bag->attrib, NID_localKeyID,
21             V_ASN1_OCTET_STRING, name, namelen)
22         != NULL)
23         return 1;
24     else
25         return 0;
26 }
27 
28 /* Add key usage to PKCS#8 structure */
29 
PKCS8_add_keyusage(PKCS8_PRIV_KEY_INFO * p8,int usage)30 int PKCS8_add_keyusage(PKCS8_PRIV_KEY_INFO *p8, int usage)
31 {
32     unsigned char us_val = (unsigned char)usage;
33     return PKCS8_pkey_add1_attr_by_NID(p8, NID_key_usage,
34         V_ASN1_BIT_STRING, &us_val, 1);
35 }
36 
37 /* Add a friendlyname to a safebag */
38 
PKCS12_add_friendlyname_asc(PKCS12_SAFEBAG * bag,const char * name,int namelen)39 int PKCS12_add_friendlyname_asc(PKCS12_SAFEBAG *bag, const char *name,
40     int namelen)
41 {
42     if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName,
43             MBSTRING_ASC, (unsigned char *)name, namelen)
44         != NULL)
45         return 1;
46     else
47         return 0;
48 }
49 
PKCS12_add_friendlyname_utf8(PKCS12_SAFEBAG * bag,const char * name,int namelen)50 int PKCS12_add_friendlyname_utf8(PKCS12_SAFEBAG *bag, const char *name,
51     int namelen)
52 {
53     if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName,
54             MBSTRING_UTF8, (unsigned char *)name, namelen)
55         != NULL)
56         return 1;
57     else
58         return 0;
59 }
60 
PKCS12_add_friendlyname_uni(PKCS12_SAFEBAG * bag,const unsigned char * name,int namelen)61 int PKCS12_add_friendlyname_uni(PKCS12_SAFEBAG *bag,
62     const unsigned char *name, int namelen)
63 {
64     if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName,
65             MBSTRING_BMP, name, namelen)
66         != NULL)
67         return 1;
68     else
69         return 0;
70 }
71 
PKCS12_add_CSPName_asc(PKCS12_SAFEBAG * bag,const char * name,int namelen)72 int PKCS12_add_CSPName_asc(PKCS12_SAFEBAG *bag, const char *name, int namelen)
73 {
74     if (X509at_add1_attr_by_NID(&bag->attrib, NID_ms_csp_name,
75             MBSTRING_ASC, (unsigned char *)name, namelen)
76         != NULL)
77         return 1;
78     else
79         return 0;
80 }
81 
PKCS12_add1_attr_by_NID(PKCS12_SAFEBAG * bag,int nid,int type,const unsigned char * bytes,int len)82 int PKCS12_add1_attr_by_NID(PKCS12_SAFEBAG *bag, int nid, int type,
83     const unsigned char *bytes, int len)
84 {
85     if (X509at_add1_attr_by_NID(&bag->attrib, nid, type, bytes, len) != NULL)
86         return 1;
87     else
88         return 0;
89 }
90 
PKCS12_add1_attr_by_txt(PKCS12_SAFEBAG * bag,const char * attrname,int type,const unsigned char * bytes,int len)91 int PKCS12_add1_attr_by_txt(PKCS12_SAFEBAG *bag, const char *attrname, int type,
92     const unsigned char *bytes, int len)
93 {
94     if (X509at_add1_attr_by_txt(&bag->attrib, attrname, type, bytes, len) != NULL)
95         return 1;
96     else
97         return 0;
98 }
99 
PKCS12_get_attr_gen(const STACK_OF (X509_ATTRIBUTE)* attrs,int attr_nid)100 ASN1_TYPE *PKCS12_get_attr_gen(const STACK_OF(X509_ATTRIBUTE) *attrs,
101     int attr_nid)
102 {
103     int i = X509at_get_attr_by_NID(attrs, attr_nid, -1);
104 
105     if (i < 0)
106         return NULL;
107     return X509_ATTRIBUTE_get0_type(X509at_get_attr(attrs, i), 0);
108 }
109 
PKCS12_get_friendlyname(PKCS12_SAFEBAG * bag)110 char *PKCS12_get_friendlyname(PKCS12_SAFEBAG *bag)
111 {
112     const ASN1_TYPE *atype;
113 
114     if ((atype = PKCS12_SAFEBAG_get0_attr(bag, NID_friendlyName)) == NULL)
115         return NULL;
116     if (atype->type != V_ASN1_BMPSTRING)
117         return NULL;
118     return OPENSSL_uni2utf8(atype->value.bmpstring->data,
119         atype->value.bmpstring->length);
120 }
121 
STACK_OF(X509_ATTRIBUTE)122 const STACK_OF(X509_ATTRIBUTE) *
123 PKCS12_SAFEBAG_get0_attrs(const PKCS12_SAFEBAG *bag)
124 {
125     return bag->attrib;
126 }
127 
PKCS12_SAFEBAG_set0_attrs(PKCS12_SAFEBAG * bag,STACK_OF (X509_ATTRIBUTE)* attrs)128 void PKCS12_SAFEBAG_set0_attrs(PKCS12_SAFEBAG *bag, STACK_OF(X509_ATTRIBUTE) *attrs)
129 {
130     if (bag->attrib != attrs)
131         sk_X509_ATTRIBUTE_free(bag->attrib);
132 
133     bag->attrib = attrs;
134 }
135