Skip to content

Commit 3d44a66

Browse files
committed
Support more PKCS7 attributes for PE Authenticode
1 parent 469c799 commit 3d44a66

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+2301
-138
lines changed

api/python/lief/PE.pyi

+45
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import lief.PE.Header # type: ignore
1717
import lief.PE.Import # type: ignore
1818
import lief.PE.LoadConfiguration # type: ignore
1919
import lief.PE.LoadConfigurationV1 # type: ignore
20+
import lief.PE.MsCounterSign # type: ignore
2021
import lief.PE.OptionalHeader # type: ignore
2122
import lief.PE.Pogo # type: ignore
2223
import lief.PE.Relocation # type: ignore
@@ -311,12 +312,15 @@ class Attribute(lief.Object):
311312
CONTENT_TYPE: ClassVar[Attribute.TYPE] = ...
312313
GENERIC_TYPE: ClassVar[Attribute.TYPE] = ...
313314
MS_COUNTER_SIGN: ClassVar[Attribute.TYPE] = ...
315+
MS_PLATFORM_MANIFEST_BINARY_ID: ClassVar[Attribute.TYPE] = ...
314316
MS_SPC_NESTED_SIGN: ClassVar[Attribute.TYPE] = ...
315317
MS_SPC_STATEMENT_TYPE: ClassVar[Attribute.TYPE] = ...
316318
PKCS9_AT_SEQUENCE_NUMBER: ClassVar[Attribute.TYPE] = ...
317319
PKCS9_COUNTER_SIGNATURE: ClassVar[Attribute.TYPE] = ...
318320
PKCS9_MESSAGE_DIGEST: ClassVar[Attribute.TYPE] = ...
319321
PKCS9_SIGNING_TIME: ClassVar[Attribute.TYPE] = ...
322+
SIGNING_CERTIFICATE_V2: ClassVar[Attribute.TYPE] = ...
323+
SPC_RELAXED_PE_MARKER_CHECK: ClassVar[Attribute.TYPE] = ...
320324
SPC_SP_OPUS_INFO: ClassVar[Attribute.TYPE] = ...
321325
UNKNOWN: ClassVar[Attribute.TYPE] = ...
322326
__name__: str
@@ -1479,6 +1483,36 @@ class LoadConfigurationV9(LoadConfigurationV8):
14791483
def __init__(self) -> None: ...
14801484
def copy(self) -> lief.PE.LoadConfigurationV9: ...
14811485

1486+
class MsCounterSign(Attribute):
1487+
class it_const_crt:
1488+
def __init__(self, *args, **kwargs) -> None: ...
1489+
def __getitem__(self, arg: int, /) -> lief.PE.x509: ...
1490+
def __iter__(self) -> lief.PE.MsCounterSign.it_const_crt: ...
1491+
def __len__(self) -> int: ...
1492+
def __next__(self) -> lief.PE.x509: ...
1493+
1494+
class it_const_signers_t:
1495+
def __init__(self, *args, **kwargs) -> None: ...
1496+
def __getitem__(self, arg: int, /) -> lief.PE.SignerInfo: ...
1497+
def __iter__(self) -> lief.PE.MsCounterSign.it_const_signers_t: ...
1498+
def __len__(self) -> int: ...
1499+
def __next__(self) -> lief.PE.SignerInfo: ...
1500+
def __init__(self, *args, **kwargs) -> None: ...
1501+
@property
1502+
def certificates(self) -> lief.PE.MsCounterSign.it_const_crt: ...
1503+
@property
1504+
def content_info(self) -> lief.PE.ContentInfo: ...
1505+
@property
1506+
def digest_algorithm(self) -> lief.PE.ALGORITHMS: ...
1507+
@property
1508+
def signers(self) -> lief.PE.MsCounterSign.it_const_signers_t: ...
1509+
@property
1510+
def version(self) -> int: ...
1511+
1512+
class MsManifestBinaryID(Attribute):
1513+
manifest_id: str
1514+
def __init__(self, *args, **kwargs) -> None: ...
1515+
14821516
class MsSpcNestedSignature(Attribute):
14831517
def __init__(self, *args, **kwargs) -> None: ...
14841518
@property
@@ -1654,6 +1688,9 @@ class PKCS9SigningTime(Attribute):
16541688
@property
16551689
def time(self) -> list[int]: ...
16561690

1691+
class PKCS9TSTInfo(ContentInfo.Content):
1692+
def __init__(self, *args, **kwargs) -> None: ...
1693+
16571694
class ParserConfig:
16581695
parse_exports: bool
16591696
parse_imports: bool
@@ -2649,6 +2686,9 @@ class SignerInfo(lief.Object):
26492686
@property
26502687
def version(self) -> int: ...
26512688

2689+
class SigningCertificateV2(Attribute):
2690+
def __init__(self, *args, **kwargs) -> None: ...
2691+
26522692
class SpcIndirectData(ContentInfo.Content):
26532693
def __init__(self, *args, **kwargs) -> None: ...
26542694
@property
@@ -2658,6 +2698,11 @@ class SpcIndirectData(ContentInfo.Content):
26582698
@property
26592699
def file(self) -> str: ...
26602700

