util.c 16.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
#include <openssl/rsa.h>
#include <openssl/bn.h>
#include <openssl/rand.h>
#include <bb_nn.h>
#include <bbtoolsapi.h>
#include <PR/bbcrl.h>
#include <sha1.h>
#include "util.h"

/* 
 * utilities for saving and retrieving openssl generated private key
 * data
 */

/* 
 * read key data from file and return RSA struct 
 */ 
void
readRsaData(FILE *readptr, RSA *prsa){

  readRsaDataNamed(readptr, prsa, 0);
}

/*
 * read key data from file, return RSA struct and name of entity with key
 */

void
readRsaDataNamed(FILE *readptr, RSA *prsa, BbServerName name){

    unsigned char e_string[MAX_KEY_SIZE], n_string[MAX_KEY_SIZE];
    int e_size, n_size, i;
    int d_size, p_size, q_size;
    unsigned char d_string[MAX_KEY_SIZE], p_string[MAX_KEY_SIZE], q_string[MAX_KEY_SIZE];
    unsigned char dmp1_string[MAX_KEY_SIZE];
    unsigned char dmq1_string[MAX_KEY_SIZE];
    unsigned char iqmp_string[MAX_KEY_SIZE];
    int dmp1_size, dmq1_size, iqmp_size;
#define read_byte(f, x)    { int tmp; fscanf(f, "%02x", &tmp); *(x) = tmp; }
    
    fscanf(readptr, "%d", &e_size);
    for( i =0 ; i< e_size; i++){
	read_byte(readptr, &e_string[i]);
    }
    prsa->e = BN_bin2bn(e_string, e_size, prsa->e);

    fscanf(readptr, "%d", &n_size);
    for( i =0 ; i< n_size; i++){
	read_byte(readptr, &n_string[i]);
    }
    prsa->n = BN_bin2bn(n_string, n_size, prsa->n);
        
    
    fscanf(readptr, "%d", &d_size);
    for( i =0 ; i< d_size; i++){
	read_byte(readptr, &d_string[i]);
    }
    prsa->d = BN_bin2bn(d_string, d_size, prsa->d);
    

    fscanf(readptr, "%d", &p_size);
    for( i =0 ; i< p_size; i++){
	read_byte(readptr, &p_string[i]);
    }
    prsa->p = BN_bin2bn(p_string, p_size, prsa->p);
    

    fscanf(readptr, "%d", &q_size);
    for( i =0 ; i< q_size; i++){
	read_byte(readptr, &q_string[i]);
    }
    prsa->q = BN_bin2bn(q_string, q_size, prsa->q);
    
    
    fscanf(readptr, "%d", &dmp1_size);
    for( i =0 ; i< dmp1_size; i++){
	read_byte(readptr, &dmp1_string[i]);
    }
    prsa->dmp1 = BN_bin2bn(dmp1_string, dmp1_size, prsa->dmp1);
    
    fscanf(readptr, "%d", &dmq1_size);
    for( i =0 ; i< dmq1_size; i++){
	read_byte(readptr, &dmq1_string[i]);
    }
    prsa->dmq1 = BN_bin2bn(dmq1_string, dmq1_size, prsa->dmq1);
    
    fscanf(readptr, "%d", &iqmp_size);
    for( i =0 ; i< iqmp_size; i++){
	read_byte(readptr, &iqmp_string[i]);
    }
    prsa->iqmp = BN_bin2bn(iqmp_string, iqmp_size, prsa->iqmp);

    if(name !=0){
      fscanf(readptr, "%s", name);
    }
}

/*
 * write RSA struct data into file 
 */

void saveRsaData(FILE *writeptr, RSA *prsa){
  saveRsaDataNamed(writeptr, prsa, 0);
}

/*
 * write rsa data to file and add name 
 */

