Update contrib.
2 * Copyright (c) 2007-2010 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 "keyconverter.h"
20 #include <cryptospi/cryptoparams.h>
21 #include <cryptospi/cryptospidef.h>
22 #include <cryptospi/keys.h>
23 #include <cryptospi/keypair.h>
25 using namespace CryptoSpi;
29 CKey* KeyConverter::CreateKeyL(const CDHPrivateKey& aPrivateKey)
32 * setup key attributes
34 TKeyProperty keyProperty;
35 keyProperty.iAlgorithmUid = KDHAgreementUid;
38 * extract key parameters from the compatibility dh key object
40 CCryptoParams* keyParameters = CCryptoParams::NewLC();
41 keyParameters->AddL(aPrivateKey.N(), KDhKeyParameterNUid);
42 keyParameters->AddL(aPrivateKey.G(), KDhKeyParameterGUid);
43 keyParameters->AddL(aPrivateKey.x(), KDhKeyParameterxUid);
46 * create a CKey from the parameters
48 CKey* newPrivateKey = CKey::NewL(keyProperty, *keyParameters);
50 CleanupStack::PopAndDestroy(1, keyParameters);
54 CKey* KeyConverter::CreateKeyL(const CDHPublicKey& aPublicKey)
57 * setup key attributes
59 TKeyProperty keyProperty;
60 keyProperty.iAlgorithmUid = KDHAgreementUid;
63 * extract key parameters from the compatibility dh key object
65 CCryptoParams* keyParameters = CCryptoParams::NewLC();
66 keyParameters->AddL(aPublicKey.N(), KDhKeyParameterNUid);
67 keyParameters->AddL(aPublicKey.G(), KDhKeyParameterGUid);
68 keyParameters->AddL(aPublicKey.X(), KDhKeyParameterXUid);
71 * create a CKey from the parameters
73 CKey* newPublicKey = CKey::NewL(keyProperty, *keyParameters);
75 CleanupStack::PopAndDestroy(1, keyParameters);
79 CKey* KeyConverter::CreateKeyL(const CDSAPrivateKey& aPrivateKey)
81 TKeyProperty keyProperty = {KDsaSignerUid, KNullUid, KDsaPrivateKeyUid, KNonEmbeddedKeyUid};
83 CCryptoParams* keyParameters = CCryptoParams::NewLC();
84 keyParameters->AddL(aPrivateKey.P(), KDsaKeyParameterPUid);
85 keyParameters->AddL(aPrivateKey.Q(), KDsaKeyParameterQUid);
86 keyParameters->AddL(aPrivateKey.G(), KDsaKeyParameterGUid);
87 keyParameters->AddL(aPrivateKey.X(), KDsaKeyParameterXUid);
89 CKey* newPrivateKey = CKey::NewL(keyProperty, *keyParameters);
90 CleanupStack::PopAndDestroy(keyParameters);
94 CKey* KeyConverter::CreateKeyL(const CDSAPublicKey& aPublicKey)
96 TKeyProperty keyProperty = {KDsaSignerUid, KNullUid, KDsaPublicKeyUid, KNonEmbeddedKeyUid};
98 CCryptoParams* keyParameters = CCryptoParams::NewLC();
99 keyParameters->AddL(aPublicKey.P(), KDsaKeyParameterPUid);
100 keyParameters->AddL(aPublicKey.Q(), KDsaKeyParameterQUid);
101 keyParameters->AddL(aPublicKey.G(), KDsaKeyParameterGUid);
102 keyParameters->AddL(aPublicKey.Y(), KDsaKeyParameterYUid);
104 CKey* newPublicKey = CKey::NewL(keyProperty, *keyParameters);
105 CleanupStack::PopAndDestroy(keyParameters);
109 // RSA convertors ///////////////////////////////////////////////////////////////////////////
110 CKey* KeyConverter::CreateKeyL(const CRSAPrivateKeyCRT& aPrivateKey)
112 TKeyProperty keyProperty = {KRsaCipherUid,
114 KRsaPrivateKeyCRTUid,
115 KNonEmbeddedKeyUid };
117 CCryptoParams* keyParameters = CCryptoParams::NewLC();
118 keyParameters->AddL(aPrivateKey.N(), KRsaKeyParameterNUid);
119 keyParameters->AddL(aPrivateKey.P(), KRsaKeyParameterPUid);
120 keyParameters->AddL(aPrivateKey.Q(), KRsaKeyParameterQUid);
121 keyParameters->AddL(aPrivateKey.QInv(), KRsaKeyParameterQInvUid);
122 keyParameters->AddL(aPrivateKey.DP(), KRsaKeyParameterDPUid);
123 keyParameters->AddL(aPrivateKey.DQ(), KRsaKeyParameterDQUid);
125 CKey* newPrivateKey = CKey::NewL(keyProperty, *keyParameters);
126 CleanupStack::PopAndDestroy(keyParameters);
127 return newPrivateKey;
130 CKey* KeyConverter::CreateKeyL(const CRSAPrivateKeyStandard& aPrivateKey)
132 TKeyProperty keyProperty = {KRsaCipherUid,
134 KRsaPrivateKeyStandardUid,
135 KNonEmbeddedKeyUid };
137 CCryptoParams* keyParameters = CCryptoParams::NewLC();
138 keyParameters->AddL(aPrivateKey.N(), KRsaKeyParameterNUid);
139 keyParameters->AddL(aPrivateKey.D(), KRsaKeyParameterDUid);
141 CKey* newPrivateKey = CKey::NewL(keyProperty, *keyParameters);
142 CleanupStack::PopAndDestroy(keyParameters);
143 return newPrivateKey;
146 CKey* KeyConverter::CreateKeyL(const CRSAPublicKey& aPublicKey)
148 TKeyProperty keyProperty = {KRsaCipherUid,
151 KNonEmbeddedKeyUid };
153 CCryptoParams* keyParameters = CCryptoParams::NewLC();
154 keyParameters->AddL(aPublicKey.N(), KRsaKeyParameterNUid);
155 keyParameters->AddL(aPublicKey.E(), KRsaKeyParameterEUid);
157 CKey* newPublicKey = CKey::NewL(keyProperty, *keyParameters);
158 CleanupStack::PopAndDestroy(keyParameters);
162 CKey* KeyConverter::CreateKeyL(const CRSAPrivateKey& aPrivateKey)
164 // Determine which type of private key
165 if (aPrivateKey.PrivateKeyType() == EStandard)
167 const CRSAPrivateKeyStandard* stdKey = static_cast<const CRSAPrivateKeyStandard*>(&aPrivateKey);
168 return KeyConverter::CreateKeyL(*stdKey);
170 else if (aPrivateKey.PrivateKeyType() == EStandardCRT)
172 const CRSAPrivateKeyCRT* crtKey = static_cast<const CRSAPrivateKeyCRT*>(&aPrivateKey);
173 return KeyConverter::CreateKeyL(*crtKey);
175 return NULL; // Keep the compiler happy
178 // Methods which are not supported or not exposed out of library can be excluded from the coverage.
179 #ifdef _BullseyeCoverage
180 #pragma suppress_warnings on
181 #pragma BullseyeCoverage off
182 #pragma suppress_warnings off
185 CKeyPair* KeyConverter::CreateKeyPairL(const CDSAKeyPair& /*aKeyPair*/)
190 CKeyPair* KeyConverter::CreateKeyPairL(const CRSAKeyPair& aKeyPair)
192 CKey* newPrivateKey = KeyConverter::CreateKeyL(aKeyPair.PrivateKey());
193 CleanupStack::PushL(newPrivateKey);
194 CKey* newPublicKey = KeyConverter::CreateKeyL(aKeyPair.PublicKey());
195 CleanupStack::PushL(newPublicKey);
196 CKeyPair* newKeyPair = CKeyPair::NewL(newPublicKey, newPrivateKey);
197 CleanupStack::Pop(2, newPrivateKey);
201 CKeyPair* KeyConverter::CreateKeyPairL(const CDHKeyPair& aKeyPair)
203 CKey* newPrivateKey = KeyConverter::CreateKeyL(aKeyPair.PrivateKey());
204 CleanupStack::PushL(newPrivateKey);
205 CKey* newPublicKey = KeyConverter::CreateKeyL(aKeyPair.PublicKey());
206 CleanupStack::PushL(newPublicKey);
207 CKeyPair* newKeyPair = CKeyPair::NewL(newPublicKey, newPrivateKey);
208 CleanupStack::Pop(2, newPrivateKey);