First public contribution.
2 * Copyright (c) 2002-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.
19 #include "keyhelper.h"
21 #include <asymmetric.h>
24 // CPKCS10KeyHelper ////////////////////////////////////////////////////////////
26 CPKCS10KeyHelper* CPKCS10KeyHelper::CreateKeyHelperL(MCTKeyStore& aKeyStore,
27 const CCTKeyInfo& aKeyInfo,
28 const TDesC8& aExportedKey,
29 const TAlgorithmId aDigestId)
31 CPKCS10KeyHelper* result = NULL;
33 switch (aKeyInfo.Algorithm())
35 case CCTKeyInfo::ERSA:
36 result = new (ELeave) CPKCS10RSAKeyHelper(aKeyStore, aKeyInfo);
39 case CCTKeyInfo::EDSA:
40 result = new (ELeave) CPKCS10DSAKeyHelper(aKeyStore, aKeyInfo);
44 User::Leave(KErrArgument);
47 CleanupStack::PushL(result);
48 result->CreateKeyEncoderL(aExportedKey, aDigestId);
49 CleanupStack::Pop(result);
54 CPKCS10KeyHelper::CPKCS10KeyHelper(MCTKeyStore& aKeyStore, const CCTKeyInfo& aKeyInfo) :
60 CPKCS10KeyHelper::~CPKCS10KeyHelper()
65 CASN1EncBase* CPKCS10KeyHelper::EncodeKeyLC()
67 return iKeyEncoder->EncodeKeyLC();
70 CASN1EncBase* CPKCS10KeyHelper::DigestInfoLC(const TDesC8& digest)
72 CASN1EncSequence* seq = CASN1EncSequence::NewLC();
74 // DigestAlgorithmIdentifier
75 CASN1EncSequence* digestAlgID =iKeyEncoder-> EncodeDigestAlgorithmLC();
77 seq->AddAndPopChildL(digestAlgID);
79 // Actual message digest
80 CASN1EncOctetString* octet = CASN1EncOctetString::NewLC(digest);
81 seq->AddAndPopChildL(octet);
87 CASN1EncSequence* CPKCS10KeyHelper::EncodeSignatureAlgorithmLC()
89 return iKeyEncoder->EncodeSignatureAlgorithmLC();
92 // CPKCS10RSAKeyHelper /////////////////////////////////////////////////////////
94 CPKCS10RSAKeyHelper::CPKCS10RSAKeyHelper(MCTKeyStore& aKeyStore, const CCTKeyInfo& aKeyInfo) :
95 CPKCS10KeyHelper(aKeyStore, aKeyInfo)
99 CPKCS10RSAKeyHelper::~CPKCS10RSAKeyHelper()
103 iRSASigner->Release();
109 delete iRSASignature;
113 void CPKCS10RSAKeyHelper::OpenSigner(TRequestStatus& aStatus)
115 iKeyStore.Open(iKeyInfo, iRSASigner, aStatus);
118 void CPKCS10RSAKeyHelper::CancelOpenSigner()
120 iKeyStore.CancelOpen();
123 void CPKCS10RSAKeyHelper::SignDigestL(const TDesC8& aDigest, TRequestStatus& aStatus)
125 CASN1EncBase* digestInfo = DigestInfoLC(aDigest);
128 iDigestBuf = HBufC8::NewMaxL(digestInfo->LengthDER());
129 TPtr8 oct(iDigestBuf->Des());
133 digestInfo->WriteDERL(oct, writePos);
135 // Sign the DER encoded digest info
136 iRSASigner->Sign(*iDigestBuf, iRSASignature, aStatus);
138 //CleanupStack::PopAndDestroy(octetData);
139 CleanupStack::PopAndDestroy(digestInfo);
142 void CPKCS10RSAKeyHelper::CancelSignDigest()
144 iRSASigner->CancelSign();
147 void CPKCS10RSAKeyHelper::CreateKeyEncoderL(const TDesC8& aExportedKey, TAlgorithmId aDigestId)
149 CX509SubjectPublicKeyInfo* ki = CX509SubjectPublicKeyInfo::NewLC(aExportedKey);
150 TX509KeyFactory factory;
151 iPublicKey = factory.RSAPublicKeyL(ki->KeyData());
152 CleanupStack::PopAndDestroy(ki);
153 iKeyEncoder = new (ELeave) TX509RSAKeyEncoder(*iPublicKey, aDigestId);
157 CASN1EncBitString* CPKCS10RSAKeyHelper::EncodeSignatureLC()
159 // Get raw signature data
160 HBufC8* sigData = iRSASignature->S().BufferLC();
162 // Create ASN.1 bit string from the signature and return it.
163 CASN1EncBitString* encSig = CASN1EncBitString::NewL(*sigData);
164 CleanupStack::PopAndDestroy(sigData);
165 CleanupStack::PushL(encSig);
170 // CPKCS10DSAKeyHelper /////////////////////////////////////////////////////////
172 CPKCS10DSAKeyHelper::CPKCS10DSAKeyHelper(MCTKeyStore& aKeyStore, const CCTKeyInfo& aKeyInfo) :
173 CPKCS10KeyHelper(aKeyStore, aKeyInfo)
177 CPKCS10DSAKeyHelper::~CPKCS10DSAKeyHelper()
180 iDSASigner->Release();
181 delete iDSASignature;
185 void CPKCS10DSAKeyHelper::OpenSigner(TRequestStatus& aStatus)
187 iKeyStore.Open(iKeyInfo, iDSASigner, aStatus);
190 void CPKCS10DSAKeyHelper::CancelOpenSigner()
192 iKeyStore.CancelOpen();
195 void CPKCS10DSAKeyHelper::SignDigestL(const TDesC8& aDigest, TRequestStatus& aStatus)
197 iDSASigner->Sign(aDigest, iDSASignature, aStatus);
200 void CPKCS10DSAKeyHelper::CancelSignDigest()
202 iDSASigner->CancelSign();
205 void CPKCS10DSAKeyHelper::CreateKeyEncoderL(const TDesC8& aExportedKey, TAlgorithmId aDigestId)
207 CX509SubjectPublicKeyInfo* ki = CX509SubjectPublicKeyInfo::NewLC(aExportedKey);
208 TX509KeyFactory factory;
209 iPublicKey = factory.DSAPublicKeyL(ki->EncodedParams(), ki->KeyData());
210 CleanupStack::PopAndDestroy(ki);
211 iKeyEncoder = new (ELeave) TX509DSAKeyEncoder(*iPublicKey, aDigestId);
215 * Override default implementation - leave DSA parameters out of the
216 * AlgorithmIdentifier when it appears outside of SubjectPublicKeyInfo.
218 CASN1EncSequence* CPKCS10DSAKeyHelper::EncodeSignatureAlgorithmLC()
220 CASN1EncSequence* seq = CASN1EncSequence::NewLC();
221 // Assume only SHA1 with DSA
222 CASN1EncObjectIdentifier* oid = CASN1EncObjectIdentifier::NewLC(KDSAWithSHA1);
223 seq->AddAndPopChildL(oid);
225 // Don't add parameters!
230 CASN1EncBitString* CPKCS10DSAKeyHelper::EncodeSignatureLC()
232 // Create sequence that will hold the two bit integers.
233 CASN1EncSequence* sigSeq = CASN1EncSequence::NewLC();
234 // Stuff two signature integers into the sequence.
236 CASN1EncBigInt* r = CASN1EncBigInt::NewLC(iDSASignature->R());
237 sigSeq->AddAndPopChildL(r);
238 CASN1EncBigInt* s = CASN1EncBigInt::NewLC(iDSASignature->S());
239 sigSeq->AddAndPopChildL(s);
241 // Wrap the sequence into a bit string
242 // Create ASN.1 encoding from the signature and return it.
243 CASN1EncBitString* sigDer = CASN1EncBitString::NewL(*sigSeq);
245 CleanupStack::PopAndDestroy(sigSeq);
246 CleanupStack::PushL(sigDer);