os/security/crypto/weakcryptospi/source/common/keyconverter.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2007-2010 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 "keyconverter.h"
    20 #include <cryptospi/cryptoparams.h>
    21 #include <cryptospi/cryptospidef.h>
    22 #include <cryptospi/keys.h>
    23 #include <cryptospi/keypair.h>
    24 
    25 using namespace CryptoSpi;
    26 
    27 /* CKeyConverter */
    28 
    29 CKey* KeyConverter::CreateKeyL(const CDHPrivateKey& aPrivateKey)
    30 	{
    31 	/*
    32 	 * setup key attributes
    33 	 */
    34 	TKeyProperty keyProperty;
    35 	keyProperty.iAlgorithmUid = KDHAgreementUid;
    36 
    37 	/*
    38 	 * extract key parameters from the compatibility dh key object
    39 	 */
    40 	CCryptoParams* keyParameters = CCryptoParams::NewLC();
    41 	keyParameters->AddL(aPrivateKey.N(), KDhKeyParameterNUid);
    42 	keyParameters->AddL(aPrivateKey.G(), KDhKeyParameterGUid);
    43 	keyParameters->AddL(aPrivateKey.x(), KDhKeyParameterxUid);
    44 
    45 	/*
    46 	 * create a CKey from the parameters
    47 	 */
    48 	CKey* newPrivateKey = CKey::NewL(keyProperty, *keyParameters);
    49 
    50 	CleanupStack::PopAndDestroy(1, keyParameters);
    51 	return newPrivateKey;
    52 	}
    53 
    54 CKey* KeyConverter::CreateKeyL(const CDHPublicKey& aPublicKey)
    55 	{
    56 	/*
    57 	 * setup key attributes
    58 	 */
    59 	TKeyProperty keyProperty;
    60 	keyProperty.iAlgorithmUid = KDHAgreementUid;
    61 
    62 	/*
    63 	 * extract key parameters from the compatibility dh key object
    64 	 */
    65 	CCryptoParams* keyParameters = CCryptoParams::NewLC();
    66 	keyParameters->AddL(aPublicKey.N(), KDhKeyParameterNUid);
    67 	keyParameters->AddL(aPublicKey.G(), KDhKeyParameterGUid);
    68 	keyParameters->AddL(aPublicKey.X(), KDhKeyParameterXUid);
    69 
    70 	/*
    71 	 * create a CKey from the parameters
    72 	 */
    73 	CKey* newPublicKey = CKey::NewL(keyProperty, *keyParameters);
    74 
    75 	CleanupStack::PopAndDestroy(1, keyParameters);
    76 	return newPublicKey;
    77 	}
    78 
    79 CKey* KeyConverter::CreateKeyL(const CDSAPrivateKey& aPrivateKey)
    80 	{
    81 	TKeyProperty keyProperty = {KDsaSignerUid, KNullUid, KDsaPrivateKeyUid, KNonEmbeddedKeyUid};
    82 
    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);
    88 		
    89 	CKey* newPrivateKey = CKey::NewL(keyProperty, *keyParameters);
    90 	CleanupStack::PopAndDestroy(keyParameters);
    91 	return newPrivateKey;
    92 	}
    93 	
    94 CKey* KeyConverter::CreateKeyL(const CDSAPublicKey& aPublicKey)
    95 	{
    96 	TKeyProperty keyProperty = {KDsaSignerUid, KNullUid, KDsaPublicKeyUid, KNonEmbeddedKeyUid};
    97 
    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);
   103 
   104 	CKey* newPublicKey = CKey::NewL(keyProperty, *keyParameters);
   105 	CleanupStack::PopAndDestroy(keyParameters);
   106 	return newPublicKey;
   107 	}
   108 
   109 // RSA convertors ///////////////////////////////////////////////////////////////////////////
   110 CKey* KeyConverter::CreateKeyL(const CRSAPrivateKeyCRT& aPrivateKey)
   111 	{
   112 	TKeyProperty keyProperty = {KRsaCipherUid,
   113 		NULL,
   114 		KRsaPrivateKeyCRTUid,
   115 		KNonEmbeddedKeyUid };
   116 
   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);
   124 		
   125 	CKey* newPrivateKey = CKey::NewL(keyProperty, *keyParameters);
   126 	CleanupStack::PopAndDestroy(keyParameters);
   127 	return newPrivateKey;
   128 	}
   129 	
   130 CKey* KeyConverter::CreateKeyL(const CRSAPrivateKeyStandard& aPrivateKey)
   131 	{
   132 	TKeyProperty keyProperty = {KRsaCipherUid,
   133 		NULL,
   134 		KRsaPrivateKeyStandardUid,
   135 		KNonEmbeddedKeyUid };
   136 
   137 	CCryptoParams* keyParameters = CCryptoParams::NewLC();
   138 	keyParameters->AddL(aPrivateKey.N(), KRsaKeyParameterNUid);
   139 	keyParameters->AddL(aPrivateKey.D(), KRsaKeyParameterDUid);
   140 		
   141 	CKey* newPrivateKey = CKey::NewL(keyProperty, *keyParameters);
   142 	CleanupStack::PopAndDestroy(keyParameters);
   143 	return newPrivateKey;
   144 	}
   145 	
   146 CKey* KeyConverter::CreateKeyL(const CRSAPublicKey& aPublicKey)
   147 	{
   148 	TKeyProperty keyProperty = {KRsaCipherUid,
   149 		NULL,
   150 		KRsaPublicKeyUid,
   151 		KNonEmbeddedKeyUid };
   152 
   153 	CCryptoParams* keyParameters = CCryptoParams::NewLC();	
   154 	keyParameters->AddL(aPublicKey.N(), KRsaKeyParameterNUid);
   155 	keyParameters->AddL(aPublicKey.E(), KRsaKeyParameterEUid);
   156 
   157 	CKey* newPublicKey = CKey::NewL(keyProperty, *keyParameters);
   158 	CleanupStack::PopAndDestroy(keyParameters);
   159 	return newPublicKey;
   160 	}
   161 
   162 CKey* KeyConverter::CreateKeyL(const CRSAPrivateKey& aPrivateKey)
   163 	{
   164 	// Determine which type of private key
   165 	if (aPrivateKey.PrivateKeyType() == EStandard)
   166 		{
   167 		const CRSAPrivateKeyStandard* stdKey = static_cast<const CRSAPrivateKeyStandard*>(&aPrivateKey);
   168 		return KeyConverter::CreateKeyL(*stdKey);
   169 		}
   170 	else if (aPrivateKey.PrivateKeyType() == EStandardCRT)
   171 		{
   172 		const CRSAPrivateKeyCRT* crtKey = static_cast<const CRSAPrivateKeyCRT*>(&aPrivateKey);
   173 		return KeyConverter::CreateKeyL(*crtKey);
   174 		}
   175 	return NULL;  // Keep the compiler happy
   176 	}
   177 
   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
   183 #endif
   184 
   185 CKeyPair* KeyConverter::CreateKeyPairL(const CDSAKeyPair& /*aKeyPair*/)
   186 	{
   187 	return NULL;	
   188 	}
   189 
   190 CKeyPair* KeyConverter::CreateKeyPairL(const CRSAKeyPair& aKeyPair)
   191 	{
   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);
   198 	return newKeyPair;
   199 	}
   200 
   201 CKeyPair* KeyConverter::CreateKeyPairL(const CDHKeyPair& aKeyPair)
   202 	{
   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);
   209 	return newKeyPair;
   210 	}