secureswitools/swisistools/source/signsislib/sissignaturecertificatechain.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /**
       
    20  @file 
       
    21  @internalComponent
       
    22  @released
       
    23 */
       
    24 
       
    25 #include <openssl/rsa.h>
       
    26 #include <openssl/evp.h>
       
    27 #include <openssl/objects.h>
       
    28 #include <openssl/x509.h>
       
    29 #include <openssl/err.h>
       
    30 #include <openssl/pem.h>
       
    31 #include <openssl/evp.h>
       
    32 
       
    33 #include "sissignaturecertificatechain.h"
       
    34 #include "signaturecertchaindata.h"
       
    35 #include "siscertificatechain.h"
       
    36 
       
    37 #include "exception.h"
       
    38 #include "utility.h"
       
    39 #include "sissignature.h"
       
    40 #include "siscontroller.h" 
       
    41 
       
    42 
       
    43 CSisSignatureCertificateChain::CSisSignatureCertificateChain (CSignatureCertChainData& aSisSignatureCertChain)
       
    44 						: iSisSignatureCertChain(aSisSignatureCertChain)
       
    45 	{
       
    46 	CSISArray <CSignatureData, CSISFieldRoot::ESISSignature>	&signatures = iSisSignatureCertChain.Signatures();
       
    47 	int count = signatures.size();
       
    48 	
       
    49 	iCertificateChain = new CSisCertificateChain(const_cast<CCertChainData&>(iSisSignatureCertChain.CertificateChain()));
       
    50 	}
       
    51 
       
    52 
       
    53 CSisSignatureCertificateChain::~CSisSignatureCertificateChain()
       
    54 	{
       
    55 	for(int i = 0; i < iSignatures.size(); ++i)
       
    56 		{
       
    57 		delete iSignatures[i];
       
    58 		}
       
    59 	iSignatures.clear();
       
    60 	delete iCertificateChain;
       
    61 	}
       
    62 
       
    63 void CSisSignatureCertificateChain::Sign (
       
    64 		const CSISSignatureAlgorithm::TAlgorithm aAlgorithm, const std::wstring& aCertificate,
       
    65 		const std::wstring& aPrivateKey, const std::wstring& aPassPhrase,  const TUint8* aBuffer, const TUint32 aBufferSize)
       
    66 	{
       
    67 	iCertificateChain->Load (aCertificate);
       
    68 	CSignatureData signatureContent;
       
    69 	CSignature* signature = new CSignature(signatureContent);
       
    70 	if (aAlgorithm != CSISSignatureAlgorithm::EAlgNone)
       
    71 		{
       
    72 		signatureContent.SetAlgorithm (aAlgorithm);
       
    73 		}
       
    74 	signature->Sign (aPrivateKey, aPassPhrase, aBuffer, aBufferSize);
       
    75 	iSisSignatureCertChain.AddSignature(signatureContent);
       
    76 	iSignatures.push_back (signature);
       
    77 	}
       
    78 
       
    79 
       
    80 void CSisSignatureCertificateChain::VerifySignature (const CSISController* aController, const TSISStream::pos_type aParentHeaderSize) const
       
    81 	{
       
    82 	int signatureCount = iSignatures.size ();
       
    83 	if (signatureCount != 0)
       
    84 		{
       
    85 		X509* x509 = iCertificateChain->GetBottomX509 ();
       
    86 		if (x509)
       
    87 			{
       
    88 			int index;
       
    89 			try
       
    90 				{
       
    91 				for (index = 0; index < signatureCount; index++)
       
    92 					{
       
    93 					int size = 0;
       
    94 
       
    95 					if (iSisSignatureCertChain.PreHeaderPos() <= 0)
       
    96 						{
       
    97 						size = aController->BufferSize ();
       
    98 						}
       
    99 					else
       
   100 						{
       
   101 						size = iSisSignatureCertChain.PreHeaderPos() - aParentHeaderSize;
       
   102 						assert (size <= aController->BufferSize ());
       
   103 						}
       
   104 	
       
   105 					iSignatures [index]->VerifySignature (x509, aController->RawBuffer(), size);
       
   106 					}
       
   107 				}
       
   108 			catch (...)
       
   109 				{
       
   110 
       
   111 				CSignature* signature = iSignatures[index];
       
   112 
       
   113 				if (!signature->SignatureAlgorithm().IsAlgorithmKnown())
       
   114 					{
       
   115 					SISLogger::Log(L"Could not verify signature with unknown algorithm, continuing.\n");
       
   116 					}
       
   117 				else
       
   118 					{
       
   119 					X509_free (x509);
       
   120 					SISLogger::Log(L"Could not verify signature with known algorithm, exiting.\n");
       
   121 					throw;
       
   122 					}
       
   123 				}
       
   124 			X509_free (x509);
       
   125 			}
       
   126 		}
       
   127 	}
       
   128