/src/Fast-DDS/src/cpp/utils/license/LicenseTools.hpp
Line | Count | Source |
1 | | // Copyright 2025 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | /** |
16 | | * @file LicenseTools.hpp |
17 | | */ |
18 | | |
19 | | #ifndef UTILS_LICENSE__LICENSETOOLS_HPP_ |
20 | | #define UTILS_LICENSE__LICENSETOOLS_HPP_ |
21 | | |
22 | | #if HAVE_SECURITY |
23 | | #include <openssl/evp.h> |
24 | | #include <openssl/x509.h> |
25 | | #include <openssl/pem.h> |
26 | | #include <openssl/err.h> |
27 | | #include <openssl/rsa.h> |
28 | | #endif // HAVE_SECURITY |
29 | | |
30 | | #include <fastdds/rtps/common/Types.hpp> |
31 | | |
32 | | namespace eprosima { |
33 | | |
34 | | using fastdds::rtps::octet; |
35 | | |
36 | | /** |
37 | | * @brief Verify signature of data using eProsima's licensing public key. |
38 | | * |
39 | | * @param data Pointer to the data to verify. |
40 | | * @param data_length Length of the data to verify. |
41 | | * @param signature Pointer to the signature. |
42 | | * @param signature_length Length of the signature. |
43 | | * |
44 | | * @return true if the signature is valid, false otherwise. |
45 | | */ |
46 | | bool verify_safedds_signature( |
47 | | const octet* data, |
48 | | size_t data_length, |
49 | | const octet* signature, |
50 | | size_t signature_length) |
51 | 0 | { |
52 | | #if HAVE_SECURITY |
53 | | static const unsigned char pubkey_der[] = |
54 | | { |
55 | | 0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00, 0x4a, 0x01, 0xe9, 0xd4, |
56 | | 0x16, 0x79, 0xbd, 0x2e, 0x17, 0xeb, 0xc9, 0x68, 0x07, 0xd7, 0x65, 0x82, 0x0d, 0x56, 0x5d, 0x61, |
57 | | 0x8c, 0xab, 0x77, 0xdb, 0x55, 0xd2, 0xa1, 0x2e, 0x31, 0xaa, 0xdb, 0xaa |
58 | | }; |
59 | | |
60 | | struct OperationCTX |
61 | | { |
62 | | EVP_PKEY* pkey = nullptr; |
63 | | EVP_MD_CTX* md_ctx = nullptr; |
64 | | |
65 | | ~OperationCTX() |
66 | | { |
67 | | if (md_ctx != nullptr) |
68 | | { |
69 | | EVP_MD_CTX_free(md_ctx); |
70 | | } |
71 | | if (pkey != nullptr) |
72 | | { |
73 | | EVP_PKEY_free(pkey); |
74 | | } |
75 | | } |
76 | | |
77 | | }; |
78 | | |
79 | | OperationCTX ctx; |
80 | | |
81 | | const unsigned char* p = pubkey_der; |
82 | | ctx.pkey = d2i_PUBKEY(nullptr, &p, sizeof(pubkey_der)); |
83 | | if (ctx.pkey == nullptr) |
84 | | { |
85 | | return false; |
86 | | } |
87 | | |
88 | | ctx.md_ctx = EVP_MD_CTX_new(); |
89 | | if (ctx.md_ctx == nullptr) |
90 | | { |
91 | | return false; |
92 | | } |
93 | | |
94 | | if (EVP_DigestVerifyInit(ctx.md_ctx, nullptr, nullptr, nullptr, ctx.pkey) != 1) |
95 | | { |
96 | | return false; |
97 | | } |
98 | | |
99 | | int verify_result = EVP_DigestVerify(ctx.md_ctx, signature, signature_length, data, data_length); |
100 | | return verify_result == 1; |
101 | | #else |
102 | 0 | static_cast<void>(data); |
103 | 0 | static_cast<void>(data_length); |
104 | 0 | static_cast<void>(signature); |
105 | 0 | static_cast<void>(signature_length); |
106 | |
|
107 | 0 | return true; |
108 | 0 | #endif // HAVE_SECURITY |
109 | 0 | } |
110 | | |
111 | | } // namespace eprosima |
112 | | |
113 | | #endif // UTILS_LICENSE__LICENSETOOLS_HPP_ |