1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tcertcommon/Tcertwriter.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,889 @@
1.4 +/*
1.5 +* Copyright (c) 1998-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 "tcertwriter.h"
1.23 +#include <x509keys.h>
1.24 +#include <asn1dec.h>
1.25 +
1.26 +EXPORT_C CertWriter::CertWriter(Output* aOut)
1.27 + :iOut(aOut)
1.28 + {
1.29 + }
1.30 +
1.31 +EXPORT_C void CertWriter::SetOut(Output* aOut)
1.32 + {
1.33 + iOut = aOut;
1.34 + }
1.35 +
1.36 +EXPORT_C void CertWriter::WriteCert(const CX509Certificate& aCertificate)
1.37 + {
1.38 +
1.39 + CX509Certificate* cert = CX509Certificate::NewLC(aCertificate);
1.40 +// iOut->writeString(_L("\n Certificate Version = ")):
1.41 +// iOut->writeNum(cert->Version());
1.42 +// iOut.writeNewLine();
1.43 + WriteEncodings(*cert);
1.44 + iOut->writeNewLine();
1.45 + ShowSerialNumber(cert->SerialNumber());
1.46 + ShowValidityPeriod(*cert);
1.47 + iOut->writeString(_L("Issuer Name = "));
1.48 + iOut->writeNewLine();
1.49 + ShowDN(cert->IssuerName());
1.50 + iOut->writeString(_L("Subject Name = "));
1.51 + iOut->writeNewLine();
1.52 + ShowDN(cert->SubjectName());
1.53 + ShowSigningAlgorithm(cert->SigningAlgorithm());
1.54 + ShowExtensions(*cert);
1.55 + ShowPublicKey(*cert);
1.56 + iOut->writeNewLine();
1.57 + ShowFingerprint(*cert);
1.58 + ShowSignature(*cert);
1.59 +
1.60 +/* CalculateSignature(*cert);
1.61 + if (IsSelfSigned(*cert))
1.62 + {
1.63 + iOut->writeString(_L("\nSelf-signed\n"));
1.64 + }
1.65 + else
1.66 + {
1.67 + iOut->writeString(_L("\nNot self-signed\n"));
1.68 + }
1.69 +*/
1.70 +
1.71 + iOut->writeString(_L("Short Issuer Name = "));
1.72 + HBufC* issuer = cert->IssuerL();
1.73 + iOut->writeString(*issuer);
1.74 + iOut->writeNewLine();
1.75 + delete issuer;
1.76 +
1.77 + iOut->writeString(_L("Short Subject Name = "));
1.78 + HBufC* subject = cert->SubjectL();
1.79 + iOut->writeString(*subject);
1.80 + iOut->writeNewLine();
1.81 + delete subject;
1.82 +
1.83 + iOut->writeNewLine();
1.84 + CleanupStack::PopAndDestroy();//cert
1.85 + }
1.86 +
1.87 +EXPORT_C void CertWriter::ShowPublicKey(const CX509Certificate& aCertificate)
1.88 + {
1.89 + const CSubjectPublicKeyInfo& spki = aCertificate.PublicKey();
1.90 + switch(spki.AlgorithmId())
1.91 + {
1.92 + case ERSA:
1.93 + ShowRSAKey(spki);
1.94 + break;
1.95 +
1.96 + case EDSA:
1.97 + ShowDSAKey(spki);
1.98 + break;
1.99 +
1.100 + case EDH:
1.101 + ShowDHKey(spki);
1.102 + break;
1.103 +
1.104 + default:
1.105 + User::Panic(_L("CertWriter"), 1);
1.106 + break;
1.107 + }
1.108 + }
1.109 +
1.110 +
1.111 +void CertWriter::ShowRSAKey(const CSubjectPublicKeyInfo& aSpki)
1.112 + {
1.113 + CX509RSAPublicKey* key = CX509RSAPublicKey::NewLC(aSpki.KeyData());
1.114 + iOut->writeString(_L("RSA Key:"));
1.115 + iOut->writeNewLine();
1.116 + iOut->writeSpaces(4);
1.117 + iOut->writeString(_L("Modulus = "));
1.118 + iOut->writeNewLine();
1.119 + const TInteger& mod = key->N();
1.120 + HBufC8* mBuf = mod.BufferLC();
1.121 + iOut->writeOctetStringL(mBuf->Des());
1.122 + iOut->writeNewLine();
1.123 +
1.124 + iOut->writeSpaces(4);
1.125 + iOut->writeString(_L("Exponent = "));
1.126 + iOut->writeNewLine();
1.127 + const TInteger& exp = key->E();
1.128 + HBufC8* eBuf = exp.BufferLC();
1.129 + iOut->writeOctetStringL(eBuf->Des());
1.130 + iOut->writeNewLine();
1.131 + CleanupStack::PopAndDestroy(eBuf);
1.132 + CleanupStack::PopAndDestroy(mBuf);
1.133 + CleanupStack::PopAndDestroy(key);
1.134 + }
1.135 +
1.136 +void CertWriter::ShowDSAKey(const CSubjectPublicKeyInfo& aSpki)
1.137 + {
1.138 + const TDesC8& encParams = aSpki.EncodedParams();
1.139 + if (encParams.Length() != 0)
1.140 + {
1.141 + CDSAPublicKey* key = CX509DSAPublicKey::NewLC(encParams, aSpki.KeyData());
1.142 + iOut->writeString(_L("DSA Key:"));
1.143 + iOut->writeNewLine();
1.144 + iOut->writeSpaces(4);
1.145 + iOut->writeString(_L("Y = "));
1.146 + iOut->writeNewLine();
1.147 + const TInteger& y = key->Y();
1.148 + HBufC8* yBuf = y.BufferLC();
1.149 + iOut->writeOctetStringL(yBuf->Des());
1.150 + iOut->writeNewLine();
1.151 +
1.152 + iOut->writeSpaces(4);
1.153 + iOut->writeString(_L("Params = "));
1.154 + iOut->writeNewLine();
1.155 +
1.156 + iOut->writeSpaces(8);
1.157 + iOut->writeString(_L("P = "));
1.158 + iOut->writeNewLine();
1.159 + const TInteger& p = key->P();
1.160 + HBufC8* pBuf = p.BufferLC();
1.161 + iOut->writeOctetStringL(pBuf->Des());
1.162 + iOut->writeNewLine();
1.163 +
1.164 + iOut->writeSpaces(8);
1.165 + iOut->writeString(_L("Q = "));
1.166 + iOut->writeNewLine();
1.167 + const TInteger& q = key->Q();
1.168 + HBufC8* qBuf = q.BufferLC();
1.169 + iOut->writeOctetStringL(qBuf->Des());
1.170 + iOut->writeNewLine();
1.171 +
1.172 + iOut->writeSpaces(8);
1.173 + iOut->writeString(_L("G = "));
1.174 + iOut->writeNewLine();
1.175 + const TInteger& g = key->G();
1.176 + HBufC8* gBuf = g.BufferLC();
1.177 + iOut->writeOctetStringL(gBuf->Des());
1.178 + iOut->writeNewLine();
1.179 +
1.180 + CleanupStack::PopAndDestroy(5);
1.181 + }
1.182 + }
1.183 +
1.184 +void CertWriter::ShowDHKey(const CSubjectPublicKeyInfo& aSpki)
1.185 + {
1.186 + TASN1DecInteger encInt;
1.187 + TInt pos = 0;
1.188 + RInteger keyVal = encInt.DecodeDERLongL(aSpki.KeyData(), pos);
1.189 + CleanupStack::PushL(keyVal);
1.190 + HBufC8* keyValBuf = keyVal.BufferLC();
1.191 + iOut->writeString(_L("Y = "));
1.192 + iOut->writeNewLine();
1.193 + iOut->writeOctetStringL(keyValBuf->Des());
1.194 + CleanupStack::PopAndDestroy(2, &keyVal);
1.195 +
1.196 + CX509DHDomainParams* params = CX509DHDomainParams::NewLC(aSpki.EncodedParams());
1.197 + const TInteger& p = params->P();
1.198 + const TInteger& g = params->G();
1.199 + const TInteger& q = params->Q();
1.200 + const TInteger& j = params->J();
1.201 + const CX509DHValidationParams* valParams = params->ValidationParams();
1.202 +
1.203 + iOut->writeNewLine();
1.204 + iOut->writeString(_L("Params = "));
1.205 + iOut->writeNewLine();
1.206 +
1.207 + iOut->writeString(_L("P = "));
1.208 + iOut->writeNewLine();
1.209 + HBufC8* pBuf = p.BufferLC();
1.210 + iOut->writeOctetStringL(pBuf->Des());
1.211 + iOut->writeNewLine();
1.212 + CleanupStack::PopAndDestroy();
1.213 +
1.214 + iOut->writeString(_L("G = "));
1.215 + iOut->writeNewLine();
1.216 + HBufC8* gBuf = g.BufferLC();
1.217 + iOut->writeOctetStringL(gBuf->Des());
1.218 + iOut->writeNewLine();
1.219 + CleanupStack::PopAndDestroy();
1.220 +
1.221 + iOut->writeString(_L("Q = "));
1.222 + iOut->writeNewLine();
1.223 + HBufC8* qBuf = q.BufferLC();
1.224 + iOut->writeOctetStringL(qBuf->Des());
1.225 + iOut->writeNewLine();
1.226 + CleanupStack::PopAndDestroy();
1.227 +
1.228 + iOut->writeString(_L("J = "));
1.229 + iOut->writeNewLine();
1.230 + HBufC8* jBuf = j.BufferLC();
1.231 + iOut->writeOctetStringL(jBuf->Des());
1.232 + iOut->writeNewLine();
1.233 + CleanupStack::PopAndDestroy();
1.234 +
1.235 + if (valParams)
1.236 + {
1.237 + iOut->writeString(_L("Seed = "));
1.238 + iOut->writeNewLine();
1.239 + iOut->writeOctetStringL(valParams->Seed());
1.240 + iOut->writeNewLine();
1.241 + const TInteger& pGC = valParams->PGenCounter();
1.242 + HBufC8* pgBuf = pGC.BufferLC();
1.243 + iOut->writeString(_L("pGenCounter = "));
1.244 + iOut->writeNewLine();
1.245 + iOut->writeOctetStringL(pgBuf->Des());
1.246 + iOut->writeNewLine();
1.247 + CleanupStack::PopAndDestroy();
1.248 + }
1.249 + CleanupStack::PopAndDestroy();
1.250 + }
1.251 +
1.252 +
1.253 +EXPORT_C void CertWriter::ShowSigningAlgorithm(const CSigningAlgorithmIdentifier& aSigningAlgorithm)
1.254 + {
1.255 + iOut->writeString(_L("Signed using: "));
1.256 + iOut->writeNewLine();
1.257 + iOut->writeSpaces(4);
1.258 + iOut->writeString(_L("Asymmetric algorithm = "));
1.259 + const CAlgorithmIdentifier& algId = aSigningAlgorithm.AsymmetricAlgorithm();
1.260 + switch(algId.Algorithm())
1.261 + {
1.262 + case ERSA:
1.263 + iOut->writeString(_L("RSA"));
1.264 + break;
1.265 +
1.266 + case EDSA:
1.267 + iOut->writeString(_L("DSA"));
1.268 + break;
1.269 +
1.270 + case EDH:
1.271 + iOut->writeString(_L("DH"));
1.272 + break;
1.273 +
1.274 + default:
1.275 + User::Panic(_L("CertWriter"), 1);
1.276 + break;
1.277 + }
1.278 + iOut->writeNewLine();
1.279 + iOut->writeSpaces(4);
1.280 + iOut->writeString(_L("Digest algorithm = "));
1.281 + const CAlgorithmIdentifier& digestId = aSigningAlgorithm.DigestAlgorithm();
1.282 + switch(digestId.Algorithm())
1.283 + {
1.284 + case EMD2:
1.285 + iOut->writeString(_L("MD2"));
1.286 + break;
1.287 +
1.288 + case EMD5:
1.289 + iOut->writeString(_L("MD5"));
1.290 + break;
1.291 +
1.292 + case ESHA1:
1.293 + iOut->writeString(_L("SHA1"));
1.294 + break;
1.295 +
1.296 + default:
1.297 + User::Panic(_L("CertWriter"), 1);
1.298 + break;
1.299 + }
1.300 + iOut->writeNewLine();
1.301 + }
1.302 +
1.303 +EXPORT_C void CertWriter::ShowSerialNumber(const TPtrC8& aSerialNumber)
1.304 + {
1.305 + iOut->writeString(_L("Serial Number = "));
1.306 + iOut->writeOctetStringL(aSerialNumber);
1.307 + iOut->writeNewLine();
1.308 + }
1.309 +
1.310 +//dn display code
1.311 +EXPORT_C void CertWriter::ShowAVA(const CX520AttributeTypeAndValue& aAva)
1.312 + {
1.313 + iOut->writeString(aAva.Type());
1.314 + HBufC* val = aAva.ValueL();
1.315 + CleanupStack::PushL(val);
1.316 + iOut->writeString(_L(" = "));
1.317 + iOut->writeString(val->Des());
1.318 + CleanupStack::PopAndDestroy();
1.319 + }
1.320 +
1.321 +EXPORT_C void CertWriter::ShowDN(const CX500DistinguishedName& aName)
1.322 + {
1.323 + iOut->writeNewLine();
1.324 + TInt count = aName.Count();
1.325 + for (TInt i = 0; i < count; i++)
1.326 + {
1.327 + const CX520AttributeTypeAndValue& ava = aName.Element(i);
1.328 + iOut->writeSpaces(4);
1.329 + ShowAVA(ava);
1.330 + iOut->writeNewLine();
1.331 + }
1.332 + }
1.333 +
1.334 +EXPORT_C void CertWriter::ShowAKI(const CX509Certificate& aCert)
1.335 + {
1.336 + const CX509CertExtension* akiExt = aCert.Extension(KAuthorityKeyId);
1.337 + if (akiExt)
1.338 + {
1.339 + iOut->writeString(_L("Authority Key ID = "));
1.340 + iOut->writeNewLine();
1.341 + const CX509AuthorityKeyIdExt* ext = CX509AuthorityKeyIdExt::NewLC(akiExt->Data());
1.342 + const CArrayPtrFlat<CX509GeneralName>& authorityName = ext->AuthorityName();
1.343 + TInt count = authorityName.Count();
1.344 + if (count > 0)
1.345 + {
1.346 + iOut->writeSpaces(4);
1.347 + iOut->writeString(_L("Authority name = "));
1.348 + iOut->writeNewLine();
1.349 + for (TInt i = 0; i < count; i++)
1.350 + {
1.351 + ShowGN(*(authorityName.At(i)));
1.352 + }
1.353 + }
1.354 + if (ext->AuthorityCertSerialNumber().Length() > 0)
1.355 + {
1.356 + iOut->writeSpaces(4);
1.357 + iOut->writeString(_L("Authority cert serial no = "));
1.358 + iOut->writeOctetStringL(ext->AuthorityCertSerialNumber());
1.359 + iOut->writeNewLine();
1.360 + }
1.361 + iOut->writeSpaces(4);
1.362 + iOut->writeString(_L("Key Id = "));
1.363 + iOut->writeOctetStringL(ext->KeyId());
1.364 + iOut->writeNewLine();
1.365 + CleanupStack::PopAndDestroy();
1.366 + }
1.367 + }
1.368 +
1.369 +EXPORT_C void CertWriter::ShowSKI(const CX509Certificate& aCert)
1.370 + {
1.371 + const CX509CertExtension* skiExt = aCert.Extension(KSubjectKeyId);
1.372 + if (skiExt)
1.373 + {
1.374 + iOut->writeString(_L("Subject Key ID = "));
1.375 + iOut->writeNewLine();
1.376 + const CX509SubjectKeyIdExt* ext = CX509SubjectKeyIdExt::NewLC(skiExt->Data());
1.377 + iOut->writeSpaces(4);
1.378 + iOut->writeString(_L("Key Id = "));
1.379 + iOut->writeOctetStringL(ext->KeyId());
1.380 + iOut->writeNewLine();
1.381 + CleanupStack::PopAndDestroy();
1.382 + }
1.383 + }
1.384 +
1.385 +EXPORT_C void CertWriter::ShowGN(const CX509GeneralName& aName)
1.386 + {
1.387 + iOut->writeSpaces(4);
1.388 + switch(aName.Tag())
1.389 + {
1.390 + case 1:
1.391 + {
1.392 + //rfc822
1.393 + CX509RFC822NameSubtree* email = CX509RFC822NameSubtree::NewLC(aName.Data());
1.394 + iOut->writeString(email->Name());
1.395 + iOut->writeNewLine();
1.396 + CleanupStack::PopAndDestroy();//email
1.397 + break;
1.398 + }
1.399 + case 2:
1.400 + {
1.401 + //dns name
1.402 + CX509DNSNameSubtree* dns = CX509DNSNameSubtree::NewLC(aName.Data());
1.403 + iOut->writeString(dns->Name());
1.404 + CleanupStack::PopAndDestroy();//dns
1.405 + break;
1.406 + }
1.407 + case 4:
1.408 + {
1.409 + //DN
1.410 + CX500DistinguishedName* dN = CX500DistinguishedName::NewLC(aName.Data());
1.411 + ShowDN(*dN);
1.412 + CleanupStack::PopAndDestroy();
1.413 + break;
1.414 + }
1.415 + case 6:
1.416 + {
1.417 + //uri
1.418 + CX509IPBasedURI* uri = CX509IPBasedURI::NewLC(aName.Data());
1.419 + iOut->writeString(uri->Name());
1.420 + iOut->writeNewLine();
1.421 + iOut->writeString(_L("Host="));
1.422 + iOut->writeString(uri->Host().Name());
1.423 + iOut->writeNewLine();
1.424 + CleanupStack::PopAndDestroy();//uri
1.425 + break;
1.426 + }
1.427 + case 7:
1.428 + {
1.429 + CX509IPAddress* ip = CX509IPAddress::NewLC(aName.Data());
1.430 + TPtrC8 addressStr(ip->Address());
1.431 + // IPv6 output not implemented yet
1.432 + iOut->write(_L("IP=%d.%d.%d.%d"), addressStr[0], addressStr[1], addressStr[2], addressStr[3]);
1.433 + iOut->writeNewLine();
1.434 + CleanupStack::PopAndDestroy();
1.435 + break;
1.436 + }
1.437 + }
1.438 + }
1.439 +
1.440 +
1.441 +EXPORT_C void CertWriter::ShowFingerprint(const CX509Certificate& aCertificate)
1.442 + {
1.443 + iOut->writeString(_L("Fingerprint:"));
1.444 + iOut->writeNewLine();
1.445 + iOut->writeOctetString(aCertificate.Fingerprint());
1.446 + iOut->writeNewLine();
1.447 + }
1.448 +
1.449 +EXPORT_C void CertWriter::ShowValidityPeriod(const CX509Certificate& aCertificate)
1.450 + {
1.451 + const CValidityPeriod& vp = aCertificate.ValidityPeriod();
1.452 + iOut->writeString(_L("Validity Period = "));
1.453 + iOut->writeNewLine();
1.454 + const TTime& start = vp.Start();
1.455 + const TTime& finish = vp.Finish();
1.456 + TBuf<30> dateString1;
1.457 + start.FormatL(dateString1,(_L("%H%:1%T:%S %*E%*D %X%*N%Y %1 %2 %3")));
1.458 +
1.459 + iOut->writeSpaces(4);
1.460 + iOut->writeString(_L("Valid From = "));
1.461 + iOut->writeString(dateString1);
1.462 + iOut->writeNewLine();
1.463 +
1.464 + TBuf<30> dateString2;
1.465 + finish.FormatL(dateString2,(_L("%H%:1%T:%S %*E%*D %X%*N%Y %1 %2 %3")));
1.466 +
1.467 + iOut->writeSpaces(4);
1.468 + iOut->writeString(_L("Valid Until = "));
1.469 +
1.470 + iOut->writeString(dateString2);
1.471 + iOut->writeNewLine();
1.472 + }
1.473 +
1.474 +//extensions
1.475 +EXPORT_C void CertWriter::ShowExtensions(const CX509Certificate& aCertificate)
1.476 + {
1.477 + const CArrayPtrFlat<CX509CertExtension>& exts = aCertificate.Extensions();
1.478 + TInt count = exts.Count();
1.479 + for (TInt i = 0; i < count; i++)
1.480 + {
1.481 + const CX509CertExtension* ext = exts.At(i);
1.482 + iOut->writeString(_L("extension"));
1.483 + iOut->writeNum(i);
1.484 + if (!(ext->Critical()))
1.485 + {
1.486 + iOut->writeString(_L(" not"));
1.487 + }
1.488 + iOut->writeString(_L(" critical"));
1.489 + iOut->writeString(_L(" ID = "));
1.490 + TPtrC id(ext->Id());
1.491 + iOut->writeString(id);
1.492 + iOut->writeNewLine();
1.493 + if (id == KBasicConstraints)
1.494 + {
1.495 + ShowBC(*ext);
1.496 + }
1.497 + if (id == KSubjectAltName)
1.498 + {
1.499 + iOut->writeString(_L("SubjectAltName: "));
1.500 + iOut->writeNewLine();
1.501 + ShowAltName(*ext);
1.502 + }
1.503 + if (id == KIssuerAltName)
1.504 + {
1.505 + iOut->writeString(_L("IssuerAltName: "));
1.506 + iOut->writeNewLine();
1.507 + ShowAltName(*ext);
1.508 + }
1.509 + if (id == KKeyUsage)
1.510 + {
1.511 + ShowKU(*ext);
1.512 + }
1.513 + if (id == KNameConstraints)
1.514 + {
1.515 + ShowNC(*ext);
1.516 + }
1.517 + if (id == KPolicyConstraints)
1.518 + {
1.519 + ShowPC(*ext);
1.520 + }
1.521 + if (id == KCertPolicies)
1.522 + {
1.523 + ShowCP(*ext);
1.524 + }
1.525 + if (id == KPolicyMapping)
1.526 + {
1.527 + ShowPM(*ext);
1.528 + }
1.529 + if (id == KAuthorityKeyId)
1.530 + {
1.531 + ShowAKI(aCertificate);
1.532 + }
1.533 + if (id == KSubjectKeyId)
1.534 + {
1.535 + ShowSKI(aCertificate);
1.536 + }
1.537 + if (id == KExtendedKeyUsage)
1.538 + {
1.539 + ShowEKU(*ext);
1.540 + }
1.541 + }
1.542 + }
1.543 +
1.544 +EXPORT_C void CertWriter::ShowBC(const CX509CertExtension& aExt)
1.545 + {
1.546 + iOut->writeString(_L("Basic Constraints:"));
1.547 + iOut->writeNewLine();
1.548 + CX509BasicConstraintsExt* ext = CX509BasicConstraintsExt::NewLC(aExt.Data());
1.549 + if (ext->IsCA())
1.550 + {
1.551 + iOut->writeSpaces(4);
1.552 + iOut->writeString(_L("CA cert"));
1.553 + iOut->writeNewLine();
1.554 + if (ext->MaxChainLength() < KMaxTInt)
1.555 + {
1.556 + iOut->writeSpaces(4);
1.557 + iOut->writeString(_L("Max Chain Length = "));
1.558 + iOut->writeNum(ext->MaxChainLength());
1.559 + iOut->writeNewLine();
1.560 + }
1.561 + }
1.562 + else
1.563 + {
1.564 + iOut->writeSpaces(4);
1.565 + iOut->writeString(_L("EE cert"));
1.566 + iOut->writeNewLine();
1.567 + }
1.568 + CleanupStack::PopAndDestroy();
1.569 + }
1.570 +
1.571 +EXPORT_C void CertWriter::ShowAltName(const CX509CertExtension& aExt)
1.572 + {
1.573 + CX509AltNameExt* ext = CX509AltNameExt::NewLC(aExt.Data());
1.574 + const CArrayPtrFlat<CX509GeneralName>& names = ext->AltName();
1.575 + TInt count = names.Count();
1.576 + for (TInt i = 0; i < count; i++)
1.577 + {
1.578 + ShowGN(*(names.At(i)));
1.579 + }
1.580 + CleanupStack::PopAndDestroy();
1.581 + }
1.582 +
1.583 +
1.584 +_LIT(KDigSig, "digital signature");
1.585 +_LIT(KNonRep, "non-repudiation");
1.586 +_LIT(KKeyEnc, "key encipherment");
1.587 +_LIT(KDataEnc, "data encipherment");
1.588 +_LIT(KKeyAgree, "key agreement");
1.589 +_LIT(KCertSign, "key cert sign");
1.590 +_LIT(KCRLSign, "crl sign");
1.591 +_LIT(KEnciph, "encipher only");
1.592 +_LIT(KDeciph, "decipher only");
1.593 +
1.594 +EXPORT_C void CertWriter::ShowKU(const CX509CertExtension& aExt)
1.595 + {
1.596 + iOut->writeString(_L("Key Usage:"));
1.597 + iOut->writeNewLine();
1.598 + CX509KeyUsageExt* ext = CX509KeyUsageExt::NewLC(aExt.Data());
1.599 + if (ext->IsSet(EX509DigitalSignature))
1.600 + {
1.601 + iOut->writeSpaces(4);
1.602 + iOut->writeString(KDigSig);
1.603 + iOut->writeNewLine();
1.604 + }
1.605 + if (ext->IsSet(EX509NonRepudiation))
1.606 + {
1.607 + iOut->writeSpaces(4);
1.608 + iOut->writeString(KNonRep);
1.609 + iOut->writeNewLine();
1.610 + }
1.611 + if (ext->IsSet(EX509KeyEncipherment))
1.612 + {
1.613 + iOut->writeSpaces(4);
1.614 + iOut->writeString(KKeyEnc);
1.615 + iOut->writeNewLine();
1.616 + }
1.617 + if (ext->IsSet(EX509DataEncipherment))
1.618 + {
1.619 + iOut->writeSpaces(4);
1.620 + iOut->writeString(KDataEnc);
1.621 + iOut->writeNewLine();
1.622 + }
1.623 + if (ext->IsSet(EX509KeyAgreement))
1.624 + {
1.625 + iOut->writeSpaces(4);
1.626 + iOut->writeString(KKeyAgree);
1.627 + iOut->writeNewLine();
1.628 + }
1.629 + if (ext->IsSet(EX509KeyCertSign))
1.630 + {
1.631 + iOut->writeSpaces(4);
1.632 + iOut->writeString(KCertSign);
1.633 + iOut->writeNewLine();
1.634 + }
1.635 + if (ext->IsSet(EX509CRLSign))
1.636 + {
1.637 + iOut->writeSpaces(4);
1.638 + iOut->writeString(KCRLSign);
1.639 + iOut->writeNewLine();
1.640 + }
1.641 + if (ext->IsSet(EX509EncipherOnly))
1.642 + {
1.643 + iOut->writeSpaces(4);
1.644 + iOut->writeString(KEnciph);
1.645 + iOut->writeNewLine();
1.646 + }
1.647 + if (ext->IsSet(EX509DecipherOnly))
1.648 + {
1.649 + iOut->writeSpaces(4);
1.650 + iOut->writeString(KDeciph);
1.651 + iOut->writeNewLine();
1.652 + }
1.653 + CleanupStack::PopAndDestroy();//ext
1.654 + }
1.655 +
1.656 +EXPORT_C void CertWriter::ShowSubtrees(const CArrayPtrFlat<CX509GeneralSubtree>& aSubtrees)
1.657 + {
1.658 + TInt count = aSubtrees.Count();
1.659 + for (TInt i = 0; i < count; i++)
1.660 + {
1.661 + const CX509GeneralSubtree* subtree = aSubtrees.At(i);
1.662 + ShowGN(subtree->Name());
1.663 + ///!!!!!don't write these outFile either!!
1.664 + }
1.665 + }
1.666 +
1.667 +EXPORT_C void CertWriter::ShowNC(const CX509CertExtension& aExt)
1.668 + {//!!!!don't write these outFile
1.669 +
1.670 + CX509NameConstraintsExt* ext = CX509NameConstraintsExt::NewLC(aExt.Data());
1.671 + ShowSubtrees(ext->ExcludedSubtrees());
1.672 + ShowSubtrees(ext->PermittedSubtrees());
1.673 + CleanupStack::PopAndDestroy();
1.674 + }
1.675 +
1.676 +EXPORT_C void CertWriter::ShowPC(const CX509CertExtension& /*aExt*/)
1.677 + {//!!!!don't write these outFile
1.678 +// CX509PolicyConstraintsExt* ext = CX509PolicyConstraintsExt::NewLC(aExt.Data());
1.679 +// TX509PolicyConstraint required = ext->ExplicitPolicyRequired();
1.680 +// TX509PolicyConstraint mapping = ext->InhibitPolicyMapping();
1.681 +// CleanupStack::PopAndDestroy();
1.682 + }
1.683 +
1.684 +EXPORT_C void CertWriter::ShowCP(const CX509CertExtension& aExt)
1.685 + {
1.686 + iOut->writeString(_L("Cert Policies = "));
1.687 + iOut->writeNewLine();
1.688 +
1.689 + CX509CertPoliciesExt* ext = CX509CertPoliciesExt::NewLC(aExt.Data());
1.690 + const CArrayPtrFlat<CX509CertPolicyInfo>& policies = ext->Policies();
1.691 + TInt count = policies.Count();
1.692 + for (TInt i = 0; i < count; i++)
1.693 + {
1.694 + const CX509CertPolicyInfo* policy = policies.At(i);
1.695 +
1.696 + iOut->writeSpaces(4);
1.697 + iOut->writeString(_L("Policy ID = "));
1.698 + iOut->writeString(policy->Id());
1.699 + iOut->writeNewLine();
1.700 + const CArrayPtrFlat<CX509PolicyQualifierInfo>& qualifiers = policy->Qualifiers();
1.701 + TInt qCount = qualifiers.Count();
1.702 +
1.703 + if (qCount > 0)
1.704 + {
1.705 +
1.706 + iOut->writeSpaces(4);
1.707 + iOut->writeString(_L("Qualified by: "));
1.708 + iOut->writeNewLine();
1.709 + }
1.710 +
1.711 + for (TInt i = 0; i < qCount; i++)
1.712 + {
1.713 + iOut->writeSpaces(8);
1.714 + iOut->writeString(_L("Qualifier ID = "));
1.715 + iOut->writeString(policy->Id());
1.716 + iOut->writeNewLine();
1.717 + }
1.718 + }
1.719 + CleanupStack::PopAndDestroy();//ext
1.720 + }
1.721 +
1.722 +EXPORT_C void CertWriter::ShowPM(const CX509CertExtension& aExt)
1.723 + {
1.724 + iOut->writeString(_L("Policy Mappings = "));
1.725 + CX509PolicyMappingExt* ext = CX509PolicyMappingExt::NewLC(aExt.Data());
1.726 + const CArrayPtrFlat<CX509PolicyMapping>& mappings = ext->Mappings();
1.727 + TInt count = mappings.Count();
1.728 + for (TInt i = 0; i < count; i++)
1.729 + {
1.730 + const CX509PolicyMapping* mapping = mappings.At(i);
1.731 + iOut->writeString(_L("Issuer Policy = "));
1.732 + iOut->writeString(mapping->IssuerPolicy());
1.733 + iOut->writeNewLine();
1.734 + iOut->writeString(_L("Subject Policy = "));
1.735 + iOut->writeString(mapping->SubjectPolicy());
1.736 + iOut->writeNewLine();
1.737 + }
1.738 + iOut->writeNewLine();
1.739 + CleanupStack::PopAndDestroy();
1.740 + }
1.741 +
1.742 +
1.743 +EXPORT_C void CertWriter::ShowEKU(const CX509CertExtension& aExt)
1.744 + {
1.745 + iOut->writeString(_L("Extended Key Usage = "));
1.746 + iOut->writeNewLine();
1.747 +
1.748 + CX509ExtendedKeyUsageExt* ext = CX509ExtendedKeyUsageExt::NewLC(aExt.Data());
1.749 + const CArrayPtrFlat<HBufC>& usages = ext->KeyUsages();
1.750 + TInt count = usages.Count();
1.751 + for (TInt i = 0; i < count; i++)
1.752 + {
1.753 + iOut->writeSpaces(4);
1.754 + iOut->writeString(usages.At(i)->Des());
1.755 + iOut->writeNewLine();
1.756 + }
1.757 + CleanupStack::PopAndDestroy();
1.758 + }
1.759 +
1.760 +EXPORT_C void CertWriter::ShowSignature(const CX509Certificate& aCert)
1.761 + {
1.762 + iOut->writeString(_L("Signature:"));
1.763 + iOut->writeNewLine();
1.764 + const TPtrC8 sig = aCert.Signature();
1.765 + iOut->writeOctetStringL(sig);
1.766 + iOut->writeNewLine();
1.767 + }
1.768 +
1.769 +void CertWriter::CalculateSignature(const CX509Certificate& aCert)
1.770 + {
1.771 + TBool res = EFalse;;
1.772 + TRAPD(err, res = aCert.VerifySignatureL(aCert.PublicKey().KeyData()));
1.773 + if (err != KErrNone)
1.774 + {
1.775 + iOut->writeString(_L("\nsignature verification could not be performed\n"));
1.776 + if (err != KErrArgument)
1.777 + {
1.778 + User::Leave(err);
1.779 + }
1.780 + }
1.781 + else
1.782 + {
1.783 + if (res)
1.784 + {
1.785 + iOut->writeString(_L("\nself-signed\n"));
1.786 + }
1.787 + else
1.788 + {
1.789 + iOut->writeString(_L("\n not self-signed\n"));
1.790 + }
1.791 + }
1.792 + }
1.793 +
1.794 +EXPORT_C TBool CertWriter::IsSelfSigned(const CX509Certificate& aCert)
1.795 + {
1.796 + TBool res = EFalse;
1.797 + const CX500DistinguishedName& subject = aCert.SubjectName();
1.798 + if (subject.Count() > 0)
1.799 + {
1.800 + res = subject.ExactMatchL(aCert.IssuerName());
1.801 + }
1.802 + else
1.803 + {
1.804 + const CX509CertExtension* subjectExt = aCert.Extension(KIssuerAltName);
1.805 + const CX509CertExtension* issuerExt = aCert.Extension(KSubjectAltName);
1.806 + if ((!subjectExt) || (!issuerExt))
1.807 + {
1.808 + }
1.809 + else
1.810 + {
1.811 + const CX509AltNameExt* issuerAltName = CX509AltNameExt::NewLC(subjectExt->Data());
1.812 + const CX509AltNameExt* subjectAltName = CX509AltNameExt::NewLC(issuerExt->Data());
1.813 + if (subjectAltName->Match(*issuerAltName))
1.814 + {
1.815 + res = ETrue;
1.816 + }
1.817 + CleanupStack::PopAndDestroy(2);//subjectAltName, issuerAltName
1.818 + }
1.819 + }
1.820 + return res;
1.821 + }
1.822 +/* EVersionNumber = 0,
1.823 + ESerialNumber = 1,
1.824 + EAlgorithmId = 2,
1.825 + EIssuerName = 3,
1.826 + EValidityPeriod = 4,
1.827 + ESubjectName = 5,
1.828 + ESubjectPublicKeyInfo = 6,
1.829 + EIssuerUID = 7,
1.830 + ESubjectUID = 8,
1.831 + EExtensionList = 9
1.832 +*/
1.833 +void CertWriter::WriteEncodings(const CX509Certificate& aCertificate)
1.834 + {
1.835 + iOut->writeString(_L("Version:"));
1.836 + iOut->writeNewLine();
1.837 + WriteEncoding(aCertificate, CX509Certificate::EVersionNumber);
1.838 + iOut->writeNewLine();
1.839 +
1.840 + iOut->writeString(_L("Serial Number:"));
1.841 + iOut->writeNewLine();
1.842 + WriteEncoding(aCertificate, CX509Certificate::ESerialNumber);
1.843 + iOut->writeNewLine();
1.844 +
1.845 + iOut->writeString(_L("Algorithm:"));
1.846 + iOut->writeNewLine();
1.847 + WriteEncoding(aCertificate, CX509Certificate::EAlgorithmId);
1.848 + iOut->writeNewLine();
1.849 +
1.850 + iOut->writeString(_L("Issuer:"));
1.851 + iOut->writeNewLine();
1.852 + WriteEncoding(aCertificate, CX509Certificate::EIssuerName);
1.853 + iOut->writeNewLine();
1.854 +
1.855 + iOut->writeString(_L("Validity:"));
1.856 + iOut->writeNewLine();
1.857 + WriteEncoding(aCertificate, CX509Certificate::EValidityPeriod);
1.858 + iOut->writeNewLine();
1.859 +
1.860 + iOut->writeString(_L("Subject:"));
1.861 + iOut->writeNewLine();
1.862 + WriteEncoding(aCertificate, CX509Certificate::ESubjectName);
1.863 + iOut->writeNewLine();
1.864 +
1.865 + iOut->writeString(_L("Public Key:"));
1.866 + iOut->writeNewLine();
1.867 + WriteEncoding(aCertificate, CX509Certificate::ESubjectPublicKeyInfo);
1.868 + iOut->writeNewLine();
1.869 +
1.870 + iOut->writeString(_L("Issuer ID:"));
1.871 + iOut->writeNewLine();
1.872 + WriteEncoding(aCertificate, CX509Certificate::EIssuerUID);
1.873 + iOut->writeNewLine();
1.874 +
1.875 + iOut->writeString(_L("Subject ID:"));
1.876 + iOut->writeNewLine();
1.877 + WriteEncoding(aCertificate, CX509Certificate::ESubjectUID);
1.878 + iOut->writeNewLine();
1.879 +
1.880 + iOut->writeString(_L("Extensions:"));
1.881 + iOut->writeNewLine();
1.882 + WriteEncoding(aCertificate, CX509Certificate::EExtensionList);
1.883 + iOut->writeNewLine();
1.884 + }
1.885 +
1.886 +void CertWriter::WriteEncoding(const CX509Certificate& aCertificate, const TUint aIndex)
1.887 + {
1.888 + if (aCertificate.DataElementEncoding(aIndex))
1.889 + {
1.890 + iOut->writeOctetStringL(*(aCertificate.DataElementEncoding(aIndex)));
1.891 + }
1.892 + }