networksecurity/tlsprovider/source/tlsprovider/CTlsProvider.cpp
branchRCL_3
changeset 11 db85996de7c4
parent 0 af10295192d8
child 53 7e41d162e158
--- a/networksecurity/tlsprovider/source/tlsprovider/CTlsProvider.cpp	Fri Mar 12 15:50:43 2010 +0200
+++ b/networksecurity/tlsprovider/source/tlsprovider/CTlsProvider.cpp	Mon Mar 15 12:46:10 2010 +0200
@@ -1,4 +1,4 @@
-// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
+// Copyright (c) 2003-2010 Nokia Corporation and/or its subsidiary(-ies).
 // All rights reserved.
 // This component and the accompanying materials are made available
 // under the terms of "Eclipse Public License v1.0"
@@ -18,6 +18,7 @@
 #include <ecom/ecom.h>
 #include <securitydefsconst.h>
 #include <x520ava.h>
+#include <asn1dec.h>
 
 #include "tlsprovider.h"
 #include "cryptostrength.h"
@@ -480,6 +481,44 @@
 	return;
 	}
 
+// This patch is to fix-up the Common Name component of the Subject field. 
+// This is due to a bug in TASN1DecBMPString::DecodeContentsL which is incorrectly decoding the BMP String contents. 
+// Since fixing TASN1DecBMPString would cause a major break in public API, this fix-up is being done here locally.
+// This patch is active only if the encoding is in BMP String. If TASN1DecBMPString::DecodeContentsL is ever fixed (which 
+// means CX500DistinguishedName::DisplayNameL is also automatically fixed) then this patch MUST to be removed.
+
+HBufC* CTlsProviderImpl::ExtractCertFieldL(const TDesC& aFieldName, const CX500DistinguishedName& aDistinguishedName)
+    {
+    TInt count = aDistinguishedName.Count();
+    for (TInt i = 0; i < count; i++)
+        {
+        const CX520AttributeTypeAndValue& ava = aDistinguishedName.Element(i);
+        if (ava.Type() == aFieldName)
+            {
+            HBufC* res = ava.ValueL();
+            CleanupStack::PushL(res);
+            TPtr pRes = res->Des();
+            TUint8* data = const_cast<TUint8 *>(reinterpret_cast<const TUint8 *>(res->Ptr())); 
+            TUint len = pRes.Length()*2;
+            TASN1DecGeneric gen(ava.EncodedValue());
+            gen.InitL();
+            if (gen.Tag() == EASN1BMPString)
+                {
+                // Bmp String internally store them as little endian, as a string we want it big
+                // endian interchange least and most significent byte
+                for(TUint i=0; i<len; i+=2)
+                     {
+                     TUint8 temp = data[i];
+                     data[i] = data[i+1];
+                     data[i+1] = temp;       
+                     }
+                }
+            CleanupStack::Pop();
+            return res;
+            }
+        }
+    return NULL;
+    }
 
 
 TBool CTlsProviderImpl::VerifySignatureL(
@@ -1526,7 +1565,8 @@
 	if(!hasAltNameExt)
 		{
 		// no name yet lets try common name from Subject
-		HBufC* commonName = aSource.SubjectName().ExtractFieldL(KX520CommonName);
+		//HBufC* commonName = aSource.SubjectName().ExtractFieldL(KX520CommonName);
+		HBufC* commonName  = ExtractCertFieldL(KX520CommonName, aSource.SubjectName());
 		if(commonName)
 			{
 			CleanupStack::PushL(commonName);