Debian Codegraph @

src/sbsign/authenticode.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <openssl/asn1t.h>
4
5 #include "authenticode.h"
6 #include "crypto-util.h"
7
8 /* OpenSSL's ASN1_SEQUENCE/ASN1_CHOICE/IMPLEMENT_ASN1_FUNCTIONS macros expand to code that references
9 * libcrypto symbols directly, which would force us to link this object against libcrypto and defeat the
10 * dlopen approach used everywhere else. We work around that in two different ways depending on where the
11 * reference appears in the macro expansion:
12 *
13 * - ASN1_item_new/ASN1_item_free/ASN1_item_d2i/ASN1_item_i2d are called from the bodies of the functions
14 * generated by IMPLEMENT_ASN1_FUNCTIONS. We can simply #define them to the matching sym_* variants, so
15 * the generated bodies end up calling our dlopen'd pointers.
16 *
17 * - ASN1_ANY_it/ASN1_BIT_STRING_it/... (the "_it" getters) are embedded as function pointers in the
18 * static const ASN1_ADB / ASN1_TEMPLATE tables emitted by the ASN1_SEQUENCE/ASN1_CHOICE macros. Static
19 * initializers can only contain constant expressions, so we can't point them at the sym_* variables
20 * (which are non-const globals filled in at dlopen time). Instead we define static trampoline
21 * functions (openssl_ASN1_*_it) whose addresses are constant, each forwarding to the corresponding
22 * sym_* pointer at runtime, and #define the OpenSSL names to our trampolines before the tables are
23 * emitted.
24 *
25 * Note that this file must only call these redefined macros indirectly, via the IMPLEMENT_ASN1_FUNCTIONS
26 * expansions, and callers of those generated wrappers (e.g. sbsign.c) must have dlopen_libcrypto()
27 * before invoking them, otherwise the sym_* pointers will still be NULL. */
28
29 static const ASN1_ITEM* openssl_ASN1_ANY_it(void) {
30 assert(sym_ASN1_ANY_it);
31 return sym_ASN1_ANY_it();
32 }
33
34 static const ASN1_ITEM* openssl_ASN1_BIT_STRING_it(void) {
35 assert(sym_ASN1_BIT_STRING_it);
36 return sym_ASN1_BIT_STRING_it();
37 }
38
39 static const ASN1_ITEM* openssl_ASN1_BMPSTRING_it(void) {
40 assert(sym_ASN1_BMPSTRING_it);
41 return sym_ASN1_BMPSTRING_it();
42 }
43
44 static const ASN1_ITEM* openssl_ASN1_IA5STRING_it(void) {
45 assert(sym_ASN1_IA5STRING_it);
46 return sym_ASN1_IA5STRING_it();
47 }
48
49 static const ASN1_ITEM* openssl_ASN1_OBJECT_it(void) {
50 assert(sym_ASN1_OBJECT_it);
51 return sym_ASN1_OBJECT_it();
52 }
53
54 static const ASN1_ITEM* openssl_ASN1_OCTET_STRING_it(void) {
55 assert(sym_ASN1_OCTET_STRING_it);
56 return sym_ASN1_OCTET_STRING_it();
57 }
58
59 #define ASN1_item_new sym_ASN1_item_new
60 #define ASN1_item_free sym_ASN1_item_free
61 #define ASN1_item_d2i sym_ASN1_item_d2i
62 #define ASN1_item_i2d sym_ASN1_item_i2d
63 #define ASN1_ANY_it openssl_ASN1_ANY_it
64 #define ASN1_BIT_STRING_it openssl_ASN1_BIT_STRING_it
65 #define ASN1_BMPSTRING_it openssl_ASN1_BMPSTRING_it
66 #define ASN1_IA5STRING_it openssl_ASN1_IA5STRING_it
67 #define ASN1_OBJECT_it openssl_ASN1_OBJECT_it
68 #define ASN1_OCTET_STRING_it openssl_ASN1_OCTET_STRING_it
69
70 ASN1_SEQUENCE(SpcAttributeTypeAndOptionalValue) = {
71 ASN1_SIMPLE(SpcAttributeTypeAndOptionalValue, type, ASN1_OBJECT),
72 ASN1_OPT(SpcAttributeTypeAndOptionalValue, value, ASN1_ANY)
73 } ASN1_SEQUENCE_END(SpcAttributeTypeAndOptionalValue);
74
75 IMPLEMENT_ASN1_FUNCTIONS(SpcAttributeTypeAndOptionalValue);
76
77 ASN1_SEQUENCE(AlgorithmIdentifier) = {
78 ASN1_SIMPLE(AlgorithmIdentifier, algorithm, ASN1_OBJECT),
79 ASN1_OPT(AlgorithmIdentifier, parameters, ASN1_ANY)
80 } ASN1_SEQUENCE_END(AlgorithmIdentifier)
81
82 IMPLEMENT_ASN1_FUNCTIONS(AlgorithmIdentifier);
83
84 ASN1_SEQUENCE(DigestInfo) = {
85 ASN1_SIMPLE(DigestInfo, digestAlgorithm, AlgorithmIdentifier),
86 ASN1_SIMPLE(DigestInfo, digest, ASN1_OCTET_STRING)
87 } ASN1_SEQUENCE_END(DigestInfo);
88
89 IMPLEMENT_ASN1_FUNCTIONS(DigestInfo);
90
91 ASN1_SEQUENCE(SpcIndirectDataContent) = {
92 ASN1_SIMPLE(SpcIndirectDataContent, data, SpcAttributeTypeAndOptionalValue),
93 ASN1_SIMPLE(SpcIndirectDataContent, messageDigest, DigestInfo)
94 } ASN1_SEQUENCE_END(SpcIndirectDataContent);
95
96 IMPLEMENT_ASN1_FUNCTIONS(SpcIndirectDataContent);
97
98 ASN1_CHOICE(SpcString) = {
99 ASN1_IMP_OPT(SpcString, value.unicode, ASN1_BMPSTRING, 0),
100 ASN1_IMP_OPT(SpcString, value.ascii, ASN1_IA5STRING, 1)
101 } ASN1_CHOICE_END(SpcString);
102
103 IMPLEMENT_ASN1_FUNCTIONS(SpcString);
104
105 ASN1_SEQUENCE(SpcSerializedObject) = {
106 ASN1_SIMPLE(SpcSerializedObject, classId, ASN1_OCTET_STRING),
107 ASN1_SIMPLE(SpcSerializedObject, serializedData, ASN1_OCTET_STRING)
108 } ASN1_SEQUENCE_END(SpcSerializedObject);
109
110 IMPLEMENT_ASN1_FUNCTIONS(SpcSerializedObject);
111
112 ASN1_CHOICE(SpcLink) = {
113 ASN1_IMP_OPT(SpcLink, value.url, ASN1_IA5STRING, 0),
114 ASN1_IMP_OPT(SpcLink, value.moniker, SpcSerializedObject, 1),
115 ASN1_EXP_OPT(SpcLink, value.file, SpcString, 2)
116 } ASN1_CHOICE_END(SpcLink);
117
118 IMPLEMENT_ASN1_FUNCTIONS(SpcLink);
119
120 ASN1_SEQUENCE(SpcPeImageData) = {
121 ASN1_SIMPLE(SpcPeImageData, flags, ASN1_BIT_STRING),
122 ASN1_EXP_OPT(SpcPeImageData, file, SpcLink, 0)
123 } ASN1_SEQUENCE_END(SpcPeImageData)
124
125 IMPLEMENT_ASN1_FUNCTIONS(SpcPeImageData);