1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 //-------------------------------------------------------------------------------------------------- 18 // 19 // Module Name:main.c 20 // 21 // General Description: This file contains the main function for the SyncML factory boot strap credential generator tool 22 // 23 //-------------------------------------------------------------------------------------------------- 24 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <ctype.h> 28 #include <time.h> 29 #include "xptport.h" 30 #include "xpttypes.h" 31 #include "xpt-b64.h" 32 #include "md5.h" 33 #define MD5_HASH_LENGTH 16 34 #define ENCODED_MD5_CRED_LENGTH (MD5_HASH_LENGTH + (MD5_HASH_LENGTH/2) + 1) 35 36 #define getch getchar 37 38 void SyncML_DM_CombineCredDataAndIMEI(char *p_CreadData_IMEI,const char *p_CreadData,const char *p_IMEI); 39 void SyncML_DM_BuildB64EncodedMD5_Factory_Bootstrap_CredData(char **pp_credential_data,const char *p_CredData,const char *p_IMEI ); 40 void SyncML_DM_BuildB64EncodedCredData_Factory_Bootstrap(char **pp_credential_data,const char *pCread_Input); 41 void user_help(void); 42 43 const char DM_STR_SLASH[] = "/"; 44 45 /*================================================================================================== 46 47 FUNCTION:SyncML_DM_BuildB64EncodedMD5_Factory_Bootstrap_CredData 48 49 DESCRIPTION: 50 This function builds the B64 encoded MD5 credential information. 51 The credential string is credential data:IMEI number . That string is then b64 encoded. 52 53 ARGUMENTS PASSED: 54 55 REFERENCE ARGUMENTS PASSED: 56 char **pp_credential_data - Output variable containing the encoded credential data 57 const char *p_CredData - credential_data . 58 const char *p_IMEI - Phone IMEI number. 59 60 61 RETURN VALUE: 62 N/A 63 64 PRE-CONDITIONS: 65 66 POST-CONDITIONS: 67 68 ==================================================================================================*/ 69 70 71 72 void SyncML_DM_BuildB64EncodedMD5_Factory_Bootstrap_CredData(char **pp_credential_data,const char *p_CredData,const char *p_IMEI ) 73 { 74 char *p_CreadData_IMEI; 75 76 MD5_CTX md5_context; 77 char md5hash[MD5_HASH_LENGTH + 1]; /* Add 1 character for NULL */ 78 BufferSize_t offset = 0; 79 BufferSize_t md5_hash_length = MD5_HASH_LENGTH; 80 BufferSize_t total_length; 81 82 memset(md5hash, '\0', (sizeof(char) * (MD5_HASH_LENGTH + 1))); 83 84 *pp_credential_data = (char *)malloc(sizeof(char) * (ENCODED_MD5_CRED_LENGTH)); 85 memset(*pp_credential_data, '\0', (sizeof(char) * ENCODED_MD5_CRED_LENGTH)); 86 /* Add space for the ":" and NULL character */ 87 total_length= strlen(p_CredData) + strlen(p_IMEI) + 2; 88 p_CreadData_IMEI = (char *) malloc(sizeof(char) * (total_length)); 89 memset(p_CreadData_IMEI, '\0', (sizeof(char) * total_length)); 90 SyncML_DM_CombineCredDataAndIMEI(p_CreadData_IMEI, p_CredData,p_IMEI); 91 92 smlMD5Init(&md5_context); 93 smlMD5Update(&md5_context, (unsigned char *)p_CreadData_IMEI,strlen(p_CreadData_IMEI)); 94 smlMD5Final((unsigned char*)md5hash, &md5_context); 95 md5hash[MD5_HASH_LENGTH] = 0; 96 free(p_CreadData_IMEI); 97 p_CreadData_IMEI=NULL; 98 base64Encode((unsigned char *)*pp_credential_data, ENCODED_MD5_CRED_LENGTH, 99 (unsigned char *)md5hash,&md5_hash_length,&offset, 100 1, /* Encode as single block */ 101 NULL); /* No incomplete bl*/ 102 103 } 104 105 /*================================================================================================== 106 107 FUNCTION: SyncML_DM_CombineCredDataAndIMEI 108 109 DESCRIPTION: 110 This function combines the credential data and IMEI strings 111 The credential string is CreadData:IMEI. 112 113 ARGUMENTS PASSED: 114 115 REFERENCE ARGUMENTS PASSED: 116 char *p_CreadData_IMEI - Output variable containing the combined credential data and IMEI 117 string "CreadData:IMEI" 118 const char *p_CreadData - credential data 119 const char *p_IMEI - phone IMEI no. 120 121 RETURN VALUE: 122 N/A 123 124 PRE-CONDITIONS: 125 126 POST-CONDITIONS: 127 128 INVARIANTS: 129 ==================================================================================================*/ 130 void SyncML_DM_CombineCredDataAndIMEI(char *p_CreadData_IMEI,const char *p_CreadData,const char *p_IMEI) 131 { 132 strcpy(p_CreadData_IMEI, p_CreadData); 133 strcat(p_CreadData_IMEI, ":"); 134 strcat(p_CreadData_IMEI, p_IMEI); 135 } 136 /*================================================================================================== 137 138 FUNCTION: SyncML_DM_BuildB64EncodedCredData_Factory_Bootstrap 139 140 DESCRIPTION: 141 This function builds the B64 encoded basic credential information. 142 The credential string is username:password. That string is then 143 B64 encoded. 144 145 ARGUMENTS PASSED: 146 147 REFERENCE ARGUMENTS PASSED: 148 char **p_credential_data - Output variable containing the encoded credential data 149 150 151 const char *pCread_Input 152 RETURN VALUE: 153 N/A 154 155 PRE-CONDITIONS: 156 157 POST-CONDITIONS: 158 159 INVARIANTS: 160 None 161 ==================================================================================================*/ 162 163 164 void SyncML_DM_BuildB64EncodedCredData_Factory_Bootstrap(char **pp_credential_data,const char *pCread_Input) 165 166 { 167 168 BufferSize_t offset = 0; 169 BufferSize_t combined_length = 0; 170 long total_length; 171 long estimated_basic_cred_length; 172 173 /* Add space for the ":" and NULL character */ 174 total_length = strlen(pCread_Input) + 1; 175 /* RFC 2045 (First paragraph of Section 6.8) 176 * "The encoding and decoding algorithms are simple, but the 177 * encoded data are consistently only about 33% larger than the 178 * unencoded data.". Calculate the estimated credential length 179 * by dw_length + (dw_length/2) 180 */ 181 estimated_basic_cred_length = total_length + (total_length/2); 182 183 *pp_credential_data = (char *)malloc(sizeof(char) * estimated_basic_cred_length); 184 memset(*pp_credential_data, '\0', (sizeof(char) * estimated_basic_cred_length)); 185 186 187 combined_length = strlen(pCread_Input); 188 189 base64Encode((unsigned char *)*pp_credential_data, estimated_basic_cred_length, 190 (unsigned char *)pCread_Input, &combined_length, &offset, 191 1, /* Encode as single block */ 192 NULL); /* No incomplete blocks */ 193 194 } 195 196 void user_help(void) 197 { 198 199 printf("\nGen_Cred < Input File Path> < Input Filename> \n\n"); 200 201 } 202 203 204 int main(int argc, char *argv[]) 205 { 206 int bytes_read; 207 int total_length; 208 int filepath_length; 209 char *p_Output_string; 210 char pServerID[150]; 211 char pServer_Nonce[]="14YJ55NI65RS25WA"; 212 char pClient_Nonce[]="53LI62MI23HE33ST"; 213 char pUsername_text[]="UserName"; 214 char *pUsername; 215 char *pFilepath; 216 char pOutputFile[40]; 217 char *ptemp_input; 218 char pcred[]="_Cred-"; 219 220 struct tm *stTm = NULL; 221 time_t Time; 222 // int err = noError;*/ 223 224 225 226 FILE *fd_IMEI,*fd_Cred; 227 228 unsigned char pIMEI[16]; 229 230 if(argc < 3) 231 { 232 user_help(); 233 printf("\n ENTER A KEY TO EXIT \n"); 234 getch(); 235 return 0; 236 } 237 else 238 { 239 240 filepath_length=strlen(argv[1])+ strlen(argv[2]); 241 pFilepath=(char *)malloc((filepath_length * sizeof(char ))+2); 242 sprintf(pFilepath, "%s%s%s", argv[1],DM_STR_SLASH, argv[2]); 243 244 } 245 246 //fd_IMEI=fopen("IMEI.txt","r"); 247 fd_IMEI=fopen(pFilepath,"r"); 248 if(fd_IMEI == NULL) 249 { 250 printf("file fd_IMEI not opened\n"); 251 printf("\n ENTER A KEY TO EXIT \n"); 252 getch(); 253 free(pFilepath); 254 return 0; 255 256 } 257 else 258 { 259 printf ("\nfile fd_IMEI opened \n"); 260 printf(" \n\nENTER THE SERVER ID \n\n"); 261 scanf("%s",pServerID); 262 /* printf(" \n\nENTER THE SERVER NONCE \n\n"); 263 scanf("%s",pServer_Nonce); 264 printf(" \n\nENTER THE CLIENT NONCE \n\n"); 265 scanf("%s",pClient_Nonce);*/ 266 267 if(strlen(pServerID) >= 136) 268 { 269 printf("\n\nLENGTH OF SERVERID IS GRETER THAN 135 IT SHOULD BE LTE 135 \n\n"); 270 printf("\n\nSERVER ID ENTERED == %s length == %d\n\n",pServerID,strlen(pServerID)); 271 printf("\n ENTER A KEY TO EXIT \n"); 272 getch(); 273 free(pFilepath); 274 return 0; 275 } 276 277 printf("\n\nSERVER ID ENTERED == %s length == %d\n\n",pServerID,strlen(pServerID)); 278 279 } 280 281 282 time(&Time); 283 stTm = localtime(&Time); 284 285 286 // filepath_length=(strlen(argv[2])-4)+ (sizeof(int) * 6)+1; 287 ptemp_input=(char *)malloc(((strlen(argv[2])-4) * sizeof(char))+1); 288 // memset(ptemp_input, '\0', (sizeof(char) * estimated_basic_cred_length)); 289 memcpy(ptemp_input,argv[2],(strlen(argv[2])-4)); 290 ptemp_input[strlen(argv[2])-4]='\0'; 291 // pOutputFile=(char *)malloc(filepath_length * sizeof(char)); 292 293 sprintf(pOutputFile,"%s%s%02d.%02d.%02d-%02d-%02d-%04d.txt",ptemp_input,pcred,stTm->tm_hour,stTm->tm_min, 294 stTm->tm_sec,stTm->tm_mday,stTm->tm_mon+1,stTm->tm_year+1900); 295 free(ptemp_input); 296 297 298 fd_Cred=fopen(pOutputFile,"w+"); 299 300 if(fd_Cred == NULL ) 301 302 { 303 printf("file fd_Cred not opened\n"); 304 printf("\n ENTER A KEY TO EXIT \n"); 305 free(pFilepath); 306 //free(pOutputFile); 307 308 309 getch(); 310 311 return 0; 312 313 } 314 else 315 printf ("\nfilew fd_Cred opened \n"); 316 317 318 319 while(!feof(fd_IMEI)) 320 { 321 bytes_read=fread(pIMEI,sizeof(char),16,fd_IMEI); //read from file 322 323 pIMEI[15]='\0'; 324 325 /* WRITE IMEI TO THE FILE */ 326 fwrite(pIMEI,sizeof(char),strlen(pIMEI),fd_Cred); 327 fwrite(" ",sizeof(char),1,fd_Cred); 328 329 SyncML_DM_BuildB64EncodedMD5_Factory_Bootstrap_CredData(&p_Output_string, (const char *)pServerID,(const char *)pIMEI); 330 331 /* Generate Server PW */ 332 333 334 335 //x.EncodeBuffer((char *)digest,sizeof(digest) /*strlen((char *)digest)*/,output_string); 336 337 fwrite(p_Output_string,sizeof(char),strlen(p_Output_string),fd_Cred); 338 339 fwrite(" ",sizeof(char),1,fd_Cred); 340 free(p_Output_string); 341 342 343 /* Generate Client PW */ 344 345 SyncML_DM_BuildB64EncodedMD5_Factory_Bootstrap_CredData(&p_Output_string, (const char *)pIMEI,(const char *)pServerID); 346 347 348 fwrite(p_Output_string,sizeof(char),strlen(p_Output_string),fd_Cred); 349 350 fwrite(" ",sizeof(char),1,fd_Cred); 351 free(p_Output_string); 352 353 /*Generate UserName */ 354 total_length=strlen(pUsername_text)+strlen(pServerID)+2; 355 356 357 pUsername=(char *)malloc(total_length * sizeof(char)); 358 memset(pUsername,'\0',total_length); 359 strcpy(pUsername,pServerID); 360 strcat(pUsername,":"); 361 strcat(pUsername,pUsername_text); 362 363 SyncML_DM_BuildB64EncodedMD5_Factory_Bootstrap_CredData(&p_Output_string, (const char *)pIMEI,(const char *)pUsername); 364 365 fwrite(p_Output_string,sizeof(char),strlen(p_Output_string),fd_Cred); 366 367 free(p_Output_string); 368 free(pUsername); 369 /* NONCE CALCULATION */ 370 371 372 fwrite(" ",sizeof(char),1,fd_Cred); 373 SyncML_DM_BuildB64EncodedCredData_Factory_Bootstrap(&p_Output_string,(const char *)pServer_Nonce); 374 fwrite(p_Output_string,sizeof(char),strlen(p_Output_string),fd_Cred); 375 fwrite(" ",sizeof(char),1,fd_Cred); 376 free(p_Output_string); 377 SyncML_DM_BuildB64EncodedCredData_Factory_Bootstrap(&p_Output_string,(const char *)pClient_Nonce); 378 fwrite(p_Output_string,sizeof(char),strlen(p_Output_string),fd_Cred); 379 free(p_Output_string); 380 381 fwrite("\n",sizeof(char),1 ,fd_Cred); 382 383 384 } 385 386 printf("\n\nGENERATED CRED VALUES EACH SEPARATED BY SPACE CHAR IN %s \n",pOutputFile); 387 printf("\nIMEI ServerPW ClientPW UserName ServerNonce ClientNonce \n\n\n\n"); 388 fclose(fd_Cred); 389 fclose(fd_IMEI); 390 391 printf("\n ENTER A KEY TO EXIT \n"); 392 free(pFilepath); 393 //free(pOutputFile); 394 395 getch(); 396 397 return 0 ; 398 } 399