secureswitools/makekeys/src/PARSECMD.CPP
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 1999-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 * handles parsing of makekeys command line args
       
    16 * INCLUDES
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 #include <windows.h>
       
    22 #include <tchar.h>
       
    23 
       
    24 #include "ParseCMD.h"
       
    25 #include "openssllicense.h"
       
    26 
       
    27 //#include <winCertificateGenerator.h>
       
    28 
       
    29 // ===========================================================================
       
    30 // CParseCmd
       
    31 // Responsable for processing and maintaining the command line options
       
    32 // ===========================================================================
       
    33 
       
    34 CParseCmd::CParseCmd()
       
    35 :m_dwOptions(0L),
       
    36 m_fShowSyntax(FALSE)
       
    37 	{
       
    38 	m_dwKeyLen = DEFAULTKEYLEN;
       
    39 	m_dwCertExpiryInDays = DEFAULTCERTEXPIRYDAYS;
       
    40 	m_bPassword = FALSE;
       
    41 	m_CMDKey = EDSACipher;
       
    42 	m_bVerbose = FALSE;
       
    43 	}
       
    44 
       
    45 BOOL CParseCmd::ParseCommandLine(const int argc, const _TCHAR *argv[], Mode* aApp)
       
    46 // Purpose  : Proceses the command line, and options
       
    47 // Inputs   : argc, argv - command line as passed to the process
       
    48 	{
       
    49 
       
    50 	// Test the number of arguments
       
    51 	if(argc < 2) throw ErrInsufficientArgs;
       
    52 	
       
    53 	int wCount = 1;
       
    54 	int dname = 0;
       
    55 	int founddname = 0;
       
    56 	int command_count = 0;
       
    57 	
       
    58 	int MBCSstrlen;	//used to store the length of MBCS strings 
       
    59 	
       
    60 	// walk through the argv[] list, picking out -parameters and parameter strings.
       
    61 	// Also sets mode of operation of the app
       
    62 	while(wCount < argc)
       
    63 		{
       
    64 
       
    65 		//check we don't have a NULL string here
       
    66 		if(argv[wCount][1] == '\0')
       
    67 			throw ErrBadCommandFlag;
       
    68 
       
    69 
       
    70 		/* Uncomment below for standalone key generation support
       
    71 		//parse command list for operational parameters
       
    72 		if (_tcscmp("-keys", argv[wCount]) == 0)	
       
    73 			{
       
    74 			*aApp = EKeys;
       
    75 			//wcscpy(m_privateKeyFile, argv[argc - 2]);
       
    76 			wcscpy(m_privateKeyFile, argv[argc - 1]);
       
    77 			}
       
    78 			
       
    79 		else
       
    80 		*/
       
    81 		
       
    82 		//display OpenSSL copyright notice.
       
    83 		if (_tcsicmp(_T("-i"), argv[wCount]) == 0)
       
    84 			{
       
    85 			for (int index = 0; index < (sizeof(openSSLLicenseString)/sizeof(openSSLLicenseString[0])); ++index)
       
    86 				{
       
    87 				std::cout << openSSLLicenseString [index] << std::endl ;
       
    88 				}
       
    89 			break;
       
    90 			}
       
    91 
       
    92 		if (_tcsicmp(_T("-cert"), argv[wCount]) == 0)
       
    93 			{
       
    94 			if ((*aApp == ECert) || (*aApp == EReq))
       
    95 				{
       
    96 				throw ErrMalformedCommand;
       
    97 				}
       
    98 			else
       
    99 				{
       
   100 				*aApp = ECert;
       
   101 				}
       
   102 				
       
   103 			if (argc < MIN_CERT_ARGS) throw ErrInsufficientArgs;
       
   104 			
       
   105 			}
       
   106 
       
   107 			else if (_tcsicmp(_T("-req"), argv[wCount]) == 0)
       
   108 				{
       
   109 				if ((*aApp == ECert) || (*aApp == EReq))
       
   110 					{
       
   111 					throw ErrMalformedCommand;
       
   112 					}
       
   113 				else
       
   114 					{
       
   115 					*aApp = EReq;
       
   116 					}
       
   117 						
       
   118 				if (argc < MIN_REQ_ARGS) throw ErrBadReqCommand;
       
   119 			
       
   120 				founddname = 1;							
       
   121 			}
       
   122 
       
   123 #ifdef OPTIONAL
       
   124 			else if (_tcsicmp(_T("-chain"), argv[wCount]) == 0)
       
   125 				{
       
   126 				*aApp = EChain;
       
   127 				command_count++;
       
   128 				}
       
   129 #endif
       
   130 			else if (_tcsicmp(_T("-view"), argv[wCount]) == 0)
       
   131 				{
       
   132 				
       
   133 				if (argc == MIN_VIEW_ARGS)
       
   134 					{
       
   135 					CCertificateGenerator* pView = new CCertificateGenerator;
       
   136 					if (!pView) throw ErrCantViewCertificate;
       
   137 
       
   138 					pView->View(argv[wCount + 1]);
       
   139 
       
   140 					delete pView;
       
   141 					}
       
   142 					else
       
   143 						throw ErrMalformedCommand;
       
   144 
       
   145 				}
       
   146 
       
   147 			else if (_tcsicmp(_T("-password"), argv[wCount]) == 0)
       
   148 				{
       
   149 				m_bPassword = true;
       
   150 				command_count+=2;
       
   151 				
       
   152 				if (wCount < argc - 1)
       
   153 					{
       
   154 					_tcscpy(m_password, argv[wCount + 1]);
       
   155 					if(!(_tcslen(m_password) >=4) )
       
   156 						{
       
   157 						_tprintf(_T("Error: Invalid Password\n"));
       
   158 						throw ErrInvalidPassword;
       
   159 						}
       
   160 					}
       
   161 				else
       
   162 					throw ErrInsufficientArgs;
       
   163 				
       
   164 				}
       
   165 
       
   166 #ifndef NORSA
       
   167 			else if (_tcsicmp(_T("-rsa"), argv[wCount]) == 0)
       
   168 				{
       
   169 				m_CMDKey = ERSACipher;
       
   170 				command_count++;
       
   171 				}
       
   172 #endif
       
   173 		
       
   174 			else if (_tcsicmp(_T("-dsa"), argv[wCount]) == 0)
       
   175 				{
       
   176 				m_CMDKey = EDSACipher;
       
   177 				command_count++;
       
   178 				}
       
   179 		
       
   180 			else if(_tcsicmp(_T("-dname"), argv[wCount]) == 0)
       
   181 				{
       
   182 				if(wCount < argc - 1)
       
   183 					{
       
   184 					//handle MBCS correctly (we are assuming that the parameter after -dname IS the dname string
       
   185 					MBCSstrlen = _tcslen(argv[wCount + 1]) * sizeof(TCHAR);
       
   186 					_tcscpy(m_dname, argv[wCount + 1]);
       
   187 					dname = 1;
       
   188 					}
       
   189 					else
       
   190 						throw ErrInsufficientArgs;
       
   191 				}
       
   192 			else if(_tcsicmp(_T("-len"), argv[wCount]) == 0)
       
   193 				{
       
   194 				//handle MBCS correctly
       
   195 				if(wCount < argc - 1)
       
   196 					{
       
   197 					_tcscpy(m_KeyLenStr, argv[wCount + 1]);
       
   198 					_stscanf(m_KeyLenStr, _T("%d"), &m_dwKeyLen);
       
   199 				
       
   200 					command_count+=2;	//len takes an additional parameter, so bump by 2
       
   201 			
       
   202 					if (m_dwKeyLen < 512 || m_dwKeyLen > 4096)
       
   203 						{
       
   204 						_tprintf(_T("Error: key length should be between 512 and 4096\n"));
       
   205 						throw ErrInsufficientArgs;
       
   206 						}
       
   207 					}
       
   208 				else
       
   209 					throw ErrInsufficientArgs;
       
   210 				}
       
   211 
       
   212 			else if (_tcsicmp(_T("-expdays") , argv[wCount]) == 0 )
       
   213 			{
       
   214 			if(wCount < argc - 1)
       
   215 				{
       
   216 				_tcscpy(m_CertExpiryInDaysStr, argv[wCount + 1]);
       
   217 				_stscanf(m_CertExpiryInDaysStr, _T("%d"), &m_dwCertExpiryInDays);
       
   218 
       
   219 				command_count+=2;	//expiry days takes an additional parameter, so command_count+2
       
   220 				}
       
   221 
       
   222 			else
       
   223 				{
       
   224 				throw ErrInsufficientArgs;
       
   225 				}
       
   226 			}
       
   227 
       
   228 			else if(_tcsicmp(_T("-v"), argv[wCount]) == 0)
       
   229 				{
       
   230 				SetVerbose(TRUE);
       
   231 				command_count++;	//-v does not take args, so bump by 1
       
   232 				}
       
   233 			else if(argv[wCount][0] == '-')
       
   234 			//if we get here we can't parse the option chosen
       
   235 				{
       
   236 				_tprintf(_T("Error: unknown command option %s\n"), argv[wCount]);
       
   237 				throw ErrUnknownCommandFlag;		
       
   238 				}
       
   239 
       
   240 			wCount++;
       
   241 		}
       
   242 
       
   243 	//distinguished names are not optional, so throw up a message
       
   244 	if (!dname && founddname)
       
   245 		{
       
   246 		_tprintf(_T("Error: Missing \"-dname\" command\n"));
       
   247 		throw ErrInsufficientArgs;
       
   248 		}
       
   249 
       
   250 
       
   251 	//check to see if all the optional commands have the correct number of arguments
       
   252 	if (((command_count + MIN_REQ_ARGS > argc) && (*aApp == EReq)) ||
       
   253 		((command_count + MIN_CERT_ARGS > argc) && (*aApp == ECert)))
       
   254 
       
   255 		{
       
   256 		printf("Error: Command Line malformed - too few arguements\n");
       
   257 		throw ErrInsufficientArgs;
       
   258 		}
       
   259 
       
   260 	if (*aApp == EReq)
       
   261 		{
       
   262  		_tcscpy(m_privateKeyFile, argv[command_count + 4]);
       
   263  		_tcscpy(m_publicKeyFile, argv[command_count + 5]);
       
   264  		_tcscpy(m_requestFile, argv[command_count + 6]);
       
   265 		
       
   266 
       
   267 		if (wCount >= (command_count+2) + MIN_REQ_ARGS - 1)
       
   268 			{
       
   269 			printf("Error: Too many arguments for -req command\n");
       
   270 			throw ErrTooManyArgs;
       
   271 			}
       
   272 		}
       
   273 
       
   274 	if (*aApp == ECert)
       
   275 		{
       
   276 		_tcscpy(m_privateKeyFile, argv[command_count + 4]);
       
   277 		_tcscpy(m_publicKeyFile, argv[command_count + 5]);
       
   278 		
       
   279 		if (wCount >= (command_count+2) + MIN_CERT_ARGS - 1)
       
   280 			{
       
   281 			_tprintf(_T("Error: too many arguments for -cert command\n"));
       
   282 			throw ErrTooManyArgs;
       
   283 			}
       
   284 		}
       
   285 	}
       
   286 
       
   287 int CParseCmd::GetCMDKeyLen()
       
   288 	{
       
   289 	return m_dwKeyLen;
       
   290 	}
       
   291 
       
   292 int CParseCmd::GetCMDCertExpiryInDays()
       
   293 	{
       
   294 	return m_dwCertExpiryInDays;
       
   295 	}
       
   296 
       
   297 _TCHAR* CParseCmd::GetPrivateName()
       
   298 	{
       
   299 	return m_privateKeyFile;
       
   300 	}
       
   301 
       
   302 _TCHAR* CParseCmd::GetPublicName()
       
   303 	{
       
   304 	return m_publicKeyFile;
       
   305 	}
       
   306 
       
   307 _TCHAR* CParseCmd::GetRequestName()
       
   308 	{
       
   309 	return m_requestFile;
       
   310 	}
       
   311 
       
   312 _TCHAR* CParseCmd::GetDNameString()
       
   313 	{
       
   314 	return m_dname;
       
   315 	}
       
   316 
       
   317 
       
   318 
       
   319 
       
   320 _TCHAR* CParseCmd::GetPassword()
       
   321 	{
       
   322 	return m_password;
       
   323 	}
       
   324 
       
   325 TKeyType CParseCmd::GetKeyType()
       
   326 	{
       
   327 	return m_CMDKey;
       
   328 	}
       
   329 
       
   330 bool CParseCmd::GetPasswordEnabled()
       
   331 	{
       
   332 	return m_bPassword;	
       
   333 	}
       
   334 
       
   335 void CParseCmd::SetVerbose(const bool bVerbose)
       
   336 	{
       
   337 	m_bVerbose = bVerbose; 
       
   338 
       
   339 	}
       
   340 
       
   341 bool CParseCmd::GetVerbose()
       
   342 	{
       
   343 	return m_bVerbose; 
       
   344 
       
   345 	}