Coverage Report

Created: 2026-03-09 06:55

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