Coverage Report

Created: 2025-12-10 06:24

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