void saveRsaDataNamed(FILE *writeptr, RSA *prsa, BbServerName name){ 
    unsigned char e_string[MAX_KEY_SIZE];
    int e_size, i;
    int n_size;
    unsigned char n_string[MAX_KEY_SIZE];
    unsigned char d_string[MAX_KEY_SIZE];
    unsigned char p_string[MAX_KEY_SIZE];
    unsigned char q_string[MAX_KEY_SIZE];
    unsigned char dmp1_string[MAX_KEY_SIZE];
    unsigned char dmq1_string[MAX_KEY_SIZE];
    unsigned char iqmp_string[MAX_KEY_SIZE];
    int d_size, p_size, q_size;
    int dmp1_size, dmq1_size, iqmp_size;

    e_size = BN_bn2bin(prsa->e, e_string);

    fprintf(writeptr, "\n%d\n", e_size);
    for( i =0 ; i< e_size; i++){
        fprintf(writeptr, "%02x ", e_string[i]);
    }
    n_size = BN_bn2bin(prsa->n, n_string);
 
    fprintf(writeptr, "\n%d\n", n_size);
    for( i =0 ; i< n_size; i++){
        fprintf(writeptr, "%02x ", n_string[i]);
    }
    d_size = BN_bn2bin(prsa->d, d_string);
    fprintf(writeptr, "\n%d\n", d_size);
    for( i =0 ; i< d_size; i++){
        fprintf(writeptr, "%02x ", d_string[i]);
    }
    p_size = BN_bn2bin(prsa->p, p_string);
    fprintf(writeptr, "\n%d\n", p_size);
    for( i =0 ; i< p_size; i++){
        fprintf(writeptr, "%02x ", p_string[i]);
    }
    q_size = BN_bn2bin(prsa->q, q_string);
    fprintf(writeptr, "\n%d\n", q_size);
    for( i =0 ; i< q_size; i++){
        fprintf(writeptr, "%02x ", q_string[i]);
    }
    dmp1_size = BN_bn2bin(prsa->dmp1, dmp1_string);
    fprintf(writeptr, "\n%d\n", dmp1_size);
    for( i =0 ; i< dmp1_size; i++){
        fprintf(writeptr, "%02x ", dmp1_string[i]);
    }
    dmq1_size = BN_bn2bin(prsa->dmq1, dmq1_string);
    fprintf(writeptr, "\n%d\n", dmq1_size);
    for( i =0 ; i< dmq1_size; i++){
        fprintf(writeptr, "%02x ", dmq1_string[i]);
    }
    iqmp_size = BN_bn2bin(prsa->iqmp, iqmp_string);
    fprintf(writeptr, "\n%d\n", iqmp_size);
    for( i =0 ; i< iqmp_size; i++){
        fprintf(writeptr, "%02x ", iqmp_string[i]);
    }
    
    if(name !=0){
      fprintf(writeptr, "\n%s", name);
    }
    
}



int validate(RSA *prsa, int num_bits){
    int size;
    unsigned char *data;
    unsigned char *padded_data;
    unsigned char *sign;
    unsigned char *verify;
    unsigned char hash_data[20];
    unsigned char e_string[512];
    unsigned char n_string[512];
    unsigned long certpublickey[128];
    unsigned long certsign[128];
    unsigned char customResult[512];
    unsigned long certexponent;
    int newsize, i, j, equal, e_size, n_size;
    int data_size;
    SHA1Context sha;
    size = RSA_size(prsa);

    
    data_size = size;
    sign = (unsigned char *) malloc(512);
    verify = (unsigned char *) malloc(512);
    
    data = (unsigned char *)malloc(512);
    padded_data = (unsigned char *) malloc(512);

    
    for (i =0; i < data_size; i+=4){
      *((unsigned long *) (data + i)) = (unsigned long) rand();
    }

    /* compute SHA-1 */
    SHA1Reset(&sha);
    SHA1Input(&sha, data, data_size);
    SHA1Result(&sha, hash_data);
#ifdef DEBUG    
    for( i = 0; i < 20; i++){
      printf("hash = %02x\n", hash_data[i]);
    }
#endif
    RSA_padding_add_PKCS1_type_2(padded_data, data_size, hash_data, 20);
        
#ifdef DEBUG
    /*print padded data */
    for(i = 0; i < data_size; i++){
      printf("padded = %02x\n", padded_data[i]);
    } 
#endif
            
    /* zero the sign */
    for (i = 0; i <  size; i++){
      sign[i] = 0;
    }
    size = RSA_private_encrypt(data_size, padded_data, sign, prsa, RSA_NO_PADDING);
    /* zero the result */
    for(i = 0; i < size; i++){
      verify[i]  = 0;
    }
    newsize = RSA_public_decrypt(RSA_size(prsa), sign, verify, prsa, RSA_NO_PADDING);
#ifdef DEBUG
    printf("size of decrypted = %d\n", newsize);
    for(i =0; i< data_size; i++){
      printf("verify [%d] = %02x padded_data = %02x\n", i, verify[i], padded_data[i]);
    }
#endif
    /* call mod_exp from integer_math.h */
    
    /* initialise bigint */

    e_size = BN_bn2bin(prsa->e, e_string);
#ifdef DEBUG
    for( i =0 ; i< e_size; i++){
      printf("e[%d] = %02x\n", i, e_string[i]);
    }
#endif

    n_size = BN_bn2bin(prsa->n, n_string);
#ifdef DEBUG
    for( i =0 ; i< n_size; i++){
      printf("n[%d] = %02x\n", i, n_string[i]);
    }
#endif 
    convertToHost(e_string, n_string, sign, e_size, n_size, &certexponent, certpublickey, certsign);
    bsl_rsa_verify(customResult, certsign, certpublickey, &certexponent, num_bits);
    /* print custom result of decryption */
    equal = 0;
    j= 0;
    /* print output*/
    for(i=data_size- 20; i < data_size; i++){
#ifdef DEBUG
      printf(" input = %02x custom = %02x\n", padded_data[i], customResult[i]);
#endif
      if(hash_data[j] != customResult[i]){
	equal = 1;
      }
      j++;
    }
        

    free(sign);
    free(verify);
    free(data);
    free(padded_data);
    return equal;

}

