|
1 /* |
|
2 * Copyright (c) 2006-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 <e32base.h> |
|
20 #include <e32cons.h> |
|
21 #include <e32std.h> |
|
22 #include <f32file.h> |
|
23 #include <x509cert.h> |
|
24 #include <utf.h> |
|
25 #include <bacline.h> |
|
26 #include <x509certext.h> |
|
27 |
|
28 #include "displaytype.h" |
|
29 |
|
30 _LIT(KAppName, "tcertdump"); |
|
31 |
|
32 |
|
33 void WriteAsUtf8L(const TDesC& aUtf16, RFile& aOutputFile) |
|
34 { |
|
35 HBufC8* utf8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(aUtf16); |
|
36 CleanupStack::PushL(utf8); |
|
37 User::LeaveIfError(aOutputFile.Write(*utf8)); |
|
38 CleanupStack::PopAndDestroy(utf8); |
|
39 } |
|
40 |
|
41 |
|
42 void PrintDistinguishedNameElementsL(const CX500DistinguishedName& dName, RFile& aOutputFile) |
|
43 { |
|
44 TInt i; |
|
45 for (i=0;i<dName.Count();i++) |
|
46 { |
|
47 User::LeaveIfError(aOutputFile.Write(_L8("\t\t"))); |
|
48 //WriteAsUtf8L(dName.Element(i).Type(), aOutputFile); |
|
49 |
|
50 |
|
51 WriteAsUtf8L(GetType(dName.Element(i).Type()), aOutputFile); |
|
52 |
|
53 |
|
54 User::LeaveIfError(aOutputFile.Write(_L8(": "))); |
|
55 |
|
56 HBufC* value = NULL; |
|
57 value=dName.Element(i).ValueL(); |
|
58 CleanupStack::PushL(value); |
|
59 |
|
60 |
|
61 if (dName.Element(i).Type().Compare(_L("2.5.4.3"))) |
|
62 { |
|
63 WriteAsUtf8L(*value, aOutputFile); |
|
64 } |
|
65 else |
|
66 { |
|
67 HBufC8* temp=NULL; |
|
68 temp=HBufC8::NewL(value->Length()); |
|
69 CleanupStack::PushL(temp); |
|
70 |
|
71 TPtr8 ptr = temp->Des(); |
|
72 TPtr valuePtr = value->Des(); |
|
73 for (TInt j = 0;j < value->Length();++j) |
|
74 { |
|
75 TChar c = valuePtr[j]; |
|
76 ptr.Append(c % 0x10000); |
|
77 } |
|
78 |
|
79 User::LeaveIfError(aOutputFile.Write(*temp)); |
|
80 CleanupStack::PopAndDestroy(temp); |
|
81 } |
|
82 |
|
83 User::LeaveIfError(aOutputFile.Write(_L8("\r\n"))); |
|
84 CleanupStack::PopAndDestroy(value); |
|
85 } |
|
86 } |
|
87 |
|
88 void DumpCertInfoL(const CX509Certificate& aCert, RFile& aOutputFile) |
|
89 { |
|
90 // common/display names |
|
91 aOutputFile.Write(_L8("Issuer Display Name: ")); |
|
92 HBufC* issuer = aCert.IssuerL(); |
|
93 CleanupStack::PushL(issuer); |
|
94 WriteAsUtf8L(*issuer, aOutputFile); |
|
95 User::LeaveIfError(aOutputFile.Write(_L8("\r\n"))); |
|
96 CleanupStack::PopAndDestroy(issuer); |
|
97 |
|
98 aOutputFile.Write(_L8("Subject Display Name: ")); |
|
99 HBufC* subject = aCert.SubjectL(); |
|
100 CleanupStack::PushL(subject); |
|
101 WriteAsUtf8L(*subject, aOutputFile); |
|
102 User::LeaveIfError(aOutputFile.Write(_L8("\r\n"))); |
|
103 CleanupStack::PopAndDestroy(subject); |
|
104 |
|
105 |
|
106 // distinguished names |
|
107 |
|
108 aOutputFile.Write(_L8("Issuer Distinguished Name:\r\n")); |
|
109 PrintDistinguishedNameElementsL(aCert.IssuerName(),aOutputFile); |
|
110 |
|
111 aOutputFile.Write(_L8("Subject Distinguished Name:\r\n")); |
|
112 PrintDistinguishedNameElementsL(aCert.SubjectName(),aOutputFile); |
|
113 |
|
114 // alt names |
|
115 const CX509CertExtension *subjectAltExt=aCert.Extension(KSubjectAltName); |
|
116 if (subjectAltExt!=NULL) |
|
117 { |
|
118 aOutputFile.Write(_L8("Subject Alt Names:\r\n")); |
|
119 CX509AltNameExt* subjectAltNameExt = CX509AltNameExt::NewLC(subjectAltExt->Data()); |
|
120 const CArrayPtrFlat<CX509GeneralName>&names = subjectAltNameExt->AltName(); |
|
121 for (TInt i = 0; i < names.Count(); i++) |
|
122 { |
|
123 CX509GeneralName* name = names.At(i); |
|
124 if ( name->Tag() == EX509DirectoryName ) |
|
125 { |
|
126 aOutputFile.Write(_L8("\tDirectory Name:\r\n")); |
|
127 CX500DistinguishedName* dirName = CX500DistinguishedName::NewLC(name->Data()); |
|
128 PrintDistinguishedNameElementsL(*dirName,aOutputFile); |
|
129 CleanupStack::PopAndDestroy(dirName); |
|
130 } |
|
131 } |
|
132 CleanupStack::PopAndDestroy(subjectAltNameExt); |
|
133 } |
|
134 |
|
135 const CX509CertExtension *issuerAltExt=aCert.Extension(KIssuerAltName); |
|
136 if (issuerAltExt!=NULL) |
|
137 { |
|
138 aOutputFile.Write(_L8("Issuer Alt Names:\r\n")); |
|
139 CX509AltNameExt* issuerAltNameExt = CX509AltNameExt::NewLC(issuerAltExt->Data()); |
|
140 const CArrayPtrFlat<CX509GeneralName>&names = issuerAltNameExt->AltName(); |
|
141 for (TInt i = 0; i < names.Count(); i++) |
|
142 { |
|
143 CX509GeneralName* name = names.At(i); |
|
144 if ( name->Tag() == EX509DirectoryName ) |
|
145 { |
|
146 aOutputFile.Write(_L8("\tDirectory Name:\r\n")); |
|
147 CX500DistinguishedName* dirName = CX500DistinguishedName::NewLC(name->Data()); |
|
148 PrintDistinguishedNameElementsL(*dirName,aOutputFile); |
|
149 CleanupStack::PopAndDestroy(dirName); |
|
150 } |
|
151 } |
|
152 CleanupStack::PopAndDestroy(issuerAltNameExt); |
|
153 } |
|
154 } |
|
155 |
|
156 void DumpCertL() |
|
157 { |
|
158 CCommandLineArguments* args = CCommandLineArguments::NewLC(); |
|
159 TInt count = args->Count(); |
|
160 if (args->Count() == 3) |
|
161 { |
|
162 const TPtrC certFileName = args->Arg(1); |
|
163 const TPtrC outputFileName = args->Arg(2); |
|
164 |
|
165 RFs fs; |
|
166 User::LeaveIfError(fs.Connect()); |
|
167 CleanupClosePushL(fs); |
|
168 |
|
169 RFile certFile; |
|
170 User::LeaveIfError(certFile.Open(fs, certFileName, EFileShareAny | EFileRead | EFileStream)); |
|
171 CleanupClosePushL(certFile); |
|
172 |
|
173 TInt fileSize; |
|
174 User::LeaveIfError(certFile.Size(fileSize)); |
|
175 |
|
176 HBufC8* certBuffer = HBufC8::NewLC(fileSize); |
|
177 TPtr8 ptr = certBuffer->Des(); |
|
178 User::LeaveIfError(certFile.Read(ptr, fileSize)); |
|
179 |
|
180 CX509Certificate* cert = CX509Certificate::NewLC(*certBuffer); |
|
181 |
|
182 RFile outputFile; |
|
183 User::LeaveIfError(outputFile.Replace(fs, outputFileName, EFileWrite | EFileShareExclusive)); |
|
184 |
|
185 DumpCertInfoL(*cert, outputFile); |
|
186 |
|
187 CleanupStack::PopAndDestroy(4, &fs); // outputFile, cert, certBuffer, certFile, fs |
|
188 } |
|
189 CleanupStack::PopAndDestroy(args); |
|
190 } |
|
191 |
|
192 void DoMainL(void) |
|
193 { |
|
194 CConsoleBase* console = Console::NewL(KAppName, TSize(KDefaultConsWidth, KDefaultConsHeight)); |
|
195 CleanupStack::PushL(console); |
|
196 __UHEAP_MARK; |
|
197 TRAPD(err, DumpCertL()); |
|
198 if (err) |
|
199 { |
|
200 console->Printf(_L("Error %d\n"), err); |
|
201 console->Getch(); |
|
202 } |
|
203 __UHEAP_MARKEND; |
|
204 CleanupStack::PopAndDestroy(console); |
|
205 } |
|
206 |
|
207 GLDEF_C TInt E32Main() // main function called by E32 |
|
208 { |
|
209 __UHEAP_MARK; |
|
210 CTrapCleanup* cleanup=CTrapCleanup::New(); |
|
211 |
|
212 TRAP_IGNORE(DoMainL()); |
|
213 |
|
214 delete cleanup; |
|
215 __UHEAP_MARKEND; |
|
216 return 0; |
|
217 } |