Coverage Report

Created: 2025-06-13 06:57

/src/openssl/crypto/x509/v3_attrdesc.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2024 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 <openssl/asn1t.h>
11
#include <openssl/x509v3.h>
12
#include <crypto/x509.h>
13
#include "ext_dat.h"
14
15
ASN1_SEQUENCE(OSSL_HASH) = {
16
    ASN1_SIMPLE(OSSL_HASH, algorithmIdentifier, X509_ALGOR),
17
    ASN1_OPT(OSSL_HASH, hashValue, ASN1_BIT_STRING),
18
} ASN1_SEQUENCE_END(OSSL_HASH)
19
20
ASN1_SEQUENCE(OSSL_INFO_SYNTAX_POINTER) = {
21
    ASN1_SIMPLE(OSSL_INFO_SYNTAX_POINTER, name, GENERAL_NAMES),
22
    ASN1_OPT(OSSL_INFO_SYNTAX_POINTER, hash, OSSL_HASH),
23
} ASN1_SEQUENCE_END(OSSL_INFO_SYNTAX_POINTER)
24
25
ASN1_CHOICE(OSSL_INFO_SYNTAX) = {
26
    ASN1_SIMPLE(OSSL_INFO_SYNTAX, choice.content, DIRECTORYSTRING),
27
    ASN1_SIMPLE(OSSL_INFO_SYNTAX, choice.pointer, OSSL_INFO_SYNTAX_POINTER)
28
} ASN1_CHOICE_END(OSSL_INFO_SYNTAX)
29
30
ASN1_SEQUENCE(OSSL_PRIVILEGE_POLICY_ID) = {
31
    ASN1_SIMPLE(OSSL_PRIVILEGE_POLICY_ID, privilegePolicy, ASN1_OBJECT),
32
    ASN1_SIMPLE(OSSL_PRIVILEGE_POLICY_ID, privPolSyntax, OSSL_INFO_SYNTAX),
33
} ASN1_SEQUENCE_END(OSSL_PRIVILEGE_POLICY_ID)
34
35
ASN1_SEQUENCE(OSSL_ATTRIBUTE_DESCRIPTOR) = {
36
    ASN1_SIMPLE(OSSL_ATTRIBUTE_DESCRIPTOR, identifier, ASN1_OBJECT),
37
    ASN1_SIMPLE(OSSL_ATTRIBUTE_DESCRIPTOR, attributeSyntax, ASN1_OCTET_STRING),
38
    ASN1_IMP_OPT(OSSL_ATTRIBUTE_DESCRIPTOR, name, ASN1_UTF8STRING, 0),
39
    ASN1_IMP_OPT(OSSL_ATTRIBUTE_DESCRIPTOR, description, ASN1_UTF8STRING, 1),
40
    ASN1_SIMPLE(OSSL_ATTRIBUTE_DESCRIPTOR, dominationRule, OSSL_PRIVILEGE_POLICY_ID),
41
} ASN1_SEQUENCE_END(OSSL_ATTRIBUTE_DESCRIPTOR)
42
43
IMPLEMENT_ASN1_FUNCTIONS(OSSL_HASH)
44
IMPLEMENT_ASN1_FUNCTIONS(OSSL_INFO_SYNTAX)
45
IMPLEMENT_ASN1_FUNCTIONS(OSSL_INFO_SYNTAX_POINTER)
46
IMPLEMENT_ASN1_FUNCTIONS(OSSL_PRIVILEGE_POLICY_ID)
47
IMPLEMENT_ASN1_FUNCTIONS(OSSL_ATTRIBUTE_DESCRIPTOR)
48
49
static int i2r_HASH(X509V3_EXT_METHOD *method,
50
                    OSSL_HASH *hash,
51
                    BIO *out, int indent)
52
0
{
53
0
    if (BIO_printf(out, "%*sAlgorithm: ", indent, "") <= 0)
54
0
        return 0;
55
0
    if (i2a_ASN1_OBJECT(out, hash->algorithmIdentifier->algorithm) <= 0)
56
0
        return 0;
57
0
    if (BIO_puts(out, "\n") <= 0)
58
0
        return 0;
59
0
    if (hash->algorithmIdentifier->parameter) {
60
0
        if (BIO_printf(out, "%*sParameter: ", indent, "") <= 0)
61
0
            return 0;
62
0
        if (ossl_print_attribute_value(out, 0, hash->algorithmIdentifier->parameter,
63
0
                                       indent + 4) <= 0)
64
0
            return 0;
65
0
        if (BIO_puts(out, "\n") <= 0)
66
0
            return 0;
67
0
    }
68
0
    if (BIO_printf(out, "%*sHash Value: ", indent, "") <= 0)
69
0
        return 0;
70
0
    return ossl_bio_print_hex(out, hash->hashValue->data, hash->hashValue->length);
71
0
}
72
73
static int i2r_INFO_SYNTAX_POINTER(X509V3_EXT_METHOD *method,
74
                                   OSSL_INFO_SYNTAX_POINTER *pointer,
75
                                   BIO *out, int indent)
