Coverage Report

Created: 2025-06-13 06:58

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