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 NET_DER_ENCODE_VALUES_H_ 6 #define NET_DER_ENCODE_VALUES_H_ 7 8 #include <stddef.h> 9 #include <stdint.h> 10 11 #include "net/base/net_export.h" 12 13 namespace base { 14 class Time; 15 } 16 17 namespace net::der { 18 19 struct GeneralizedTime; 20 21 // Encodes |time|, a UTC-based time, to DER |generalized_time|, for comparing 22 // against other GeneralizedTime objects. 23 NET_EXPORT bool EncodeTimeAsGeneralizedTime(const base::Time& time, 24 GeneralizedTime* generalized_time); 25 26 // Encodes |posix_time|, a posix time in seconds, to DER |generalized_time|, for 27 // comparing against other GeneralizedTime objects, returning true on success or 28 // false if |posix_time| is outside of the range from year 0000 to 9999. 29 NET_EXPORT bool EncodePosixTimeAsGeneralizedTime( 30 int64_t posix_time, 31 GeneralizedTime* generalized_time); 32 33 // Converts a GeneralizedTime struct to a base::Time, returning true on success 34 // or false if |generalized| was invalid or cannot be represented by 35 // base::Time. 36 NET_EXPORT bool GeneralizedTimeToTime(const der::GeneralizedTime& generalized, 37 base::Time* result); 38 39 // Converts a GeneralizedTime struct to a posix time in seconds in |result|, 40 // returning true on success or false if |generalized| was invalid or cannot be 41 // represented as a posix time in the range from the year 0000 to 9999. 42 NET_EXPORT bool GeneralizedTimeToPosixTime( 43 const der::GeneralizedTime& generalized, 44 int64_t* result); 45 46 static const size_t kGeneralizedTimeLength = 15; 47 48 // Encodes |time| to |out| as a DER GeneralizedTime value. Returns true on 49 // success and false on error. 50 NET_EXPORT bool EncodeGeneralizedTime(const GeneralizedTime& time, 51 uint8_t out[kGeneralizedTimeLength]); 52 53 static const size_t kUTCTimeLength = 13; 54 55 // Encodes |time| to |out| as a DER UTCTime value. Returns true on success and 56 // false on error. 57 NET_EXPORT bool EncodeUTCTime(const GeneralizedTime& time, 58 uint8_t out[kUTCTimeLength]); 59 60 } // namespace net::der 61 62 #endif // NET_DER_ENCODE_VALUES_H_ 63