os/security/cryptomgmtlibs/cryptotokenfw/source/ctframework/Cctcertinfo.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2001-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 "cctcertinfo.h"
    20 
    21 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
    22 
    23 /** Mask constants used for serializing iDeletable and iFormat attributes 
    24 */
    25 const TUint KReadOnlyFlagMask = 128;
    26 const TUint KFormatMask = 127;
    27 
    28 /** The UID of a CertInfo MCTTokenObject. */
    29 const TInt KCTObjectCertInfo = 0x101F50E6;
    30 
    31 #endif
    32 
    33 // MCertInfo ///////////////////////////////////////////////////////////////////
    34 
    35 EXPORT_C MCertInfo::MCertInfo() :
    36 	iDeletable(ETrue)
    37 	{
    38 	}
    39 
    40 EXPORT_C MCertInfo::MCertInfo(const TDesC& aLabel,
    41 							  TCertificateFormat aFormat,
    42 							  TCertificateOwnerType aCertificateOwnerType, 
    43 							  TInt aSize,
    44 							  const TKeyIdentifier* aSubjectKeyId,
    45 							  const TKeyIdentifier* aIssuerKeyId,
    46 							  TInt aCertificateId,
    47 							  TBool aDeletable) :
    48 	iLabel(aLabel), iCertificateId(aCertificateId),
    49 	iFormat(aFormat), iCertificateOwnerType(aCertificateOwnerType),
    50 	iSize(aSize), iDeletable(aDeletable)
    51 	{
    52 	if (aSubjectKeyId)
    53 		{
    54 		iSubjectKeyId = *aSubjectKeyId;
    55 		}
    56 	else
    57 		{
    58 		iSubjectKeyId = KNullDesC8;
    59 		}
    60 	if (aIssuerKeyId)
    61 		{
    62 		iIssuerKeyId = *aIssuerKeyId;
    63 		}
    64 	else
    65 		{
    66 		iIssuerKeyId = KNullDesC8;
    67 		}
    68 	}
    69 
    70 EXPORT_C MCertInfo::MCertInfo(const MCertInfo& aOther) :
    71 	iLabel(aOther.iLabel),
    72 	iCertificateId(aOther.iCertificateId),
    73 	iFormat(aOther.iFormat),
    74 	iCertificateOwnerType(aOther.iCertificateOwnerType),
    75 	iSize(aOther.iSize),
    76 	iSubjectKeyId(aOther.iSubjectKeyId),
    77 	iIssuerKeyId(aOther.iIssuerKeyId),
    78 	iDeletable(aOther.iDeletable)
    79 	{
    80 	}
    81 
    82 EXPORT_C MCertInfo::~MCertInfo()
    83 	{
    84 	delete iIssuerHash;
    85 	}
    86 
    87 const TDesC8* MCertInfo::IssuerHash() const
    88 	{
    89 	return iIssuerHash;
    90 	}
    91 
    92 EXPORT_C void MCertInfo::ConstructL(const TDesC8* aIssuerHash)
    93 	{
    94 	delete iIssuerHash;
    95 	iIssuerHash = NULL;
    96 	if (aIssuerHash)
    97 		{
    98 		iIssuerHash = aIssuerHash->AllocL();
    99 		}
   100 
   101 	if (!Valid())
   102 		{
   103 		User::Leave(KErrArgument);
   104 		}
   105 	}
   106 	
   107 TBool MCertInfo::Valid() const
   108 	{
   109 	if (iLabel.Length() == 0)
   110 		{
   111 		return EFalse;
   112 		}
   113 
   114 	if (iCertificateId < 0)
   115 		{
   116 		return EFalse;
   117 		}
   118 
   119 	if (iFormat != EX509Certificate && iFormat != EWTLSCertificate &&
   120 		iFormat != EX968Certificate && iFormat != EX509CertificateUrl &&
   121 		iFormat != EWTLSCertificateUrl && iFormat != EX968CertificateUrl)
   122 		{
   123 		return EFalse;
   124 		}
   125 	
   126 	if (iCertificateOwnerType != ECACertificate &&
   127 		iCertificateOwnerType != EUserCertificate &&
   128 		iCertificateOwnerType != EPeerCertificate)
   129 		{
   130 		return EFalse;
   131 		}
   132 
   133 	if (iSize <= 0)
   134 		{
   135 		return EFalse;
   136 		}
   137 
   138 	if (iIssuerHash && *iIssuerHash == KNullDesC8)
   139 		{
   140 		return EFalse;
   141 		}
   142 
   143 	return ETrue;
   144 	}
   145 
   146 /**
   147 		EXPORT_C void MCertInfo::ExternalizeL(RWriteStream& aStream) const
   148 		
   149 	This method externalizes the MCertInfo object to the given stream.
   150 	The iDeletable boolean attribute is combined with the iFormat attribute
   151 	for certstore backward compatibility
   152 */
   153 EXPORT_C void MCertInfo::ExternalizeL(RWriteStream& aStream) const
   154 	{
   155 	// insert iDeletable flag as most significant digit of iFormat in order
   156 	//   store the flag without changing the externalized record format
   157 	//   The value is OPPOSITE for backward compatibility
   158 	TUint8 tmpValue = static_cast <TUint8>(iFormat | (iDeletable ? 0 : KReadOnlyFlagMask));
   159 	
   160 	aStream.WriteUint8L(tmpValue);
   161 	aStream.WriteInt32L(iSize);
   162 	aStream << iLabel;
   163 	aStream.WriteInt32L(iCertificateId);
   164 	aStream.WriteUint8L(iCertificateOwnerType);
   165 	aStream << iSubjectKeyId;
   166 	aStream << iIssuerKeyId;
   167 	}
   168 
   169 /**
   170 	EXPORT_C void MCertInfo::InternalizeL(RReadStream& aStream)
   171 	
   172 	This method internalizes a MCertInfo object from the given stream.
   173 	The iDeletable boolean and iFormat attributes are both extracted
   174 	from the stored iFormat value using for certstore backward compatibility
   175 */
   176 EXPORT_C void MCertInfo::InternalizeL(RReadStream& aStream)
   177 	{
   178 	// get first byte from stream containing iDeletable flag and iFormat value
   179 	TUint8 tmpValue = aStream.ReadUint8L();
   180 	
   181 	// extract iDeletable flag from most significant digit of iFormat
   182 	//   set iDeletable to the OPPOSITE of the 8th bit value
   183 	iDeletable = !(tmpValue & KReadOnlyFlagMask);
   184 		
   185 	// extract iFormat = the value of the 7 least significant bits
   186 	iFormat = static_cast <TCertificateFormat>(tmpValue & KFormatMask);
   187 
   188 	iSize = aStream.ReadInt32L();
   189 	aStream >> iLabel;
   190 	iCertificateId = aStream.ReadInt32L();
   191 	iCertificateOwnerType = static_cast<TCertificateOwnerType>(aStream.ReadUint8L());
   192 	aStream >> iSubjectKeyId;
   193 	aStream >> iIssuerKeyId;
   194 	
   195 	if (!Valid())
   196 		{
   197 		User::Leave(KErrCorrupt);
   198 		}
   199 	}
   200 
   201 // CCTCertInfo /////////////////////////////////////////////////////////////////
   202 
   203 EXPORT_C CCTCertInfo* CCTCertInfo::NewL(RReadStream& aStream, MCTToken& aToken)
   204 	{
   205 	CCTCertInfo* self = CCTCertInfo::NewLC(aStream, aToken);
   206 	CleanupStack::Pop(self);
   207 	return self;
   208 	}
   209 
   210 EXPORT_C CCTCertInfo* CCTCertInfo::NewLC(RReadStream& aStream, MCTToken& aToken)
   211 	{
   212 	CCTCertInfo* self = new(ELeave) CCTCertInfo(aToken);
   213 	CleanupReleasePushL(*self);
   214 	self->ConstructL(aStream);
   215 	return self;
   216 	}
   217 
   218 EXPORT_C CCTCertInfo* CCTCertInfo::NewL(const TDesC& aLabel,
   219 										TCertificateFormat aFormat,
   220 										TCertificateOwnerType aCertificateOwnerType,
   221 										TInt aSize,
   222 										const TKeyIdentifier* aSubjectKeyId,		
   223 										const TKeyIdentifier* aIssuerKeyId,
   224 										MCTToken& aToken,		
   225 										TInt aCertificateId)
   226 	{
   227 	CCTCertInfo* self = CCTCertInfo::NewLC(aLabel,
   228 		aFormat, aCertificateOwnerType, aSize, aSubjectKeyId, aIssuerKeyId, aToken,
   229 		aCertificateId, ETrue);
   230 	CleanupStack::Pop(self);
   231 	return self;
   232 	}
   233 
   234 EXPORT_C CCTCertInfo* CCTCertInfo::NewL(const TDesC& aLabel,
   235 										TCertificateFormat aFormat,
   236 										TCertificateOwnerType aCertificateOwnerType,
   237 										TInt aSize,
   238 										const TKeyIdentifier* aSubjectKeyId,		
   239 										const TKeyIdentifier* aIssuerKeyId,
   240 										MCTToken& aToken,		
   241 										TInt aCertificateId, 
   242 										TBool aIsDeletable, 
   243 										const TDesC8* aIssuerHash)
   244 	{
   245 	CCTCertInfo* self = CCTCertInfo::NewLC(aLabel,
   246 		aFormat, aCertificateOwnerType, aSize, aSubjectKeyId, aIssuerKeyId, aToken,
   247 		aCertificateId, aIsDeletable, aIssuerHash);
   248 	CleanupStack::Pop(self);
   249 	return self;
   250 	}
   251 
   252 EXPORT_C CCTCertInfo* CCTCertInfo::NewLC(const TDesC& aLabel,
   253 										 TCertificateFormat aFormat,
   254 										 TCertificateOwnerType aCertificateOwnerType,
   255 										 TInt aSize,
   256 										 const TKeyIdentifier* aSubjectKeyId,
   257 										 const TKeyIdentifier* aIssuerKeyId,
   258 										 MCTToken& aToken, 
   259 										 TInt aCertificateId)
   260 	{
   261 	return CCTCertInfo::NewLC(aLabel,
   262 		aFormat, aCertificateOwnerType, aSize, aSubjectKeyId, aIssuerKeyId, 
   263 		aToken, aCertificateId, ETrue);
   264 	}
   265 
   266 EXPORT_C CCTCertInfo* CCTCertInfo::NewLC(const TDesC& aLabel,
   267 										 TCertificateFormat aFormat,
   268 										 TCertificateOwnerType aCertificateOwnerType,
   269 										 TInt aSize,
   270 										 const TKeyIdentifier* aSubjectKeyId,
   271 										 const TKeyIdentifier* aIssuerKeyId,
   272 										 MCTToken& aToken, 
   273 										 TInt aCertificateId, 
   274 										 TBool aIsDeletable, 
   275 										 const TDesC8* aIssuerHash)
   276 	{
   277 	CCTCertInfo* self = new(ELeave) CCTCertInfo(aLabel,
   278 		aFormat, aCertificateOwnerType, aSize, aSubjectKeyId,
   279 		aIssuerKeyId, aToken, aCertificateId, aIsDeletable);
   280 	CleanupReleasePushL(*self);
   281 	self->ConstructL(aIssuerHash);
   282 	return self;
   283 	}
   284 
   285 EXPORT_C CCTCertInfo* CCTCertInfo::NewL(const CCTCertInfo& aCertInfo)
   286 	{
   287 	CCTCertInfo* self = CCTCertInfo::NewLC(aCertInfo);
   288 	CleanupStack::Pop(self);
   289 	return self;
   290 	}
   291 
   292 EXPORT_C CCTCertInfo* CCTCertInfo::NewLC(const CCTCertInfo& aCertInfo)
   293 	{
   294 	CCTCertInfo* self = new(ELeave) CCTCertInfo(aCertInfo);
   295 	CleanupReleasePushL(*self);
   296 	self->ConstructL(aCertInfo.IssuerHash());
   297 	return self;
   298 	}
   299 
   300 CCTCertInfo::CCTCertInfo(MCTToken& aToken)
   301 	: MCTTokenObject(aToken), iToken(aToken)
   302 	{
   303 	}
   304 
   305 CCTCertInfo::CCTCertInfo(const TDesC& aLabel, 
   306 						 TCertificateFormat aFormat,
   307 						 TCertificateOwnerType aCertificateOwnerType,
   308 						 TInt aSize,
   309 						 const TKeyIdentifier* aSubjectKeyId,
   310 						 const TKeyIdentifier* aIssuerKeyId,
   311 						 MCTToken& aToken, 
   312 						 TInt aCertificateId,
   313 						 TBool aIsDeletable)
   314 	: MCTTokenObject(aToken),
   315 	  MCertInfo(aLabel, aFormat, aCertificateOwnerType, aSize, aSubjectKeyId,
   316 				aIssuerKeyId, aCertificateId, aIsDeletable),
   317 	  iToken(aToken)
   318 	{
   319 	}
   320 
   321 CCTCertInfo::CCTCertInfo(const CCTCertInfo& aOther)
   322 	: MCTTokenObject(aOther.iToken), MCertInfo(aOther), iToken(aOther.iToken)
   323 	{
   324 	}
   325 
   326 void CCTCertInfo::ConstructL(RReadStream& aStream)
   327 	{
   328 	InternalizeL(aStream);
   329 	}
   330 
   331 void CCTCertInfo::ConstructL(const TDesC8* aIssuerHash)
   332 	{
   333 	MCertInfo::ConstructL(aIssuerHash);
   334 	}
   335 
   336 EXPORT_C CCTCertInfo::~CCTCertInfo()
   337 	{
   338 	}
   339 
   340 const TDesC& CCTCertInfo::Label() const
   341 	{
   342 	return iLabel;
   343 	}
   344 
   345 TUid CCTCertInfo::Type() const
   346 	{
   347 	TUid uid = { KCTObjectCertInfo };
   348 	return uid;
   349 	}
   350 
   351 EXPORT_C const TKeyIdentifier& CCTCertInfo::SubjectKeyId() const
   352 	{
   353 	return iSubjectKeyId;
   354 	}
   355 
   356 EXPORT_C const TKeyIdentifier& CCTCertInfo::IssuerKeyId() const
   357 	{
   358 	return iIssuerKeyId;
   359 	}
   360 
   361 EXPORT_C TCertificateFormat CCTCertInfo::CertificateFormat() const
   362 	{
   363 	return iFormat;
   364 	}
   365 
   366 EXPORT_C TCertificateOwnerType CCTCertInfo::CertificateOwnerType() const
   367 	{
   368 	return iCertificateOwnerType;
   369 	}
   370 
   371 EXPORT_C TInt CCTCertInfo::Size() const
   372 	{
   373 	return iSize;
   374 	}
   375 
   376 MCTToken& CCTCertInfo::Token() const
   377 	{
   378 	return iToken;
   379 	}
   380 
   381 EXPORT_C TCTTokenObjectHandle CCTCertInfo::Handle() const
   382 	{
   383 	return TCTTokenObjectHandle(iToken.Handle(), iCertificateId);
   384 	}
   385 
   386 EXPORT_C TBool CCTCertInfo::IsDeletable() const
   387 	{
   388 	return iDeletable;
   389 	}
   390 
   391 EXPORT_C const TDesC8* CCTCertInfo::IssuerHash() const
   392 	{
   393 	return MCertInfo::IssuerHash();
   394 	}
   395 
   396 EXPORT_C TBool CCTCertInfo::operator==(const CCTCertInfo& aCertInfo) const
   397 	{
   398 	return aCertInfo.iLabel == iLabel;
   399 	}
   400 
   401 EXPORT_C void CCTCertInfo::SetCertificateId(TInt aCertId)
   402 {
   403 	iCertificateId = aCertId;
   404 }