secureswitools/makekeys/src/UTILS.CPP
changeset 0 ba25891c3a9e
equal deleted inserted replaced
-1:000000000000 0:ba25891c3a9e
       
     1 /*
       
     2 * Copyright (c) 1997-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 * Various utility functions
       
    16 * INCLUDES
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 #include "utils.h"
       
    22 #include <stdio.h>
       
    23 // ===========================================================================
       
    24 // GLOBAL UTILS FUNCTIONS
       
    25 // ===========================================================================
       
    26 
       
    27 LPSTR MakeMBCSString(LPCWSTR uniStr, UINT codePage, DWORD& length)
       
    28 // Convert a UNICODE string to a multi-byte string
       
    29 	{
       
    30 	LPSTR mbStr;
       
    31 	// get num unicode chars required
       
    32 	DWORD len = WideCharToMultiByte(codePage, 0, uniStr, length, NULL, 0, NULL, NULL);
       
    33 	mbStr = new CHAR[len+1];
       
    34 	if (!mbStr) throw ErrNotEnoughMemory;
       
    35 	// convert
       
    36 	WideCharToMultiByte(codePage, 0, uniStr, length, mbStr, len, NULL, NULL);
       
    37 	mbStr[len]='\0';
       
    38 	length=len;
       
    39 
       
    40 	return mbStr;
       
    41 	}
       
    42 
       
    43 LPWSTR  MakeUnicodeString(LPSTR mbcsStr, UINT codePage)
       
    44 // Convert a multi-byte string to a UNICODE string
       
    45 {
       
    46 	int retVal=0, len;
       
    47 	LPWSTR uniStr;
       
    48 
       
    49 	if(mbcsStr == NULL) return NULL;
       
    50 
       
    51 	len = MultiByteToWideChar(codePage, 0, mbcsStr, strlen(mbcsStr)+1, NULL, 0);
       
    52 	uniStr = new WCHAR[len+1];
       
    53 	retVal = MultiByteToWideChar(codePage, 0, mbcsStr, strlen(mbcsStr)+1, uniStr, len);
       
    54 
       
    55 	return uniStr;
       
    56 }
       
    57 
       
    58 void PrintError(char* aMsg, TErrType aErrType, bool aVerbose, const char* aFileName, int aLineNum)
       
    59 //Print error messages in the generation of a key or certificate
       
    60 {
       
    61    	if(!aMsg)
       
    62 		return;
       
    63 
       
    64 	fprintf(stderr, "\n** %s ", aMsg);
       
    65 
       
    66 	if(aVerbose)
       
    67 	{	
       
    68 		if(aErrType == EOPENSSL)
       
    69 		{
       
    70 			fprintf(stderr, " %s:%i\n", aFileName, aLineNum);
       
    71 			ERR_print_errors_fp(stderr);
       
    72 		
       
    73 		}else if(aErrType == EMSCrypto)
       
    74 		{
       
    75 			LPVOID lpMsgBuf = NULL;
       
    76    
       
    77 			FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
       
    78 						   | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), 0, 
       
    79 						   (LPTSTR)&lpMsgBuf, 0, NULL);
       
    80      
       
    81 			fprintf(stderr, "%s:%i %s\n", aFileName, aLineNum, (LPCTSTR)lpMsgBuf);
       
    82 
       
    83 			// Free the buffer.
       
    84 			LocalFree( lpMsgBuf ); 
       
    85 
       
    86 		} else
       
    87 		{
       
    88 			fprintf(stderr, "%s:%i\n", aFileName, aLineNum);
       
    89 		}
       
    90 	}
       
    91 }
       
    92 
       
    93 // ===========================================================================
       
    94 // These functions format a DN string
       
    95 // ===========================================================================
       
    96 
       
    97 // ===========================================================================
       
    98 // CONSTANTS
       
    99 // ===========================================================================
       
   100 
       
   101 _TCHAR TSN_commonName[]				= _T("CN");
       
   102 _TCHAR TSN_countryName[]			= _T("C");
       
   103 _TCHAR TSN_countryNameOld[]			= _T("CO");
       
   104 _TCHAR TSN_localityName[]			= _T("L");
       
   105 _TCHAR TSN_stateOrProvinceName[]	= _T("ST");
       
   106 _TCHAR TSN_organizationName[]		= _T("O");
       
   107 _TCHAR TSN_organizationNameOld[]	= _T("OR");
       
   108 _TCHAR TSN_organizationalUnitName[]	= _T("OU");
       
   109 _TCHAR TSN_email[]					= _T("EM");
       
   110 
       
   111 void DoFormatted(_TCHAR* aDN, entry_pack** aEntPack)
       
   112 //Given Distinguished Name string is parsed and filled into entry pack structure
       
   113 	{
       
   114 	
       
   115 	_TCHAR* pToken  = NULL;
       
   116 	_TCHAR* pLstr   = NULL;
       
   117 	_TCHAR* pRstr   = NULL;
       
   118     char*   pKeyWord = NULL;
       
   119 
       
   120 	*aEntPack = (struct entry_pack*)malloc(sizeof(struct entry_pack ));
       
   121 	InitEntryPack(aEntPack);
       
   122 
       
   123 	_TCHAR sep[] = _T("=");
       
   124 	pToken = _tcstok(aDN, sep);
       
   125 	
       
   126 	if(!(pKeyWord = IsValid(pToken)))
       
   127 		{
       
   128 		return;
       
   129 		}
       
   130 	
       
   131 	int i = 0;
       
   132 	strcpy((*aEntPack)->entries[i].key, pKeyWord);
       
   133 	pToken = _tcstok(NULL, sep);
       
   134 
       
   135 	bool bMayBadParam = false;
       
   136 	int  retVal		  = 0;
       
   137 
       
   138 	while (pToken != NULL)
       
   139 		{	
       
   140 		if(bMayBadParam)
       
   141 			{
       
   142 			i = 0;
       
   143 			break;
       
   144 			}
       
   145 
       
   146 		retVal = SplitString(pToken, &pLstr, &pRstr);
       
   147 		
       
   148 		if(retVal)
       
   149 			{	
       
   150 			_tcscpy((*aEntPack)->entries[i++].value, RemoveSpaces(pLstr));
       
   151 			if(pRstr)
       
   152 				{
       
   153 				strcpy((*aEntPack)->entries[i].key, IsValid(pRstr));
       
   154 				}
       
   155 			}
       
   156 		else
       
   157 			{	
       
   158 			_tcscpy((*aEntPack)->entries[i++].value, RemoveSpaces(pLstr));
       
   159 			
       
   160 			if(!pRstr)
       
   161 				{	
       
   162 				bMayBadParam = true;
       
   163 				}
       
   164 			}
       
   165 		
       
   166 		pToken = _tcstok(NULL, sep);
       
   167 		}
       
   168 	
       
   169 		(*aEntPack)->num = i;
       
   170 
       
   171 	}
       
   172 
       
   173 
       
   174 _TCHAR* RemoveSpaces(_TCHAR* aStr)
       
   175 //Removes unnecessary spaces from a given string
       
   176 	{
       
   177 
       
   178 	LPCTSTR lpszTargetList = _T(" ");
       
   179 	LPTSTR  lpsz = aStr;
       
   180 	LPTSTR  lpszLast = NULL;
       
   181 
       
   182 	//Trim Left
       
   183 	while (*lpsz != '\0')
       
   184 		{
       
   185 		if (_tcschr(lpszTargetList, *lpsz) == NULL)
       
   186 			break;
       
   187 		lpsz = _tcsinc(lpsz);
       
   188 		}
       
   189 
       
   190 	if (lpsz != aStr)
       
   191 		{
       
   192 		int nDataLength = _tcslen(aStr) - (lpsz - aStr);
       
   193 		wmemmove(aStr, lpsz, (nDataLength+1)*sizeof(TCHAR));
       
   194 		}
       
   195 
       
   196 	lpsz = aStr;
       
   197 	lpszLast = NULL;
       
   198 
       
   199 	//Trim Right
       
   200 	while (*lpsz != '\0')
       
   201 		{
       
   202 		if (_tcschr(lpszTargetList, *lpsz) != NULL)
       
   203 			{
       
   204 			if (lpszLast == NULL)
       
   205 				lpszLast = lpsz;
       
   206 			}
       
   207 		else
       
   208 			lpszLast = NULL;
       
   209 		lpsz = _tcsinc(lpsz);
       
   210 		}
       
   211 
       
   212 	if (lpszLast != NULL)
       
   213 		{
       
   214 		// truncate at left-most matching character
       
   215 		*lpszLast = '\0';
       
   216 		}
       
   217 	return aStr;
       
   218 	}
       
   219 
       
   220 char* IsValid(_TCHAR* aTk)
       
   221 //Checks whether a found string is a valid distinguished name key
       
   222 	{
       
   223 
       
   224 	if(!aTk)
       
   225 		return NULL;
       
   226 
       
   227 	if(!_tcscmp(aTk, TSN_commonName))
       
   228 		{
       
   229 		return LN_commonName;
       
   230 		}
       
   231 	else if(!_tcscmp(aTk, TSN_countryName) || !_tcscmp(aTk, TSN_countryNameOld))
       
   232 		{
       
   233 		return LN_countryName;
       
   234 		}
       
   235 	else if(!_tcscmp(aTk, TSN_localityName))
       
   236 		{
       
   237 		return LN_localityName;
       
   238 		}
       
   239 	else if(!_tcscmp(aTk, TSN_stateOrProvinceName))
       
   240 		{
       
   241 		return LN_stateOrProvinceName;
       
   242 		}
       
   243 	else if(!_tcscmp(aTk, TSN_organizationName) || !_tcscmp(aTk, TSN_organizationNameOld))
       
   244 		{
       
   245 		return LN_organizationName;
       
   246 		}
       
   247 	else if(!_tcscmp(aTk, TSN_organizationalUnitName))
       
   248 		{
       
   249 		return LN_organizationalUnitName;
       
   250 		}
       
   251 	else if(!_tcscmp(aTk, TSN_email))
       
   252 		{
       
   253 		return LN_pkcs9_emailAddress;
       
   254 		}
       
   255 	else
       
   256 		{
       
   257 		return NULL;
       
   258 		} 
       
   259 	
       
   260 	}
       
   261 
       
   262 
       
   263 int SplitString(_TCHAR* aToken, _TCHAR** aStrFirst, _TCHAR** aStrSecond)
       
   264 //A given string is divided in two parts if applicable. First part is DN value
       
   265 //second part is key value.
       
   266 	{
       
   267 	int i = 0;
       
   268 	_TCHAR *pTmp = NULL;
       
   269 
       
   270 	pTmp = aToken;
       
   271 	pTmp = pTmp + (_tcslen(pTmp)-2);
       
   272 
       
   273 	if(IsValid(pTmp) && (aToken[(pTmp - aToken)-1] == 32))
       
   274 		{
       
   275 		*aStrFirst = aToken;
       
   276 		aToken[_tcslen(aToken)-3] = '\0';
       
   277 		
       
   278 		*aStrSecond = pTmp;
       
   279 		} 
       
   280 	else 
       
   281 		{
       
   282 		pTmp = aToken;
       
   283 		pTmp = pTmp + (_tcslen(pTmp)-1);
       
   284 
       
   285 		if(IsValid(pTmp) && (aToken[(pTmp - aToken)-1] == 32))
       
   286 			{
       
   287 			*aStrFirst = aToken;
       
   288 			aToken[_tcslen(aToken)-2] = '\0';
       
   289 		
       
   290 			*aStrSecond = pTmp;
       
   291 			} 
       
   292 		else
       
   293 			{
       
   294 			*aStrFirst = aToken;
       
   295 			*aStrSecond = NULL;
       
   296 			return 0;
       
   297 			}
       
   298 		}
       
   299 
       
   300 	return 1;
       
   301 	}
       
   302 
       
   303 void InitEntryPack(struct entry_pack** aEntPack)
       
   304 //Initialize an entry pack structure pointer
       
   305 	{
       
   306 	memset(*aEntPack,0, sizeof(struct entry_pack));
       
   307 
       
   308 	(*aEntPack)->num = 0;
       
   309 	}
       
   310 
       
   311 
       
   312