os/security/cryptoservices/filebasedcertificateandkeystores/test/keytool/keytool_utils.cpp
First public contribution.
2 * Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
22 #include "keytool_utils.h"
23 #include "securityerr.h"
24 #include "compiledsecuritypolicy.h"
30 _LIT(KExtractable, "Extractable");
31 _LIT(KSensitive, "Sensitive");
32 _LIT(KAlwaysSensitive, "AlwaysSensitive");
33 _LIT(KNeverExtractable, "NeverExtractable");
34 _LIT(KLocal, "Local");
35 _LIT(KEllipsis, "...");
38 _LIT(allusage, "AllUsage");
39 _LIT(nousage, "NoUsage");
40 _LIT(encrypt, "Encrypt");
41 _LIT(decrypt, "Decrypt");
43 _LIT(signrecover, "SignRecover");
45 _LIT(unwrap, "Unwrap");
46 _LIT(verify, "Verify");
47 _LIT(verifyrecover, "VerifyRecover");
48 _LIT(derive, "Derive");
49 _LIT(nonrepudiation, "NonRepudiation");
51 const TInt KCursorOffset = 2;
52 static const TInt KMaxLabel = 32;
53 CConsoleBase* KeyToolUtils::iConsole = NULL;
54 RFile* KeyToolUtils::iFile = NULL;
56 /*static*/ void KeyToolUtils::WriteErrorL(TInt aError)
59 if ((aError <= -12000) && (aError >= -12100))
61 PrintInfoL(_L("You have tsecdlg.dll in z:\\system\\notifiers."));
62 PrintInfoL(_L("This notifier is for *testing only* please remove it and try again!"));
69 PrintInfoL(_L("KErrNone"));
72 case KErrNotFound: // -1
73 PrintInfoL(_L("KErrNotFound"));
76 case KErrNotSupported: // -5
77 PrintInfoL(_L("KErrNotSupported"));
80 case KErrInUse: // -14
81 PrintInfoL(_L("KErrInUse"));
84 case KErrNotReady: // -18
85 PrintInfoL(_L("KErrNotReady"));
88 case KErrKeyAlgorithm: //-11006
89 PrintInfoL(_L("KErrKeyAlgorithm"));
92 case KErrKeyUsage: //-11007
93 PrintInfoL(_L("KErrKeyUsage"));
96 case KErrKeyValidity: //-11008
97 PrintInfoL(_L("KErrKeyValidity"));
100 case KErrKeySize: //-11009
101 PrintInfoL(_L("KErrKeySize"));
104 case KErrKeyAccess: //-11010
105 PrintInfoL(_L("KErrKeyAccess"));
108 case KRequestPending:
109 PrintInfoL(_L("KRequestPending"));
112 case KErrAlreadyExists:
113 PrintInfoL(_L("KErrAlreadyExists"));
117 PrintInfoL(_L("KErrArgument"));
120 case KErrBadName: // -28
121 PrintInfoL(_L("KErrBadName"));
124 case KErrCorrupt: // -20
125 PrintInfoL(_L("KErrCorrupt"));
127 case KErrPrivateKeyNotFound: //-11011
128 PrintInfoL(_L("KErrPrivateKeyNotFound"));
134 msg.Format(_L("Unknown (Code %d)"), aError);
141 /*static*/ void KeyToolUtils::WriteOctetStringL(const TDesC8& aString)
145 counter = aString.Length();
146 HBufC* buf = HBufC::NewLC(counter*6);
147 TPtr pBuf = buf->Des();
148 for (index = 0; index < counter; index++)
150 pBuf.AppendFormat(_L("%01x"),aString[index]>>4);
151 pBuf.AppendFormat(_L("%01x"),aString[index]&0xf);
152 pBuf.Append(_L(" "));
155 CleanupStack::PopAndDestroy();//buf
159 /*static*/ void KeyToolUtils::PrintUsageL(TUint aUsage)
161 PrintInfoL(_L("\t\tUsage : "));
162 if (aUsage & 0x0000ffff) // In this range we are talking PKCS15
164 PrintInfoL(_L("PKCS15 "));
166 if (aUsage & 0xffff0000) // In this range we are talking X509
170 if (aUsage == (TUint)EPKCS15UsageAll)
172 PrintInfoL(allusage);
175 if (aUsage == EPKCS15UsageNone)
180 if (aUsage & EPKCS15UsageEncrypt)
184 if (aUsage & EPKCS15UsageDecrypt)
188 if (aUsage & EPKCS15UsageSign)
192 if (aUsage & EPKCS15UsageSignRecover)
194 PrintInfoL(signrecover);
196 if (aUsage & EPKCS15UsageWrap)
200 if (aUsage & EPKCS15UsageUnwrap)
204 if (aUsage & EPKCS15UsageVerify)
208 if (aUsage & EPKCS15UsageVerifyRecover)
210 PrintInfoL(verifyrecover);
212 if (aUsage & EPKCS15UsageDerive)
216 if (aUsage & EPKCS15UsageNonRepudiation)
218 PrintInfoL(nonrepudiation);
220 if (aUsage & (TUint)EX509UsageDigitalSignature)
222 PrintInfoL(_L("DigitalSignature "));
224 if (aUsage & EX509UsageNonRepudiation)
226 PrintInfoL(_L("NonRepudiation "));
228 if (aUsage & EX509UsageKeyEncipherment)
230 PrintInfoL(_L("KeyEncipherment "));
232 if (aUsage & EX509UsageDataEncipherment )
234 PrintInfoL(_L("DataEncipherment "));
236 if (aUsage & EX509UsageKeyAgreement )
238 PrintInfoL(_L("KeyAgreement "));
240 if (aUsage & EX509UsageKeyCertSign )
242 PrintInfoL(_L("KeyCertSign "));
244 if (aUsage & EX509UsageCRLSign )
246 PrintInfoL(_L("CRLSign "));
248 if (aUsage & EX509UsageEncipherOnly )
250 PrintInfoL(_L("EncipherOnly "));
252 if (aUsage & EX509UsageDecipherOnly)
254 PrintInfoL(_L("DecipherOnly "));
258 _LIT(KKeyUsage, "\tCode: 0x%x ");
259 buf.Format(KKeyUsage, aUsage);
265 /*static*/ HBufC* KeyToolUtils::KeyAccessDesLC(TInt aAccess)
269 if (aAccess & CCTKeyInfo::ESensitive)
271 access.Append(KSensitive);
272 access.Append(space);
275 if (aAccess & CCTKeyInfo::EExtractable)
277 access.Append(KExtractable);
278 access.Append(space);
281 if (aAccess & CCTKeyInfo::EAlwaysSensitive)
283 access.Append(KAlwaysSensitive);
284 access.Append(space);
287 if (aAccess & CCTKeyInfo::ENeverExtractable)
289 access.Append(KNeverExtractable);
290 access.Append(space);
293 if (aAccess & CCTKeyInfo::ELocal)
295 access.Append(KLocal);
296 access.Append(space);
298 return access.AllocLC();
302 /*static*/ HBufC* KeyToolUtils::AlgorithmDesLC(CCTKeyInfo::EKeyAlgorithm aAlgorithm)
308 case CCTKeyInfo::EInvalidAlgorithm:
310 alg = _L("Invalid Algorithm");
313 case CCTKeyInfo::ERSA:
318 case CCTKeyInfo::EDSA:
323 case CCTKeyInfo::EDH :
325 alg = _L("Diffie Hellman");
330 alg = _L("Unsupported algorithm!");
333 return alg.AllocLC();
338 /*static*/ void KeyToolUtils::PrintKeyInfoL(const CCTKeyInfo& aKey, TBool aIsDetailed,TBool aPageWise)
340 HBufC* alg = KeyToolUtils::AlgorithmDesLC(aKey.Algorithm());
343 MCTToken& token = aKey.Token();
345 const TDesC& tmp = token.Label();
346 msg.Format(_L("Store Label: %S"), &tmp);
348 const TDesC& tmp1 = token.Information(MCTToken::EVersion);
349 const TDesC& tmp2 = token.Information(MCTToken::ESerialNo);
350 const TDesC& tmp3 = token.Information(MCTToken::EManufacturer);
352 msg1.Format(_L("Version: %S \t Serial Number: %S \t Manufacturer: %S Store Type: Read only"), &tmp1, &tmp2, &tmp3);
356 PrintInfoL(msg, aPageWise);
357 PrintInfoL(newline, aPageWise);
358 PrintInfoL(aKey.Label().Left(KMaxLabel));
359 if (aKey.Label().Length() > KMaxLabel)
361 PrintInfoL(KEllipsis);
363 PrintInfoL(_L(" is a "));
364 _LIT(KKeySizeShort, "%d");
365 buf.Format(KKeySizeShort, aKey.Size());
367 PrintInfoL(_L(" bits "));
369 PrintInfoL(_L(" key "));
370 CleanupStack::PopAndDestroy(alg);
371 PrintInfoL(newline, aPageWise);
375 PrintInfoL(msg, aPageWise);
376 PrintInfoL(newline, aPageWise);
377 PrintInfoL(msg1, aPageWise);
378 PrintInfoL(newline, aPageWise);
380 PrintInfoL(_L("\t\tAlgorithm: "));
382 CleanupStack::PopAndDestroy(alg);
384 _LIT(KKeySize, "\tSize: %d ");
385 buf.Format(KKeySize, aKey.Size());
387 PrintInfoL(_L(" bits"));
388 PrintInfoL(newline, aPageWise);
390 KeyToolUtils::PrintUsageL(aKey.Usage());
392 _LIT(KKeyOwner, "\t\tOwner : 0x%x ");
393 _LIT(KKeyUser, "\t\tUser : 0x%x ");
394 TCompiledSecurityPolicy managementPolicy = aKey.ManagementPolicy();
395 TCompiledSecurityPolicy usePolicy = aKey.UsePolicy();
397 TUint32 mSecureid = managementPolicy.SecureId();
398 if (mSecureid == 0xffffffff)
400 TCapability cab = managementPolicy.Capability(0);
401 _LIT(KKeyOwnerCab, "\t\tOwner : %S ");
402 // currently only possible for keyowner other than secure id.
403 if (cab == ECapabilityWriteDeviceData)
405 _LIT(KCab, "WriteDeviceData");
406 buf.Format(KKeyOwnerCab, &KCab);
411 _LIT(KCab, "Unknown");
412 buf.Format(KKeyOwnerCab, &KCab);
418 buf.Format(KKeyOwner, mSecureid);
421 PrintInfoL(newline, aPageWise);
423 TUint32 uSecureid = usePolicy.SecureId();
424 if (uSecureid == 0xffffffff)
426 TSecurityPolicy::TType utype = usePolicy.Type();
427 _LIT(KKeyUserType, "\t\tUser : %S ");
428 // currently only possible for keyuser other than secure id
429 if (utype == TSecurityPolicy::ETypePass)
432 buf.Format(KKeyUserType, &KType);
437 _LIT(KType, "Unknown");
438 buf.Format(KKeyUserType, &KType);
444 buf.Format(KKeyUser, uSecureid);
446 if (uSecureid == KUidSecurityKeytool)
448 _LIT(KUser, " (KeyTool)");
452 PrintInfoL(newline, aPageWise);
455 _LIT(KKeySens, "\t\tAccess flags: ");
456 PrintInfoL(KKeySens);
458 HBufC* access = KeyToolUtils::KeyAccessDesLC(aKey.AccessType());
460 CleanupStack::PopAndDestroy(access);
462 PrintInfoL(newline, aPageWise);
464 _LIT(KKeyID, "\t\tID: ");
466 KeyToolUtils::WriteOctetStringL(aKey.ID());
467 PrintInfoL(newline, aPageWise);
469 _LIT(KKeyLabel, "\t\tLabel: ");
470 PrintInfoL(KKeyLabel);
471 PrintInfoL(aKey.Label().Left(KMaxLabel));
472 if (aKey.Label().Length() > KMaxLabel)
474 PrintInfoL(KEllipsis);
476 PrintInfoL(newline, aPageWise);
478 _LIT(KKeyNative, "\t\tNative: Yes ");
479 _LIT(KKeyNotNative, "\t\tNative: No ");
482 PrintInfoL(KKeyNative);
486 PrintInfoL(KKeyNotNative);
488 PrintInfoL(newline, aPageWise);
490 _LIT(KTimeFormat, "%1/%2/%3 %H:%T:%S");
492 _LIT(KKeyStartDate, "\t\tStart date: ");
493 PrintInfoL(KKeyStartDate);
494 if (aKey.StartDate().Int64() == 0)
496 PrintInfoL(_L("not set"));
500 aKey.StartDate().FormatL(buf, KTimeFormat);
504 _LIT(KKeyEndDate, "\tEnd date: ");
505 PrintInfoL(KKeyEndDate);
507 if (aKey.EndDate().Int64() == 0)
509 PrintInfoL(_L("not set"));
513 aKey.EndDate().FormatL(buf, KTimeFormat);
516 PrintInfoL(newline, aPageWise);
519 /*static*/ TKeyUsagePKCS15 KeyToolUtils::ParseKeyUsage(TPtrC aUsage)
521 TKeyUsagePKCS15 usage = EPKCS15UsageNone;
523 if (aUsage.Compare(allusage)==0)
525 return EPKCS15UsageAll;
527 if (aUsage.Compare(nousage)==0)
529 return EPKCS15UsageNone;
531 if (aUsage.Compare(encrypt)==0)
533 return EPKCS15UsageEncrypt;
535 if (aUsage.Compare(decrypt)==0)
537 return EPKCS15UsageDecrypt;
539 if (aUsage.Compare(sign)==0)
541 return EPKCS15UsageSign;
543 if (aUsage.Compare(signrecover)==0)
545 return EPKCS15UsageSignRecover;
547 if (aUsage.Compare(wrap)==0)
549 return EPKCS15UsageWrap;
551 if (aUsage.Compare(unwrap)==0)
553 return EPKCS15UsageUnwrap;
555 if (aUsage.Compare(verify)==0)
557 return EPKCS15UsageVerify;
559 if (aUsage.Compare(verifyrecover)==0)
561 return EPKCS15UsageVerifyRecover;
563 if (aUsage.Compare(derive)==0)
565 return EPKCS15UsageDerive;
567 if (aUsage.Compare(nonrepudiation)==0)
569 return EPKCS15UsageNonRepudiation;
574 /*static*/ CCTKeyInfo::EKeyAccess KeyToolUtils::ParseKeyAccess(TPtrC aAccess)
576 CKeyInfoBase::EKeyAccess access = CKeyInfoBase::EInvalidAccess;
578 if (aAccess.Compare(KSensitive)==0)
580 access = CCTKeyInfo::ESensitive;
583 if (aAccess.Compare(KExtractable)==0)
585 access = CCTKeyInfo::EExtractable;
588 if (aAccess.Compare(KAlwaysSensitive)==0)
590 access = CCTKeyInfo::EAlwaysSensitive;
593 if (aAccess.Compare(KNeverExtractable)==0)
595 access = CCTKeyInfo::ENeverExtractable;
598 if (aAccess.Compare(KLocal)==0)
600 access = CCTKeyInfo::ELocal;
607 /*static*/ RArray<CCTKeyInfo*> KeyToolUtils::MatchKey(RMPointerArray<CCTKeyInfo>& aKeyList, TDesC& aLabel)
609 RArray<CCTKeyInfo*> ret;
611 for (TInt j = 0; j < aKeyList.Count(); j++)
613 if (aKeyList[j]->Label().Match(aLabel) != KErrNotFound)
615 ret.Append(aKeyList[j]);
621 /*static*/ CCTKeyInfo* KeyToolUtils::findKey(RMPointerArray<CCTKeyInfo>& aKeyList, TDesC& aLabel)
624 // Select the key with the label we want!
625 for (TInt j = 0; j < aKeyList.Count(); j++)
627 if (aKeyList[j]->Label() == aLabel)
638 return aKeyList[keyIndex];
641 /*static*/ const TDesC& KeyToolUtils::Uid2Des(TUid aUid)
643 if (aUid.iUid == swinstalluid)
647 else if (aUid.iUid == swinstallocspuid)
649 return KSWInstallOCSP;
651 else if (aUid.iUid == midletinstalluid)
653 return KMidletInstall;
655 else if (aUid.iUid == tlsuid)
659 else if (aUid.iUid == tocspuid)
668 /*static*/ void KeyToolUtils::FilterCertsL(RMPointerArray<CCTCertInfo>& aCertList, TDesC& aLabel)
671 while (j < aCertList.Count())
673 if ( aCertList[j]->Label().Match(aLabel) == KErrNotFound)
675 CCTCertInfo* tmp = aCertList[j];
686 PrintInfoL(_L("Cannot find the specified Certificate"));
691 /*static*/ void KeyToolUtils::FilterCertsL(RMPointerArray<CCTCertInfo>& aCertList, TCertificateOwnerType& aOwnerType)
694 while (j < aCertList.Count())
696 if ( aCertList[j]->CertificateOwnerType() != aOwnerType)
698 CCTCertInfo* tmp = aCertList[j];
709 PrintInfoL(_L("No Certificate Exist"));
714 /*static*/ void KeyToolUtils::PrintCertInfoL(CCTCertInfo& aCertInfo, CCertificate& aCertificate,RArray<TUid> aApps, TBool aTrusted, TBool aIsDetailed,TBool aPageWise)
716 TFileName certFormat,ownerType;
718 switch (aCertInfo.CertificateFormat())
720 case EX509Certificate :
722 certFormat.Copy(_L("X509"));
725 case EWTLSCertificate :
727 certFormat.Copy(_L("WTLS"));
730 case EX968Certificate :
732 certFormat.Copy(_L("X968"));
737 certFormat.Copy(_L("Unknown certificate format!"));
741 switch (aCertInfo.CertificateOwnerType())
743 case EPeerCertificate :
745 ownerType.Copy(_L("Peer"));
748 case EUserCertificate :
750 ownerType.Copy(_L("User"));
753 case ECACertificate :
755 ownerType.Copy(_L("Root (CA)"));
760 ownerType.Copy(_L("Unknown"));
764 MCTToken& token = aCertInfo.Token();
766 const TDesC& tmp = token.Label();
767 msg.Format(_L("Store Label: %S"), &tmp);
769 const TDesC& tmp1 = token.Information(MCTToken::EVersion);
770 const TDesC& tmp2 = token.Information(MCTToken::ESerialNo);
771 const TDesC& tmp3 = token.Information(MCTToken::EManufacturer);
773 msg1.Format(_L("Version: %S \t Serial Number: %S \t Manufacturer: %S "), &tmp1, &tmp2, &tmp3);
777 PrintInfoL(msg, aPageWise);
778 PrintInfoL(newline, aPageWise);
779 PrintInfoL(aCertInfo.Label());
780 PrintInfoL(_L(" is a "));
781 PrintInfoL(certFormat);
782 PrintInfoL(_L(" format which is a "));
783 PrintInfoL(ownerType);
784 PrintInfoL(_L(" type "));
785 PrintInfoL(newline, aPageWise);
789 PrintInfoL(msg, aPageWise);
790 PrintInfoL(newline, aPageWise);
791 PrintInfoL(msg1, aPageWise);
792 PrintInfoL(newline, aPageWise);
793 PrintInfoL(_L("\tLabel: "));
794 PrintInfoL(aCertInfo.Label());
796 PrintInfoL(_L("\t\t"));
798 PrintInfoL(_L("Format: "));
799 PrintInfoL(certFormat);
800 PrintInfoL(_L("\t"));
801 PrintInfoL(_L("Owner Type: "));
802 PrintInfoL(ownerType);
803 PrintInfoL(newline, aPageWise);
804 PrintInfoL(_L("\tIssuer Name: "));
805 HBufC* issuer = aCertificate.IssuerL();
808 HBufC* subj = aCertificate.SubjectL();
809 PrintInfoL(_L("\t\tSubject Name: "));
813 PrintInfoL(newline, aPageWise);
814 CValidityPeriod vp = aCertificate.ValidityPeriod();
816 const TTime& start = vp.Start();
817 const TTime& finish = vp.Finish();
818 TBuf<30> dateString1;
819 start.FormatL(dateString1,(_L("%H%:1%T:%S %*E%*D %X%*N%Y %1 %2 %3")));
821 PrintInfoL(_L("\tValid From "));
822 PrintInfoL(dateString1, aPageWise);
824 TBuf<30> dateString2;
825 finish.FormatL(dateString2,(_L("%H%:1%T:%S %*E%*D %X%*N%Y %1 %2 %3")));
827 PrintInfoL(_L("\tValid Until "));
828 PrintInfoL(dateString2, aPageWise);
829 PrintInfoL(newline, aPageWise);
830 PrintInfoL(_L("\tTrusted for Applications: "));
831 for (TInt k = 0; k < aApps.Count(); k++)
833 PrintInfoL(Uid2Des(aApps[k]), aPageWise);
836 PrintInfoL(newline, aPageWise);
837 PrintInfoL(_L("\tMarked as trusted: "));
838 PrintInfoL( aTrusted ? _L("Yes"): _L("No"));
839 PrintInfoL(newline, aPageWise);
840 PrintInfoL(_L("\tMarked as Deletable : "));
841 PrintInfoL( aCertInfo.IsDeletable() ? _L("Yes"): _L("No"));
843 PrintInfoL(newline, aPageWise);
846 void KeyToolUtils::SetConsole(CConsoleBase *aConsole )
851 void KeyToolUtils::SetFile(RFile* aFile)
856 TBool KeyToolUtils::DoesFileExistsL(const RFs& aFs, const TDesC& aFileName)
858 TBool exists = EFalse;
859 TEntry* e = new(ELeave) TEntry();
860 TInt err = aFs.Entry(aFileName, *e);
861 if (err == KErrNone && ! e->IsDir())
869 /*static*/ void KeyToolUtils::PrintInfoL(const TDesC& aValue, TBool aPageWise)
873 TSize screenSize = iConsole->ScreenSize();
874 if(aPageWise && (iConsole->WhereY() >= screenSize.iHeight - KCursorOffset))
876 iConsole->Printf(_L("\nPress a key to continue..."));
878 iConsole->ClearScreen();
881 iConsole->Printf(aValue);
888 iFile->Seek(ESeekEnd, seekPos);
889 User::LeaveIfError(iFile->Write(buf8));