void
convertToHost(u8 *e_string, u8 *n_string, u8 *sign, 
	      int e_size, int n_size, 
	      u32 *certexponent, u32 *certpublickey, u32 *certsign){
  int i, count;
  u32 mask = 0x00;
  for(i =0 ; i < e_size; i++){
    mask = (mask << 8) | (0xff);
  }
	
  *certexponent = get_word(e_string) & mask;
  count = 0;
  for(i=0; i< n_size; i= i+4){
    certpublickey[count] = htonl(get_word(n_string + i));
    certsign[count] = htonl(get_word(sign + i));
    count++;
  }
}

/*
 * for verifying certs use this call, pass in the cert as it is
 * read in from a file, and pass in public key and exponent
 * from the next cert with no conversion from a cert. if 
 * root public key and exponent are typecast from words, 
 * swap before passing in.
 * see tools/tests/certcheck.c for example to check a list of cert files.
 */

int verifyCertSign( void *subjectcert, int subjectsize, 
		    u32 signerexponent, 
		    u8 *signerpublic, int signerpublicsize){
  SHA1Context sha;
  unsigned char hash_data[20];
  u8 *sign;
  unsigned long certpublickey[512];
  unsigned long certsign[512];
  unsigned char customResult[512];
  int i, count, equal, j;
  u8 *name;
  SHA1Reset(&sha);
  if(subjectsize == sizeof(BbEccCert)){
      SHA1Input(&sha, (u8 *)subjectcert, BB_ECC_CERT_SIGNED_BYTES);
      name = ((BbEccCert *)subjectcert)->certId.name.bbid;
      sign = (u8 *)(((BbEccCert *)subjectcert)->signature.rsa2048);
  }
  else{
      SHA1Input(&sha, (u8 *)subjectcert, BB_RSA_CERT_SIGNED_BYTES);
      name = ((BbRsaCert *)subjectcert)->certId.name.server;
      if(signerpublicsize*8 == 2048){
	sign = (u8 *)(((BbRsaCert *)subjectcert)->signature.rsa2048);
      }
      else{
	sign = (u8 *)(((BbRsaCert *)subjectcert)->signature.rsa4096);
      }
  }
  SHA1Result(&sha, hash_data);
  signerexponent= htonl(signerexponent);
  count = 0;
  for(i=0; i< signerpublicsize; i= i+4){
    certpublickey[count] = htonl(get_word(signerpublic + i));
    certsign[count] = htonl(get_word(sign + i));
    count++;
  }
  bsl_rsa_verify(customResult, certsign, certpublickey, &signerexponent, signerpublicsize*8);
  
  equal = 0;
  j= 0;
  /* compare output*/
  for(i=signerpublicsize - sizeof(BbShaHash); i < signerpublicsize; i++){
    if(hash_data[j] != customResult[i]){
      equal = 1;
    }
    j++;
  }
  return equal;
}


