diff options
author | lloyd <[email protected]> | 2010-10-07 18:38:41 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-10-07 18:38:41 +0000 |
commit | f8165e1fcdefebd9bd60449e93c4a7fc70179ad5 (patch) | |
tree | a0895ac53c07a566291d0ede93aed9e012e5d88d /src | |
parent | 6f07316c66604dbbbf4a1db76d6e8a0ab5462385 (diff) |
Fix a bug in X509_Time: when decoding, the tag would not be set, so
reencoding the same object would fail. This affected CRL updates.
Diffstat (limited to 'src')
-rw-r--r-- | src/asn1/asn1_tm.cpp | 42 |
1 files changed, 24 insertions, 18 deletions
diff --git a/src/asn1/asn1_tm.cpp b/src/asn1/asn1_tm.cpp index 2f4de39fa..54af6154f 100644 --- a/src/asn1/asn1_tm.cpp +++ b/src/asn1/asn1_tm.cpp @@ -36,10 +36,7 @@ X509_Time::X509_Time(u64bit timer) minute = cal.minutes; second = cal.seconds; - if(year >= 2050) - tag = GENERALIZED_TIME; - else - tag = UTC_TIME; + tag = (year >= 2050) ? GENERALIZED_TIME : UTC_TIME; } /* @@ -58,6 +55,7 @@ void X509_Time::set_to(const std::string& time_str) if(time_str == "") { year = month = day = hour = minute = second = 0; + tag = NO_OBJECT; return; } @@ -88,10 +86,7 @@ void X509_Time::set_to(const std::string& time_str) minute = (params.size() >= 5) ? to_u32bit(params[4]) : 0; second = (params.size() == 6) ? to_u32bit(params[5]) : 0; - if(year >= 2050) - tag = GENERALIZED_TIME; - else - tag = UTC_TIME; + tag = (year >= 2050) ? GENERALIZED_TIME : UTC_TIME; if(!passes_sanity_check()) throw Invalid_Argument("Invalid time specification " + time_str); @@ -100,18 +95,21 @@ void X509_Time::set_to(const std::string& time_str) /* * Set the time with an ISO time format string */ -void X509_Time::set_to(const std::string& t_spec, ASN1_Tag tag) +void X509_Time::set_to(const std::string& t_spec, ASN1_Tag spec_tag) { - if(tag != GENERALIZED_TIME && tag != UTC_TIME) - throw Invalid_Argument("X509_Time: Invalid tag " + to_string(tag)); - if(tag == GENERALIZED_TIME && t_spec.size() != 13 && t_spec.size() != 15) + if(spec_tag != GENERALIZED_TIME && spec_tag != UTC_TIME) + throw Invalid_Argument("X509_Time: Invalid tag " + to_string(spec_tag)); + + if(spec_tag == GENERALIZED_TIME && t_spec.size() != 13 && t_spec.size() != 15) throw Invalid_Argument("Invalid GeneralizedTime: " + t_spec); - if(tag == UTC_TIME && t_spec.size() != 11 && t_spec.size() != 13) + + if(spec_tag == UTC_TIME && t_spec.size() != 11 && t_spec.size() != 13) throw Invalid_Argument("Invalid UTCTime: " + t_spec); + if(t_spec[t_spec.size()-1] != 'Z') throw Invalid_Argument("Invalid time encoding: " + t_spec); - const u32bit YEAR_SIZE = (tag == UTC_TIME) ? 2 : 4; + const u32bit YEAR_SIZE = (spec_tag == UTC_TIME) ? 2 : 4; std::vector<std::string> params; std::string current; @@ -137,8 +135,9 @@ void X509_Time::set_to(const std::string& t_spec, ASN1_Tag tag) hour = to_u32bit(params[3]); minute = to_u32bit(params[4]); second = (params.size() == 6) ? to_u32bit(params[5]) : 0; + tag = spec_tag; - if(tag == UTC_TIME) + if(spec_tag == UTC_TIME) { if(year >= 50) year += 1900; else year += 2000; @@ -155,9 +154,11 @@ void X509_Time::encode_into(DER_Encoder& der) const { if(tag != GENERALIZED_TIME && tag != UTC_TIME) throw Invalid_Argument("X509_Time: Bad encoding tag"); + der.add_object(tag, UNIVERSAL, Charset::transcode(as_string(), - LOCAL_CHARSET, LATIN1_CHARSET)); + LOCAL_CHARSET, + LATIN1_CHARSET)); } /* @@ -166,8 +167,10 @@ void X509_Time::encode_into(DER_Encoder& der) const void X509_Time::decode_from(BER_Decoder& source) { BER_Object ber_time = source.get_next_object(); + set_to(Charset::transcode(ASN1::to_string(ber_time), - LATIN1_CHARSET, LOCAL_CHARSET), + LATIN1_CHARSET, + LOCAL_CHARSET), ber_time.type_tag); } @@ -182,7 +185,7 @@ std::string X509_Time::as_string() const std::string asn1rep; if(tag == GENERALIZED_TIME) asn1rep = to_string(year, 4); - else + else if(tag == UTC_TIME) { if(year < 1950 || year >= 2050) throw Encoding_Error("X509_Time: The time " + readable_string() + @@ -190,6 +193,9 @@ std::string X509_Time::as_string() const u32bit asn1year = (year >= 2000) ? (year - 2000) : (year - 1900); asn1rep = to_string(asn1year, 2); } + else + throw Invalid_Argument("X509_Time: Invalid tag " + to_string(tag)); + asn1rep += to_string(month, 2) + to_string(day, 2); asn1rep += to_string(hour, 2) + to_string(minute, 2) + to_string(second, 2); asn1rep += "Z"; |