smf/smfcredentialmgr/smfcredmgrclient/smfcredmgrclient_p.cpp
changeset 14 a469c0e6e7fb
child 18 013a02bf2bb0
equal deleted inserted replaced
13:b5d63d5fc252 14:a469c0e6e7fb
       
     1 /**
       
     2  * Copyright (c) 2010 Sasken Communication Technologies Ltd.
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the "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  * Pritam Roy Biswas, Sasken Communication Technologies Ltd - Initial contribution
       
    11  *
       
    12  * Description:
       
    13  * This is the private implementation Class on Symbian OS for Credential Manager Client.
       
    14  *  Derives from CAtive to support asynchronous requests.
       
    15  *
       
    16  */
       
    17 //  Include Files
       
    18 #include <smfcredmgrcommon.h>
       
    19 #include <smfcredmgrclientdatastruct.h>
       
    20 #include <smfutils.h>
       
    21 #include <securitydefs.h>
       
    22 #include "smfcredmgrclientutil.h"
       
    23 #include "smfcredmgrclient_p.h"
       
    24 
       
    25 CSmfCredMgrClientSymbian::CSmfCredMgrClientSymbian(
       
    26 		SmfCredMgrClient* aPublicImpl) :
       
    27 	iPublicImpl(aPublicImpl), CActive(EPriorityStandard)
       
    28 	{
       
    29 
       
    30 	}
       
    31 
       
    32 CSmfCredMgrClientSymbian* CSmfCredMgrClientSymbian::NewL(
       
    33 		SmfCredMgrClient* aPublicImpl)
       
    34 	{
       
    35 	CSmfCredMgrClientSymbian* self = NewLC(aPublicImpl);
       
    36 	CleanupStack::Pop(self);
       
    37 	return (self);
       
    38 	}
       
    39 
       
    40 CSmfCredMgrClientSymbian* CSmfCredMgrClientSymbian::NewLC(
       
    41 		SmfCredMgrClient* aPublicImpl)
       
    42 	{
       
    43 	CSmfCredMgrClientSymbian* self = new (ELeave) CSmfCredMgrClientSymbian(
       
    44 			aPublicImpl);
       
    45 	CleanupStack::PushL(self);
       
    46 	self->ConstructL();
       
    47 	return (self);
       
    48 	}
       
    49 
       
    50 void CSmfCredMgrClientSymbian::ConstructL()
       
    51 	{
       
    52 	User::LeaveIfError(iSession.connectToServer());
       
    53 	}
       
    54 
       
    55 CSmfCredMgrClientSymbian::~CSmfCredMgrClientSymbian()
       
    56 	{
       
    57 	//cancel pending request before close session
       
    58 	Cancel();
       
    59 	iSession.Close();
       
    60 	}
       
    61 
       
    62 void CSmfCredMgrClientSymbian::RunL()
       
    63 	{
       
    64 
       
    65 	}
       
    66 
       
    67 void CSmfCredMgrClientSymbian::DoCancel()
       
    68 	{
       
    69 
       
    70 	}
       
    71 
       
    72 TBool CSmfCredMgrClientSymbian::AuthDataSetL(QString RegToken,
       
    73 		QDateTime Validity, SmfAuthParams& AuthTokenSet)
       
    74 	{
       
    75 	CSmfFetchAuthTokenSet* fetchAuthTokenSetParams =
       
    76 			new (ELeave) CSmfFetchAuthTokenSet;
       
    77 
       
    78 	CleanupStack::PushL(fetchAuthTokenSetParams);
       
    79 
       
    80 	//fill the input params
       
    81 	fetchAuthTokenSetParams->iRegistrationToken = qt_QString2HBufC(RegToken);
       
    82 	fetchAuthTokenSetParams->iValidity = Validity.toTime_t();
       
    83 
       
    84 	//create buffer to serialize data
       
    85 	CBufFlat* buf = CBufFlat::NewL(KMaxBufSize);
       
    86 	CleanupStack::PushL(buf);
       
    87 	RBufWriteStream stream(*buf);
       
    88 	CleanupClosePushL(stream);
       
    89 
       
    90 	fetchAuthTokenSetParams->ExternalizeL(stream);
       
    91 	stream.CommitL();
       
    92 
       
    93 	TPtr8 bufPtr = buf->Ptr(0);
       
    94 
       
    95 	TIpcArgs args;
       
    96 	args.Set(0, &bufPtr);
       
    97 
       
    98 	// to get the data from server, we create a space.
       
    99 	HBufC8* retBuf = HBufC8::NewL(KMaxBufSize);
       
   100 	CleanupStack::PushL(retBuf);
       
   101 
       
   102 	TPtr8 outputptr = retBuf->Des();
       
   103 	args.Set(1, &outputptr);
       
   104 
       
   105 	iSession.RequestService(ESendAuthDataSet, args);
       
   106 
       
   107 	//create buffer to read data sent by server
       
   108 	RBuf8 dataBuf;
       
   109 	CleanupClosePushL(dataBuf);
       
   110 	dataBuf.Create(outputptr);
       
   111 
       
   112 	fetchAuthTokenSetParams->InternalizeL(dataBuf);
       
   113 
       
   114 	//return EFalse if count is 0
       
   115 	if (fetchAuthTokenSetParams->iAuthTokenArray.Count() == 0)
       
   116 		{
       
   117 		return EFalse;
       
   118 		}
       
   119 
       
   120 	smfcredmgrclientutil::RArrayToSmfAuthParams(
       
   121 			fetchAuthTokenSetParams->iAuthTokenArray, AuthTokenSet);
       
   122 
       
   123 	CleanupStack::PopAndDestroy(&dataBuf);
       
   124 	CleanupStack::PopAndDestroy(retBuf);
       
   125 	CleanupStack::PopAndDestroy(&stream);
       
   126 	CleanupStack::PopAndDestroy(buf);
       
   127 	CleanupStack::PopAndDestroy(fetchAuthTokenSetParams);
       
   128 
       
   129 	return ETrue;
       
   130 	}
       
   131 
       
   132 QString CSmfCredMgrClientSymbian::storeAuthDataL(SmfAuthParams Set,
       
   133 		QDateTime Validity, QList<QUrl> URLList, QStringList PluginList,
       
   134 		QString AuthAppAID, bool Flag)
       
   135 	{
       
   136 
       
   137 	CSmfStoreAuthParams* authenticationProcessData =
       
   138 			new (ELeave) CSmfStoreAuthParams;
       
   139 	CleanupStack::PushL(authenticationProcessData);
       
   140 
       
   141 	//create space for iRegistrationToken
       
   142 	authenticationProcessData->iRegistrationToken = HBufC::NewL(
       
   143 			KMaxRegistrationTokenLength);
       
   144 
       
   145 	TPtr8 regTokenPtr(
       
   146 			authenticationProcessData->iRegistrationToken->Des().Collapse());
       
   147 
       
   148 	//fill the input params
       
   149 	smfcredmgrclientutil::SmfAuthParamstoRArray(Set,
       
   150 			authenticationProcessData->iAuthTokenArray);
       
   151 	authenticationProcessData->pluginIDEnabled = Flag;
       
   152 	//set iValidity
       
   153 	authenticationProcessData->iValidity = Validity.toTime_t();
       
   154 
       
   155 
       
   156 	//set authappid
       
   157 	authenticationProcessData->iAuthAppID = qt_QString2HBufC(AuthAppAID);
       
   158 	//set the lists
       
   159 	authenticationProcessData->iURLList
       
   160 			= smfcredmgrclientutil::convertToSymbianURLList(URLList);
       
   161 	authenticationProcessData->iPluginIDList
       
   162 			= smfcredmgrclientutil::convertToSymbianPluginList(
       
   163 					PluginList);
       
   164 
       
   165 	//create buffer to serialize data
       
   166 	CBufFlat* buf = CBufFlat::NewL(KMaxBufSize);
       
   167 	CleanupStack::PushL(buf);
       
   168 	RBufWriteStream stream(*buf);
       
   169 	CleanupClosePushL(stream);
       
   170 
       
   171 	authenticationProcessData->ExternalizeL(stream);
       
   172 	stream.CommitL();
       
   173 
       
   174 	TPtr8 bufPtr = buf->Ptr(0);
       
   175 
       
   176 	TIpcArgs args;
       
   177 	args.Set(0, &bufPtr);
       
   178 	args.Set(1, &regTokenPtr);
       
   179 	iSession.RequestService(EStoreAuthData, args);
       
   180 
       
   181 	TPtr wideRegToken = regTokenPtr.Expand();
       
   182 
       
   183 	//convert and return
       
   184 	QString regString = qt_TDes2QString(wideRegToken);
       
   185 
       
   186 	CleanupStack::PopAndDestroy(&stream);
       
   187 	CleanupStack::PopAndDestroy(buf);
       
   188 	CleanupStack::Pop();
       
   189 
       
   190 	return regString;
       
   191 	}
       
   192 
       
   193 TBool CSmfCredMgrClientSymbian::isPluginAuthenticatedL(QString PluginID)
       
   194 	{
       
   195 	CBufFlat* buf = CBufFlat::NewL(KMinBufSize);
       
   196 	CleanupStack::PushL(buf);
       
   197 
       
   198 	RBufWriteStream stream(*buf);
       
   199 	CleanupClosePushL(stream);
       
   200 
       
   201 	TPtr idPtr(qt_QString2HBufC(PluginID)->Des());
       
   202 	SmfUtils::ExternalizeDesL(idPtr, stream);
       
   203 
       
   204 	stream.CommitL();
       
   205 
       
   206 	// to get the data from server, we create a space.
       
   207 	HBufC8* retBuf = HBufC8::NewL(32);
       
   208 	CleanupStack::PushL(retBuf);
       
   209 
       
   210 	TPtr8 bufPtr = buf->Ptr(0);
       
   211 	TPtr8 flag(retBuf->Des());
       
   212 
       
   213 	TIpcArgs args;
       
   214 
       
   215 	args.Set(0, &bufPtr);
       
   216 	args.Set(1, &flag);
       
   217 
       
   218 	iSession.RequestService(ECheckPluginAuthentication, args);
       
   219 
       
   220 	TLex8 iLex = TLex8(flag);
       
   221 	TInt value = 0;
       
   222 	iLex.Val(value);
       
   223 
       
   224 	CleanupStack::PopAndDestroy(retBuf);
       
   225 	CleanupStack::PopAndDestroy(&stream);
       
   226 	CleanupStack::PopAndDestroy(buf);
       
   227 
       
   228 	if (value)
       
   229 		{
       
   230 		RDebug::Printf("flag returned is ETrue");
       
   231 		return ETrue;
       
   232 		}
       
   233 	else
       
   234 		{
       
   235 		RDebug::Printf("flag returned is EFalse");
       
   236 		return EFalse;
       
   237 		}
       
   238 	}
       
   239 
       
   240 void CSmfCredMgrClientSymbian::authenticatedPluginListL(
       
   241 		QString RegistrationToken, QStringList& List)
       
   242 	{
       
   243 	CSmfPluginIDListParams* fetchPluginListParams =
       
   244 			new (ELeave) CSmfPluginIDListParams;
       
   245 	CleanupStack::PushL(fetchPluginListParams);
       
   246 
       
   247 	//fill input params
       
   248 	fetchPluginListParams->iRegistrationToken = qt_QString2HBufC(
       
   249 			RegistrationToken);
       
   250 
       
   251 	//create buffer to serialize data
       
   252 	CBufFlat* buf = CBufFlat::NewL(KMinBufSize);
       
   253 	CleanupStack::PushL(buf);
       
   254 	RBufWriteStream stream(*buf);
       
   255 	CleanupClosePushL(stream);
       
   256 
       
   257 	fetchPluginListParams->ExternalizeL(stream);
       
   258 	stream.CommitL();
       
   259 
       
   260 	TPtr8 bufPtr1 = buf->Ptr(0);
       
   261 	TIpcArgs args;
       
   262 	args.Set(0, &bufPtr1);
       
   263 
       
   264 	// to get the data from server, we create a space.
       
   265 	HBufC8* retBuf = HBufC8::NewL(KMaxBufSize);
       
   266 	CleanupStack::PushL(retBuf);
       
   267 
       
   268 	TPtr8 outputptr = retBuf->Des();
       
   269 	args.Set(1, &outputptr);
       
   270 
       
   271 	iSession.RequestService(ESendPluginIDList, args);
       
   272 
       
   273 	//create buffer to read data received
       
   274 	RBuf8 dataBuf;
       
   275 	CleanupClosePushL(dataBuf);
       
   276 	dataBuf.Create(outputptr);
       
   277 
       
   278 	fetchPluginListParams->InternalizeL(dataBuf);
       
   279 
       
   280 	smfcredmgrclientutil::convertToQStringList(
       
   281 			fetchPluginListParams->iPluginList, List);
       
   282 
       
   283 	CleanupStack::PopAndDestroy(&dataBuf);
       
   284 	CleanupStack::PopAndDestroy(retBuf);
       
   285 	CleanupStack::PopAndDestroy(&stream);
       
   286 	CleanupStack::PopAndDestroy(buf);
       
   287 	CleanupStack::PopAndDestroy(fetchPluginListParams);
       
   288 	}
       
   289 
       
   290 void CSmfCredMgrClientSymbian::URLListL(QString PluginID, QList<QUrl>& List)
       
   291 
       
   292 	{
       
   293 	CSmfURLListParams* fetchURLListParams = new (ELeave) CSmfURLListParams;
       
   294 	CleanupStack::PushL(fetchURLListParams);
       
   295 
       
   296 	fetchURLListParams->iPluginID = qt_QString2HBufC(PluginID);
       
   297 
       
   298 	//create buffer to serialize data
       
   299 	CBufFlat* buf = CBufFlat::NewL(KMinBufSize);
       
   300 	CleanupStack::PushL(buf);
       
   301 	RBufWriteStream stream(*buf);
       
   302 	CleanupClosePushL(stream);
       
   303 
       
   304 	fetchURLListParams->ExternalizeL(stream);
       
   305 	stream.CommitL();
       
   306 
       
   307 	TPtr8 bufPtr = buf->Ptr(0);
       
   308 
       
   309 	TIpcArgs args;
       
   310 	args.Set(0, &bufPtr);
       
   311 
       
   312 	// to get the data from server, we create a space.
       
   313 	HBufC8* retBuf = HBufC8::NewL(KMaxBufSize);
       
   314 	CleanupStack::PushL(retBuf);
       
   315 
       
   316 	TPtr8 outputptr = retBuf->Des();
       
   317 	args.Set(1, &outputptr);
       
   318 
       
   319 	iSession.RequestService(ESendURLList, args);
       
   320 
       
   321 	//create buffer to read data received
       
   322 	RBuf8 dataBuf;
       
   323 	CleanupClosePushL(dataBuf);
       
   324 	dataBuf.Create(outputptr);
       
   325 
       
   326 	fetchURLListParams->InternalizeL(dataBuf);
       
   327 
       
   328 	smfcredmgrclientutil::convertToQUrlList(
       
   329 			fetchURLListParams->iURLList, List);
       
   330 
       
   331 	CleanupStack::PopAndDestroy(&dataBuf);
       
   332 	CleanupStack::PopAndDestroy(retBuf);
       
   333 	CleanupStack::PopAndDestroy(&stream);
       
   334 	CleanupStack::PopAndDestroy(buf);
       
   335 	CleanupStack::Pop(fetchURLListParams);
       
   336 
       
   337 	}
       
   338 
       
   339 QString CSmfCredMgrClientSymbian::generateNONCE(const qint64 Length)
       
   340 	{
       
   341 	srand(time(0));
       
   342 	QDateTime UniqueNumber = QDateTime::currentDateTime();
       
   343 
       
   344 	//read upto milliseconds
       
   345 	QString RetString(UniqueNumber.toString("hh:mm:ss.zzz"));
       
   346 
       
   347 	QString Letters(
       
   348 			"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
       
   349 
       
   350 	//append a randomly generated string to RetString
       
   351 	for (int i = RetString.count(); i < Length; i++)
       
   352 		{
       
   353 		RetString.insert((i), Letters.at(rand() % Letters.size()));
       
   354 		}
       
   355 
       
   356 	return RetString;
       
   357 	}
       
   358 
       
   359 void CSmfCredMgrClientSymbian::changePluginIDListL(const QString NewPluginID,
       
   360 		const bool Flag, const QString OldPluginID)
       
   361 	{
       
   362 	CSmfPluginIDUpdate* changePluginListParams =
       
   363 			new (ELeave) CSmfPluginIDUpdate;
       
   364 	CleanupStack::PushL(changePluginListParams);
       
   365 
       
   366 	//set the input params
       
   367 	changePluginListParams->iNewPluginID = qt_QString2HBufC(NewPluginID);
       
   368 	changePluginListParams->iOldPluginID = qt_QString2HBufC(OldPluginID);
       
   369 	changePluginListParams->pluginIDEnabled = Flag;
       
   370 
       
   371 	//create buffer to serialize data
       
   372 	CBufFlat* buf = CBufFlat::NewL(KMaxBufSize);
       
   373 	CleanupStack::PushL(buf);
       
   374 	RBufWriteStream stream(*buf);
       
   375 	CleanupClosePushL(stream);
       
   376 
       
   377 	changePluginListParams->ExternalizeL(stream);
       
   378 	stream.CommitL();
       
   379 
       
   380 	TPtr8 bufPtr = buf->Ptr(0);
       
   381 
       
   382 	TIpcArgs args;
       
   383 	args.Set(0, &bufPtr);
       
   384 
       
   385 	iSession.RequestService(EUpdatePluginIDList, args);
       
   386 
       
   387 	CleanupStack::PopAndDestroy(&stream);
       
   388 	CleanupStack::PopAndDestroy(buf);
       
   389 	CleanupStack::PopAndDestroy(changePluginListParams);
       
   390 
       
   391 	}
       
   392 
       
   393 SMFCredMgrErrorCode CSmfCredMgrClientSymbian::signMessageL(QString Message,
       
   394 		QString Key, QString& Signature, SmfSignatureMethod AlgorithmUsed)
       
   395 	{
       
   396 	SMFCredMgrErrorCode signError = SmfErrNone;
       
   397 	TPtr msgPtr((qt_QString2HBufC(Message))->Des());
       
   398 	TPtr keyPtr((qt_QString2HBufC(Key))->Des());
       
   399 
       
   400 	CSmfSignParameters* signMsgParams = CSmfSignParameters::NewL(
       
   401 			msgPtr.Collapse(), keyPtr.Collapse());
       
   402 	CleanupStack::PushL(signMsgParams);
       
   403 
       
   404 	CBufFlat* buf = CBufFlat::NewL(KMinBufSize);
       
   405 	CleanupStack::PushL(buf);
       
   406 	RBufWriteStream stream(*buf);
       
   407 	CleanupClosePushL(stream);
       
   408 
       
   409 	signMsgParams->ExternalizeL(stream);
       
   410 	stream.CommitL();
       
   411 
       
   412 	TPtr8 bufPtr = buf->Ptr(0);
       
   413 
       
   414 	TIpcArgs args;
       
   415 	args.Set(0, &bufPtr);
       
   416 
       
   417 	HBufC8* msgBuf = HBufC8::NewL(KMaxSignedMsgLength);
       
   418 	TPtr8 msgBufPtr(msgBuf->Des());
       
   419 	CleanupStack::PushL(msgBuf);
       
   420 	args.Set(1, &msgBufPtr);
       
   421 
       
   422 	switch (AlgorithmUsed)
       
   423 		{
       
   424 		case ESMFRSAProtocol:
       
   425 			{
       
   426 			iSession.RequestService(ESmfRSASignMessage, args);
       
   427 			}
       
   428 			break;
       
   429 		case ESMFHMACProtocol:
       
   430 			{
       
   431 			iSession.RequestService(ESmfHMACSHA1SignMessage, args);
       
   432 			}
       
   433 			break;
       
   434 		default:
       
   435 			{
       
   436 			RDebug::Printf("Unsupported Algo:");
       
   437 			return SmfErrBadParameter;
       
   438 			}
       
   439 		}
       
   440 
       
   441 	TBuf<KMaxSignedMsgLength> signedMsg;
       
   442 	signedMsg.Copy(msgBufPtr);
       
   443 
       
   444 	Signature = qt_TDesC2QString(signedMsg);
       
   445 
       
   446 	CleanupStack::Pop(msgBuf);
       
   447 	CleanupStack::PopAndDestroy(&stream);
       
   448 	CleanupStack::PopAndDestroy(buf);
       
   449 	CleanupStack::PopAndDestroy(signMsgParams);
       
   450 
       
   451 	return signError;
       
   452 	}
       
   453 
       
   454 QString CSmfCredMgrClientSymbian::storeRSAKeysL(const QString KeyLabel,
       
   455 		const QString keydata, const QDateTime Validity)
       
   456 	{
       
   457 	RDebug::Printf("Sending store RSA key message to server");
       
   458 
       
   459 	TPtrC labelPtr(qt_QString2TPtrC(KeyLabel));
       
   460 	TPtr dataPtr((qt_QString2HBufC(keydata)->Des()));
       
   461 
       
   462 	QDateTime CurrentTime = QDateTime::currentDateTime();
       
   463 	TTimeIntervalSeconds duration(CurrentTime.secsTo(Validity));
       
   464 
       
   465 	TTime startDate;
       
   466 	startDate.UniversalTime();
       
   467 
       
   468 	TTime endDate(startDate);
       
   469 	endDate += duration;
       
   470 
       
   471 	CSmfRsaKeyParameters* storeRSAKeysparams = CSmfRsaKeyParameters::NewL(
       
   472 			labelPtr, startDate, endDate, (dataPtr.Collapse()));
       
   473 	CleanupStack::PushL(storeRSAKeysparams);
       
   474 
       
   475 	CBufFlat* buf = CBufFlat::NewL(KMinBufSize);
       
   476 	CleanupStack::PushL(buf);
       
   477 	RBufWriteStream stream(*buf);
       
   478 	CleanupClosePushL(stream);
       
   479 
       
   480 	storeRSAKeysparams->ExternalizeL(stream);
       
   481 	stream.CommitL();
       
   482 
       
   483 	TPtr8 bufPtr = buf->Ptr(0);
       
   484 
       
   485 	TIpcArgs args;
       
   486 	args.Set(0, &bufPtr);
       
   487 
       
   488 	CleanupStack::PopAndDestroy(&stream);
       
   489 
       
   490 	HBufC8* retBuf = HBufC8::NewLC(KSHA1HashLengthBytes);
       
   491 	TPtr8 retBufPtr(retBuf->Des());
       
   492 	args.Set(1, &retBufPtr);
       
   493 
       
   494 	iSession.RequestService(ESmfStoreRSAKey, args);
       
   495 
       
   496 	RDebug::Printf("SMF: Message completed");
       
   497 
       
   498 	TBuf<KSHA1HashLengthBytes> key;
       
   499 	key.Copy(retBufPtr);
       
   500 
       
   501 	QString RetString(qt_TDesC2QString(key));
       
   502 
       
   503 	CleanupStack::Pop(retBuf);
       
   504 	CleanupStack::PopAndDestroy(buf);
       
   505 	CleanupStack::PopAndDestroy(storeRSAKeysparams);
       
   506 
       
   507 	return (RetString);
       
   508 
       
   509 	}
       
   510 
       
   511 void CSmfCredMgrClientSymbian::deleteRSAKey(QString KeyLabel)
       
   512 	{
       
   513 	TPtr bufPtr((qt_QString2HBufC(KeyLabel))->Des());
       
   514 
       
   515 	TIpcArgs args;
       
   516 	args.Set(0, &bufPtr);
       
   517 
       
   518 	iSession.RequestService(ESmfDeleteKeys, args);
       
   519 	}