2701+
class SpcRelaxedPeMarkerCheck(Attribute):
2702+
def __init__(self, *args, **kwargs) -> None: ...
2703+
@property
2704+
def value(self) -> int: ...
2705+
26612706
class SpcSpOpusInfo(Attribute):
26622707
def __init__(self, *args, **kwargs) -> None: ...
26632708
@property

api/python/src/PE/enums.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -564,5 +564,6 @@ void init_enums(nb::module_& m) {
564564
.value(PY_ENUM(ALGORITHMS::SHA_256_ECDSA))
565565
.value(PY_ENUM(ALGORITHMS::SHA_384_ECDSA))
566566
.value(PY_ENUM(ALGORITHMS::SHA_512_ECDSA));
567+
567568
}
568569
}

api/python/src/PE/init.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "LIEF/PE/resources/LangCodeItem.hpp"
3535
#include "LIEF/PE/resources/langs.hpp"
3636
#include "LIEF/PE/signature/attributes.hpp"
37+
#include "LIEF/PE/signature/PKCS9TSTInfo.hpp"
3738
#include "LIEF/PE/signature/SpcIndirectData.hpp"
3839
#include "LIEF/PE/signature/GenericContent.hpp"
3940

@@ -99,11 +100,16 @@ void init_signature(nb::module_& m) {
99100
CREATE(GenericType, m);
100101
CREATE(MsSpcNestedSignature, m);
101102
CREATE(MsSpcStatementType, m);
103+
CREATE(MsManifestBinaryID, m);
102104
CREATE(PKCS9AtSequenceNumber, m);
103105
CREATE(PKCS9CounterSignature, m);
104106
CREATE(PKCS9MessageDigest, m);
105107
CREATE(PKCS9SigningTime, m);
106108
CREATE(SpcSpOpusInfo, m);
109+
CREATE(MsCounterSign, m);
110+
CREATE(SpcRelaxedPeMarkerCheck, m);
111+
CREATE(SigningCertificateV2, m);
112+
CREATE(PKCS9TSTInfo, m);
107113
}
108114

