]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/pkcs12/p12_attr.c
x509_att.c: improve error checking and reporting and coding style
[thirdparty/openssl.git] / crypto / pkcs12 / p12_attr.c
CommitLineData
0f113f3e 1/*
eec0ad10 2 * Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
8d8c7266 3 *
54fffdf4 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
b1322259
RS
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
8d8c7266
DSH
8 */
9
10#include <stdio.h>
b39fc560 11#include "internal/cryptlib.h"
ec577822 12#include <openssl/pkcs12.h>
706457b7 13#include "p12_local.h"
8d8c7266
DSH
14
15/* Add a local keyid to a safebag */
16
f2a253e0 17int PKCS12_add_localkeyid(PKCS12_SAFEBAG *bag, unsigned char *name,
0f113f3e 18 int namelen)
8d8c7266 19{
0f113f3e 20 if (X509at_add1_attr_by_NID(&bag->attrib, NID_localKeyID,
c5ec6dcf 21 V_ASN1_OCTET_STRING, name, namelen) != NULL)
0f113f3e
MC
22 return 1;
23 else
24 return 0;
8d8c7266
DSH
25}
26
27/* Add key usage to PKCS#8 structure */
28
f2a253e0 29int PKCS8_add_keyusage(PKCS8_PRIV_KEY_INFO *p8, int usage)
8d8c7266 30{
54dbf423
DSH
31 unsigned char us_val = (unsigned char)usage;
32 return PKCS8_pkey_add1_attr_by_NID(p8, NID_key_usage,
33 V_ASN1_BIT_STRING, &us_val, 1);
8d8c7266
DSH
34}
35
36/* Add a friendlyname to a safebag */
37
f2a253e0 38int PKCS12_add_friendlyname_asc(PKCS12_SAFEBAG *bag, const char *name,
0f113f3e 39 int namelen)
8d8c7266 40{
0f113f3e 41 if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName,
c5ec6dcf 42 MBSTRING_ASC, (unsigned char *)name, namelen) != NULL)
0f113f3e
MC
43 return 1;
44 else
45 return 0;
8d8c7266 46}
8d8c7266 47
b799aef8
AP
48int PKCS12_add_friendlyname_utf8(PKCS12_SAFEBAG *bag, const char *name,
49 int namelen)
50{
51 if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName,
c5ec6dcf 52 MBSTRING_UTF8, (unsigned char *)name, namelen) != NULL)
b799aef8
AP
53 return 1;
54 else
55 return 0;
56}
57
f2a253e0 58int PKCS12_add_friendlyname_uni(PKCS12_SAFEBAG *bag,
0f113f3e 59 const unsigned char *name, int namelen)
8d8c7266 60{
0f113f3e 61 if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName,
c5ec6dcf 62 MBSTRING_BMP, name, namelen) != NULL)
0f113f3e
MC
63 return 1;
64 else
65 return 0;
f2a253e0
DSH
66}
67
0f113f3e 68int PKCS12_add_CSPName_asc(PKCS12_SAFEBAG *bag, const char *name, int namelen)
f2a253e0 69{
0f113f3e 70 if (X509at_add1_attr_by_NID(&bag->attrib, NID_ms_csp_name,
c5ec6dcf
JS
71 MBSTRING_ASC, (unsigned char *)name, namelen) != NULL)
72 return 1;
73 else
74 return 0;
75}
76
77int PKCS12_add1_attr_by_NID(PKCS12_SAFEBAG *bag, int nid, int type,
78 const unsigned char *bytes, int len)
79{
80 if (X509at_add1_attr_by_NID(&bag->attrib, nid, type, bytes, len) != NULL)
81 return 1;
82 else
83 return 0;
84}
85
86int PKCS12_add1_attr_by_txt(PKCS12_SAFEBAG *bag, const char *attrname, int type,
87 const unsigned char *bytes, int len)
88{
89 if (X509at_add1_attr_by_txt(&bag->attrib, attrname, type, bytes, len) != NULL)
0f113f3e
MC
90 return 1;
91 else
92 return 0;
8d8c7266
DSH
93}
94
b2e57e09
MC
95ASN1_TYPE *PKCS12_get_attr_gen(const STACK_OF(X509_ATTRIBUTE) *attrs,
96 int attr_nid)
8d8c7266 97{
ba9e3721
DDO
98 int i = X509at_get_attr_by_NID(attrs, attr_nid, -1);
99
100 if (i < 0)
101 return NULL;
102 return X509_ATTRIBUTE_get0_type(X509at_get_attr(attrs, i), 0);
8d8c7266
DSH
103}
104
6b691a5c 105char *PKCS12_get_friendlyname(PKCS12_SAFEBAG *bag)
8d8c7266 106{
28da1455 107 const ASN1_TYPE *atype;
75ebbd9a 108
762ee38d 109 if ((atype = PKCS12_SAFEBAG_get0_attr(bag, NID_friendlyName)) == NULL)
0f113f3e
MC
110 return NULL;
111 if (atype->type != V_ASN1_BMPSTRING)
112 return NULL;
b799aef8
AP
113 return OPENSSL_uni2utf8(atype->value.bmpstring->data,
114 atype->value.bmpstring->length);
8d8c7266 115}
1387a2ec 116
28da1455
MC
117const STACK_OF(X509_ATTRIBUTE) *
118PKCS12_SAFEBAG_get0_attrs(const PKCS12_SAFEBAG *bag)
1387a2ec
DSH
119{
120 return bag->attrib;
121}