Skip to content

Commit 081937b

Browse files
authoredMar 16, 2025
CMake Build Support (johnpatek#2)
* added CMakeLists.txt to build on Linux and Windows
1 parent 57634f8 commit 081937b

File tree

3 files changed

+201
-14
lines changed

3 files changed

+201
-14
lines changed
 

‎CMakeLists.txt

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
3+
project(cpp-xml-security)
4+
5+
find_package(Threads REQUIRED)
6+
find_package(XercesC REQUIRED)
7+
find_package(OpenSSL REQUIRED)
8+
9+
# Define variables that control feature detection
10+
include(CheckIncludeFile)
11+
include(CheckSymbolExists)
12+
include(CheckCXXSourceCompiles)
13+
14+
15+
if(NOT WIN32)
16+
if(OPENSSL_FOUND)
17+
include_directories(${OPENSSL_INCLUDE_DIR})
18+
set(CMAKE_REQUIRED_LIBRARIES OpenSSL::Crypto OpenSSL::SSL)
19+
set(XSEC_HAVE_OPENSSL 1)
20+
set(XSEC_OPENSSL_HAVE_AES 1)
21+
set(XSEC_OPENSSL_HAVE_GCM 1)
22+
set(XSEC_OPENSSL_HAVE_EC 1)
23+
set(XSEC_OPENSSL_HAVE_SHA2 1)
24+
set(XSEC_OPENSSL_HAVE_MGF1 1)
25+
else()
26+
set(XSEC_HAVE_OPENSSL 0)
27+
set(XSEC_OPENSSL_HAVE_AES 0)
28+
set(XSEC_OPENSSL_HAVE_GCM 0)
29+
set(XSEC_OPENSSL_cHAVE_EC 0)
30+
set(XSEC_OPENSSL_HAVE_SHA2 0)
31+
set(XSEC_OPENSSL_HAVE_MGF1 0)
32+
endif()
33+
34+
# Check for getcwd(NULL, 0)
35+
check_symbol_exists(getcwd "unistd.h" XSEC_HAVE_GETCWD_DYN)
36+
37+
# Check for EVP_CIPHER_CTX_set_padding
38+
check_symbol_exists(EVP_CIPHER_CTX_set_padding "openssl/evp.h" XSEC_OPENSSL_CANSET_PADDING)
39+
40+
# Check if OpenSSL has const input buffers
41+
check_symbol_exists(X509_get_serialNumber "openssl/x509.h" XSEC_OPENSSL_CONST_BUFFERS)
42+
check_symbol_exists(d2i_X509 "openssl/x509.h" XSEC_OPENSSL_D2IX509_CONST_BUFFER)
43+
44+
# Check if OpenSSL has CRYPTO_cleanup_all_ex_data
45+
check_symbol_exists(CRYPTO_cleanup_all_ex_data "openssl/crypto.h" XSEC_OPENSSL_HAVE_CRYPTO_CLEANUP_ALL_EX_DATA)
46+
47+
# Generate the configured header
48+
configure_file(
49+
${CMAKE_CURRENT_SOURCE_DIR}/xsec/framework/XSECConfig.hpp.cmake.in
50+
${CMAKE_CURRENT_BINARY_DIR}/xsec/framework/XSECConfig.hpp
51+
)
52+
53+
# Ensure the generated header is in the include path
54+
include_directories(${CMAKE_CURRENT_BINARY_DIR}/xsec/framework)
55+
endif()
56+
57+
set(CANON_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xsec/canon)
58+
set(DSIG_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xsec/dsig)
59+
set(ENC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xsec/enc)
60+
set(OPENSSL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xsec/enc/OpenSSL)
61+
set(XSCRYPT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xsec/enc/XSCrypt)
62+
set(FRAMEWORK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xsec/framework)
63+
set(SAMPLES_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xsec/samples)
64+
set(TOOLS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xsec/tools)
65+
set(TRANSFORMERS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xsec/transformers)
66+
set(UTILS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xsec/utils)
67+
set(XENC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xsec/xenc)
68+
set(IMPL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/xsec/xenc/impl)
69+
70+
# Add source files
71+
file(
72+
GLOB
73+
XSEC_SOURCES
74+
${CANON_DIR}/*.cpp ${DSIG_DIR}/*.cpp ${ENC_DIR}/*.cpp ${OPENSSL_DIR}/*.cpp ${XSCRYPT_DIR}/*.cpp ${FRAMEWORK_DIR}/*.cpp ${TRANSFORMERS_DIR}/*.cpp ${UTILS_DIR}/*.cpp ${XENC_DIR}/*.cpp ${IMPL_DIR}/*.cpp)
75+
76+
message(STATUS "xerces library: ${XercesC_LIBRARY}")
77+
# Add libxml-security-c
78+
if(WIN32)
79+
add_library(xml-security-c-static STATIC ${XSEC_SOURCES})
80+
add_library(xml-security-c-dynamic SHARED ${XSEC_SOURCES})
81+
target_include_directories(
82+
xml-security-c-static
83+
PUBLIC
84+
${CMAKE_CURRENT_SOURCE_DIR}
85+
${XercesC_INCLUDE_DIRS}
86+
${OPENSSL_INCLUDE_DIR})
87+
target_include_directories(
88+
xml-security-c-dynamic
89+
PUBLIC
90+
${CMAKE_CURRENT_SOURCE_DIR}
91+
${XercesC_INCLUDE_DIRS}
92+
${OPENSSL_INCLUDE_DIR})
93+
target_link_libraries(xml-security-c-static PUBLIC ${XercesC_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} OpenSSL::Crypto OpenSSL::SSL)
94+
target_link_libraries(xml-security-c-dynamic PUBLIC ${XercesC_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} OpenSSL::Crypto OpenSSL::SSL)
95+
set_target_properties(xml-security-c-static PROPERTIES OUTPUT_NAME xml-security-c)
96+
set_target_properties(xml-security-c-dynamic PROPERTIES OUTPUT_NAME xml-security-c)
97+
else()
98+
add_library(xml-security-c SHARED ${XSEC_SOURCES})
99+
target_include_directories(
100+
xml-security-c
101+
PUBLIC
102+
${CMAKE_CURRENT_SOURCE_DIR}
103+
${XercesC_INCLUDE_DIRS}
104+
${OPENSSL_INCLUDE_DIR})
105+
target_link_libraries(xml-security-c PUBLIC ${XercesC_LIBRARY} ${CMAKE_THREAD_LIBS_INIT} OpenSSL::Crypto OpenSSL::SSL)
106+
endif()
107+
108+
if(OPENSSL_FOUND)
109+
if(WIN32)
110+
target_compile_definitions(xml-security-c-static PUBLIC XSEC_HAVE_OPENSSL=1 XSEC_BUILDING_LIBRARY=1)
111+
target_compile_definitions(xml-security-c-dynamic PUBLIC XSEC_HAVE_OPENSSL=1 XSEC_BUILDING_LIBRARY=1)
112+
else()
113+
target_compile_definitions(xml-security-c PUBLIC XSEC_HAVE_OPENSSL=1 XSEC_OPENSSL_HAVE_AES=1 XSEC_OPENSSL_HAVE_GCM=1 XSEC_OPENSSL_HAVE_EC=1 XSEC_OPENSSL_HAVE_SHA2=1 XSEC_OPENSSL_HAVE_MGF1=1)
114+
endif()
115+
endif()
116+
117+
add_executable(xsec-xtest ${TOOLS_DIR}/xtest/xtest.cpp)
118+
if(WIN32)
119+
target_link_libraries(xsec-xtest xml-security-c-static)
120+
else()
121+
target_link_libraries(xsec-xtest xml-security-c)
122+
endif()
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* copyright(c) 2025 John R Patek Sr
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*
21+
* xsec/framework/XSECConfig.hpp. Generated by CMake.
22+
*
23+
*/
24+
25+
/* Define if OpenSSL is in use. */
26+
#cmakedefine01 XSEC_HAVE_OPENSSL
27+
28+
/* Define to 1 if getcwd(NULL, 0) works. */
29+
#cmakedefine01 XSEC_HAVE_GETCWD_DYN
30+
31+
/* Define to 1 if OpenSSL has EVP_CIPHER_CTX_set_padding. */
32+
#cmakedefine01 XSEC_OPENSSL_CANSET_PADDING
33+
34+
/* Define to 1 if OpenSSL uses const input buffers. */
35+
#cmakedefine01 XSEC_OPENSSL_CONST_BUFFERS
36+
37+
/* Define to 1 if OpenSSL X509 API has const input buffer. */
38+
#cmakedefine01 XSEC_OPENSSL_D2IX509_CONST_BUFFER
39+
40+
/* Define to 1 if OpenSSL has full AES support. */
41+
#cmakedefine01 XSEC_OPENSSL_HAVE_AES
42+
43+
/* Define to 1 if OpenSSL has GCM support. */
44+
#cmakedefine01 XSEC_OPENSSL_HAVE_GCM
45+
46+
/* Define to 1 if OpenSSL has CRYPTO_cleanup_all_ex_data. */
47+
#cmakedefine01 XSEC_OPENSSL_HAVE_CRYPTO_CLEANUP_ALL_EX_DATA
48+
49+
/* Define to 1 if OpenSSL has EC support. */
50+
#cmakedefine01 XSEC_OPENSSL_HAVE_EC
51+
52+
/* Define to 1 if OpenSSL has SHA2 support. */
53+
#cmakedefine01 XSEC_OPENSSL_HAVE_SHA2
54+
55+
#include <xsec/framework/XSECVersion.hpp>

‎xsec/tools/xtest/xtest.cpp

+24-14
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
#include <memory.h>
3737
#include <iostream>
38+
#include <memory>
3839
#include <stdlib.h>
3940

4041
#include <xercesc/util/PlatformUtils.hpp>
@@ -69,6 +70,7 @@
6970
#include <xsec/enc/XSECCryptoSymmetricKey.hpp>
7071
#include <xsec/framework/XSECError.hpp>
7172
#include <xsec/framework/XSECProvider.hpp>
73+
#include <xsec/framework/XSECURIResolverXerces.hpp>
7274
#include <xsec/xenc/XENCCipher.hpp>
7375
#include <xsec/xenc/XENCEncryptedData.hpp>
7476
#include <xsec/xenc/XENCEncryptedKey.hpp>
@@ -433,6 +435,7 @@ void outputDoc(DOMImplementation * impl, DOMDocument * doc) {
433435
bool reValidateSig(DOMImplementation *impl, DOMDocument * inDoc, XSECCryptoKey *k) {
434436

435437
// Take a signature in DOM, serialise and re-validate
438+
std::unique_ptr<XSECURIResolver> resolver(new XSECURIResolverXerces());
436439

437440
try {
438441

@@ -486,6 +489,7 @@ bool reValidateSig(DOMImplementation *impl, DOMDocument * inDoc, XSECCryptoKey *
486489
*/
487490

488491
XSECProvider prov;
492+
prov.setDefaultURIResolver(resolver.get());
489493
DSIGSignature * sig = prov.newSignatureFromDOM(doc);
490494
sig->load();
491495
sig->setSigningKey(k);
@@ -525,7 +529,7 @@ void unitTestEnvelopingSignature(DOMImplementation * impl) {
525529
// This tests an enveloping signature as the root node
526530

527531
cerr << "Creating enveloping signature ... ";
528-
532+
std::unique_ptr<XSECURIResolver> resolver(new XSECURIResolverXerces());
529533
try {
530534

531535
// Create a document
@@ -538,6 +542,7 @@ void unitTestEnvelopingSignature(DOMImplementation * impl) {
538542
DSIGSignature *sig;
539543
DOMElement *sigNode;
540544

545+
prov.setDefaultURIResolver(resolver.get());
541546
sig = prov.newSignature();
542547
sig->setDSIGNSPrefix(MAKE_UNICODE_STRING("ds"));
543548
sig->setPrettyPrint(true);
@@ -634,7 +639,7 @@ void unitTestBase64NodeSignature(DOMImplementation * impl) {
634639
// This tests a normal signature with a reference to a Base64 element
635640

636641
cerr << "Creating a base64 Element reference ... ";
637-
642+
std::unique_ptr<XSECURIResolver> resolver(new XSECURIResolverXerces());
638643
try {
639644

640645
// Create a document
@@ -647,6 +652,7 @@ void unitTestBase64NodeSignature(DOMImplementation * impl) {
647652
DSIGSignature *sig;
648653
DOMElement *sigNode;
649654

655+
prov.setDefaultURIResolver(resolver.get());
650656
sig = prov.newSignature();
651657
sig->setDSIGNSPrefix(MAKE_UNICODE_STRING("ds"));
652658
sig->setPrettyPrint(true);
@@ -745,7 +751,7 @@ void unitTestLongSHA(DOMImplementation * impl) {
745751
// This tests an enveloping signature as the root node, using SHA224/256/384/512
746752

747753
cerr << "Creating long SHA references using HMAC... ";
748-
754+
std::unique_ptr<XSECURIResolver> resolver(new XSECURIResolverXerces());
749755
try {
750756

751757
// Create a document
@@ -759,6 +765,7 @@ void unitTestLongSHA(DOMImplementation * impl) {
759765
DOMElement *sigNode;
760766
DSIGReference *ref[4];
761767

768+
prov.setDefaultURIResolver(resolver.get());
762769
sig = prov.newSignature();
763770
sig->setDSIGNSPrefix(MAKE_UNICODE_STRING("ds"));
764771
sig->setPrettyPrint(true);
@@ -935,7 +942,7 @@ void unitTestLongSHA(DOMImplementation * impl) {
935942
void unitTestSig(DOMImplementation * impl, XSECCryptoKey * k, const XMLCh * AlgURI) {
936943

937944
// Given a specific RSA/EC key and particular algorithm URI, sign and validate a document
938-
945+
std::unique_ptr<XSECURIResolver> resolver(new XSECURIResolverXerces());
939946
try {
940947

941948
// Create a document
@@ -948,6 +955,7 @@ void unitTestSig(DOMImplementation * impl, XSECCryptoKey * k, const XMLCh * AlgU
948955
DSIGSignature *sig;
949956
DOMElement *sigNode;
950957

958+
prov.setDefaultURIResolver(resolver.get());
951959
sig = prov.newSignature();
952960
sig->setDSIGNSPrefix(MAKE_UNICODE_STRING("ds"));
953961
sig->setPrettyPrint(true);
@@ -1143,12 +1151,14 @@ void testSignature(DOMImplementation *impl) {
11431151
DOMElement *sigNode;
11441152
int refCount;
11451153

1154+
std::unique_ptr<XSECURIResolver> resolver(new XSECURIResolverXerces());
1155+
11461156
try {
11471157

11481158
/*
11491159
* Now we have a document, create a signature for it.
11501160
*/
1151-
1161+
prov.setDefaultURIResolver(resolver.get());
11521162
sig = prov.newSignature();
11531163
sig->setDSIGNSPrefix(MAKE_UNICODE_STRING("ds"));
11541164
sig->setPrettyPrint(true);
@@ -1608,15 +1618,15 @@ void unitTestElementContentEncrypt(DOMImplementation *impl, XSECCryptoKey * key,
16081618
// Create and execute cipher
16091619

16101620
XSECProvider prov;
1611-
XENCCipher * cipher;
1621+
std::unique_ptr<XENCCipher> cipher;
16121622

16131623
try {
16141624

16151625
/*
16161626
* Now we have a document, find the data node.
16171627
*/
16181628

1619-
cipher = prov.newCipher(doc);
1629+
cipher.reset(prov.newCipher(doc));
16201630
cipher->setXENCNSPrefix(MAKE_UNICODE_STRING("xenc"));
16211631
cipher->setPrettyPrint(true);
16221632

@@ -2394,40 +2404,40 @@ int main(int argc, char **argv) {
23942404

23952405
while (paramCount < argc) {
23962406

2397-
if (_stricmp(argv[paramCount], "--help") == 0 || _stricmp(argv[paramCount], "-h") == 0) {
2407+
if (strcmp(argv[paramCount], "--help") == 0 || strcmp(argv[paramCount], "-h") == 0) {
23982408
printUsage();
23992409
exit(0);
24002410
}
2401-
else if (_stricmp(argv[paramCount], "--print-docs") == 0 || _stricmp(argv[paramCount], "-p") == 0) {
2411+
else if (strcmp(argv[paramCount], "--print-docs") == 0 || strcmp(argv[paramCount], "-p") == 0) {
24022412
g_printDocs = true;
24032413
paramCount++;
24042414
}
24052415

2406-
else if (_stricmp(argv[paramCount], "--signature-only") == 0 || _stricmp(argv[paramCount], "-s") == 0) {
2416+
else if (strcmp(argv[paramCount], "--signature-only") == 0 || strcmp(argv[paramCount], "-s") == 0) {
24072417
doEncryptionTest = false;
24082418
doEncryptionUnitTests = false;
24092419
doSignatureUnitTests = false;
24102420
paramCount++;
24112421
}
2412-
else if (_stricmp(argv[paramCount], "--encryption-only") == 0 || _stricmp(argv[paramCount], "-e") == 0) {
2422+
else if (strcmp(argv[paramCount], "--encryption-only") == 0 || strcmp(argv[paramCount], "-e") == 0) {
24132423
doSignatureTest = false;
24142424
doEncryptionUnitTests = false;
24152425
doSignatureUnitTests = false;
24162426
paramCount++;
24172427
}
2418-
else if (_stricmp(argv[paramCount], "--encryption-unit-only") == 0 || _stricmp(argv[paramCount], "-u") == 0) {
2428+
else if (strcmp(argv[paramCount], "--encryption-unit-only") == 0 || strcmp(argv[paramCount], "-u") == 0) {
24192429
doEncryptionTest = false;
24202430
doSignatureTest = false;
24212431
doSignatureUnitTests = false;
24222432
paramCount++;
24232433
}
2424-
else if (_stricmp(argv[paramCount], "--signature-unit-only") == 0 || _stricmp(argv[paramCount], "-t") == 0) {
2434+
else if (strcmp(argv[paramCount], "--signature-unit-only") == 0 || strcmp(argv[paramCount], "-t") == 0) {
24252435
doEncryptionTest = false;
24262436
doSignatureTest = false;
24272437
doEncryptionUnitTests = false;
24282438
paramCount++;
24292439
}
2430-
else if (_stricmp(argv[paramCount], "--no-gcm") == 0) {
2440+
else if (strcmp(argv[paramCount], "--no-gcm") == 0) {
24312441
g_testGCM = false;
24322442
paramCount++;
24332443
}

0 commit comments

Comments
 (0)
Please sign in to comment.