Coverage Report

Created: 2025-12-31 06:58

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