Vanetza
Loading...
Searching...
No Matches
persistence.cpp
1#include <vanetza/common/serialization.hpp>
2#include <vanetza/security/persistence.hpp>
3#include <vanetza/security/v2/persistence.hpp>
4#include <boost/variant/get.hpp>
5#include <algorithm>
6#include <fstream>
7#include <stdexcept>
8
9#ifdef VANETZA_WITH_OPENSSL
10#include <vanetza/security/backend_openssl.hpp>
11#endif
12
13#ifdef VANETZA_WITH_CRYPTOPP
14#include <vanetza/security/backend_cryptopp.hpp>
15#endif
16
17namespace vanetza
18{
19namespace security
20{
21namespace v2
22{
23
24ecdsa256::KeyPair load_private_key_from_file(const std::string& key_path)
25{
26 const PrivateKey private_key = load_private_key_from_der_file(key_path);
27 if (private_key.type != KeyType::NistP256) {
28 // ETSI TS 103 097 v1.2.1 only defines NIST P-256 keys for v2 security.
29 throw std::runtime_error("v2 private key must use the NIST P-256 curve: " + key_path);
30 }
31
32#if defined(VANETZA_WITH_OPENSSL)
33 const security::PublicKey public_key = openssl::derive_public_key(private_key);
34#elif defined(VANETZA_WITH_CRYPTOPP)
35 const security::PublicKey public_key = cryptopp::derive_public_key(private_key);
36#else
37# warning "no crypto backend available for v2 private key loading"
38 const security::PublicKey public_key;
39#endif
40
41 ecdsa256::KeyPair key_pair;
42 std::copy(private_key.key.begin(), private_key.key.end(), key_pair.private_key.key.begin());
43 std::copy(public_key.x.begin(), public_key.x.end(), key_pair.public_key.x.begin());
44 std::copy(public_key.y.begin(), public_key.y.end(), key_pair.public_key.y.begin());
45 return key_pair;
46}
47
48bool save_private_key_pkcs8_der(std::ostream& os, const ecdsa256::KeyPair& key_pair)
49{
50 // PKCS#8 PrivateKeyInfo wrapping SEC 1 ECPrivateKey for secp256r1
51 static const uint8_t header[] = {
52 0x30, 0x81, 0x87, /*< SEQUENCE, length 135 */
53 0x02, 0x01, 0x00, /*< INTEGER 0 (version) */
54 0x30, 0x13, /*< SEQUENCE (AlgorithmIdentifier) */
55 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01, /*< OID 1.2.840.10045.2.1 (ecPublicKey) */
56 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, /*< OID 1.2.840.10045.3.1.7 (secp256r1) */
57 0x04, 0x6d, /*< OCTET STRING, length 109 */
58 0x30, 0x6b, /*< SEQUENCE (ECPrivateKey), length 107 */
59 0x02, 0x01, 0x01, /*< INTEGER 1 (version) */
60 0x04, 0x20, /*< OCTET STRING, length 32 */
61 };
62 static const uint8_t pub_header[] = {
63 0xa1, 0x44, /*< [1] CONSTRUCTED, length 68 */
64 0x03, 0x42, /*< BIT STRING, length 66 */
65 0x00, /*< 0 unused bits */
66 0x04, /*< uncompressed point (x, y) */
67 };
68
69 os.write(reinterpret_cast<const char*>(header), sizeof(header));
70 os.write(reinterpret_cast<const char*>(key_pair.private_key.key.data()), key_pair.private_key.key.size());
71 os.write(reinterpret_cast<const char*>(pub_header), sizeof(pub_header));
72 os.write(reinterpret_cast<const char*>(key_pair.public_key.x.data()), key_pair.public_key.x.size());
73 os.write(reinterpret_cast<const char*>(key_pair.public_key.y.data()), key_pair.public_key.y.size());
74 return os.good();
75}
76
77PublicKey load_public_key_from_file(const std::string& key_path)
78{
79 PublicKey public_key;
80
81 std::ifstream key_src;
82 key_src.open(key_path, std::ios::in | std::ios::binary);
83 vanetza::InputArchive key_archive(key_src);
84 deserialize(key_archive, public_key);
85
86 return public_key;
87}
88
89void save_public_key_to_file(const std::string& key_path, const PublicKey& public_key)
90{
91 std::ofstream dest;
92 dest.open(key_path.c_str(), std::ios::out | std::ios::binary);
93
94 OutputArchive archive(dest);
95 serialize(archive, public_key);
96}
97
98Certificate load_certificate_from_file(const std::string& certificate_path)
99{
100 Certificate certificate;
101
102 std::ifstream certificate_src;
103 certificate_src.open(certificate_path, std::ios::in | std::ios::binary);
104 vanetza::InputArchive certificate_archive(certificate_src);
105 deserialize(certificate_archive, certificate);
106
107 return certificate;
108}
109
110void save_certificate_to_file(const std::string& certificate_path, const Certificate& certificate)
111{
112 std::ofstream dest;
113 dest.open(certificate_path.c_str(), std::ios::out | std::ios::binary);
114
115 OutputArchive archive(dest);
116 serialize(archive, certificate);
117}
118
119} // namespace v2
120} // namespace security
121} // namespace vanetza
described in TS 103 097 v1.2.1 (2015-06), section 6.1