|
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 CRSAKeyGenerator class |
|
16 * INCLUDES |
|
17 * |
|
18 */ |
|
19 |
|
20 |
|
21 #include "RSAKeyGenerator.h" |
|
22 #include <iostream.h> |
|
23 |
|
24 // =========================================================================== |
|
25 // Construction/Destruction |
|
26 // =========================================================================== |
|
27 |
|
28 CRSAKeyGenerator::CRSAKeyGenerator() |
|
29 { |
|
30 |
|
31 } |
|
32 |
|
33 CRSAKeyGenerator::~CRSAKeyGenerator() |
|
34 { |
|
35 |
|
36 } |
|
37 |
|
38 int CRSAKeyGenerator::Generate() |
|
39 //Generate an RSA key with pre-determined length |
|
40 { |
|
41 RSA* pRSAKey = NULL; |
|
42 FILE* fp = NULL; |
|
43 LPSTR pbPassword = NULL; |
|
44 const _TCHAR* pPrivKeyFile = NULL; |
|
45 |
|
46 int retVal = FAIL; |
|
47 int retFunc = FAIL; |
|
48 |
|
49 pPrivKeyFile = GetPrivateKeyFile(); |
|
50 if(!pPrivKeyFile) |
|
51 { |
|
52 PrintErrorInfo("Bad parameter error!", EGeneric, constparams); |
|
53 return 0; |
|
54 } |
|
55 |
|
56 OPENSSL_add_all_algorithms_conf(); |
|
57 ERR_load_crypto_strings(); |
|
58 |
|
59 int keyLength = 0; |
|
60 keyLength = GetKeyLength(); |
|
61 try |
|
62 { |
|
63 //Generate RSA key |
|
64 _tprintf(_T("\nGenerating RSA key .")); |
|
65 do |
|
66 { |
|
67 pRSAKey = RSA_generate_key(keyLength, RSA_F4, RSAKeyStatus, NULL); |
|
68 |
|
69 } |
|
70 while((retVal = RSA_check_key(pRSAKey)) == 0); // if return is 0, the key should be regenerated! |
|
71 |
|
72 if(retVal != 1) |
|
73 { |
|
74 PrintErrorInfo("RSA key generation failed!", EOPENSSL, constparams); |
|
75 throw EOPENSSL; |
|
76 } |
|
77 |
|
78 _tprintf(_T("Generated!\n")); |
|
79 //Create a key file |
|
80 fp = _tfopen(pPrivKeyFile, _T("w")); |
|
81 |
|
82 if(!fp) |
|
83 { |
|
84 PrintErrorInfo("Error creating key file!", EGeneric, constparams); |
|
85 throw EGeneric; |
|
86 } |
|
87 |
|
88 //Write generated DSA key to the key file |
|
89 if(m_bPassword) |
|
90 { |
|
91 DWORD len = 0; |
|
92 len = _tcslen(GetPassword()); |
|
93 pbPassword = MakeMBCSString(GetPassword(), CP_UTF8, len); |
|
94 retVal = PEM_write_RSAPrivateKey(fp, pRSAKey, EVP_des_ede3_cbc(), (unsigned char *) pbPassword, len, NULL, NULL); |
|
95 delete pbPassword; |
|
96 } |
|
97 else if(m_bAsk) |
|
98 { |
|
99 retVal = PEM_write_RSAPrivateKey(fp, pRSAKey, EVP_des_ede3_cbc(), NULL, 0, NULL, NULL); |
|
100 } |
|
101 |
|
102 if(!retVal) |
|
103 { |
|
104 PrintErrorInfo("Error writing to key file", EOPENSSL, constparams); |
|
105 throw EOPENSSL; |
|
106 } |
|
107 |
|
108 //Free variables |
|
109 RSA_free(pRSAKey); |
|
110 fclose(fp); |
|
111 |
|
112 //Get command prompt handle |
|
113 HANDLE hndl = 0; |
|
114 DWORD bytesWritten; |
|
115 hndl = GetStdHandle(STD_OUTPUT_HANDLE); |
|
116 _tprintf(_T("\nCreated key: ")); |
|
117 WriteConsole(hndl, pPrivKeyFile, wcslen(pPrivKeyFile), &bytesWritten, 0); |
|
118 retFunc = SUCCESS; |
|
119 |
|
120 } |
|
121 catch (...) |
|
122 { |
|
123 //Delete rsa params |
|
124 if(pRSAKey != NULL) |
|
125 { |
|
126 RSA_free(pRSAKey); |
|
127 } |
|
128 |
|
129 } |
|
130 return retFunc; |
|
131 } |
|
132 |
|
133 |
|
134 //RSA key generation callback function |
|
135 static void RSAKeyStatus(int aCode, int aArg, void *aCbArg) |
|
136 { |
|
137 |
|
138 if (aCode == 1 && aArg && !(aArg % 3)) |
|
139 { |
|
140 printf("."); |
|
141 } |
|
142 return; |
|
143 } |