xref: /aosp_15_r20/external/cronet/third_party/boringssl/src/pki/encode_values.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BSSL_DER_ENCODE_VALUES_H_
6 #define BSSL_DER_ENCODE_VALUES_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <openssl/base.h>
12 
13 namespace bssl::der {
14 
15 struct GeneralizedTime;
16 
17 // Encodes |posix_time|, a posix time in seconds, to DER |generalized_time|, for
18 // comparing against other GeneralizedTime objects, returning true on success or
19 // false if |posix_time| is outside of the range from year 0000 to 9999.
20 OPENSSL_EXPORT bool EncodePosixTimeAsGeneralizedTime(
21     int64_t posix_time, GeneralizedTime *generalized_time);
22 
23 // Converts a GeneralizedTime struct to a posix time in seconds in |result|,
24 // returning true on success or false if |generalized| was invalid or cannot be
25 // represented as a posix time in the range from the year 0000 to 9999.
26 OPENSSL_EXPORT bool GeneralizedTimeToPosixTime(
27     const der::GeneralizedTime &generalized, int64_t *result);
28 
29 static const size_t kGeneralizedTimeLength = 15;
30 
31 // Encodes |time| to |out| as a DER GeneralizedTime value. Returns true on
32 // success and false on error.
33 OPENSSL_EXPORT bool EncodeGeneralizedTime(const GeneralizedTime &time,
34                                           uint8_t out[kGeneralizedTimeLength]);
35 
36 static const size_t kUTCTimeLength = 13;
37 
38 // Encodes |time| to |out| as a DER UTCTime value. Returns true on success and
39 // false on error.
40 OPENSSL_EXPORT bool EncodeUTCTime(const GeneralizedTime &time,
41                                   uint8_t out[kUTCTimeLength]);
42 
43 }  // namespace bssl::der
44 
45 #endif  // BSSL_DER_ENCODE_VALUES_H_
46