Coverage Report

Created: 2026-02-22 06:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/x509/v3_ac_tgt.c
Line
Count
Source
1
/*
2
 * Copyright 1999-2025 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 <openssl/x509_acert.h>
12
#include <crypto/x509_acert.h>
13
#include "internal/cryptlib.h"
14
#include <openssl/asn1.h>
15
#include <openssl/asn1t.h>
16
#include <openssl/conf.h>
17
#include <openssl/x509v3.h>
18
#include "ext_dat.h"
19
#include "x509_local.h"
20
#include "crypto/asn1.h"
21
#include "crypto/evp.h"
22
23
static int i2r_ISSUER_SERIAL(X509V3_EXT_METHOD *method,
24
    OSSL_ISSUER_SERIAL *iss,
25
    BIO *out, int indent);
26
static int i2r_OBJECT_DIGEST_INFO(X509V3_EXT_METHOD *method,
27
    OSSL_OBJECT_DIGEST_INFO *odi,
28
    BIO *out, int indent);
29
static int i2r_TARGET_CERT(X509V3_EXT_METHOD *method,
30
    OSSL_TARGET_CERT *tc,
31
    BIO *out, int indent);
32
static int i2r_TARGET(X509V3_EXT_METHOD *method,
33
    OSSL_TARGET *target,
34
    BIO *out, int indent);
35
static int i2r_TARGETING_INFORMATION(X509V3_EXT_METHOD *method,
36
    OSSL_TARGETING_INFORMATION *tinfo,
37
    BIO *out, int indent);
38
39
ASN1_SEQUENCE(OSSL_ISSUER_SERIAL) = {
40
    ASN1_SEQUENCE_OF(OSSL_ISSUER_SERIAL, issuer, GENERAL_NAME),
41
    ASN1_EMBED(OSSL_ISSUER_SERIAL, serial, ASN1_INTEGER),
42
    ASN1_OPT(OSSL_ISSUER_SERIAL, issuerUID, ASN1_BIT_STRING),
43
0
} static_ASN1_SEQUENCE_END(OSSL_ISSUER_SERIAL)
44
0
45
0
ASN1_SEQUENCE(OSSL_OBJECT_DIGEST_INFO)
46
0
    = {
47
0
          ASN1_EMBED(OSSL_OBJECT_DIGEST_INFO, digestedObjectType, ASN1_ENUMERATED),
48
0
          ASN1_OPT(OSSL_OBJECT_DIGEST_INFO, otherObjectTypeID, ASN1_OBJECT),
49
0
          ASN1_EMBED(OSSL_OBJECT_DIGEST_INFO, digestAlgorithm, X509_ALGOR),
50
0
          ASN1_EMBED(OSSL_OBJECT_DIGEST_INFO, objectDigest, ASN1_BIT_STRING),
51
0
      } static_ASN1_SEQUENCE_END(OSSL_OBJECT_DIGEST_INFO)
52
0
53
0
ASN1_SEQUENCE(OSSL_TARGET_CERT)
54
0
    = {
55
0
          ASN1_SIMPLE(OSSL_TARGET_CERT, targetCertificate, OSSL_ISSUER_SERIAL),
56
0
          ASN1_OPT(OSSL_TARGET_CERT, targetName, GENERAL_NAME),
57
0
          ASN1_OPT(OSSL_TARGET_CERT, certDigestInfo, OSSL_OBJECT_DIGEST_INFO),
58
0
      } static_ASN1_SEQUENCE_END(OSSL_TARGET_CERT)
59
0
60
0
ASN1_CHOICE(OSSL_TARGET)
61
0
    = {
62
0
          ASN1_EXP(OSSL_TARGET, choice.targetName, GENERAL_NAME, 0),
63
0
          ASN1_EXP(OSSL_TARGET, choice.targetGroup, GENERAL_NAME, 1),
64
0
          ASN1_IMP(OSSL_TARGET, choice.targetCert, OSSL_TARGET_CERT, 2),
65
0
      } ASN1_CHOICE_END(OSSL_TARGET)
66
0
67
0
ASN1_ITEM_TEMPLATE(OSSL_TARGETS) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, Targets, OSSL_TARGET)
68
0
ASN1_ITEM_TEMPLATE_END(OSSL_TARGETS)
69
70
ASN1_ITEM_TEMPLATE(OSSL_TARGETING_INFORMATION) = ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, TargetingInformation, OSSL_TARGETS)
71
0
ASN1_ITEM_TEMPLATE_END(OSSL_TARGETING_INFORMATION)
72
73
IMPLEMENT_ASN1_FUNCTIONS(OSSL_TARGET)
74
IMPLEMENT_ASN1_FUNCTIONS(OSSL_TARGETS)
75
IMPLEMENT_ASN1_FUNCTIONS(OSSL_TARGETING_INFORMATION)
76
77
static int i2r_ISSUER_SERIAL(X509V3_EXT_METHOD *method,
78
    OSSL_ISSUER_SERIAL *iss,
79
    BIO *out, int indent)
80
0
{
81
0
    if (iss->issuer != NULL) {
82
0
        BIO_printf(out, "%*sIssuer Names:\n", indent, "");
83
0
        OSSL_GENERAL_NAMES_print(out, iss->issuer, indent);
84
0
        BIO_puts(out, "\n");
85
0
    } else {
86
0
        BIO_printf(out, "%*sIssuer Names: <none>\n", indent, "");
87
0
    }
88
0
    BIO_printf(out, "%*sIssuer Serial: ", indent, "");
89
0
    if (i2a_ASN1_INTEGER(out, &(iss->serial)) <= 0)
90
0
        return 0;
91
0
    BIO_puts(out, "\n");
92
0
    if (iss->issuerUID != NULL) {
93
0
        BIO_printf(out, "%*sIssuer UID: ", indent, "");
94
0
        if (i2a_ASN1_STRING(out, iss->issuerUID, V_ASN1_BIT_STRING) <= 0)
95
0
            return 0;
96
0
        BIO_puts(out, "\n");
97
0
    } else {
98
0
        BIO_printf(out, "%*sIssuer UID: <none>\n", indent, "");
99
0
    }
100
0
    return 1;
101
0
}
102
103
static int i2r_OBJECT_DIGEST_INFO(X509V3_EXT_METHOD *method,
104
    OSSL_OBJECT_DIGEST_INFO *odi,
105
    BIO *out, int indent)
106
0
{
107
0
    int64_t dot = 0;
108
0
#ifndef OPENSSL_NO_DEPRECATED_3_6
109
0
    int sig_nid;
110
0
    X509_ALGOR *digalg;
111
0
#endif
112
0
    ASN1_STRING *sig;
113
114
0
    if (odi == NULL) {
115
0
        ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
116
0
        return 0;
117
0
    }
118
0
    sig = &odi->objectDigest;
119
0
    if (!ASN1_ENUMERATED_get_int64(&dot, &odi->digestedObjectType)) {
120
0
        return 0;
121
0
    }
122
0
    switch (dot) {
123
0
    case OSSL_ODI_TYPE_PUBLIC_KEY:
124
0
        BIO_printf(out, "%*sDigest Type: Public Key\n", indent, "");
125
0
        break;
126
0
    case OSSL_ODI_TYPE_PUBLIC_KEY_CERT:
127
0
        BIO_printf(out, "%*sDigest Type: Public Key Certificate\n", indent, "");
128
0
        break;
129
0
    case OSSL_ODI_TYPE_OTHER:
130
0
        BIO_printf(out, "%*sDigest Type: Other\n", indent, "");
131
0
        break;
132
0
    }
133
0
    if (odi->otherObjectTypeID != NULL) {
134
0
        BIO_printf(out, "%*sDigest Type Identifier: ", indent, "");
135
0
        i2a_ASN1_OBJECT(out, odi->otherObjectTypeID);
136
0
        BIO_puts(out, "\n");
137
0
    }
138
0
    if (BIO_printf(out, "%*sSignature Algorithm: ", indent, "") <= 0)
139
0
        return 0;
140
0
    if (i2a_ASN1_OBJECT(out, odi->digestAlgorithm.algorithm) <= 0)
141
0
        return 0;
142
0
    BIO_puts(out, "\n");
143
0
    if (BIO_printf(out, "\n%*sSignature Value: ", indent, "") <= 0)
144
0
        return 0;
145
0
#ifndef OPENSSL_NO_DEPRECATED_3_6
146
0
    digalg = &odi->digestAlgorithm;
147
0
    sig_nid = OBJ_obj2nid(odi->digestAlgorithm.algorithm);
148
0
    if (sig_nid != NID_undef) {
149
0
        int pkey_nid, dig_nid;
150
0
        const EVP_PKEY_ASN1_METHOD *ameth;
151
0
        if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) {
152
0
            ameth = evp_pkey_asn1_find(pkey_nid);
153
0
            if (ameth && ameth->sig_print)
154
0
                return ameth->sig_print(out, digalg, sig, indent + 4, 0);
155
0
        }
156
0
    }
157
0
#endif
158
0
    if (BIO_write(out, "\n", 1) != 1)
159
0
        return 0;
160
0
    if (sig)
161
0
        return X509_signature_dump(out, sig, indent + 4);
162
0
    return 1;
163
0
}
164
165
static int i2r_TARGET_CERT(X509V3_EXT_METHOD *method,
166
    OSSL_TARGET_CERT *tc,
167
    BIO *out, int indent)
168
0
{
169
0
    BIO_printf(out, "%*s", indent, "");
170
0
    if (tc->targetCertificate != NULL) {
171
0
        BIO_puts(out, "Target Certificate:\n");
172
0
        i2r_ISSUER_SERIAL(method, tc->targetCertificate, out, indent + 2);
173
0
    }
174
0
    if (tc->targetName != NULL) {
175
0
        BIO_printf(out, "%*sTarget Name: ", indent, "");
176
0
        GENERAL_NAME_print(out, tc->targetName);
177
0
        BIO_puts(out, "\n");
178
0
    }
179
0
    if (tc->certDigestInfo != NULL) {
180
0
        BIO_printf(out, "%*sCertificate Digest Info:\n", indent, "");
181
0
        i2r_OBJECT_DIGEST_INFO(method, tc->certDigestInfo, out, indent + 2);
182
0
    }
183
0
    BIO_puts(out, "\n");
184
0
    return 1;
185
0
}
186
187
static int i2r_TARGET(X509V3_EXT_METHOD *method,
188
    OSSL_TARGET *target,
189
    BIO *out, int indent)
190
0
{
191
0
    switch (target->type) {
192
0
    case OSSL_TGT_TARGET_NAME:
193
0
        BIO_printf(out, "%*sTarget Name: ", indent, "");
194
0
        GENERAL_NAME_print(out, target->choice.targetName);
195
0
        BIO_puts(out, "\n");
196
0
        break;
197
0
    case OSSL_TGT_TARGET_GROUP:
198
0
        BIO_printf(out, "%*sTarget Group: ", indent, "");
199
0
        GENERAL_NAME_print(out, target->choice.targetGroup);
200
0
        BIO_puts(out, "\n");
201
0
        break;
202
0
    case OSSL_TGT_TARGET_CERT:
203
0
        BIO_printf(out, "%*sTarget Cert:\n", indent, "");
204
0
        i2r_TARGET_CERT(method, target->choice.targetCert, out, indent + 2);
205
0
        break;
206
0
    }
207
0
    return 1;
208
0
}
209
210
static int i2r_TARGETS(X509V3_EXT_METHOD *method,
211
    OSSL_TARGETS *targets,
212
    BIO *out, int indent)
213
0
{
214
0
    int i;
215
0
    OSSL_TARGET *target;
216
217
0
    for (i = 0; i < sk_OSSL_TARGET_num(targets); i++) {
218
0
        BIO_printf(out, "%*sTarget:\n", indent, "");
219
0
        target = sk_OSSL_TARGET_value(targets, i);
220
0
        i2r_TARGET(method, target, out, indent + 2);
221
0
    }
222
0
    return 1;
223
0
}
224
225
static int i2r_TARGETING_INFORMATION(X509V3_EXT_METHOD *method,
226
    OSSL_TARGETING_INFORMATION *tinfo,
227
    BIO *out, int indent)
228
0
{
229
0
    int i;
230
0
    OSSL_TARGETS *targets;
231
232
0
    for (i = 0; i < sk_OSSL_TARGETS_num(tinfo); i++) {
233
0
        BIO_printf(out, "%*sTargets:\n", indent, "");
234
0
        targets = sk_OSSL_TARGETS_value(tinfo, i);
235
0
        i2r_TARGETS(method, targets, out, indent + 2);
236
0
    }
237
0
    return 1;
238
0
}
239
240
const X509V3_EXT_METHOD ossl_v3_targeting_information = {
241
    NID_target_information, 0, ASN1_ITEM_ref(OSSL_TARGETING_INFORMATION),
242
    0, 0, 0, 0,
243
    0,
244
    0,
245
    0, 0,
246
    (X509V3_EXT_I2R)i2r_TARGETING_INFORMATION,
247
    0,
248
    NULL
249
};