From bc5b3a02de2e9131bff940c9667f28b2ae588807 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Remko=20Tron=C3=A7on?= <git@el-tramo.be>
Date: Tue, 9 Nov 2010 22:18:05 +0100
Subject: Certificate::getCommonName -> getCommonNames.


diff --git a/Swiften/QA/OpenSSLTest/OpenSSLCertificateTest.cpp b/Swiften/QA/OpenSSLTest/OpenSSLCertificateTest.cpp
index 43519d7..59aee47 100644
--- a/Swiften/QA/OpenSSLTest/OpenSSLCertificateTest.cpp
+++ b/Swiften/QA/OpenSSLTest/OpenSSLCertificateTest.cpp
@@ -17,11 +17,13 @@ class OpenSSLCertificateTest : public CppUnit::TestFixture {
 		CPPUNIT_TEST_SUITE(OpenSSLCertificateTest);
 		CPPUNIT_TEST(testConstructFromDER);
 		CPPUNIT_TEST(testToDER);
+		CPPUNIT_TEST(testGetSubjectName);
 		CPPUNIT_TEST_SUITE_END();
 
 	public:
 		void setUp() {
 			pathProvider = new PlatformApplicationPathProvider("FileReadBytestreamTest");
+			certificateData.readFromFile((pathProvider->getExecutableDir() / "jabber_org.crt").string());
 		}
 
 		void tearDown() {
@@ -29,23 +31,26 @@ class OpenSSLCertificateTest : public CppUnit::TestFixture {
 		}
 
 		void testConstructFromDER() {
-			ByteArray in;
-			in.readFromFile((pathProvider->getExecutableDir() / "jabber_org.crt").string());
-			OpenSSLCertificate testling(in);
+			OpenSSLCertificate testling(certificateData);
 
-			CPPUNIT_ASSERT_EQUAL(String("*.jabber.org"), testling.getCommonName());
+			CPPUNIT_ASSERT_EQUAL(String("*.jabber.org"), testling.getCommonNames()[0]);
 		}
 		
 		void testToDER() {
-			ByteArray in;
-			in.readFromFile((pathProvider->getExecutableDir() / "jabber_org.crt").string());
-			OpenSSLCertificate testling(in);
+			OpenSSLCertificate testling(certificateData);
 
-			CPPUNIT_ASSERT_EQUAL(in, testling.toDER());
+			CPPUNIT_ASSERT_EQUAL(certificateData, testling.toDER());
+		}
+
+		void testGetSubjectName() {
+			OpenSSLCertificate testling(certificateData);
+
+			CPPUNIT_ASSERT_EQUAL(String("/description=114072-VMk8pdi1aj5kTXxO/C=US/ST=Colorado/L=Denver/O=Peter Saint-Andre/OU=StartCom Trusted Certificate Member/CN=*.jabber.org/emailAddress=hostmaster@jabber.org"), testling.getSubjectName());
 		}
 	
 	private:
 		PlatformApplicationPathProvider* pathProvider;
+		ByteArray certificateData;
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(OpenSSLCertificateTest);
diff --git a/Swiften/TLS/Certificate.h b/Swiften/TLS/Certificate.h
index 6b62457..4a3cc8b 100644
--- a/Swiften/TLS/Certificate.h
+++ b/Swiften/TLS/Certificate.h
@@ -18,7 +18,9 @@ namespace Swift {
 
 			virtual ~Certificate();
 
-			virtual String getCommonName() const = 0;
+			virtual String getSubjectName() const = 0;
+
+			virtual std::vector<String> getCommonNames() const = 0;
 			virtual std::vector<String> getSRVNames() const = 0;
 			virtual std::vector<String> getDNSNames() const = 0;
 			virtual std::vector<String> getXMPPAddresses() const = 0;
diff --git a/Swiften/TLS/OpenSSL/OpenSSLCertificate.cpp b/Swiften/TLS/OpenSSL/OpenSSLCertificate.cpp
index 7f13f88..77c0aee 100644
--- a/Swiften/TLS/OpenSSL/OpenSSLCertificate.cpp
+++ b/Swiften/TLS/OpenSSL/OpenSSLCertificate.cpp
@@ -35,14 +35,22 @@ ByteArray OpenSSLCertificate::toDER() const {
 }
 
 void OpenSSLCertificate::parse() {
-	// Common name
+	// Subject name
 	X509_NAME* subjectName = X509_get_subject_name(cert.get());
 	if (subjectName) {
+		// Subject name
+		ByteArray subjectNameData;
+		subjectNameData.resize(256);
+		X509_NAME_oneline(X509_get_subject_name(cert.get()), subjectNameData.getData(), subjectNameData.getSize());
+		this->subjectName = String(subjectNameData.getData());
+
+		// Common name
 		int cnLoc = X509_NAME_get_index_by_NID(subjectName, NID_commonName, -1);
-		if (cnLoc != -1) {
+		while (cnLoc != -1) {
 			X509_NAME_ENTRY* cnEntry = X509_NAME_get_entry(subjectName, cnLoc);
 			ASN1_STRING* cnData = X509_NAME_ENTRY_get_data(cnEntry);
-			setCommonName(ByteArray(cnData->data, cnData->length).toString());
+			commonNames.push_back(ByteArray(cnData->data, cnData->length).toString());
+			cnLoc = X509_NAME_get_index_by_NID(subjectName, NID_commonName, cnLoc);
 		}
 	}
 
diff --git a/Swiften/TLS/OpenSSL/OpenSSLCertificate.h b/Swiften/TLS/OpenSSL/OpenSSLCertificate.h
index fe2cddf..ad16081 100644
--- a/Swiften/TLS/OpenSSL/OpenSSLCertificate.h
+++ b/Swiften/TLS/OpenSSL/OpenSSLCertificate.h
@@ -18,8 +18,12 @@ namespace Swift {
 			OpenSSLCertificate(boost::shared_ptr<X509>);
 			OpenSSLCertificate(const ByteArray& der);
 
-			String getCommonName() const {
-				return commonName;
+			String getSubjectName() const {
+				return subjectName;
+			}
+
+			std::vector<String> getCommonNames() const {
+				return commonNames;
 			}
 
 			std::vector<String> getSRVNames() const {
@@ -51,13 +55,10 @@ namespace Swift {
 				xmppAddresses.push_back(addr);
 			}
 
-			void setCommonName(const String& commonName) {
-				this->commonName = commonName;
-			}
-
 		private:
 			boost::shared_ptr<X509> cert;
-			String commonName;
+			String subjectName;
+			std::vector<String> commonNames;
 			std::vector<String> dnsNames;
 			std::vector<String> xmppAddresses;
 			std::vector<String> srvNames;
-- 
cgit v0.10.2-6-g49f6