|
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 * Implementation of the CKeyGenerator class |
|
16 * INCLUDES |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 #include "KeyGenerator.h" |
|
22 |
|
23 |
|
24 // =========================================================================== |
|
25 // Construction/Destruction |
|
26 // =========================================================================== |
|
27 |
|
28 CKeyGenerator::CKeyGenerator() |
|
29 { |
|
30 m_nKeyLength = DEFAULT_KEY_LENGTH; |
|
31 m_Cipher = EDSACipher; |
|
32 m_bPassword = FALSE; |
|
33 m_bAsk = FALSE; |
|
34 m_bVerbose = FALSE; |
|
35 memset(m_PrivateKeyFile,0, MAXLEN); |
|
36 memset(m_sPassword, 0, MAXLEN); |
|
37 } |
|
38 |
|
39 CKeyGenerator::~CKeyGenerator() |
|
40 { |
|
41 |
|
42 } |
|
43 |
|
44 const int CKeyGenerator::GetKeyLength() const |
|
45 { |
|
46 return m_nKeyLength; |
|
47 } |
|
48 |
|
49 void CKeyGenerator::SetKeyLength(int aKeyLen) |
|
50 { |
|
51 m_nKeyLength = aKeyLen; |
|
52 } |
|
53 |
|
54 const TCHAR* CKeyGenerator::GetPrivateKeyFile() const |
|
55 { |
|
56 return m_PrivateKeyFile; |
|
57 } |
|
58 |
|
59 void CKeyGenerator::SetPrivateKeyFile(_TCHAR* aPk) |
|
60 { |
|
61 _tcscpy(m_PrivateKeyFile, aPk); |
|
62 } |
|
63 |
|
64 void CKeyGenerator::SetPassword(_TCHAR* aPw) |
|
65 { |
|
66 _tcscpy(m_sPassword, aPw); |
|
67 } |
|
68 |
|
69 void CKeyGenerator::SetPasswordRequirement(bool aPwReq) |
|
70 { |
|
71 m_bPassword = aPwReq; |
|
72 } |
|
73 |
|
74 int CKeyGenerator::GenerateSeed(int aLength, unsigned char** aBuf) |
|
75 //Generate a seed data for DSA key generation |
|
76 { |
|
77 HCRYPTPROV hProv = 0; |
|
78 |
|
79 *aBuf = (unsigned char *)malloc(aLength * sizeof(unsigned char)); |
|
80 |
|
81 // Get handle to default key context |
|
82 if(!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0)) |
|
83 { |
|
84 if(!CryptAcquireContext(&hProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) |
|
85 { |
|
86 if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0)) |
|
87 { |
|
88 if(!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET)) |
|
89 { |
|
90 PrintErrorInfo("Error acquiring context!", EMSCrypto, constparams); |
|
91 return FAIL; |
|
92 } |
|
93 } |
|
94 } |
|
95 } |
|
96 |
|
97 //Generate a random number |
|
98 if(!CryptGenRandom(hProv, aLength, *aBuf)) |
|
99 { |
|
100 PrintErrorInfo("Error generating random number!", EMSCrypto, constparams); |
|
101 return FAIL; |
|
102 } |
|
103 |
|
104 return SUCCESS; |
|
105 } |
|
106 |
|
107 const _TCHAR* CKeyGenerator::GetPassword() |
|
108 { |
|
109 return m_sPassword; |
|
110 } |
|
111 |
|
112 void CKeyGenerator::SetAskPassword(bool aAsk) |
|
113 { |
|
114 m_bAsk = aAsk; |
|
115 } |
|
116 |
|
117 void CKeyGenerator::SetVerbose(bool aVerbose) |
|
118 { |
|
119 m_bVerbose = aVerbose; |
|
120 } |
|
121 |
|
122 void CKeyGenerator::PrintErrorInfo(char* aMsg, TErrType aEntType, const char* aFileName, int aLineNum) |
|
123 { |
|
124 PrintError(aMsg, aEntType, m_bVerbose, aFileName, aLineNum); |
|
125 } |