os/security/cryptoservices/certificateandkeymgmt/pkcs7/pkcs7digestinfo.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2005-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 #include "pkcs7digestinfo.h"
    20 #include "pkcs7asn1.h"
    21 
    22 EXPORT_C CPKCS7DigestInfo* CPKCS7DigestInfo::NewL(const TDesC8& aRawData)
    23 	{
    24 	CPKCS7DigestInfo* self = new (ELeave) CPKCS7DigestInfo();
    25 	CleanupStack::PushL(self);
    26 	self->ConstructL(aRawData);
    27 	CleanupStack::Pop(self);
    28 	return self;	
    29 	}
    30 	
    31 CPKCS7DigestInfo::~CPKCS7DigestInfo()
    32 	{
    33     }
    34 
    35 CPKCS7DigestInfo::CPKCS7DigestInfo()
    36 	{
    37 	}
    38 	
    39 EXPORT_C const TDesC8& CPKCS7DigestInfo::Digest() const
    40 	{
    41 	return iDigest;
    42 	}
    43 
    44 EXPORT_C TAlgorithmId CPKCS7DigestInfo::Algorithm() const
    45 	{
    46 	return iAlgorithmId;
    47 	}
    48 
    49 EXPORT_C const TPtrC8& CPKCS7DigestInfo::EncodedParams() const
    50 	{
    51 	return iEncodedParams;
    52 	}
    53 	
    54 void CPKCS7DigestInfo::ConstructL(const TDesC8& aDigestData)
    55 	{
    56     CArrayPtr<TASN1DecGeneric>* digestInfo = PKCS7ASN1::DecodeSequenceLC(aDigestData);
    57 
    58 	// Check if both the digestAlgorithm and the Digest are present.
    59 	if ( digestInfo->Count() != 2 )
    60 		{
    61 	    User::Leave(KErrArgument);
    62 		}
    63 
    64 	// DIGEST ALGORITHM
    65 	// Get the algorithm identifier and the encoded parameters present in the sequence.
    66 	const TASN1DecGeneric* digestInfoAt0 = digestInfo->At(0);
    67 	if(digestInfoAt0->Tag() != EASN1Sequence || digestInfoAt0->Class() != EUniversal)
    68 		{
    69 		User::Leave(KErrArgument);
    70 		}
    71 	CX509AlgorithmIdentifier* digestAlg = CX509AlgorithmIdentifier::NewLC(digestInfo->At(0)->Encoding());
    72 	TAlgorithmId algorithmId = digestAlg->Algorithm();
    73 	switch(algorithmId)
    74 		{
    75 		case EMD2:
    76 			{
    77 			iAlgorithmId = EMD2;
    78 			}
    79 			break;
    80 		case EMD5:
    81 			{
    82 			iAlgorithmId = EMD5;
    83 			}
    84 			break;
    85 		case ESHA1:
    86 			{
    87 			iAlgorithmId = ESHA1;	
    88 			}
    89 			break;
    90 		default:
    91 			User::Leave(KErrNotSupported);
    92 		}
    93 	
    94 	iEncodedParams.Set(digestAlg->EncodedParams()); 
    95 		
    96 	// DIGEST  
    97 	const TASN1DecGeneric* digestInfoAt1 = digestInfo->At(1);
    98 	if(digestInfoAt1->Tag() != EASN1OctetString || digestInfoAt1->Class() != EUniversal)
    99 		{
   100 		User::Leave(KErrArgument);
   101 		}
   102 	TASN1DecGeneric octetStringDecoder(*(digestInfo->At(1)));
   103 	octetStringDecoder.InitL();
   104 	iDigest.Set(octetStringDecoder.GetContentDER());
   105 	
   106 	CleanupStack::PopAndDestroy(2,digestInfo);// digestInfo, digestAlg
   107     }