Vanetza
 
Loading...
Searching...
No Matches
verification.cpp
1#include <vanetza/security/v2/verification.hpp>
2
3namespace vanetza
4{
5namespace security
6{
7namespace v2
8{
9
10bool check_generation_time(const SecuredMessage& message, Clock::time_point now)
11{
12 using namespace std::chrono;
13
14 bool valid = false;
15 const Time64* generation_time = message.header_field<HeaderFieldType::Generation_Time>();
16 if (generation_time) {
17 // Values are picked from C2C-CC Basic System Profile v1.1.0, see RS_BSP_168
18 static const auto generation_time_future = milliseconds(40);
19 static const Clock::duration generation_time_past_default = minutes(10);
20 static const Clock::duration generation_time_past_ca = seconds(2);
21 auto generation_time_past = generation_time_past_default;
22
23 const IntX* its_aid = message.header_field<HeaderFieldType::Its_Aid>();
24 if (its_aid && aid::CA == *its_aid) {
25 generation_time_past = generation_time_past_ca;
26 }
27
28 if (*generation_time > convert_time64(now + generation_time_future)) {
29 valid = false;
30 } else if (*generation_time < convert_time64(now - generation_time_past)) {
31 valid = false;
32 } else {
33 valid = true;
34 }
35 }
36
37 return valid;
38}
39
40bool check_generation_location(const SecuredMessage& message, const Certificate& cert)
41{
42 const IntX* its_aid = message.header_field<HeaderFieldType::Its_Aid>();
43 if (its_aid && aid::CA == *its_aid) {
44 return true; // no check required for CAMs, field not even allowed
45 }
46
47 const ThreeDLocation* generation_location = message.header_field<HeaderFieldType::Generation_Location>();
48 if (generation_location) {
49 auto region = cert.get_restriction<ValidityRestrictionType::Region>();
50
51 if (!region || get_type(*region) == RegionType::None) {
52 return true;
53 }
54
55 return is_within(TwoDLocation(*generation_location), *region);
56 }
57
58 return false;
59}
60
61bool check_certificate_time(const Certificate& certificate, Clock::time_point now)
62{
63 auto time = certificate.get_restriction<ValidityRestrictionType::Time_Start_And_End>();
64 auto time_now = convert_time32(now);
65
66 if (!time) {
67 return false; // must be present
68 }
69
70 if (time->start_validity > time_now || time->end_validity < time_now) {
71 return false; // premature or outdated
72 }
73
74 return true;
75}
76
77bool check_certificate_region(const Certificate& certificate, const PositionFix& position)
78{
79 auto region = certificate.get_restriction<ValidityRestrictionType::Region>();
80
81 if (!region || get_type(*region) == RegionType::None) {
82 return true;
83 }
84
85 if (!position.confidence) {
86 return false; // cannot check region restrictions without good position fix
87 }
88
89 return is_within(TwoDLocation(position.latitude, position.longitude), *region);
90}
91
92} // namespace v2
93} // namespace security
94} // namespace vanetza