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