Coverage Report

Created: 2024-07-27 06:36

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