109115
void init_objects(nb::module_& m) {
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
target_sources(pyLIEF PRIVATE
2-
pySignerInfo.cpp
32
pyAttribute.cpp
4-
pyRsaInfo.cpp
5-
pyx509.cpp
63
pyContentInfo.cpp
74
pyGenericContent.cpp
8-
pySpcIndirectData.cpp
5+
pyPKCS9TSTInfo.cpp
6+
pyRsaInfo.cpp
97
pySignature.cpp
8+
pySignerInfo.cpp
109
pySpcIndirectData.cpp
11-
pyGenericContent.cpp
10+
pyx509.cpp
1211
)
1312
add_subdirectory(attributes)
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
target_sources(pyLIEF PRIVATE
2-
pyMsCounterSign.cpp
32
pyContentType.cpp
43
pyGenericType.cpp
5-
pySpcSpOpusInfo.cpp
6-
pyMsSpcStatementType.cpp
4+
pyMsCounterSign.cpp
5+
pyMsManifestBinaryID.cpp
76
pyMsSpcNestedSignature.cpp
8-
pyPKCS9SigningTime.cpp
9-
pyPKCS9MessageDigest.cpp
7+
pyMsSpcStatementType.cpp
108
pyPKCS9AtSequenceNumber.cpp
119
pyPKCS9CounterSignature.cpp
10+
pyPKCS9MessageDigest.cpp
11+
pyPKCS9SigningTime.cpp
12+
pySpcRelaxedPeMarkerCheck.cpp
13+
pySigningCertificateV2.cpp
14+
pySpcSpOpusInfo.cpp
1215
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* Copyright 2017 - 2023 R. Thomas
2+
* Copyright 2017 - 2023 Quarkslab
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "PE/pyPE.hpp"
17+
#include "pyIterator.hpp"
18+
19+
#include "LIEF/PE/signature/attributes/MsCounterSign.hpp"
20+
21+
#include <string>
22+
#include <sstream>
23+
#include <nanobind/stl/string.h>
24+
25+
namespace LIEF::PE::py {
26+
27+
template<>
28+
void create<MsCounterSign>(nb::module_& m) {
29+
using namespace LIEF::py;
30+
31+
nb::class_<MsCounterSign, Attribute> CounterSig(m, "MsCounterSign",
32+
R"delim(
33+
This class exposes the ms-counter-signature.
34+
)delim"_doc);
35+
36+
init_ref_iterator<MsCounterSign::it_certificates>(CounterSig, "it_const_crt");
37+
init_ref_iterator<MsCounterSign::it_signers>(CounterSig, "it_const_signers_t");
38+
39+
CounterSig
40+
.def_prop_ro("version", &MsCounterSign::version)
41+
.def_prop_ro("digest_algorithm", &MsCounterSign::digest_algorithm)
42+
.def_prop_ro("content_info", &MsCounterSign::content_info)
43+
.def_prop_ro("certificates",
44+
nb::overload_cast<>(&MsCounterSign::certificates),
45+
"Return an iterator over " RST_CLASS_REF(lief.PE.x509) " certificates"_doc,
46+
nb::keep_alive<0, 1>())
47+
.def_prop_ro("signers",
48+
nb::overload_cast<>(&MsCounterSign::signers),
49+
"Return an iterator over the signers (" RST_CLASS_REF(lief.PE.SignerInfo) ")"_doc,
50+
nb::keep_alive<0, 1>());
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* Copyright 2017 - 2023 R. Thomas
2+
* Copyright 2017 - 2023 Quarkslab
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "PE/pyPE.hpp"
17+
18+
#include "LIEF/PE/signature/attributes/MsManifestBinaryID.hpp"
19+
#include <nanobind/stl/string.h>
20+
21+
#include <string>
22+
#include <sstream>
23+
24+
namespace LIEF::PE::py {
25+
26+
template<>
27+
void create<MsManifestBinaryID>(nb::module_& m) {
28+
nb::class_<MsManifestBinaryID, Attribute>(m, "MsManifestBinaryID",
29+
R"delim(
30+
Interface over the structure described by the OID ``1.3.6.1.4.1.311.10.3.28`` (``szOID_PLATFORM_MANIFEST_BINARY_ID``)
31+
32+
The internal structure is not documented but we can infer the following structure:
33+
34+
.. code-block:: text
35+
36+
szOID_PLATFORM_MANIFEST_BINARY_ID ::= SET OF BinaryID
37+
38+
BinaryID ::= UTF8STRING
39+
40+
)delim")
41+
42+
.def_prop_rw("manifest_id",
43+
nb::overload_cast<>(&MsManifestBinaryID::manifest_id, nb::const_),
44+
nb::overload_cast<const std::string&>(&MsManifestBinaryID::manifest_id),
45+
"The manifest id")
46+
LIEF_DEFAULT_STR(MsManifestBinaryID);
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* Copyright 2017 - 2024 R. Thomas
2+
* Copyright 2017 - 2024 Quarkslab
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "PE/pyPE.hpp"
17+
18+
#include "LIEF/PE/signature/attributes/SigningCertificateV2.hpp"
19+
20+
namespace LIEF::PE::py {
21+
22+
template<>
23+
void create<SigningCertificateV2>(nb::module_& m) {
24+
nb::class_<SigningCertificateV2, Attribute>(m, "SigningCertificateV2",
25+
R"doc(
26+
.. code-block:: text
27+
28+
SigningCertificateV2 ::= SEQUENCE {
29+
certs SEQUENCE OF ESSCertIDv2,
30+
policies SEQUENCE OF PolicyInformation OPTIONAL
31+
}
32+
33+
ESSCertIDv2 ::= SEQUENCE {
34+
hashAlgorithm AlgorithmIdentifier DEFAULT {algorithm id-sha256},
35+
certHash OCTET STRING,
36+
issuerSerial IssuerSerial OPTIONAL
37+
}
38+
39+
IssuerSerial ::= SEQUENCE {
40+
issuer GeneralNames,
41+
serialNumber CertificateSerialNumber
42+
}
43+
44+
PolicyInformation ::= SEQUENCE {
45+
policyIdentifier OBJECT IDENTIFIER,
46+
policyQualifiers SEQUENCE SIZE (1..MAX) OF PolicyQualifierInfo OPTIONAL
47+
}
48+
)doc"_doc
49+
)
50+
;
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* Copyright 2017 - 2024 R. Thomas
2+
* Copyright 2017 - 2024 Quarkslab
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#include "PE/pyPE.hpp"
17+
18+
#include "LIEF/PE/signature/attributes/SpcRelaxedPeMarkerCheck.hpp"
19+
20+
namespace LIEF::PE::py {
21+
22+
template<>
23+
void create<SpcRelaxedPeMarkerCheck>(nb::module_& m) {
24+
nb::class_<SpcRelaxedPeMarkerCheck, Attribute>(m, "SpcRelaxedPeMarkerCheck",
25+
R"delim(
26+
)delim"_doc
27+
)
28+
.def_prop_ro("value", nb::overload_cast<>(&SpcRelaxedPeMarkerCheck::value, nb::const_))
29+
;
30+
}
31+
32+
}

api/python/src/PE/objects/signature/pyAttribute.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ void create<Attribute>(nb::module_& m) {
3939
ENTRY(MS_COUNTER_SIGN)
4040
ENTRY(MS_SPC_NESTED_SIGN)
4141
ENTRY(MS_SPC_STATEMENT_TYPE)
42+
ENTRY(SPC_RELAXED_PE_MARKER_CHECK)
43+
ENTRY(SIGNING_CERTIFICATE_V2)
44+
ENTRY(MS_PLATFORM_MANIFEST_BINARY_ID)
4245
ENTRY(PKCS9_AT_SEQUENCE_NUMBER)
4346
ENTRY(PKCS9_COUNTER_SIGNATURE)
4447
ENTRY(PKCS9_MESSAGE_DIGEST)

0 commit comments

Comments
 (0)