summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'Swiften/TLS')
-rw-r--r--Swiften/TLS/Certificate.cpp19
-rw-r--r--Swiften/TLS/Certificate.h2
-rw-r--r--Swiften/TLS/SimpleCertificate.h7
-rw-r--r--Swiften/TLS/UnitTest/CertificateTest.cpp32
4 files changed, 58 insertions, 2 deletions
diff --git a/Swiften/TLS/Certificate.cpp b/Swiften/TLS/Certificate.cpp
index 7d61b22..984d668 100644
--- a/Swiften/TLS/Certificate.cpp
+++ b/Swiften/TLS/Certificate.cpp
@@ -4,7 +4,12 @@
* See Documentation/Licenses/GPLv3.txt for more information.
*/
-#include "Swiften/TLS/Certificate.h"
+#include <Swiften/TLS/Certificate.h>
+
+#include <sstream>
+
+#include <Swiften/StringCodecs/SHA1.h>
+#include <Swiften/StringCodecs/Hexify.h>
namespace Swift {
@@ -14,4 +19,16 @@ const char* Certificate::ID_ON_DNSSRV_OID = "1.3.6.1.5.5.7.8.7";
Certificate::~Certificate() {
}
+String Certificate::getSHA1Fingerprint() const {
+ ByteArray hash = SHA1::getHash(toDER());
+ std::ostringstream s;
+ for (size_t i = 0; i < hash.getSize(); ++i) {
+ if (i > 0) {
+ s << ":";
+ }
+ s << Hexify::hexify(hash[i]).getUTF8String();
+ }
+ return String(s.str());
+}
+
}
diff --git a/Swiften/TLS/Certificate.h b/Swiften/TLS/Certificate.h
index 89c1de6..e01aa74 100644
--- a/Swiften/TLS/Certificate.h
+++ b/Swiften/TLS/Certificate.h
@@ -31,6 +31,8 @@ namespace Swift {
virtual ByteArray toDER() const = 0;
+ virtual String getSHA1Fingerprint() const;
+
protected:
static const char* ID_ON_XMPPADDR_OID;
static const char* ID_ON_DNSSRV_OID;
diff --git a/Swiften/TLS/SimpleCertificate.h b/Swiften/TLS/SimpleCertificate.h
index 2db8291..7af8530 100644
--- a/Swiften/TLS/SimpleCertificate.h
+++ b/Swiften/TLS/SimpleCertificate.h
@@ -55,7 +55,11 @@ namespace Swift {
}
ByteArray toDER() const {
- return ByteArray();
+ return der;
+ }
+
+ void setDER(const ByteArray& der) {
+ this->der = der;
}
private:
@@ -63,6 +67,7 @@ namespace Swift {
private:
String subjectName;
+ ByteArray der;
std::vector<String> commonNames;
std::vector<String> dnsNames;
std::vector<String> xmppAddresses;
diff --git a/Swiften/TLS/UnitTest/CertificateTest.cpp b/Swiften/TLS/UnitTest/CertificateTest.cpp
new file mode 100644
index 0000000..b5e69c3
--- /dev/null
+++ b/Swiften/TLS/UnitTest/CertificateTest.cpp
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2010 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <Swiften/Base/ByteArray.h>
+
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <boost/smart_ptr/make_shared.hpp>
+
+#include <Swiften/TLS/Certificate.h>
+#include <Swiften/TLS/SimpleCertificate.h>
+
+using namespace Swift;
+
+class CertificateTest : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(CertificateTest);
+ CPPUNIT_TEST(testGetSHA1Fingerprint);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void testGetSHA1Fingerprint() {
+ SimpleCertificate::ref testling = boost::make_shared<SimpleCertificate>();
+ testling->setDER(ByteArray("abcdefg"));
+
+ CPPUNIT_ASSERT_EQUAL(String("2f:b5:e1:34:19:fc:89:24:68:65:e7:a3:24:f4:76:ec:62:4e:87:40"), testling->getSHA1Fingerprint());
+ }
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(CertificateTest);