76
0
{
77
0
    if (BIO_printf(out, "%*sNames:\n", indent, "") <= 0)
78
0
        return 0;
79
0
    if (OSSL_GENERAL_NAMES_print(out, pointer->name, indent) <= 0)
80
0
        return 0;
81
0
    if (BIO_puts(out, "\n") <= 0)
82
0
        return 0;
83
0
    if (pointer->hash != NULL) {
84
0
        if (BIO_printf(out, "%*sHash:\n", indent, "") <= 0)
85
0
            return 0;
86
0
        if (i2r_HASH(method, pointer->hash, out, indent + 4) <= 0)
87
0
            return 0;
88
0
    }
89
0
    return 1;
90
0
}
91
92
static int i2r_OSSL_INFO_SYNTAX(X509V3_EXT_METHOD *method,
93
                                OSSL_INFO_SYNTAX *info,
94
                                BIO *out, int indent)
95
0
{
96
0
    switch (info->type) {
97
0
    case OSSL_INFO_SYNTAX_TYPE_CONTENT:
98
0
        if (BIO_printf(out, "%*sContent: ", indent, "") <= 0)
99
0
            return 0;
100
0
        if (BIO_printf(out, "%.*s", info->choice.content->length, info->choice.content->data) <= 0)
101
0
            return 0;
102
0
        if (BIO_puts(out, "\n") <= 0)
103
0
            return 0;
104
0
        return 1;
105
0
    case OSSL_INFO_SYNTAX_TYPE_POINTER:
106
0
        if (BIO_printf(out, "%*sPointer:\n", indent, "") <= 0)
107
0
            return 0;
108
0
        return i2r_INFO_SYNTAX_POINTER(method, info->choice.pointer, out, indent + 4);
109
0
    default:
110
0
        return 0;
111
0
    }
112
0
    return 0;
113
0
}
114
115
static int i2r_OSSL_PRIVILEGE_POLICY_ID(X509V3_EXT_METHOD *method,
116
                                        OSSL_PRIVILEGE_POLICY_ID *ppid,
117
                                        BIO *out, int indent)
118
0
{
119
0
    char buf[80];
120
121
    /* Intentionally display the numeric OID, rather than the textual name. */
122
0
    if (OBJ_obj2txt(buf, sizeof(buf), ppid->privilegePolicy, 1) <= 0)
123
0
        return 0;
124
0
    if (BIO_printf(out, "%*sPrivilege Policy Identifier: %s\n", indent, "", buf) <= 0)
125
0
        return 0;
126
0
    if (BIO_printf(out, "%*sPrivilege Policy Syntax:\n", indent, "") <= 0)
127
0
        return 0;
128
0
    return i2r_OSSL_INFO_SYNTAX(method, ppid->privPolSyntax, out, indent + 4);
129
0
}
130
131
static int i2r_OSSL_ATTRIBUTE_DESCRIPTOR(X509V3_EXT_METHOD *method,
132
                                         OSSL_ATTRIBUTE_DESCRIPTOR *ad,
133
                                         BIO *out, int indent)
134
0
{
135
0
    char buf[80];
136
137
    /* Intentionally display the numeric OID, rather than the textual name. */
138
0
    if (OBJ_obj2txt(buf, sizeof(buf), ad->identifier, 1) <= 0)
139
0
        return 0;
140
0
    if (BIO_printf(out, "%*sIdentifier: %s\n", indent, "", buf) <= 0)
141
0
        return 0;
142
0
    if (BIO_printf(out, "%*sSyntax:\n", indent, "") <= 0)
143
0
        return 0;
144
0
    if (BIO_printf(out, "%*s%.*s", indent + 4, "",
145
0
                   ad->attributeSyntax->length, ad->attributeSyntax->data) <= 0)
146
0
        return 0;
147
0
    if (BIO_puts(out, "\n\n") <= 0)
148
0
        return 0;
149
0
    if (ad->name != NULL) {
150
0
        if (BIO_printf(out, "%*sName: %.*s\n", indent, "", ad->name->length, ad->name->data) <= 0)
151
0
            return 0;
152
0
    }
153
0
    if (ad->description != NULL) {
154
0
        if (BIO_printf(out, "%*sDescription: %.*s\n", indent, "",
155
0
                       ad->description->length, ad->description->data) <= 0)
156
0
            return 0;
157
0
    }
158
0
    if (BIO_printf(out, "%*sDomination Rule:\n", indent, "") <= 0)
159
0
        return 0;
160
0
    return i2r_OSSL_PRIVILEGE_POLICY_ID(method, ad->dominationRule, out, indent + 4);
161
0
}
162
163
const X509V3_EXT_METHOD ossl_v3_attribute_descriptor = {
164
    NID_attribute_descriptor, X509V3_EXT_MULTILINE,
165
    ASN1_ITEM_ref(OSSL_ATTRIBUTE_DESCRIPTOR),
166
    0, 0, 0, 0,
167
    0, 0,
168
    0,
169
    0,
170
    (X509V3_EXT_I2R)i2r_OSSL_ATTRIBUTE_DESCRIPTOR,
171
    NULL,
172
    NULL
173
};