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 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
|
/* ****************************************************************************
* eID Middleware Project.
* Copyright (C) 2008-2009 FedICT.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version
* 3.0 as published by the Free Software Foundation.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, see
* http://www.gnu.org/licenses/.
**************************************************************************** */
//new P15 implementation
/*
- TokenInfo aplication id=30 (iAppVersion)is decoded but not specified in the standard !!!
*/
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "../common/eidErrors.h"
#include "../common/MWException.h"
#include "../common/Util.h"
#include "PKCS15Parser.h"
#include "../pkcs11/asn1.h"
extern "C" {
#include "../pkcs11/asn1.c"
}
namespace eIDMW{
#define CASN1Object int //not needed for non-cyrptlib implementation, just to eliminate temporary compile errors
/* convert variable length binary bit-stream into int-type */
static unsigned int bin2int(const unsigned char *p_ucDat, unsigned int iLen)
{
unsigned int uiResult = 0;
//parameter check
if (iLen > 4)
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
//add all bytes
while(iLen--)
{
uiResult = uiResult<<8 | *(p_ucDat++);
}
return uiResult;
}
/* convert variable length binary bit-stream into unsigned long-type */
static unsigned long bin2ulong(const unsigned char *p_ucDat, unsigned int iLen)
{
unsigned long ulResult = 0;
//parameter check
if (iLen > 8)
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
//add all bytes
while(iLen--)
{
ulResult = ulResult<<8 | *(p_ucDat++);
}
return ulResult;
}
static unsigned int p15_bitstring2ul(unsigned char *in, unsigned long l_in)
{
unsigned long lBits = 0L;
unsigned char *p = in+1;
int i, j;
//put first bit (most left bit of first asn1 byte) in LSB of flags (unsinged int)
//so asn1 bytes are swapped before keeping in unsigned int
if (l_in == 0)
return 0;
l_in--;
for (i=0; i < (int)l_in; p++, i++)
{
for (j=7; j>=0; j--)
{
lBits |= ((*p >> j) & 0x01) << ((7-j) + (i*8));
}
}
return(lBits);
}
//----------------------------------------------------------------------------------------
tCommonObjAttr ParseCommonObjectAttributes(ASN1_ITEM* p_xLev0Item)
{
tCommonObjAttr coAttr;
ASN1_ITEM xLev1Item; // CommonObjectAttributes sequence
ASN1_ITEM xLev2Item; // Optional elements
coAttr.Reset();
//--- get sequence
if ((p_xLev0Item->l_data < 2) || (asn1_next_item(p_xLev0Item, &xLev1Item)!= 0)||(xLev1Item.tag != ASN_SEQUENCE))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
//--- scan the common object attributes (all optional)
while(xLev1Item.l_data > 0)
{
//get next object
if ((xLev1Item.l_data < 2) || (asn1_next_item(&xLev1Item, &xLev2Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
switch(xLev2Item.tag)
{
case ASN_UTF8STRING: //label
{
std::string csLabel((char *)xLev2Item.p_data, 0, xLev2Item.l_data);
coAttr.csLabel = csLabel;
}
break;
case ASN_BIT_STRING: // common object flags
coAttr.ulFlags = p15_bitstring2ul((unsigned char*)xLev2Item.p_data, xLev2Item.l_data);
break;
case ASN_OCTET_STRING: // authority id
coAttr.ulAuthID = bin2ulong(xLev2Item.p_data, xLev2Item.l_data);
break;
case ASN_INTEGER: // user consent
coAttr.ulUserConsent = bin2ulong(xLev2Item.p_data, xLev2Item.l_data);
break;
case ASN_SEQUENCE: // access control rules
default: // do not yet know this item
break;
}
}
return coAttr;
}
//----------------------------------------------------------------------------------------
// parse a path object
std::string ParsePath(ASN1_ITEM* p_xLev0Item // in: Item with single nested sequence of Octet-string
)
{
std::string csPath = "";
ASN1_ITEM xLev1Item;
//check input
if ((p_xLev0Item->tag != ASN_SEQUENCE))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
//get path
if ((p_xLev0Item->l_data < 2)||(asn1_next_item(p_xLev0Item, &xLev1Item)!= 0) ||((xLev1Item.tag) != ASN_OCTET_STRING))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
//convert to ascii-hex string
char* p_cBuf = bin2AsciiHex(xLev1Item.p_data, xLev1Item.l_data);
std::string csData(p_cBuf, xLev1Item.l_data*2);
csPath = csData;
delete [] p_cBuf;
return csPath;
}
std::string ParsePath2(ASN1_ITEM* p_xLev0Item // in: Item with double nested sequence of Octet-string
)
{
std::string csPath = "";
ASN1_ITEM xLev1Item;
ASN1_ITEM xLev2Item;
//check input
if ((p_xLev0Item->tag != ASN_SEQUENCE))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
//get path
if ((p_xLev0Item->l_data < 2)||(asn1_next_item(p_xLev0Item, &xLev1Item)!= 0) ||((xLev1Item.tag) != ASN_SEQUENCE))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
//get path
if ((xLev1Item.l_data < 2)||(asn1_next_item(&xLev1Item, &xLev2Item)!= 0) ||((xLev2Item.tag) != ASN_OCTET_STRING))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
//convert to ascii-hex string
char* p_cBuf = bin2AsciiHex(xLev2Item.p_data, xLev2Item.l_data);
std::string csData(p_cBuf, xLev2Item.l_data*2);
csPath = csData;
delete [] p_cBuf;
return csPath;
}
//----------------------------------------------------------------------------------------
void PKCS15Parser::FillCOAPin(tPin* pin,tCommonObjAttr *coa){
pin->csLabel = coa->csLabel ;
pin->ulFlags = coa->ulFlags ;
pin->ulAuthID = coa->ulAuthID ;
pin->ulUserConsent = coa->ulUserConsent;
}
//----------------------------------------------------------------------------------------
void PKCS15Parser::FillCOAPrKey(tPrivKey* key,tCommonObjAttr *coa){
key->csLabel = coa->csLabel ;
key->ulFlags = coa->ulFlags ;
key->ulAuthID = coa->ulAuthID ;
key->ulUserConsent = coa->ulUserConsent;
}
//----------------------------------------------------------------------------------------
void PKCS15Parser::FillCOACert(tCert* cert,tCommonObjAttr *coa){
cert->csLabel = coa->csLabel ;
cert->ulFlags = coa->ulFlags ;
cert->ulAuthID = coa->ulAuthID ;
cert->ulUserConsent = coa->ulUserConsent;
}
//----------------------------------------------------------------------------------------
/* parse P15 top structure into AID/LABEL/PATH
*/
tDirInfo PKCS15Parser::ParseDir(const CByteArray & contents)
{
tDirInfo dir;
ASN1_ITEM xLev0Item; //
ASN1_ITEM xLev1Item; //
ASN1_ITEM xLev2Item; //
//check if top-level = APLI.1
xLev0Item.p_data = (unsigned char*)contents.GetBytes();
xLev0Item.l_data = contents.Size();
if ((asn1_next_item(&xLev0Item, &xLev1Item)!= 0) ||(xLev1Item.tag != ASNP15_DIRREC))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
// get second level data of apli 15/16/17
// the order in which the objects appear is not checked
while(xLev1Item.l_data > 0)
{
if (asn1_next_item(&xLev1Item, &xLev2Item)!= 0)
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
switch( xLev2Item.tag )
{
case ASNP15_DIRAID:
{
char* p_cBuf = bin2AsciiHex(xLev2Item.p_data, xLev2Item.l_data);
std::string csData(p_cBuf, xLev2Item.l_data*2);
dir.oAppID = csData;
delete [] p_cBuf;
}
break;
case ASNP15_DIRLABEL:
{
std::string csData((char *)xLev2Item.p_data, 0, xLev2Item.l_data);
dir.csLabel = csData;
}
break;
case ASNP15_DIRPATH:
{
char* p_cBuf = bin2AsciiHex(xLev2Item.p_data, xLev2Item.l_data);
std::string csData(p_cBuf, xLev2Item.l_data*2);
dir.csAppPath = csData;
delete [] p_cBuf;
}
break;
case 19: // object ID
default:
// ignore these objects
break;
}
}
return dir;
}
//----------------------------------------------------------------------------------------
tTokenInfo PKCS15Parser::ParseTokenInfo(const CByteArray & contents){
#ifdef VERBOSE
std::cerr<<"\n----------- PKCS15Parser::ParseTokenInfo ------------ "<<std::endl;
#endif
tTokenInfo tInfo;
ASN1_ITEM xLev0Item; //
ASN1_ITEM xLev1Item; //
ASN1_ITEM xLev2Item; //
//---check top-level sequence
xLev0Item.p_data = (unsigned char*)contents.GetBytes();
xLev0Item.l_data = contents.Size();
if ((asn1_next_item(&xLev0Item, &xLev1Item)!= 0) ||(xLev1Item.tag != ASN_SEQUENCE))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
//--- get Version
if ((xLev1Item.l_data < 2)|| (asn1_next_item(&xLev1Item, &xLev2Item)!= 0) ||(xLev2Item.tag != ASN_INTEGER))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
tInfo.iVersion = bin2int(xLev2Item.p_data, xLev2Item.l_data);
//--- get Serial number
if ((xLev1Item.l_data < 2)|| (asn1_next_item(&xLev1Item, &xLev2Item)!= 0) ||(xLev2Item.tag != ASN_OCTET_STRING))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
std::string csSerial((char *)xLev2Item.p_data, 0, xLev2Item.l_data);
tInfo.csSerial = csSerial;
if ((xLev1Item.l_data < 2)|| (asn1_next_item(&xLev1Item, &xLev2Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
//--- get optional manufactureID
tInfo.csManufactID = ""; //default value
if (xLev2Item.tag == ASN_UTF8STRING)
{
std::string csManufactID((char *)xLev2Item.p_data, 0, xLev2Item.l_data);
tInfo.csManufactID = csManufactID;
if ((xLev1Item.l_data < 2)|| (asn1_next_item(&xLev1Item, &xLev2Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
}
//--- get optional label
tInfo.csLabel = ""; //default value
if (xLev2Item.tag == ASN_PRIMITIVE_CONTEXT_N(0))
{
std::string csLabel((char *)xLev2Item.p_data, 0, xLev2Item.l_data);
tInfo.csLabel = csLabel;
if ((xLev1Item.l_data < 2)|| (asn1_next_item(&xLev1Item, &xLev2Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
}
//--- get token flags
if (xLev2Item.tag != ASN_BIT_STRING)
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
tInfo.ulFlags = p15_bitstring2ul((unsigned char*)xLev2Item.p_data, xLev2Item.l_data);
//default values
tInfo.iAppVersion = 0;
while(xLev1Item.l_data > 0)
{
//get next item
if ((xLev1Item.l_data < 2)|| (asn1_next_item(&xLev1Item, &xLev2Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
switch(xLev2Item.tag)
{
case ASN_CONSTRUCTED_CONTEXT_N(1): //Record Info
{
ASN1_ITEM xLev3Item; //
ASN1_ITEM xLev4Item; //
//get sequence
if ((xLev2Item.l_data < 2) || (asn1_next_item(&xLev2Item, &xLev3Item)!= 0)||(xLev3Item.tag != ASN_SEQUENCE))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
for(int iIndex = 0;iIndex<7;++iIndex) //clear destination
tInfo.piRecordInfo[iIndex] = 0;
// loop to read the integers
while (xLev3Item.l_data > 0)
{
if ((xLev3Item.l_data < 2) || (asn1_next_item(&xLev3Item, &xLev4Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
int iIndex = xLev4Item.tag >> 3;
int iMode = xLev4Item.tag & (ASN_CLASS | ASN_TYPE);
if((iMode == ASN_CONTEXT) && (iIndex < 7) && (iIndex >= 0))
{
tInfo.piRecordInfo[iIndex] = bin2int(xLev4Item.p_data, xLev4Item.l_data);
}
}
}
break;
//case ASN_CONSTRUCTED_CONTEXT_N(2): //Supported Algorithm Info
case ASN_PRIMITIVE_CONTEXT_N(3): //Isser ID
{
std::string csIssuer((char *)xLev2Item.p_data, 0, xLev2Item.l_data);
tInfo.csIssuer = csIssuer;
}
break;
case ASN_PRIMITIVE_CONTEXT_N(4): //Holder ID
{
std::string csHolder((char *)xLev2Item.p_data, 0, xLev2Item.l_data);
tInfo.csHolder = csHolder;
}
break;
case ASN_PRIMITIVE_CONTEXT_N(5): //Last Update
{
std::string csLastUpdate((char *)xLev2Item.p_data, 0, xLev2Item.l_data);
tInfo.csLastUpdate = csLastUpdate;
}
break;
case ASN_PRIMITIVE_CONTEXT_N(30): //Application version
tInfo.iAppVersion = bin2int(xLev2Item.p_data, xLev2Item.l_data);
break;
//case ASN_SEQUENCE: //security Environment Information
//case ASN_PRINTABLE_STRING: //preferred Languague
default:
break;
}
//point to next item
if (xLev2Item.l_raw > xLev1Item.l_data) xLev2Item.l_raw = xLev1Item.l_data; //make sure not to pass the end
xLev1Item.p_data += xLev2Item.l_raw; //next item
xLev1Item.l_data -= xLev2Item.l_raw;
}
return tInfo;
}
//----------------------------------------------------------------------------------------
std::vector <tPin> PKCS15Parser::ParseAodf(const CByteArray & contents)
{
#ifdef VERBOSE
std::cerr<<"\n----------- PKCS15Parser::ParseAodf ------------ "<<std::endl;
#endif
std::vector <tPin> pinVector; //queue of all pin's
tPin pin; //info of one pin
// RAW: DATA:
ASN1_ITEM xLev0Item; // all pins all info of one of PIN
ASN1_ITEM xLev1Item; // 1 sequence sequence+sequence+numb_sequence
ASN1_ITEM xLev2Item; //sequence/sequence/numb_sequence attributes...
ASN1_ITEM xLev3Item; //
ASN1_ITEM xLev4Item; //
xLev0Item.p_data = (unsigned char*)contents.GetBytes();
xLev0Item.l_data = contents.Size();
pinVector.clear();
//decode all Pin's
while(xLev0Item.l_data > 0)
{
//--- get level.1 sequence: Authentication object
if ((xLev0Item.l_data < 2) || (asn1_next_item(&xLev0Item, &xLev1Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
if (xLev1Item.tag == ASN_SEQUENCE) //level.1 sequence
{
pin.ulPinFlags = 0;
pin.ulPinType = 0;
pin.ulMinLen = 0;
pin.ulStoredLen = 0;
pin.ulMaxLen = 0;
pin.ulPinRef = 0;
pin.ucPadChar = '\0';
pin.csLastChange = "";
// ---- common object attributes, level.2
tCommonObjAttr coAttr = ParseCommonObjectAttributes(&xLev1Item); //parse level.2 sequence, updates xLev1Item.data-info!
pin.bValid = true;
FillCOAPin(&pin,&coAttr);
// ------- common authentication object attributes; level.2 second sequence
if ((xLev1Item.l_data < 2)||(asn1_next_item(&xLev1Item, &xLev2Item)!= 0) ||(xLev2Item.tag != ASN_SEQUENCE))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
// Authority ID
if ((xLev2Item.l_data < 2)||(asn1_next_item(&xLev2Item, &xLev3Item)!= 0) ||(xLev3Item.tag != ASN_OCTET_STRING))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
pin.ulID = bin2ulong(xLev3Item.p_data, xLev3Item.l_data);
//--- PIN attributes
if ((xLev1Item.l_data < 2)||(asn1_next_item(&xLev1Item, &xLev2Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
if ((xLev2Item.tag & ASN_CLASS) == ASN_CONTEXT)
{
if ((xLev2Item.tag >> 3) == 1)
{/*
pin.ulPinFlags = 0;
pin.ulPinType = 0;
pin.ulMinLen = 0;
pin.ulStoredLen = 0;
pin.ulMaxLen = 0;
pin.ulPinRef = 0;
pin.ucPadChar = '\0';
pin.csLastChange = "";
*/
//--- sequence
if ((xLev2Item.l_data < 2)||(asn1_next_item(&xLev2Item, &xLev3Item)!= 0) ||(xLev3Item.tag != ASN_SEQUENCE))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
// pin flags
if ((xLev3Item.l_data < 2)||(asn1_next_item(&xLev3Item, &xLev4Item)!= 0) ||(xLev4Item.tag != ASN_BIT_STRING))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
pin.ulPinFlags = p15_bitstring2ul((unsigned char*)xLev4Item.p_data, xLev4Item.l_data);
// pin type
if ((xLev3Item.l_data < 2)||(asn1_next_item(&xLev3Item, &xLev4Item)!= 0) ||(xLev4Item.tag != ASN_ENUM))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
pin.ulPinType = bin2ulong(xLev4Item.p_data, xLev4Item.l_data);
// min length
if ((xLev3Item.l_data < 2)||(asn1_next_item(&xLev3Item, &xLev4Item)!= 0) ||(xLev4Item.tag != ASN_INTEGER))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
pin.ulMinLen = bin2ulong(xLev4Item.p_data, xLev4Item.l_data);
// stored length
if ((xLev3Item.l_data < 2)||(asn1_next_item(&xLev3Item, &xLev4Item)!= 0) ||(xLev4Item.tag != ASN_INTEGER))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
pin.ulStoredLen = bin2ulong(xLev4Item.p_data, xLev4Item.l_data);
#ifdef VERBOSE
std::cerr<<"stored length = "<<pin.ulStoredLen<<std::endl;
#endif
pin.ulMaxLen = pin.ulStoredLen;
while(xLev3Item.l_data > 0)
{
if ((xLev3Item.l_data < 2)||(asn1_next_item(&xLev3Item, &xLev4Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
switch(xLev4Item.tag)
{
case ASN_INTEGER: //maxLength
pin.ulMaxLen = bin2ulong(xLev4Item.p_data, xLev4Item.l_data);
break;
case ASN_PRIMITIVE_CONTEXT_N(0): //pin reference, implicit integer
pin.ulPinRef = bin2ulong(xLev4Item.p_data, xLev4Item.l_data);
break;
case ASN_OCTET_STRING: // padding char
pin.ucPadChar = xLev4Item.p_data[0];
break;
case ASN_GENERALIZEDTIME: // last change
{
std::string csLastChange((char *)xLev4Item.p_data, 0, xLev4Item.l_data);
pin.csLastChange = csLastChange;
}
break;
case ASN_SEQUENCE: // path
pin.csPath = ParsePath(&xLev4Item);
break;
default:
break;
}
}
// pin encoding
switch(pin.ulPinType)
{
case 0: // bcd
pin.encoding = PIN_ENC_BCD;
break;
case 1: // ascii
pin.encoding = PIN_ENC_ASCII;
break;
default:
pin.encoding = PIN_ENC_BCD; // ??
break;
}
}
}
pinVector.push_back(pin);
}
/* else if(item.tagClass == CONTEXT) {
// tag 0: biometric template
// tag 1: authKey
// tag 2: external
asn1.Skip(item.length);
}
*/
}
return pinVector;
}
//----------------------------------------------------------------------------------------
tOdfInfo PKCS15Parser::ParseOdf(const CByteArray & contents){
#ifdef VERBOSE
std::cerr<<"\n----------- PKCS15Parser::ParseOdf ------------ "<<std::endl;
#endif
tOdfInfo result;
ASN1_ITEM xLev0Item; //
ASN1_ITEM xLev1Item; //
ASN1_ITEM xLev2Item; //
int iItemTag;
xLev0Item.l_data = contents.Size();
xLev0Item.p_data = (unsigned char*)contents.GetBytes();
while(xLev0Item.l_data > 0)
{
//check if first level = APLI.x
if ((xLev0Item.l_data < 2)||(asn1_next_item(&xLev0Item, &xLev1Item)!= 0) ||((xLev1Item.tag & ASN_CLASS) != ASN_CONTEXT))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
iItemTag = xLev1Item.tag;
//get second level
if ((xLev1Item.l_data < 2)||(asn1_next_item(&xLev1Item, &xLev2Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
if(xLev2Item.tag == ASN_SEQUENCE)
{
std::string csPath = ParsePath(&xLev2Item);
switch(iItemTag)
{
case ASN_CONSTRUCTED_CONTEXT_N(0):
result.csPrkdfPath = csPath;
break;
case ASN_CONSTRUCTED_CONTEXT_N(1):
result.csPukdfPath = csPath;
break;
case ASN_CONSTRUCTED_CONTEXT_N(2):
result.csTrustedPukdfPath = csPath;
break;
case ASN_CONSTRUCTED_CONTEXT_N(3):
result.csSecretPukdfPath = csPath;
break;
case ASN_CONSTRUCTED_CONTEXT_N(4):
result.csCdfPath = csPath;
break;
case ASN_CONSTRUCTED_CONTEXT_N(5):
result.csTrustedCdfPath = csPath;
break;
case ASN_CONSTRUCTED_CONTEXT_N(6):
result.csUsefulCdfPath = csPath;
break;
case ASN_CONSTRUCTED_CONTEXT_N(7):
result.csDodfPath = csPath;
break;
case ASN_CONSTRUCTED_CONTEXT_N(8):
result.csAodfPath = csPath;
break;
}
}
}
return result;
}
//----------------------------------------------------------------------------------------
std::vector <tPrivKey> PKCS15Parser::ParsePrkdf(const CByteArray & contents)
{
std::vector<tPrivKey> oResult;
ASN1_ITEM xLev0Item; //
ASN1_ITEM xLev1Item; //
ASN1_ITEM xLev2Item; //
ASN1_ITEM xLev3Item; //
ASN1_ITEM xLev4Item; //
tPrivKey xKey;
xLev0Item.p_data = (unsigned char*)contents.GetBytes();
xLev0Item.l_data = contents.Size();
oResult.clear();
#ifdef VERBOSE
std::cerr<<"\n----------- PKCS15Parser::ParsePrkdf ------------ "<<std::endl;
#endif
// loop over the possible paths
while(xLev0Item.l_data > 0)
{
//--- get level.1 sequence: Authentication object
if ((xLev0Item.l_data < 2) || (asn1_next_item(&xLev0Item, &xLev1Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
if(xLev1Item.tag == ASN_SEQUENCE)
{
// ------------- Private RSA Key ------------------
xKey.bValid = true;
xKey.bUsedInP11 = true;
// ---- common object attributes
tCommonObjAttr coAttr = ParseCommonObjectAttributes(&xLev1Item);
FillCOAPrKey(&xKey, &coAttr);
//---- common xKey attributes
// get sequence
if ((xLev1Item.l_data < 2) || (asn1_next_item(&xLev1Item, &xLev2Item)!= 0)||(xLev2Item.tag != ASN_SEQUENCE))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
// identifier
if ((xLev2Item.l_data < 2) || (asn1_next_item(&xLev2Item, &xLev3Item)!= 0)||(xLev3Item.tag != ASN_OCTET_STRING))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
xKey.ulID = bin2ulong(xLev3Item.p_data, xLev3Item.l_data);
// xKey usage flags
if ((xLev2Item.l_data < 2) || (asn1_next_item(&xLev2Item, &xLev3Item)!= 0)||(xLev3Item.tag != ASN_BIT_STRING))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
xKey.ulKeyUsageFlags = p15_bitstring2ul((unsigned char*)xLev3Item.p_data, xLev3Item.l_data);
// key access flags
if ((xLev2Item.l_data < 2) || (asn1_next_item(&xLev2Item, &xLev3Item)!= 0)||(xLev3Item.tag != ASN_BIT_STRING))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
xKey.ulKeyAccessFlags = p15_bitstring2ul((unsigned char*)xLev3Item.p_data, xLev3Item.l_data);
// key reference
if ((xLev2Item.l_data < 2) || (asn1_next_item(&xLev2Item, &xLev3Item)!= 0)||(xLev3Item.tag != ASN_INTEGER))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
xKey.ulKeyRef = bin2ulong(xLev3Item.p_data, xLev3Item.l_data);
//---- private key attributes
if ((xLev1Item.l_data < 2) || (asn1_next_item(&xLev1Item, &xLev2Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
if (xLev2Item.tag == ASN_CONSTRUCTED_CONTEXT_N(1))
{
if ((xLev2Item.l_data < 2) || (asn1_next_item(&xLev2Item, &xLev3Item)!= 0) ||(xLev3Item.tag != ASN_SEQUENCE))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
xKey.csPath = ParsePath2(&xLev3Item);
// modulus length
if ((xLev3Item.l_data < 2) || (asn1_next_item(&xLev3Item, &xLev4Item)!= 0) ||(xLev4Item.tag != ASN_INTEGER))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
xKey.ulKeyLenBytes = (bin2ulong(xLev4Item.p_data, xLev4Item.l_data) + 7) / 8; //convert bit to byte, round up
}
}
oResult.push_back(xKey);
}
return oResult;
}
//----------------------------------------------------------------------------------------
std::vector <tCert> PKCS15Parser::ParseCdf(const CByteArray & contents){
std::vector<tCert> oResult;
ASN1_ITEM xLev0Item; //
ASN1_ITEM xLev1Item; //
ASN1_ITEM xLev2Item; //
ASN1_ITEM xLev3Item; //
ASN1_ITEM xLev4Item; //
tCert cert;
oResult.clear();
#ifdef VERBOSE
std::cerr<<"\n----------- PKCS15Parser::ParseCdf ------------ "<<std::endl;
#endif
//--- X509 Certificate
xLev0Item.p_data = (unsigned char*)contents.GetBytes();
xLev0Item.l_data = contents.Size();
// loop over the possible certificate
while(xLev0Item.l_data > 0)
{
if ((xLev0Item.l_data < 2) || (asn1_next_item(&xLev0Item, &xLev1Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
if(xLev1Item.tag == ASN_SEQUENCE)
{
cert.bValid = true;
//---- Common Object Attributes -----
tCommonObjAttr coAttr = ParseCommonObjectAttributes(&xLev1Item);
FillCOACert(&cert, &coAttr);
//---- Common Certificate Attributes ----
if ((xLev1Item.l_data < 2) || (asn1_next_item(&xLev1Item, &xLev2Item)!= 0)||(xLev2Item.tag != ASN_SEQUENCE))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
if ((xLev2Item.l_data < 2) || (asn1_next_item(&xLev2Item, &xLev3Item)!= 0)||(xLev3Item.tag != ASN_OCTET_STRING))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
cert.ulID = bin2ulong(xLev3Item.p_data, xLev3Item.l_data);
// look for the optional objects, Authority (boolean default false)
cert.bAuthority = false;
cert.bImplicitTrust = false;
while(xLev2Item.l_data > 0)
{
if ((xLev2Item.l_data < 2) || (asn1_next_item(&xLev2Item, &xLev3Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
if(xLev3Item.tag == ASN_BOOLEAN)
cert.bAuthority = bin2int(xLev3Item.p_data, xLev3Item.l_data) ? true : false;
else if(xLev3Item.tag == ASN_CONSTRUCTED_CONTEXT_N(3)) // implicit trust (default false)
{
if ((xLev3Item.l_data < 2) || (asn1_next_item(&xLev3Item, &xLev4Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
cert.bImplicitTrust = bin2int(xLev4Item.p_data, xLev4Item.l_data) ? true : false;
}
//else if(xLev3Item.tagClass == ASN_CONSTRUCTED_CONTEXT_N(0) // OOBCertHash
//else if(xLev3Item.tagClass == ASN_CONSTRUCTED_CONTEXT_N(1) // trusted usage
//else if(xLev3Item.tagClass == ASN_CONSTRUCTED_CONTEXT_N(2) // sequence of credential identif.
}
while(xLev1Item.l_data > 0)
{
if ((xLev1Item.l_data < 2) || (asn1_next_item(&xLev1Item, &xLev2Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
if(xLev2Item.tag == ASN_CONSTRUCTED_CONTEXT_N(1))
//---- [1]X509 CertificateAttributes ----
{
// -------- type attributes: path
if ((xLev2Item.l_data < 2) || (asn1_next_item(&xLev2Item, &xLev3Item)!= 0))
throw CMWEXCEPTION(EIDMW_WRONG_ASN1_FORMAT);
cert.csPath = ParsePath2(&xLev3Item);
}
}
/* else if(xLev1Item.tag == ASN_CONSTRUCTED_CONTEXT_N(1)){
// tag 0: X509 Attribute Certificate
// tag 1: SPKI Certificate
// tag 2: PGP Certificate
// tag 3: WTLS Certificate
// tag 4: X9-68 Certificate
// tag 5: CV Certificate
*/
}
oResult.push_back(cert);
}
return oResult;
}
}
|