/src/boringssl/pki/encode_values.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2016 The Chromium Authors |
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 | | // https://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 | | #include "encode_values.h" |
16 | | |
17 | | #include "parse_values.h" |
18 | | |
19 | | #include <openssl/posix_time.h> |
20 | | |
21 | | BSSL_NAMESPACE_BEGIN |
22 | | namespace der { |
23 | | |
24 | | namespace { |
25 | | |
26 | 0 | bool WriteFourDigit(uint16_t value, uint8_t out[4]) { |
27 | 0 | if (value >= 10000) { |
28 | 0 | return false; |
29 | 0 | } |
30 | 0 | out[3] = '0' + (value % 10); |
31 | 0 | value /= 10; |
32 | 0 | out[2] = '0' + (value % 10); |
33 | 0 | value /= 10; |
34 | 0 | out[1] = '0' + (value % 10); |
35 | 0 | value /= 10; |
36 | 0 | out[0] = '0' + value; |
37 | 0 | return true; |
38 | 0 | } |
39 | | |
40 | 0 | bool WriteTwoDigit(uint8_t value, uint8_t out[2]) { |
41 | 0 | if (value >= 100) { |
42 | 0 | return false; |
43 | 0 | } |
44 | 0 | out[0] = '0' + (value / 10); |
45 | 0 | out[1] = '0' + (value % 10); |
46 | 0 | return true; |
47 | 0 | } |
48 | | |
49 | | } // namespace |
50 | | |
51 | | bool EncodePosixTimeAsGeneralizedTime(int64_t posix_time, |
52 | 0 | GeneralizedTime *generalized_time) { |
53 | 0 | struct tm tmp_tm; |
54 | 0 | if (!OPENSSL_posix_to_tm(posix_time, &tmp_tm)) { |
55 | 0 | return false; |
56 | 0 | } |
57 | | |
58 | 0 | generalized_time->year = tmp_tm.tm_year + 1900; |
59 | 0 | generalized_time->month = tmp_tm.tm_mon + 1; |
60 | 0 | generalized_time->day = tmp_tm.tm_mday; |
61 | 0 | generalized_time->hours = tmp_tm.tm_hour; |
62 | 0 | generalized_time->minutes = tmp_tm.tm_min; |
63 | 0 | generalized_time->seconds = tmp_tm.tm_sec; |
64 | 0 | return true; |
65 | 0 | } |
66 | | |
67 | | bool GeneralizedTimeToPosixTime(const der::GeneralizedTime &generalized, |
68 | 0 | int64_t *result) { |
69 | 0 | struct tm tmp_tm; |
70 | 0 | tmp_tm.tm_year = generalized.year - 1900; |
71 | 0 | tmp_tm.tm_mon = generalized.month - 1; |
72 | 0 | tmp_tm.tm_mday = generalized.day; |
73 | 0 | tmp_tm.tm_hour = generalized.hours; |
74 | 0 | tmp_tm.tm_min = generalized.minutes; |
75 | 0 | tmp_tm.tm_sec = generalized.seconds; |
76 | | // BoringSSL POSIX time, like POSIX itself, does not support leap seconds. |
77 | | // Collapse to previous second. |
78 | 0 | if (tmp_tm.tm_sec == 60) { |
79 | 0 | tmp_tm.tm_sec = 59; |
80 | 0 | } |
81 | 0 | return OPENSSL_tm_to_posix(&tmp_tm, result); |
82 | 0 | } |
83 | | |
84 | | bool EncodeGeneralizedTime(const GeneralizedTime &time, |
85 | 0 | uint8_t out[kGeneralizedTimeLength]) { |
86 | 0 | if (!WriteFourDigit(time.year, out) || !WriteTwoDigit(time.month, out + 4) || |
87 | 0 | !WriteTwoDigit(time.day, out + 6) || |
88 | 0 | !WriteTwoDigit(time.hours, out + 8) || |
89 | 0 | !WriteTwoDigit(time.minutes, out + 10) || |
90 | 0 | !WriteTwoDigit(time.seconds, out + 12)) { |
91 | 0 | return false; |
92 | 0 | } |
93 | 0 | out[14] = 'Z'; |
94 | 0 | return true; |
95 | 0 | } |
96 | | |
97 | 0 | bool EncodeUTCTime(const GeneralizedTime &time, uint8_t out[kUTCTimeLength]) { |
98 | 0 | if (!time.InUTCTimeRange()) { |
99 | 0 | return false; |
100 | 0 | } |
101 | | |
102 | 0 | uint16_t year = time.year - 1900; |
103 | 0 | if (year >= 100) { |
104 | 0 | year -= 100; |
105 | 0 | } |
106 | |
|
107 | 0 | if (!WriteTwoDigit(year, out) || !WriteTwoDigit(time.month, out + 2) || |
108 | 0 | !WriteTwoDigit(time.day, out + 4) || |
109 | 0 | !WriteTwoDigit(time.hours, out + 6) || |
110 | 0 | !WriteTwoDigit(time.minutes, out + 8) || |
111 | 0 | !WriteTwoDigit(time.seconds, out + 10)) { |
112 | 0 | return false; |
113 | 0 | } |
114 | 0 | out[12] = 'Z'; |
115 | 0 | return true; |
116 | 0 | } |
117 | | |
118 | | } // namespace der |
119 | | BSSL_NAMESPACE_END |