/* sign a hash value (20 bytes)  and return sign in BB format */
void
signRsa(u8 *hash_data, RSA * prsa2, unsigned long *certsign){
  unsigned char e_string[512];
  unsigned char n_string[512];
  
  unsigned char signature[512];
  unsigned char *padded_data;
  unsigned char *verify;
  int num_bytes, size, newsize;
  int e_size, n_size, i, count;
    
  padded_data = (unsigned char *) malloc(512);
  verify = (unsigned char *) malloc(512);

  num_bytes = RSA_size(prsa2);
 /* compute hash */
      
  RSA_padding_add_PKCS1_type_2(padded_data, num_bytes, hash_data, 20);
#ifdef DEBUG
  for (i = 0; i < 20; i++){
    printf("hash[%d] of cert= %02x\n",i,hash_data[i]);
  }

  for (i = 0; i < num_bytes; i++){
    printf(" padded data = %02x\n", padded_data[i]);
  }
#endif
    
  
  /* take the signer data */
  size = RSA_private_encrypt(num_bytes, 
                      padded_data, signature, prsa2, RSA_NO_PADDING);

/* zero the result: just verifying for fun using RSA */

  for(i = 0; i < size; i++){
    verify[i]  = 0;
  }
  newsize = RSA_public_decrypt(RSA_size(prsa2), signature, verify, prsa2, RSA_NO_PADDING);

#ifdef DEBUG
  printf("size of decrypted = %d\n", newsize);
  for(i =0; i< newsize; i++){
    printf("cert signature = %02x verify [%d] = %02x padded_data = %02x\n", signature[i] , i, verify[i], padded_data[i]);
  }
#endif

  /* initialise bigint */

  e_size = BN_bn2bin(prsa2->e, e_string);
  n_size = BN_bn2bin(prsa2->n, n_string);
    
  count = 0;
  for(i=0; i< n_size; i= i+4){
    certsign[count] = htonl(get_word(signature + i));
    count++;
  }

  /* verify here using bsl_rsa_verify if in doubt: swap back to save */
  count = 0;
  for(i=0; i< n_size; i= i+4){
    certsign[count] = htonl(certsign[count]);
    count++;
  }
}



/*
 * this call takes two key files generated for certsgen tool
 * and creates a signed cert for the first key, signing by the second
 * key. the names are for the cert to be created
 */


     
void
generateCertFromKeyData(u8 *keydatafile, u8 *signerkeydatafile, u8 *subjectname, u8 *issuername, unsigned long *certdata){
  unsigned char e_string[512];
  unsigned char n_string[512];
  unsigned long certpublickey[128];
  unsigned long certsign[128];
  unsigned long certexponent;
  unsigned char hash_data[20];
  unsigned char *padded_data;
  unsigned char *verify;
  FILE *rsareadptr;
  int num_bytes;
  SHA1Context sha;
  int e_size, n_size, i, count;
  RSA *prsa1;
  RSA *prsa2;
  u32 sigtype;
  BbServerName embedsubjectname;
  BbServerName embedissuername;
  BbServerName tmpname1;
  BbServerName tmpname2;
  char *substr;
  
  padded_data = (unsigned char *) malloc(512);
  verify = (unsigned char *) malloc(512);

  /* read in key data to openssl  RSA pointers */
  rsareadptr = fopen(keydatafile, "r");
  prsa1 = RSA_new();
  if(subjectname !=0 ){
    readRsaData(rsareadptr, prsa1);
  }
  else {
    /* read in subject name */
    bzero(embedsubjectname, sizeof(BbServerName));
    readRsaDataNamed(rsareadptr, prsa1, embedsubjectname);
  }
  fclose(rsareadptr);
  prsa2 = RSA_new();
  
  rsareadptr = fopen(signerkeydatafile, "r");
  if(issuername !=0){
    readRsaData(rsareadptr, prsa2);
  }
  else{
    bzero(embedissuername, sizeof(BbServerName));
    readRsaDataNamed(rsareadptr, prsa2, embedissuername);
  }
  num_bytes = RSA_size(prsa2);
  fclose(rsareadptr);
  
  e_size = BN_bn2bin(prsa1->e, e_string);
#ifdef DEBUG
  for( i =0 ; i< e_size; i++){
    printf("e[%d] = %02x\n", i, e_string[i]);
  }

#endif
  n_size = BN_bn2bin(prsa1->n, n_string);
#ifdef DEBUG
  for( i =0 ; i< n_size; i++){
    printf("n[%d] = %02x\n", i, n_string[i]);
  }
#endif  

#define get_word(x)	*(u32*)(x)
  certexponent = get_word(e_string) & 0xff;
  count = 0;
  for(i=0; i< n_size; i= i+4){
    certpublickey[count] = htonl(get_word(n_string + i));
    count++;
  }
  if(num_bytes == 256){
    sigtype = BB_SIG_TYPE_RSA2048;
  }
  else{
    sigtype = BB_SIG_TYPE_RSA4096;
  }

  if((subjectname !=0) && (issuername !=0)){
    generateUnsignedRSACert(BB_CERT_TYPE_SERVER, sigtype, 0, subjectname, issuername,  certpublickey, certexponent, certdata, SIZE_RSA_CERTBLOB_WORDS);
  }
  else{
    bzero(tmpname1, sizeof(BbServerName));
    sprintf(tmpname1, "%s", embedissuername);
    if(strncmp(embedissuername, "Root", 4) !=0){
      /* its the CA : add the root */
      bzero(tmpname2, sizeof(BbServerName));
      sprintf(tmpname2, "Root-%s", tmpname1);
      strcpy(tmpname1, tmpname2);
    }
    substr=strrchr(embedsubjectname,'-');
    if(substr){
        bzero(tmpname2, sizeof(BbServerName));
        strcpy((char *)tmpname2,substr+1);
    }
    generateUnsignedRSACert(BB_CERT_TYPE_SERVER, sigtype, 0, substr?tmpname2:embedsubjectname, tmpname1,  certpublickey, certexponent, certdata, SIZE_RSA_CERTBLOB_WORDS);
      
  }


  /* compute hash */
  
  SHA1Reset(&sha);
  SHA1Input(&sha, (u8 *)&(certdata[0]),BB_RSA_CERT_SIGNED_BYTES);
  SHA1Result(&sha, hash_data);
  
  /* now go sign the cert */
  signRsa(hash_data, prsa2, certsign);

  /* copy the sign to its rightful place : already right endianness*/
  
  for (i = 0; i <  num_bytes/4; i++){
    certdata[(BB_RSA_CERT_SIGNED_BYTES/4) + i] = (certsign[i]);     
  }
        
  /*just return cert data */
  free(prsa1);
  free(prsa2);
  free(padded_data);
  free(verify);
}

