os/security/cryptomgmtlibs/cryptotokenfw/source/ctframework/Cctcertinfo.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptomgmtlibs/cryptotokenfw/source/ctframework/Cctcertinfo.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,404 @@
     1.4 +/*
     1.5 +* Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include "cctcertinfo.h"
    1.23 +
    1.24 +#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
    1.25 +
    1.26 +/** Mask constants used for serializing iDeletable and iFormat attributes 
    1.27 +*/
    1.28 +const TUint KReadOnlyFlagMask = 128;
    1.29 +const TUint KFormatMask = 127;
    1.30 +
    1.31 +/** The UID of a CertInfo MCTTokenObject. */
    1.32 +const TInt KCTObjectCertInfo = 0x101F50E6;
    1.33 +
    1.34 +#endif
    1.35 +
    1.36 +// MCertInfo ///////////////////////////////////////////////////////////////////
    1.37 +
    1.38 +EXPORT_C MCertInfo::MCertInfo() :
    1.39 +	iDeletable(ETrue)
    1.40 +	{
    1.41 +	}
    1.42 +
    1.43 +EXPORT_C MCertInfo::MCertInfo(const TDesC& aLabel,
    1.44 +							  TCertificateFormat aFormat,
    1.45 +							  TCertificateOwnerType aCertificateOwnerType, 
    1.46 +							  TInt aSize,
    1.47 +							  const TKeyIdentifier* aSubjectKeyId,
    1.48 +							  const TKeyIdentifier* aIssuerKeyId,
    1.49 +							  TInt aCertificateId,
    1.50 +							  TBool aDeletable) :
    1.51 +	iLabel(aLabel), iCertificateId(aCertificateId),
    1.52 +	iFormat(aFormat), iCertificateOwnerType(aCertificateOwnerType),
    1.53 +	iSize(aSize), iDeletable(aDeletable)
    1.54 +	{
    1.55 +	if (aSubjectKeyId)
    1.56 +		{
    1.57 +		iSubjectKeyId = *aSubjectKeyId;
    1.58 +		}
    1.59 +	else
    1.60 +		{
    1.61 +		iSubjectKeyId = KNullDesC8;
    1.62 +		}
    1.63 +	if (aIssuerKeyId)
    1.64 +		{
    1.65 +		iIssuerKeyId = *aIssuerKeyId;
    1.66 +		}
    1.67 +	else
    1.68 +		{
    1.69 +		iIssuerKeyId = KNullDesC8;
    1.70 +		}
    1.71 +	}
    1.72 +
    1.73 +EXPORT_C MCertInfo::MCertInfo(const MCertInfo& aOther) :
    1.74 +	iLabel(aOther.iLabel),
    1.75 +	iCertificateId(aOther.iCertificateId),
    1.76 +	iFormat(aOther.iFormat),
    1.77 +	iCertificateOwnerType(aOther.iCertificateOwnerType),
    1.78 +	iSize(aOther.iSize),
    1.79 +	iSubjectKeyId(aOther.iSubjectKeyId),
    1.80 +	iIssuerKeyId(aOther.iIssuerKeyId),
    1.81 +	iDeletable(aOther.iDeletable)
    1.82 +	{
    1.83 +	}
    1.84 +
    1.85 +EXPORT_C MCertInfo::~MCertInfo()
    1.86 +	{
    1.87 +	delete iIssuerHash;
    1.88 +	}
    1.89 +
    1.90 +const TDesC8* MCertInfo::IssuerHash() const
    1.91 +	{
    1.92 +	return iIssuerHash;
    1.93 +	}
    1.94 +
    1.95 +EXPORT_C void MCertInfo::ConstructL(const TDesC8* aIssuerHash)
    1.96 +	{
    1.97 +	delete iIssuerHash;
    1.98 +	iIssuerHash = NULL;
    1.99 +	if (aIssuerHash)
   1.100 +		{
   1.101 +		iIssuerHash = aIssuerHash->AllocL();
   1.102 +		}
   1.103 +
   1.104 +	if (!Valid())
   1.105 +		{
   1.106 +		User::Leave(KErrArgument);
   1.107 +		}
   1.108 +	}
   1.109 +	
   1.110 +TBool MCertInfo::Valid() const
   1.111 +	{
   1.112 +	if (iLabel.Length() == 0)
   1.113 +		{
   1.114 +		return EFalse;
   1.115 +		}
   1.116 +
   1.117 +	if (iCertificateId < 0)
   1.118 +		{
   1.119 +		return EFalse;
   1.120 +		}
   1.121 +
   1.122 +	if (iFormat != EX509Certificate && iFormat != EWTLSCertificate &&
   1.123 +		iFormat != EX968Certificate && iFormat != EX509CertificateUrl &&
   1.124 +		iFormat != EWTLSCertificateUrl && iFormat != EX968CertificateUrl)
   1.125 +		{
   1.126 +		return EFalse;
   1.127 +		}
   1.128 +	
   1.129 +	if (iCertificateOwnerType != ECACertificate &&
   1.130 +		iCertificateOwnerType != EUserCertificate &&
   1.131 +		iCertificateOwnerType != EPeerCertificate)
   1.132 +		{
   1.133 +		return EFalse;
   1.134 +		}
   1.135 +
   1.136 +	if (iSize <= 0)
   1.137 +		{
   1.138 +		return EFalse;
   1.139 +		}
   1.140 +
   1.141 +	if (iIssuerHash && *iIssuerHash == KNullDesC8)
   1.142 +		{
   1.143 +		return EFalse;
   1.144 +		}
   1.145 +
   1.146 +	return ETrue;
   1.147 +	}
   1.148 +
   1.149 +/**
   1.150 +		EXPORT_C void MCertInfo::ExternalizeL(RWriteStream& aStream) const
   1.151 +		
   1.152 +	This method externalizes the MCertInfo object to the given stream.
   1.153 +	The iDeletable boolean attribute is combined with the iFormat attribute
   1.154 +	for certstore backward compatibility
   1.155 +*/
   1.156 +EXPORT_C void MCertInfo::ExternalizeL(RWriteStream& aStream) const
   1.157 +	{
   1.158 +	// insert iDeletable flag as most significant digit of iFormat in order
   1.159 +	//   store the flag without changing the externalized record format
   1.160 +	//   The value is OPPOSITE for backward compatibility
   1.161 +	TUint8 tmpValue = static_cast <TUint8>(iFormat | (iDeletable ? 0 : KReadOnlyFlagMask));
   1.162 +	
   1.163 +	aStream.WriteUint8L(tmpValue);
   1.164 +	aStream.WriteInt32L(iSize);
   1.165 +	aStream << iLabel;
   1.166 +	aStream.WriteInt32L(iCertificateId);
   1.167 +	aStream.WriteUint8L(iCertificateOwnerType);
   1.168 +	aStream << iSubjectKeyId;
   1.169 +	aStream << iIssuerKeyId;
   1.170 +	}
   1.171 +
   1.172 +/**
   1.173 +	EXPORT_C void MCertInfo::InternalizeL(RReadStream& aStream)
   1.174 +	
   1.175 +	This method internalizes a MCertInfo object from the given stream.
   1.176 +	The iDeletable boolean and iFormat attributes are both extracted
   1.177 +	from the stored iFormat value using for certstore backward compatibility
   1.178 +*/
   1.179 +EXPORT_C void MCertInfo::InternalizeL(RReadStream& aStream)
   1.180 +	{
   1.181 +	// get first byte from stream containing iDeletable flag and iFormat value
   1.182 +	TUint8 tmpValue = aStream.ReadUint8L();
   1.183 +	
   1.184 +	// extract iDeletable flag from most significant digit of iFormat
   1.185 +	//   set iDeletable to the OPPOSITE of the 8th bit value
   1.186 +	iDeletable = !(tmpValue & KReadOnlyFlagMask);
   1.187 +		
   1.188 +	// extract iFormat = the value of the 7 least significant bits
   1.189 +	iFormat = static_cast <TCertificateFormat>(tmpValue & KFormatMask);
   1.190 +
   1.191 +	iSize = aStream.ReadInt32L();
   1.192 +	aStream >> iLabel;
   1.193 +	iCertificateId = aStream.ReadInt32L();
   1.194 +	iCertificateOwnerType = static_cast<TCertificateOwnerType>(aStream.ReadUint8L());
   1.195 +	aStream >> iSubjectKeyId;
   1.196 +	aStream >> iIssuerKeyId;
   1.197 +	
   1.198 +	if (!Valid())
   1.199 +		{
   1.200 +		User::Leave(KErrCorrupt);
   1.201 +		}
   1.202 +	}
   1.203 +
   1.204 +// CCTCertInfo /////////////////////////////////////////////////////////////////
   1.205 +
   1.206 +EXPORT_C CCTCertInfo* CCTCertInfo::NewL(RReadStream& aStream, MCTToken& aToken)
   1.207 +	{
   1.208 +	CCTCertInfo* self = CCTCertInfo::NewLC(aStream, aToken);
   1.209 +	CleanupStack::Pop(self);
   1.210 +	return self;
   1.211 +	}
   1.212 +
   1.213 +EXPORT_C CCTCertInfo* CCTCertInfo::NewLC(RReadStream& aStream, MCTToken& aToken)
   1.214 +	{
   1.215 +	CCTCertInfo* self = new(ELeave) CCTCertInfo(aToken);
   1.216 +	CleanupReleasePushL(*self);
   1.217 +	self->ConstructL(aStream);
   1.218 +	return self;
   1.219 +	}
   1.220 +
   1.221 +EXPORT_C CCTCertInfo* CCTCertInfo::NewL(const TDesC& aLabel,
   1.222 +										TCertificateFormat aFormat,
   1.223 +										TCertificateOwnerType aCertificateOwnerType,
   1.224 +										TInt aSize,
   1.225 +										const TKeyIdentifier* aSubjectKeyId,		
   1.226 +										const TKeyIdentifier* aIssuerKeyId,
   1.227 +										MCTToken& aToken,		
   1.228 +										TInt aCertificateId)
   1.229 +	{
   1.230 +	CCTCertInfo* self = CCTCertInfo::NewLC(aLabel,
   1.231 +		aFormat, aCertificateOwnerType, aSize, aSubjectKeyId, aIssuerKeyId, aToken,
   1.232 +		aCertificateId, ETrue);
   1.233 +	CleanupStack::Pop(self);
   1.234 +	return self;
   1.235 +	}
   1.236 +
   1.237 +EXPORT_C CCTCertInfo* CCTCertInfo::NewL(const TDesC& aLabel,
   1.238 +										TCertificateFormat aFormat,
   1.239 +										TCertificateOwnerType aCertificateOwnerType,
   1.240 +										TInt aSize,
   1.241 +										const TKeyIdentifier* aSubjectKeyId,		
   1.242 +										const TKeyIdentifier* aIssuerKeyId,
   1.243 +										MCTToken& aToken,		
   1.244 +										TInt aCertificateId, 
   1.245 +										TBool aIsDeletable, 
   1.246 +										const TDesC8* aIssuerHash)
   1.247 +	{
   1.248 +	CCTCertInfo* self = CCTCertInfo::NewLC(aLabel,
   1.249 +		aFormat, aCertificateOwnerType, aSize, aSubjectKeyId, aIssuerKeyId, aToken,
   1.250 +		aCertificateId, aIsDeletable, aIssuerHash);
   1.251 +	CleanupStack::Pop(self);
   1.252 +	return self;
   1.253 +	}
   1.254 +
   1.255 +EXPORT_C CCTCertInfo* CCTCertInfo::NewLC(const TDesC& aLabel,
   1.256 +										 TCertificateFormat aFormat,
   1.257 +										 TCertificateOwnerType aCertificateOwnerType,
   1.258 +										 TInt aSize,
   1.259 +										 const TKeyIdentifier* aSubjectKeyId,
   1.260 +										 const TKeyIdentifier* aIssuerKeyId,
   1.261 +										 MCTToken& aToken, 
   1.262 +										 TInt aCertificateId)
   1.263 +	{
   1.264 +	return CCTCertInfo::NewLC(aLabel,
   1.265 +		aFormat, aCertificateOwnerType, aSize, aSubjectKeyId, aIssuerKeyId, 
   1.266 +		aToken, aCertificateId, ETrue);
   1.267 +	}
   1.268 +
   1.269 +EXPORT_C CCTCertInfo* CCTCertInfo::NewLC(const TDesC& aLabel,
   1.270 +										 TCertificateFormat aFormat,
   1.271 +										 TCertificateOwnerType aCertificateOwnerType,
   1.272 +										 TInt aSize,
   1.273 +										 const TKeyIdentifier* aSubjectKeyId,
   1.274 +										 const TKeyIdentifier* aIssuerKeyId,
   1.275 +										 MCTToken& aToken, 
   1.276 +										 TInt aCertificateId, 
   1.277 +										 TBool aIsDeletable, 
   1.278 +										 const TDesC8* aIssuerHash)
   1.279 +	{
   1.280 +	CCTCertInfo* self = new(ELeave) CCTCertInfo(aLabel,
   1.281 +		aFormat, aCertificateOwnerType, aSize, aSubjectKeyId,
   1.282 +		aIssuerKeyId, aToken, aCertificateId, aIsDeletable);
   1.283 +	CleanupReleasePushL(*self);
   1.284 +	self->ConstructL(aIssuerHash);
   1.285 +	return self;
   1.286 +	}
   1.287 +
   1.288 +EXPORT_C CCTCertInfo* CCTCertInfo::NewL(const CCTCertInfo& aCertInfo)
   1.289 +	{
   1.290 +	CCTCertInfo* self = CCTCertInfo::NewLC(aCertInfo);
   1.291 +	CleanupStack::Pop(self);
   1.292 +	return self;
   1.293 +	}
   1.294 +
   1.295 +EXPORT_C CCTCertInfo* CCTCertInfo::NewLC(const CCTCertInfo& aCertInfo)
   1.296 +	{
   1.297 +	CCTCertInfo* self = new(ELeave) CCTCertInfo(aCertInfo);
   1.298 +	CleanupReleasePushL(*self);
   1.299 +	self->ConstructL(aCertInfo.IssuerHash());
   1.300 +	return self;
   1.301 +	}
   1.302 +
   1.303 +CCTCertInfo::CCTCertInfo(MCTToken& aToken)
   1.304 +	: MCTTokenObject(aToken), iToken(aToken)
   1.305 +	{
   1.306 +	}
   1.307 +
   1.308 +CCTCertInfo::CCTCertInfo(const TDesC& aLabel, 
   1.309 +						 TCertificateFormat aFormat,
   1.310 +						 TCertificateOwnerType aCertificateOwnerType,
   1.311 +						 TInt aSize,
   1.312 +						 const TKeyIdentifier* aSubjectKeyId,
   1.313 +						 const TKeyIdentifier* aIssuerKeyId,
   1.314 +						 MCTToken& aToken, 
   1.315 +						 TInt aCertificateId,
   1.316 +						 TBool aIsDeletable)
   1.317 +	: MCTTokenObject(aToken),
   1.318 +	  MCertInfo(aLabel, aFormat, aCertificateOwnerType, aSize, aSubjectKeyId,
   1.319 +				aIssuerKeyId, aCertificateId, aIsDeletable),
   1.320 +	  iToken(aToken)
   1.321 +	{
   1.322 +	}
   1.323 +
   1.324 +CCTCertInfo::CCTCertInfo(const CCTCertInfo& aOther)
   1.325 +	: MCTTokenObject(aOther.iToken), MCertInfo(aOther), iToken(aOther.iToken)
   1.326 +	{
   1.327 +	}
   1.328 +
   1.329 +void CCTCertInfo::ConstructL(RReadStream& aStream)
   1.330 +	{
   1.331 +	InternalizeL(aStream);
   1.332 +	}
   1.333 +
   1.334 +void CCTCertInfo::ConstructL(const TDesC8* aIssuerHash)
   1.335 +	{
   1.336 +	MCertInfo::ConstructL(aIssuerHash);
   1.337 +	}
   1.338 +
   1.339 +EXPORT_C CCTCertInfo::~CCTCertInfo()
   1.340 +	{
   1.341 +	}
   1.342 +
   1.343 +const TDesC& CCTCertInfo::Label() const
   1.344 +	{
   1.345 +	return iLabel;
   1.346 +	}
   1.347 +
   1.348 +TUid CCTCertInfo::Type() const
   1.349 +	{
   1.350 +	TUid uid = { KCTObjectCertInfo };
   1.351 +	return uid;
   1.352 +	}
   1.353 +
   1.354 +EXPORT_C const TKeyIdentifier& CCTCertInfo::SubjectKeyId() const
   1.355 +	{
   1.356 +	return iSubjectKeyId;
   1.357 +	}
   1.358 +
   1.359 +EXPORT_C const TKeyIdentifier& CCTCertInfo::IssuerKeyId() const
   1.360 +	{
   1.361 +	return iIssuerKeyId;
   1.362 +	}
   1.363 +
   1.364 +EXPORT_C TCertificateFormat CCTCertInfo::CertificateFormat() const
   1.365 +	{
   1.366 +	return iFormat;
   1.367 +	}
   1.368 +
   1.369 +EXPORT_C TCertificateOwnerType CCTCertInfo::CertificateOwnerType() const
   1.370 +	{
   1.371 +	return iCertificateOwnerType;
   1.372 +	}
   1.373 +
   1.374 +EXPORT_C TInt CCTCertInfo::Size() const
   1.375 +	{
   1.376 +	return iSize;
   1.377 +	}
   1.378 +
   1.379 +MCTToken& CCTCertInfo::Token() const
   1.380 +	{
   1.381 +	return iToken;
   1.382 +	}
   1.383 +
   1.384 +EXPORT_C TCTTokenObjectHandle CCTCertInfo::Handle() const
   1.385 +	{
   1.386 +	return TCTTokenObjectHandle(iToken.Handle(), iCertificateId);
   1.387 +	}
   1.388 +
   1.389 +EXPORT_C TBool CCTCertInfo::IsDeletable() const
   1.390 +	{
   1.391 +	return iDeletable;
   1.392 +	}
   1.393 +
   1.394 +EXPORT_C const TDesC8* CCTCertInfo::IssuerHash() const
   1.395 +	{
   1.396 +	return MCertInfo::IssuerHash();
   1.397 +	}
   1.398 +
   1.399 +EXPORT_C TBool CCTCertInfo::operator==(const CCTCertInfo& aCertInfo) const
   1.400 +	{
   1.401 +	return aCertInfo.iLabel == iLabel;
   1.402 +	}
   1.403 +
   1.404 +EXPORT_C void CCTCertInfo::SetCertificateId(TInt aCertId)
   1.405 +{
   1.406 +	iCertificateId = aCertId;
   1.407 +}