security: Fixing decoding/encoding certificates. Now the test is fully working
Change-Id: Ieaecba4576d1af3abc68e48baa87e8d4d312b702
diff --git a/src/encoding/cryptopp/asn_ext.cpp b/src/encoding/cryptopp/asn_ext.cpp
index 6698426..a1ee39d 100644
--- a/src/encoding/cryptopp/asn_ext.cpp
+++ b/src/encoding/cryptopp/asn_ext.cpp
@@ -16,6 +16,8 @@
#include <sys/time.h>
#endif
+#include "../../util/time.hpp"
+
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
@@ -26,10 +28,6 @@
size_t
DEREncodeGeneralTime(CryptoPP::BufferedTransformation &bt, MillisecondsSince1970 time)
{
-#ifndef NDN_CPP_HAVE_GMTIME_SUPPORT
- throw Asn::Error("Time functions are not supported by the standard library");
-#endif
-
if (time < 0)
throw Asn::Error("Calendar time value out of range");
else if (time > 2e14)
@@ -39,14 +37,13 @@
time_t secondsSince1970 = time / 1000;
struct tm* gmt = gmtime(&secondsSince1970);
- std::string asn1time ((boost::format("%04d%02d%02d%02d%02d%02d%sZ")
+ std::string asn1time ((boost::format("%04d%02d%02d%02d%02d%02dZ")
% (1900 + gmt->tm_year)
% (gmt->tm_mon + 1)
% gmt->tm_mday
% gmt->tm_hour
% gmt->tm_min
% gmt->tm_sec).str());
- // = os.str();
bt.Put(GENERALIZED_TIME);
size_t lengthBytes = DERLengthEncode(bt, asn1time.size());
@@ -69,39 +66,17 @@
if (bc != bt.Get(time_str, bc))
BERDecodeError();
- std::vector<std::string> params;
- std::string current;
- std::locale cLocale("C");
+ std::string str;
+ str.assign (time_str.begin(), time_str.end());
- for(uint32_t j = 0; j != time_str.size(); ++j)
- {
- if(std::isdigit(reinterpret_cast<char&>(time_str[j]), cLocale))
- current += time_str[j];
- else
- {
- if(current != "")
- params.push_back(current);
- current.clear();
- }
- }
- if(current != "")
- params.push_back(current);
-
- if(params.size() < 3 || params.size() > 6)
- throw Asn::Error("Invalid time specification " + std::string(time_str.begin(), time_str.end()));
-
- struct tm gmt;
- gmt.tm_year = boost::lexical_cast<int>(params[0]);
- gmt.tm_mon = boost::lexical_cast<int>(params[1]) - 1;
- gmt.tm_mday = boost::lexical_cast<int>(params[2]);
- gmt.tm_hour = (params.size() >= 4) ? boost::lexical_cast<int>(params[3]) : 0;
- gmt.tm_min = (params.size() >= 5) ? boost::lexical_cast<int>(params[4]) : 0;
- gmt.tm_sec = (params.size() == 6) ? boost::lexical_cast<int>(params[5]) : 0;
-
- if (b == GENERALIZED_TIME)
- gmt.tm_year -= 1900;
-
- time = timegm(&gmt) * 1000;
+ if (b == UTC_TIME) {
+ if (boost::lexical_cast<int>(str.substr(0,2)) < 50)
+ str = "20" + str;
+ else
+ str = "19" + str;
+ }
+
+ time = fromIsoString(str.substr(0, 8) + "T" + str.substr(8, 6));
}
} // namespace ndn
diff --git a/src/encoding/oid.cpp b/src/encoding/oid.cpp
index 2db72f7..0fb6697 100644
--- a/src/encoding/oid.cpp
+++ b/src/encoding/oid.cpp
@@ -16,8 +16,19 @@
namespace ndn {
+OID::OID(const char *oid)
+{
+ construct(oid);
+}
+
OID::OID(const string& oid)
{
+ construct(oid);
+}
+
+void
+OID::construct(const std::string &oid)
+{
string str = oid + ".";
size_t pos = 0;
diff --git a/src/security/certificate/certificate.cpp b/src/security/certificate/certificate.cpp
index da88444..7a70cdc 100644
--- a/src/security/certificate/certificate.cpp
+++ b/src/security/certificate/certificate.cpp
@@ -250,7 +250,7 @@
void
Certificate::printCertificate(std::ostream &os) const
{
- os << "Certificate name: " << endl;
+ os << "Certificate name:" << endl;
os << " " << getName() << endl;
os << "Validity:" << endl;
{
@@ -265,7 +265,7 @@
os << " " << it->getOidString() << ": " << it->getValue() << endl;
}
- os << "Public key bits: " << endl;
+ os << "Public key bits:" << endl;
CryptoPP::Base64Encoder encoder(new CryptoPP::FileSink(os), true, 64);
key_.encode(encoder);