int verifyTicketSig(BbTicket *ticket, BbRsaCert *signer)
{
    SHA1Context sha;
    unsigned char hashData[20];
    u32 signerexponent;
    u8 sigPaddedHash[512];
    u32 certPubKeyLE[512];
    u32 sigLE[512];
    int sigBytes = sizeof(BbRsaSig2048);
    int i,j,equal;

    SHA1Reset(&sha);
    SHA1Input(&sha, (u8 *)ticket, sizeof(BbTicket) - sigBytes);
    SHA1Result(&sha, hashData);

    signerexponent= htonl(signer->exponent);
    for(i=0; i<sigBytes/4; i++){
        certPubKeyLE[i] = htonl(signer->publicKey[i]);
        sigLE[i] = htonl(ticket->head.ticketSign[i]);
    }
    bsl_rsa_verify(sigPaddedHash, sigLE, certPubKeyLE, 
                   &signerexponent, sigBytes*8);
  
    equal = 0;
    j= 0;
    /* compare output*/
    for(i=sigBytes - sizeof(BbShaHash); i < sigBytes; i++){
        if(hashData[j] != sigPaddedHash[i]){
          equal = 1;
        }
        j++;
    }
    return equal;
}

/* XXX: initial version will only work with RL signed by cert.
 *      support for RL signed by root must be added.
 */
int verifyRlSig(BbCrlHead *head, u8 *crl, BbRsaCert *signer)
{
    SHA1Context sha;
    unsigned char hashData[20];
    u32 signerexponent;
    u8 sigPaddedHash[512];
    u32 certPubKeyLE[512];
    u32 sigLE[512];
    int sigBytes = sizeof(BbRsaSig2048);
    int i,j,equal;

    SHA1Reset(&sha);
    SHA1Input(&sha, (u8 *)(&head->type), sizeof(BbCrlHead) - sizeof(BbGenericSig));
    SHA1Input(&sha, crl, htonl(head->numberRevoked) * sizeof(BbCrlEntry));
    SHA1Result(&sha, hashData);

    signerexponent= htonl(signer->exponent);
    for(i=0; i<sigBytes/4; i++){
        certPubKeyLE[i] = htonl(signer->publicKey[i]);
        sigLE[i] = htonl(head->signature.rsa2048[i]);
    }
    bsl_rsa_verify(sigPaddedHash, sigLE, certPubKeyLE, 
                   &signerexponent, sigBytes*8);
  
    equal = 0;
    j= 0;
    /* compare output*/
    for(i=sigBytes - sizeof(BbShaHash); i < sigBytes; i++){
        if(hashData[j] != sigPaddedHash[i]){
          equal = 1;
        }
        j++;
    }
    return equal;
}