Coverage Report

Created: 2025-07-23 06:08

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