1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tcertdump/tcertdump.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,218 @@
1.4 +/*
1.5 +* Copyright (c) 2006-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 <e32base.h>
1.23 +#include <e32cons.h>
1.24 +#include <e32std.h>
1.25 +#include <f32file.h>
1.26 +#include <x509cert.h>
1.27 +#include <utf.h>
1.28 +#include <bacline.h>
1.29 +#include <x509certext.h>
1.30 +
1.31 +#include "displaytype.h"
1.32 +
1.33 +_LIT(KAppName, "tcertdump");
1.34 +
1.35 +
1.36 +void WriteAsUtf8L(const TDesC& aUtf16, RFile& aOutputFile)
1.37 + {
1.38 + HBufC8* utf8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(aUtf16);
1.39 + CleanupStack::PushL(utf8);
1.40 + User::LeaveIfError(aOutputFile.Write(*utf8));
1.41 + CleanupStack::PopAndDestroy(utf8);
1.42 + }
1.43 +
1.44 +
1.45 +void PrintDistinguishedNameElementsL(const CX500DistinguishedName& dName, RFile& aOutputFile)
1.46 + {
1.47 + TInt i;
1.48 + for (i=0;i<dName.Count();i++)
1.49 + {
1.50 + User::LeaveIfError(aOutputFile.Write(_L8("\t\t")));
1.51 + //WriteAsUtf8L(dName.Element(i).Type(), aOutputFile);
1.52 +
1.53 +
1.54 + WriteAsUtf8L(GetType(dName.Element(i).Type()), aOutputFile);
1.55 +
1.56 +
1.57 + User::LeaveIfError(aOutputFile.Write(_L8(": ")));
1.58 +
1.59 + HBufC* value = NULL;
1.60 + value=dName.Element(i).ValueL();
1.61 + CleanupStack::PushL(value);
1.62 +
1.63 +
1.64 + if (dName.Element(i).Type().Compare(_L("2.5.4.3")))
1.65 + {
1.66 + WriteAsUtf8L(*value, aOutputFile);
1.67 + }
1.68 + else
1.69 + {
1.70 + HBufC8* temp=NULL;
1.71 + temp=HBufC8::NewL(value->Length());
1.72 + CleanupStack::PushL(temp);
1.73 +
1.74 + TPtr8 ptr = temp->Des();
1.75 + TPtr valuePtr = value->Des();
1.76 + for (TInt j = 0;j < value->Length();++j)
1.77 + {
1.78 + TChar c = valuePtr[j];
1.79 + ptr.Append(c % 0x10000);
1.80 + }
1.81 +
1.82 + User::LeaveIfError(aOutputFile.Write(*temp));
1.83 + CleanupStack::PopAndDestroy(temp);
1.84 + }
1.85 +
1.86 + User::LeaveIfError(aOutputFile.Write(_L8("\r\n")));
1.87 + CleanupStack::PopAndDestroy(value);
1.88 + }
1.89 + }
1.90 +
1.91 +void DumpCertInfoL(const CX509Certificate& aCert, RFile& aOutputFile)
1.92 + {
1.93 + // common/display names
1.94 + aOutputFile.Write(_L8("Issuer Display Name: "));
1.95 + HBufC* issuer = aCert.IssuerL();
1.96 + CleanupStack::PushL(issuer);
1.97 + WriteAsUtf8L(*issuer, aOutputFile);
1.98 + User::LeaveIfError(aOutputFile.Write(_L8("\r\n")));
1.99 + CleanupStack::PopAndDestroy(issuer);
1.100 +
1.101 + aOutputFile.Write(_L8("Subject Display Name: "));
1.102 + HBufC* subject = aCert.SubjectL();
1.103 + CleanupStack::PushL(subject);
1.104 + WriteAsUtf8L(*subject, aOutputFile);
1.105 + User::LeaveIfError(aOutputFile.Write(_L8("\r\n")));
1.106 + CleanupStack::PopAndDestroy(subject);
1.107 +
1.108 +
1.109 + // distinguished names
1.110 +
1.111 + aOutputFile.Write(_L8("Issuer Distinguished Name:\r\n"));
1.112 + PrintDistinguishedNameElementsL(aCert.IssuerName(),aOutputFile);
1.113 +
1.114 + aOutputFile.Write(_L8("Subject Distinguished Name:\r\n"));
1.115 + PrintDistinguishedNameElementsL(aCert.SubjectName(),aOutputFile);
1.116 +
1.117 + // alt names
1.118 + const CX509CertExtension *subjectAltExt=aCert.Extension(KSubjectAltName);
1.119 + if (subjectAltExt!=NULL)
1.120 + {
1.121 + aOutputFile.Write(_L8("Subject Alt Names:\r\n"));
1.122 + CX509AltNameExt* subjectAltNameExt = CX509AltNameExt::NewLC(subjectAltExt->Data());
1.123 + const CArrayPtrFlat<CX509GeneralName>&names = subjectAltNameExt->AltName();
1.124 + for (TInt i = 0; i < names.Count(); i++)
1.125 + {
1.126 + CX509GeneralName* name = names.At(i);
1.127 + if ( name->Tag() == EX509DirectoryName )
1.128 + {
1.129 + aOutputFile.Write(_L8("\tDirectory Name:\r\n"));
1.130 + CX500DistinguishedName* dirName = CX500DistinguishedName::NewLC(name->Data());
1.131 + PrintDistinguishedNameElementsL(*dirName,aOutputFile);
1.132 + CleanupStack::PopAndDestroy(dirName);
1.133 + }
1.134 + }
1.135 + CleanupStack::PopAndDestroy(subjectAltNameExt);
1.136 + }
1.137 +
1.138 + const CX509CertExtension *issuerAltExt=aCert.Extension(KIssuerAltName);
1.139 + if (issuerAltExt!=NULL)
1.140 + {
1.141 + aOutputFile.Write(_L8("Issuer Alt Names:\r\n"));
1.142 + CX509AltNameExt* issuerAltNameExt = CX509AltNameExt::NewLC(issuerAltExt->Data());
1.143 + const CArrayPtrFlat<CX509GeneralName>&names = issuerAltNameExt->AltName();
1.144 + for (TInt i = 0; i < names.Count(); i++)
1.145 + {
1.146 + CX509GeneralName* name = names.At(i);
1.147 + if ( name->Tag() == EX509DirectoryName )
1.148 + {
1.149 + aOutputFile.Write(_L8("\tDirectory Name:\r\n"));
1.150 + CX500DistinguishedName* dirName = CX500DistinguishedName::NewLC(name->Data());
1.151 + PrintDistinguishedNameElementsL(*dirName,aOutputFile);
1.152 + CleanupStack::PopAndDestroy(dirName);
1.153 + }
1.154 + }
1.155 + CleanupStack::PopAndDestroy(issuerAltNameExt);
1.156 + }
1.157 + }
1.158 +
1.159 +void DumpCertL()
1.160 + {
1.161 + CCommandLineArguments* args = CCommandLineArguments::NewLC();
1.162 + TInt count = args->Count();
1.163 + if (args->Count() == 3)
1.164 + {
1.165 + const TPtrC certFileName = args->Arg(1);
1.166 + const TPtrC outputFileName = args->Arg(2);
1.167 +
1.168 + RFs fs;
1.169 + User::LeaveIfError(fs.Connect());
1.170 + CleanupClosePushL(fs);
1.171 +
1.172 + RFile certFile;
1.173 + User::LeaveIfError(certFile.Open(fs, certFileName, EFileShareAny | EFileRead | EFileStream));
1.174 + CleanupClosePushL(certFile);
1.175 +
1.176 + TInt fileSize;
1.177 + User::LeaveIfError(certFile.Size(fileSize));
1.178 +
1.179 + HBufC8* certBuffer = HBufC8::NewLC(fileSize);
1.180 + TPtr8 ptr = certBuffer->Des();
1.181 + User::LeaveIfError(certFile.Read(ptr, fileSize));
1.182 +
1.183 + CX509Certificate* cert = CX509Certificate::NewLC(*certBuffer);
1.184 +
1.185 + RFile outputFile;
1.186 + User::LeaveIfError(outputFile.Replace(fs, outputFileName, EFileWrite | EFileShareExclusive));
1.187 +
1.188 + DumpCertInfoL(*cert, outputFile);
1.189 +
1.190 + CleanupStack::PopAndDestroy(4, &fs); // outputFile, cert, certBuffer, certFile, fs
1.191 + }
1.192 + CleanupStack::PopAndDestroy(args);
1.193 + }
1.194 +
1.195 +void DoMainL(void)
1.196 + {
1.197 + CConsoleBase* console = Console::NewL(KAppName, TSize(KDefaultConsWidth, KDefaultConsHeight));
1.198 + CleanupStack::PushL(console);
1.199 + __UHEAP_MARK;
1.200 + TRAPD(err, DumpCertL());
1.201 + if (err)
1.202 + {
1.203 + console->Printf(_L("Error %d\n"), err);
1.204 + console->Getch();
1.205 + }
1.206 + __UHEAP_MARKEND;
1.207 + CleanupStack::PopAndDestroy(console);
1.208 + }
1.209 +
1.210 +GLDEF_C TInt E32Main() // main function called by E32
1.211 + {
1.212 + __UHEAP_MARK;
1.213 + CTrapCleanup* cleanup=CTrapCleanup::New();
1.214 +
1.215 + TRAP_IGNORE(DoMainL());
1.216 +
1.217 + delete cleanup;
1.218 + __UHEAP_MARKEND;
1.219 + return 0;
1.220 + }
1.221 +