Skip to content
This repository was archived by the owner on Dec 27, 2025. It is now read-only.

Commit 2e9d3b6

Browse files
committed
Fix ASN1 Integer & Timestamp encoding
1 parent 25c648f commit 2e9d3b6

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

proto/asn1-pdu/asn1_universal_types_to_der.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ void Encode(const Boolean& boolean, std::vector<uint8_t>& der) {
4141
}
4242

4343
void Encode(const Integer& integer, std::vector<uint8_t>& der) {
44+
// Cannot have an empty integer, so make sure the length is at least 1.
4445
EncodeTagAndLength(kAsn1Integer,
45-
std::min<size_t>(0x01u, integer.val().size()), der.size(),
46+
std::max<size_t>(0x01u, integer.val().size()), der.size(),
4647
der);
4748

4849
if (!integer.val().empty()) {
@@ -139,7 +140,9 @@ void EncodeTimestamp(const google::protobuf::Timestamp& timestamp,
139140
bool use_two_digit_year,
140141
std::vector<uint8_t>& der) {
141142
std::string iso_date = google::protobuf::util::TimeUtil::ToString(timestamp);
142-
if (iso_date.size() < 25) {
143+
144+
// Expected date format: yyyy-MM-ddTHH:mm:ssZ (20 characters).
145+
if (iso_date.size() < 20) {
143146
return;
144147
}
145148

proto/asn1-pdu/common.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#ifndef PROTO_ASN1_PDU_COMMON_H_
1818
#define PROTO_ASN1_PDU_COMMON_H_
1919

20+
#include <stddef.h>
21+
2022
#include <stdint.h>
2123

2224
#include <vector>
@@ -82,4 +84,4 @@ void EncodeTagAndLength(uint8_t tag_byte,
8284
// that |der| remains a valid DER encoding.
8385
void ReplaceTag(uint8_t tag_byte, size_t pos_of_tag, std::vector<uint8_t>& der);
8486

85-
#endif // PROTO_ASN1_PDU_COMMON_H_
87+
#endif // PROTO_ASN1_PDU_COMMON_H_

0 commit comments

Comments
 (0)