/src/openssl/crypto/pkcs12/p12_attr.c
Line | Count | Source |
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 | | #include <crypto/asn1.h> |
16 | | |
17 | | /* Add a local keyid to a safebag */ |
18 | | |
19 | | int PKCS12_add_localkeyid(PKCS12_SAFEBAG *bag, const unsigned char *name, |
20 | | int namelen) |
21 | 0 | { |
22 | 0 | if (X509at_add1_attr_by_NID(&bag->attrib, NID_localKeyID, |
23 | 0 | V_ASN1_OCTET_STRING, name, namelen) |
24 | 0 | != NULL) |
25 | 0 | return 1; |
26 | 0 | else |
27 | 0 | return 0; |
28 | 0 | } |
29 | | |
30 | | /* Add key usage to PKCS#8 structure */ |
31 | | |
32 | | int PKCS8_add_keyusage(PKCS8_PRIV_KEY_INFO *p8, int usage) |
33 | 0 | { |
34 | 0 | unsigned char us_val = (unsigned char)usage; |
35 | 0 | return PKCS8_pkey_add1_attr_by_NID(p8, NID_key_usage, |
36 | 0 | V_ASN1_BIT_STRING, &us_val, 1); |
37 | 0 | } |
38 | | |
39 | | /* Add a friendlyname to a safebag */ |
40 | | |
41 | | int PKCS12_add_friendlyname_asc(PKCS12_SAFEBAG *bag, const char *name, |
42 | | int namelen) |
43 | 0 | { |
44 | 0 | if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName, |
45 | 0 | MBSTRING_ASC, (unsigned char *)name, namelen) |
46 | 0 | != NULL) |
47 | 0 | return 1; |
48 | 0 | else |
49 | 0 | return 0; |
50 | 0 | } |
51 | | |
52 | | int PKCS12_add_friendlyname_utf8(PKCS12_SAFEBAG *bag, const char *name, |
53 | | int namelen) |
54 | 0 | { |
55 | 0 | if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName, |
56 | 0 | MBSTRING_UTF8, (unsigned char *)name, namelen) |
57 | 0 | != NULL) |
58 | 0 | return 1; |
59 | 0 | else |
60 | 0 | return 0; |
61 | 0 | } |
62 | | |
63 | | int PKCS12_add_friendlyname_uni(PKCS12_SAFEBAG *bag, |
64 | | const unsigned char *name, int namelen) |
65 | 0 | { |
66 | 0 | if (X509at_add1_attr_by_NID(&bag->attrib, NID_friendlyName, |
67 | 0 | MBSTRING_BMP, name, namelen) |
68 | 0 | != NULL) |
69 | 0 | return 1; |
70 | 0 | else |
71 | 0 | return 0; |
72 | 0 | } |
73 | | |
74 | | int PKCS12_add_CSPName_asc(PKCS12_SAFEBAG *bag, const char *name, int namelen) |
75 | 0 | { |
76 | 0 | if (X509at_add1_attr_by_NID(&bag->attrib, NID_ms_csp_name, |
77 | 0 | MBSTRING_ASC, (unsigned char *)name, namelen) |
78 | 0 | != NULL) |
79 | 0 | return 1; |
80 | 0 | else |
81 | 0 | return 0; |
82 | 0 | } |
83 | | |
84 | | int PKCS12_add1_attr_by_NID(PKCS12_SAFEBAG *bag, int nid, int type, |
85 | | const unsigned char *bytes, int len) |
86 | 0 | { |
87 | 0 | if (X509at_add1_attr_by_NID(&bag->attrib, nid, type, bytes, len) != NULL) |
88 | 0 | return 1; |
89 | 0 | else |
90 | 0 | return 0; |
91 | 0 | } |
92 | | |
93 | | int PKCS12_add1_attr_by_txt(PKCS12_SAFEBAG *bag, const char *attrname, int type, |
94 | | const unsigned char *bytes, int len) |
95 | 0 | { |
96 | 0 | if (X509at_add1_attr_by_txt(&bag->attrib, attrname, type, bytes, len) != NULL) |
97 | 0 | return 1; |
98 | 0 | else |
99 | 0 | return 0; |
100 | 0 | } |
101 | | |
102 | | const ASN1_TYPE *PKCS12_get_attr_gen(const STACK_OF(X509_ATTRIBUTE) *attrs, |
103 | | int attr_nid) |
104 | 0 | { |
105 | 0 | int i = X509at_get_attr_by_NID(attrs, attr_nid, -1); |
106 | |
|
107 | 0 | if (i < 0) |
108 | 0 | return NULL; |
109 | 0 | return X509_ATTRIBUTE_get0_type(X509at_get_attr(attrs, i), 0); |
110 | 0 | } |
111 | | |
112 | | char *PKCS12_get_friendlyname(PKCS12_SAFEBAG *bag) |
113 | 0 | { |
114 | 0 | const ASN1_TYPE *atype; |
115 | |
|
116 | 0 | if ((atype = PKCS12_SAFEBAG_get0_attr(bag, NID_friendlyName)) == NULL) |
117 | 0 | return NULL; |
118 | 0 | if (atype->type != V_ASN1_BMPSTRING) |
119 | 0 | return NULL; |
120 | 0 | return OPENSSL_uni2utf8(atype->value.bmpstring->data, |
121 | 0 | atype->value.bmpstring->length); |
122 | 0 | } |
123 | | |
124 | | const STACK_OF(X509_ATTRIBUTE) * |
125 | | PKCS12_SAFEBAG_get0_attrs(const PKCS12_SAFEBAG *bag) |
126 | 0 | { |
127 | 0 | return bag->attrib; |
128 | 0 | } |
129 | | |
130 | | void PKCS12_SAFEBAG_set0_attrs(PKCS12_SAFEBAG *bag, STACK_OF(X509_ATTRIBUTE) *attrs) |
131 | 0 | { |
132 | 0 | if (bag->attrib != attrs) |
133 | 0 | sk_X509_ATTRIBUTE_free(bag->attrib); |
134 | |
|
135 | 0 | bag->attrib = attrs; |
136 | 0 | } |