Vanetza
 
Loading...
Searching...
No Matches
backend_openssl.cpp
1#include <vanetza/security/backend_openssl.hpp>
2#include <vanetza/security/key_type.hpp>
3#include <vanetza/security/openssl_wrapper.hpp>
4#include <vanetza/security/v2/public_key.hpp>
5#include <vanetza/security/v2/signature.hpp>
6#include <openssl/bn.h>
7#include <openssl/ec.h>
8#include <openssl/ecdsa.h>
9#include <openssl/obj_mac.h>
10#include <openssl/sha.h>
11#include <cassert>
12
13namespace vanetza
14{
15namespace security
16{
17
18namespace
19{
20
21int openssl_nid(KeyType key)
22{
23 int nid;
24 switch (key) {
25 case KeyType::NistP256:
26 nid = NID_X9_62_prime256v1;
27 break;
28 case KeyType::BrainpoolP256r1:
29 nid = NID_brainpoolP256r1;
30 break;
31 case KeyType::BrainpoolP384r1:
32 nid = NID_brainpoolP384r1;
33 break;
34 default:
35 nid = NID_undef;
36 break;
37 }
38 return nid;
39}
40
41} // namespace
42
43BackendOpenSsl::BackendOpenSsl()
44{
45#if OPENSSL_API_COMPAT < 0x10100000L
46 ERR_load_crypto_strings();
47#else
48 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, nullptr);
49#endif
50}
51
53{
54 auto priv_key = internal_private_key(key);
55 auto digest = calculate_sha256_digest(data);
56
57 // sign message data represented by the digest
58 openssl::Signature signature { ECDSA_do_sign(digest.data(), digest.size(), priv_key) };
59#if OPENSSL_API_COMPAT < 0x10100000L
60 const BIGNUM* sig_r = signature->r;
61 const BIGNUM* sig_s = signature->s;
62#else
63 const BIGNUM* sig_r = nullptr;
64 const BIGNUM* sig_s = nullptr;
65 ECDSA_SIG_get0(signature, &sig_r, &sig_s);
66#endif
67
68 EcdsaSignature ecdsa_signature;
69 X_Coordinate_Only coordinate;
70
71 if (sig_r && sig_s) {
72 const size_t len = field_size(v2::PublicKeyAlgorithm::ECDSA_NISTP256_With_SHA256);
73
74 const auto num_bytes_s = BN_num_bytes(sig_s);
75 assert(len >= static_cast<size_t>(num_bytes_s));
76 ecdsa_signature.s.resize(len, 0x00);
77 BN_bn2bin(sig_s, ecdsa_signature.s.data() + len - num_bytes_s);
78
79 const auto num_bytes_r = BN_num_bytes(sig_r);
80 assert(len >= static_cast<size_t>(num_bytes_r));
81 coordinate.x.resize(len, 0x00);
82 BN_bn2bin(sig_r, coordinate.x.data() + len - num_bytes_r);
83 } else {
84 throw openssl::Exception();
85 }
86
87 ecdsa_signature.R = std::move(coordinate);
88 return ecdsa_signature;
89}
90
91bool BackendOpenSsl::verify_data(const ecdsa256::PublicKey& key, const ByteBuffer& data, const EcdsaSignature& sig)
92{
93 auto digest = calculate_sha256_digest(data);
94 auto pub = internal_public_key(key);
95 openssl::Signature signature(sig);
96
97 return (ECDSA_do_verify(digest.data(), digest.size(), signature, pub) == 1);
98}
99
100bool BackendOpenSsl::verify_digest(const PublicKey& gpub, const ByteBuffer& digest, const Signature& gsig)
101{
102 if (gpub.type != gsig.type) {
103 return false;
104 }
105
107 openssl::Signature sig { gsig };
108 return ECDSA_do_verify(digest.data(), digest.size(), sig, pub) == 1;
109}
110
111boost::optional<Uncompressed> BackendOpenSsl::decompress_point(const EccPoint& ecc_point)
112{
113 struct DecompressionVisitor : public boost::static_visitor<bool>
114 {
115 bool operator()(const X_Coordinate_Only&)
116 {
117 return false;
118 }
119
120 bool operator()(const Compressed_Lsb_Y_0& p)
121 {
122 return decompress(p.x, 0);
123 }
124
125 bool operator()(const Compressed_Lsb_Y_1& p)
126 {
127 return decompress(p.x, 1);
128 }
129
130 bool operator()(const Uncompressed& p)
131 {
132 result = p;
133 return true;
134 }
135
136 bool decompress(const ByteBuffer& x, int y_bit)
137 {
139 openssl::BigNumber x_coordinate(x);
140 openssl::Group group(NID_X9_62_prime256v1);
141 openssl::Point point(group);
142 openssl::BigNumber y_coordinate;
143
144 result.x = x;
145 result.y.resize(result.x.size());
146
147#if OPENSSL_API_COMPAT < 0x10101000L
148 EC_POINT_set_compressed_coordinates_GFp(group, point, x_coordinate, y_bit, ctx);
149 EC_POINT_get_affine_coordinates_GFp(group, point, nullptr, y_coordinate, ctx);
150 std::size_t y_coordinate_bytes = BN_num_bytes(y_coordinate);
151 if (y_coordinate_bytes <= result.y.size()) {
152 BN_bn2bin(y_coordinate, result.y.data() + (result.y.size() - y_coordinate_bytes));
153 return true;
154 } else {
155 return false;
156 }
157#else
158 EC_POINT_set_compressed_coordinates(group, point, x_coordinate, y_bit, ctx);
159 EC_POINT_get_affine_coordinates(group, point, nullptr, y_coordinate, ctx);
160 return (BN_bn2binpad(y_coordinate, result.y.data(), result.y.size()) != -1);
161#endif
162 }
163
164 Uncompressed result;
165 };
166
167 DecompressionVisitor visitor;
168 if (boost::apply_visitor(visitor, ecc_point)) {
169 return visitor.result;
170 } else {
171 return boost::none;
172 }
173}
174
175ByteBuffer BackendOpenSsl::calculate_hash(KeyType key, const ByteBuffer& data)
176{
177 ByteBuffer result;
178 switch (key)
179 {
180 case KeyType::NistP256:
181 case KeyType::BrainpoolP256r1: {
182 auto digest = calculate_sha256_digest(data);
183 result.assign(digest.begin(), digest.end());
184 break;
185 }
186 case KeyType::BrainpoolP384r1: {
187 auto digest = calculate_sha384_digest(data);
188 result.assign(digest.begin(), digest.end());
189 break;
190 }
191 default:
192 break;
193 }
194 return result;
195}
196
197std::array<uint8_t, 32> BackendOpenSsl::calculate_sha256_digest(const ByteBuffer& data) const
198{
199 static_assert(SHA256_DIGEST_LENGTH == 32, "Unexpected length of SHA256 digest");
200
201 std::array<uint8_t, 32> digest;
202 SHA256_CTX ctx;
203 SHA256_Init(&ctx);
204 SHA256_Update(&ctx, data.data(), data.size());
205 SHA256_Final(digest.data(), &ctx);
206 return digest;
207}
208
209std::array<uint8_t, 48> BackendOpenSsl::calculate_sha384_digest(const ByteBuffer& data) const
210{
211 static_assert(SHA384_DIGEST_LENGTH == 48, "Unexpected length of SHA384 digest");
212
213 std::array<uint8_t, 48> digest;
214 SHA384(data.data(), data.size(), digest.data());
215 return digest;
216}
217
219{
220 openssl::Key key(NID_X9_62_prime256v1);
221 openssl::BigNumber prv(generic.key);
222 EC_KEY_set_private_key(key, prv);
223
224 // OpenSSL requires public key, so we recreate it from private key
226 const EC_GROUP* group = EC_KEY_get0_group(key);
227 openssl::Point pub(group);
228 openssl::check(EC_POINT_mul(group, pub, prv, nullptr, nullptr, ctx));
229 EC_KEY_set_public_key(key, pub);
230
231 openssl::check(EC_KEY_check_key(key));
232 return key;
233}
234
236{
237 openssl::Key key(NID_X9_62_prime256v1);
238 openssl::BigNumber x(generic.x);
239 openssl::BigNumber y(generic.y);
240 EC_KEY_set_public_key_affine_coordinates(key, x, y);
241
242 openssl::check(EC_KEY_check_key(key));
243 return key;
244}
245
247{
248 openssl::Key key(openssl_nid(generic.type));
249 openssl::Point point = internal_ec_point(generic);
250 EC_KEY_set_public_key(key, point);
251
252 openssl::check(EC_KEY_check_key(key));
253 return key;
254}
255
257{
258 openssl::Group group { openssl_nid(generic.type) };
259 openssl::Point point { group };
261
262 switch (generic.compression)
263 {
264 case KeyCompression::NoCompression:
265 EC_POINT_set_affine_coordinates(group, point,
266 openssl::BigNumber { generic.x }, openssl::BigNumber {generic.y },
267 bn_ctx);
268 break;
269 case KeyCompression::Y0:
270 EC_POINT_set_compressed_coordinates(group, point, openssl::BigNumber { generic.x }, 0, bn_ctx);
271 break;
272 case KeyCompression::Y1:
273 EC_POINT_set_compressed_coordinates(group, point, openssl::BigNumber { generic.x }, 1, bn_ctx);
274 break;
275 default:
276 // no-op
277 break;
278 }
279
280 return point;
281}
282
283} // namespace security
284} // namespace vanetza
std::array< uint8_t, 32 > calculate_sha256_digest(const ByteBuffer &data) const
calculate SHA256 digest of data buffer
bool verify_data(const ecdsa256::PublicKey &public_key, const ByteBuffer &data, const EcdsaSignature &sig) override
bool verify_digest(const PublicKey &, const ByteBuffer &digest, const Signature &) override
std::array< uint8_t, 48 > calculate_sha384_digest(const ByteBuffer &data) const
calculate SHA384 digest of data buffer
openssl::Key internal_public_key(const ecdsa256::PublicKey &) const
convert to internal format of public key
openssl::Point internal_ec_point(const PublicKey &) const
convert to internal format of an EC point
EcdsaSignature sign_data(const ecdsa256::PrivateKey &private_key, const ByteBuffer &data_buffer) override
boost::optional< Uncompressed > decompress_point(const EccPoint &ecc_point) override
openssl::Key internal_private_key(const ecdsa256::PrivateKey &) const
convert to internal format of private key
Compressed_Lsb_Y_0 specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition: ecc_point.hpp:20
Compressed_Lsb_Y_1 specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition: ecc_point.hpp:26
EcdsaSignature specified in TS 103 097 v1.2.1, section 4.2.9.
Definition: signature.hpp:17
Uncompressed specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition: ecc_point.hpp:32
X_Coordinate_Only specified in TS 103 097 v1.2.1 in section 4.2.5.
Definition: ecc_point.hpp:14