Skip to content

Commit bedecec

Browse files
committed
Improve Open SSL includes (#6240)
Signed-off-by: Miguel Company <[email protected]> (cherry picked from commit 338f840)
1 parent 2279f8a commit bedecec

File tree

5 files changed

+48
-22
lines changed

5 files changed

+48
-22
lines changed

src/cpp/security/OpenSSLInit.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include <cstdint>
1516
#include <memory>
1617

17-
#include <openssl/evp.h>
18-
#include <openssl/engine.h>
19-
#include <openssl/rand.h>
20-
#include <openssl/err.h>
18+
#include <openssl/crypto.h>
2119

2220
namespace eprosima {
2321
namespace fastdds {

src/cpp/security/artifact_providers/FileProvider.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818

1919
#include <security/artifact_providers/FileProvider.hpp>
2020

21+
#include <openssl/err.h>
22+
#include <openssl/ssl.h>
23+
2124
#include <cassert>
2225
#include <cstring>
2326
#include <iostream>

src/cpp/security/artifact_providers/FileProvider.hpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,12 @@
2020
#define _SECURITY_ARTIFACTPROVIDERS_FILEPROVIDER_HPP_
2121

2222
#include <functional>
23+
#include <string>
2324

24-
#include <openssl/engine.h>
25-
#include <openssl/err.h>
26-
#include <openssl/ssl.h>
25+
#include <openssl/types.h>
2726

2827
#include <rtps/security/exceptions/SecurityException.h>
2928

30-
3129
namespace eprosima {
3230
namespace fastdds {
3331
namespace rtps {
@@ -63,10 +61,10 @@ class FileProvider
6361

6462
};
6563

66-
} // namespace detail
67-
} //namespace security
68-
} //namespace rtps
69-
} //namespace fastdds
70-
} //namespace eprosima
64+
} // namespace detail
65+
} // namespace security
66+
} // namespace rtps
67+
} // namespace fastdds
68+
} // namespace eprosima
7169

7270
#endif // _SECURITY_ARTIFACTPROVIDERS_FILEPROVIDER_HPP_

src/cpp/security/artifact_providers/Pkcs11Provider.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@
2626
#include <security/artifact_providers/Pkcs11Provider.hpp>
2727

2828
#include <iostream>
29+
#include <string>
30+
31+
#include <openssl/conf.h>
32+
#include <openssl/err.h>
33+
#include <openssl/ssl.h>
34+
#include <openssl/types.h>
35+
36+
#if !defined(OPENSSL_NO_ENGINE)
37+
#include <openssl/engine.h>
38+
#include <openssl/ui.h>
39+
#endif // !defined(OPENSSL_NO_ENGINE)
2940

3041
#include <fastdds/dds/log/Log.hpp>
3142
#include <utils/SystemInfo.hpp>
@@ -42,6 +53,8 @@ namespace rtps {
4253
namespace security {
4354
namespace detail {
4455

56+
#if !defined(OPENSSL_NO_ENGINE)
57+
4558
constexpr const char* FASTDDS_PKCS11_PIN = "FASTDDS_PKCS11_PIN";
4659
constexpr const char* PKCS11_ENGINE_ID = "pkcs11";
4760

@@ -79,8 +92,15 @@ static int ui_close(
7992
return UI_method_get_closer(UI_OpenSSL())(ui);
8093
}
8194

95+
#endif // !defined(OPENSSL_NO_ENGINE)
96+
8297
Pkcs11Provider::Pkcs11Provider()
8398
{
99+
#if defined(OPENSSL_NO_ENGINE)
100+
has_initialization_error_ = true;
101+
initialization_exception_ =
102+
_SecurityException_(std::string("Cannot retrieve 'pkcs11' engine because 'OPENSSL_NO_ENGINE' is defined"));
103+
#else
84104
SSL_load_error_strings(); /* readable error messages */
85105
SSL_library_init(); /* initialize library */
86106

@@ -123,17 +143,20 @@ Pkcs11Provider::Pkcs11Provider()
123143
ENGINE_free(pkcs11_);
124144
return;
125145
}
146+
#endif // defined(OPENSSL_NO_ENGINE)
126147
}
127148

128149
Pkcs11Provider::~Pkcs11Provider()
129150
{
151+
#if !defined(OPENSSL_NO_ENGINE)
130152
ENGINE_finish(pkcs11_);
131153
ENGINE_free(pkcs11_);
132154

133155
if (ui_method_)
134156
{
135157
UI_destroy_method(ui_method_);
136158
}
159+
#endif // !defined(OPENSSL_NO_ENGINE)
137160
}
138161

139162
EVP_PKEY* Pkcs11Provider::load_private_key(
@@ -142,6 +165,12 @@ EVP_PKEY* Pkcs11Provider::load_private_key(
142165
const std::string& /*password*/,
143166
SecurityException& exception)
144167
{
168+
#if defined(OPENSSL_NO_ENGINE)
169+
static_cast<void>(certificate);
170+
static_cast<void>(pkey);
171+
exception = initialization_exception_;
172+
return nullptr;
173+
#else
145174
if (has_initialization_error_)
146175
{
147176
exception = initialization_exception_;
@@ -165,6 +194,7 @@ EVP_PKEY* Pkcs11Provider::load_private_key(
165194
}
166195

167196
return returnedValue;
197+
#endif // defined(OPENSSL_NO_ENGINE)
168198
}
169199

170200
} // namespace detail

src/cpp/security/artifact_providers/Pkcs11Provider.hpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#ifndef _SECURITY_ARTIFACTPROVIDERS_PKCS11PROVIDER_HPP_
2020
#define _SECURITY_ARTIFACTPROVIDERS_PKCS11PROVIDER_HPP_
2121

22-
#include <openssl/engine.h>
23-
#include <openssl/err.h>
24-
#include <openssl/ssl.h>
22+
#include <string>
23+
24+
#include <openssl/types.h>
2525

2626
#include <rtps/security/exceptions/SecurityException.h>
2727

@@ -48,16 +48,13 @@ class Pkcs11Provider
4848

4949
private:
5050

51-
EVP_PKEY* load_private_key_impl(
52-
X509* certificate,
53-
const std::string& file,
54-
const std::string& password,
55-
SecurityException& exception);
56-
5751
SecurityException initialization_exception_;
5852
bool has_initialization_error_ = false;
53+
54+
#if !defined(OPENSSL_NO_ENGINE)
5955
ENGINE* pkcs11_ = nullptr;
6056
UI_METHOD* ui_method_ = nullptr;
57+
#endif // !defined(OPENSSL_NO_ENGINE)
6158
};
6259

6360
} // namespace detail

0 commit comments

Comments
 (0)