secureswitools/makekeys/src/DSAKeyGenerator.cpp
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 2007-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 CDSAKeyGenerator class
       
    16 * INCLUDES
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 #include "DSAKeyGenerator.h"
       
    22 #include <iostream.h>
       
    23 
       
    24 // ===========================================================================
       
    25 // Construction/Destruction
       
    26 // ===========================================================================
       
    27 
       
    28 CDSAKeyGenerator::CDSAKeyGenerator()
       
    29 	{
       
    30 
       
    31 	}
       
    32 
       
    33 CDSAKeyGenerator::~CDSAKeyGenerator()
       
    34 	{
       
    35 
       
    36 	}
       
    37 
       
    38 int CDSAKeyGenerator::Generate()
       
    39 //Generate a DSA key with pre-determined length
       
    40 	{
       
    41 	unsigned char* pbSeed       = NULL; 
       
    42 	DSA*		   pDSAParams   = NULL;
       
    43 	FILE*		   fp		    = NULL;
       
    44 	LPSTR		   pbPassword   = NULL;
       
    45 	const _TCHAR*  pPrivKeyFile = NULL;
       
    46 	
       
    47 	int retVal  = FAIL;
       
    48 	int retFunc = FAIL;
       
    49 
       
    50 	pPrivKeyFile = GetPrivateKeyFile();
       
    51 	if(!pPrivKeyFile)
       
    52 		{
       
    53 		PrintErrorInfo("Bad parameter error!", EGeneric, constparams);
       
    54 		return 0;
       
    55 		}
       
    56 	
       
    57 	OPENSSL_add_all_algorithms_conf();
       
    58 	ERR_load_crypto_strings();
       
    59 
       
    60 	int dwKeyLength = 0;
       
    61 	dwKeyLength = GetKeyLength();
       
    62 	
       
    63 	try
       
    64 		{
       
    65 		retVal = GenerateSeed(dwKeyLength, &pbSeed);
       
    66 		if(retVal != SUCCESS)
       
    67 			{
       
    68 			throw EMSCrypto;
       
    69 			}
       
    70 
       
    71 		//Generate DSA params (p,q and g)
       
    72 		_tprintf(_T("\nGenerating DSA key ."));
       
    73 		pDSAParams = DSA_generate_parameters(dwKeyLength, pbSeed, dwKeyLength, NULL, NULL, DSAKeyStatus, NULL);
       
    74 		if(!pDSAParams)
       
    75 			{
       
    76 			PrintErrorInfo("Error generating DSA key params!", EOPENSSL, constparams);
       
    77 			throw EOPENSSL;
       
    78 			}
       
    79 		
       
    80 		//Generate DSA key
       
    81 		retVal = DSA_generate_key(pDSAParams);
       
    82 		if(!retVal)
       
    83 			{
       
    84 			PrintErrorInfo("DSA key generation failed!", EOPENSSL, constparams);
       
    85 			throw EOPENSSL;
       
    86 			}
       
    87 
       
    88 		_tprintf(_T("Generated!\n"));
       
    89 		//Create a key file
       
    90 		fp = _tfopen(pPrivKeyFile, _T("w"));
       
    91 
       
    92 		if(!fp)
       
    93 			{
       
    94 			PrintErrorInfo("Error creating key file!", EGeneric, constparams);
       
    95 			throw EOPENSSL;
       
    96 			}
       
    97 		
       
    98 		//Write generated DSA key to the key file
       
    99 		if(m_bPassword)
       
   100 			{
       
   101 			DWORD len = 0;
       
   102 			len = _tcslen(GetPassword());
       
   103 			pbPassword = MakeMBCSString(GetPassword(), CP_UTF8, len);
       
   104 			retVal = PEM_write_DSAPrivateKey(fp, pDSAParams, EVP_des_ede3_cbc(), (unsigned char *) pbPassword, len, NULL, NULL);
       
   105 			delete pbPassword;
       
   106 			}
       
   107 		else if(m_bAsk)
       
   108 			{
       
   109 			retVal = PEM_write_DSAPrivateKey(fp, pDSAParams, EVP_des_ede3_cbc(), NULL, 0, NULL, NULL);
       
   110 			}
       
   111 		else 
       
   112 			{
       
   113 			_tprintf(_T("\n"));
       
   114 			retVal = PEM_write_DSAPrivateKey(fp, pDSAParams, NULL , NULL, 0, NULL, NULL);
       
   115 			}
       
   116 
       
   117 		if(!retVal)
       
   118 			{
       
   119 			PrintErrorInfo("Error writing to key file", EOPENSSL, constparams);
       
   120 			throw EOPENSSL;
       
   121 			}
       
   122 		//Free variables
       
   123 		DSA_free(pDSAParams);
       
   124 		fclose(fp);
       
   125 		SYMBIAN_FREE_MEM(pbSeed);
       
   126 
       
   127 		//Get command prompt handle
       
   128 		HANDLE hndl = 0;
       
   129 		hndl = GetStdHandle(STD_OUTPUT_HANDLE);
       
   130 		_tprintf(_T("\nCreated key: "));
       
   131 		DWORD bytesWritten;
       
   132 		WriteConsole(hndl, pPrivKeyFile, wcslen(pPrivKeyFile), &bytesWritten, NULL);
       
   133 		retFunc = SUCCESS;	
       
   134 
       
   135 		}
       
   136 	catch (...)
       
   137 		{
       
   138 		//Delete dsa params
       
   139 		if(pDSAParams)
       
   140 			{
       
   141 			DSA_free(pDSAParams);
       
   142 			}
       
   143 		if (fp)
       
   144 			{
       
   145 			fclose(fp);
       
   146 			}
       
   147 		SYMBIAN_FREE_MEM(pbSeed);
       
   148 		}
       
   149 
       
   150 	return retFunc;
       
   151 }
       
   152 
       
   153 //DSA key generation callback function
       
   154 static void DSAKeyStatus(int aCode, int aArg, void* aCbArg)
       
   155 	{
       
   156 	
       
   157 	if (aCode == 1 && aArg && !(aArg % 3))
       
   158 		{
       
   159 		printf(".");
       
   160 		}
       
   161 	return;
       
   162 	}