Vanetza
Loading...
Searching...
No Matches
public_key.cpp
1#include <vanetza/security/public_key.hpp>
2#include <boost/algorithm/hex.hpp>
3#include <cstdint>
4#include <iterator>
5
6namespace vanetza
7{
8namespace security
9{
10
11std::string canonical_hexstring(const PublicKey& key)
12{
13 const std::size_t expected_length = key_length(key.type);
14 if (expected_length == 0 || key.x.size() != expected_length) {
15 return {};
16 }
17
18 std::string input;
19 switch (key.compression) {
20 case KeyCompression::NoCompression:
21 if (key.y.size() != expected_length) {
22 return {};
23 }
24 input.push_back(*key.y.rbegin() % 2 == 0 ? 0x02 : 0x03);
25 break;
26 case KeyCompression::Y0:
27 input.push_back(0x02);
28 break;
29 case KeyCompression::Y1:
30 input.push_back(0x03);
31 break;
32 default:
33 return {};
34 }
35 std::copy(key.x.begin(), key.x.end(), std::back_inserter(input));
36 return boost::algorithm::hex(input);
37}
38
39ByteBuffer encode_subject_public_key_info(const PublicKey& key)
40{
41 const std::size_t expected_length = key_length(key.type);
42 if (expected_length == 0 || key.x.size() != expected_length) {
43 return {};
44 }
45
46 // RFC 5480: id-ecPublicKey OBJECT IDENTIFIER ::= { 1.2.840.10045.2.1 }
47 static const uint8_t oid_ec_public_key[] = {
48 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01
49 };
50 // RFC 5480: secp256r1 (NIST P-256) OID 1.2.840.10045.3.1.7
51 static const uint8_t oid_secp256r1[] = {
52 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07
53 };
54 // RFC 5639: brainpoolP256r1 OID 1.3.36.3.3.2.8.1.1.7
55 static const uint8_t oid_brainpoolP256r1[] = {
56 0x06, 0x09, 0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x07
57 };
58 // RFC 5639: brainpoolP384r1 OID 1.3.36.3.3.2.8.1.1.11
59 static const uint8_t oid_brainpoolP384r1[] = {
60 0x06, 0x09, 0x2B, 0x24, 0x03, 0x03, 0x02, 0x08, 0x01, 0x01, 0x0B
61 };
62
63 const uint8_t* named_curve_oid = nullptr;
64 std::size_t named_curve_oid_size = 0;
65 switch (key.type) {
66 case KeyType::NistP256:
67 named_curve_oid = oid_secp256r1;
68 named_curve_oid_size = sizeof(oid_secp256r1);
69 break;
70 case KeyType::BrainpoolP256r1:
71 named_curve_oid = oid_brainpoolP256r1;
72 named_curve_oid_size = sizeof(oid_brainpoolP256r1);
73 break;
74 case KeyType::BrainpoolP384r1:
75 named_curve_oid = oid_brainpoolP384r1;
76 named_curve_oid_size = sizeof(oid_brainpoolP384r1);
77 break;
78 default:
79 return {};
80 }
81
82 uint8_t ec_point_prefix = 0;
83 std::size_t ec_point_size = 0;
84 switch (key.compression) {
85 case KeyCompression::NoCompression:
86 if (key.y.size() != expected_length) {
87 return {};
88 }
89 ec_point_prefix = 0x04;
90 ec_point_size = 1 + 2 * expected_length;
91 break;
92 case KeyCompression::Y0:
93 ec_point_prefix = 0x02;
94 ec_point_size = 1 + expected_length;
95 break;
96 case KeyCompression::Y1:
97 ec_point_prefix = 0x03;
98 ec_point_size = 1 + expected_length;
99 break;
100 default:
101 return {};
102 }
103
104 const std::size_t algo_content_size = sizeof(oid_ec_public_key) + named_curve_oid_size;
105 const std::size_t bit_string_content_size = 1 + ec_point_size; // leading "unused bits" byte
106 const std::size_t spki_content_size = 2 + algo_content_size + 2 + bit_string_content_size;
107
108 ByteBuffer result;
109 result.reserve(2 + spki_content_size);
110
111 result.push_back(0x30); // SEQUENCE (SubjectPublicKeyInfo)
112 result.push_back(static_cast<uint8_t>(spki_content_size));
113
114 result.push_back(0x30); // SEQUENCE (AlgorithmIdentifier)
115 result.push_back(static_cast<uint8_t>(algo_content_size));
116 result.insert(result.end(), oid_ec_public_key, oid_ec_public_key + sizeof(oid_ec_public_key));
117 result.insert(result.end(), named_curve_oid, named_curve_oid + named_curve_oid_size);
118
119 result.push_back(0x03); // BIT STRING (subjectPublicKey)
120 result.push_back(static_cast<uint8_t>(bit_string_content_size));
121 result.push_back(0x00); // unused bits
122 result.push_back(ec_point_prefix);
123 result.insert(result.end(), key.x.begin(), key.x.end());
124 if (key.compression == KeyCompression::NoCompression) {
125 result.insert(result.end(), key.y.begin(), key.y.end());
126 }
127
128 return result;
129}
130
131} // namespace security
132} // namespace vanetza