31 { NULL, 0 }, { NULL, 0 }
38 { NULL, 0 }, { NULL, 0 }
42 { NULL, 0 }, { NULL, 0 }
64 destPkcs12Info->flags |= srcPkcs12Info->
flags;
67 memcpy( &destPkcs12Info->certInfo, &srcPkcs12Info->
certInfo,
72 memcpy( &destPkcs12Info->keyInfo, &srcPkcs12Info->
keyInfo,
75 if( destPkcs12Info->labelLength <= 0 && \
78 memcpy( destPkcs12Info->label, srcPkcs12Info->label,
80 destPkcs12Info->labelLength = srcPkcs12Info->
labelLength;
82 if( destPkcs12Info->idLength <= 0 && \
85 memcpy( destPkcs12Info->id, srcPkcs12Info->id,
87 destPkcs12Info->idLength = srcPkcs12Info->
idLength;
125 IN_FLAGS( PKCS12 )
const int flags )
128 const PKCS12_INFO *pkcs12firstItem = &pkcs12info[ 0 ];
134 maxNoPkcs12objects ) );
135 assert( (
id == NULL && idLength == 0 ) || \
138 REQUIRES( maxNoPkcs12objects >= 1 && \
140 REQUIRES( (
id == NULL && idLength == 0 ) || \
145 *pkcs12infoPtrPtr = NULL;
148 if( pkcs12FindFreeEntry( pkcs12info, maxNoPkcs12objects, \
149 &index ) != NULL && index == 0 )
151 *pkcs12infoPtrPtr = (
PKCS12_INFO * ) pkcs12firstItem;
164 pkcs12firstItem->
idLength == 0 ) ||
id == NULL )
179 pkcs12infoPtr = NULL;
180 for( i = 0; i < maxNoPkcs12objects && \
190 if( pkcs12infoPtr != NULL )
194 pkcs12infoPtr = &pkcs12info[ i ];
196 ENSURES( i < FAILSAFE_ITERATIONS_MED );
197 ENSURES( pkcs12infoPtr != NULL )
201 *pkcs12infoPtrPtr = (
PKCS12_INFO * ) pkcs12infoPtr;
208 pkcs12infoPtr = pkcs12FindEntry( pkcs12info, maxNoPkcs12objects,
209 CRYPT_IKEYID_KEYID,
id, idLength );
210 if( pkcs12infoPtr != NULL )
212 *pkcs12infoPtrPtr = (
PKCS12_INFO * ) pkcs12infoPtr;
219 pkcs12infoPtr = pkcs12FindFreeEntry( pkcs12info, maxNoPkcs12objects,
221 if( pkcs12infoPtr == NULL )
223 *pkcs12infoPtrPtr = (
PKCS12_INFO * ) pkcs12infoPtr;
242 void *certObjectData,
252 assert(
isReadPtr( password, passwordLen ) );
253 assert(
isWritePtr( certObjectData, certObjectDataLen ) );
268 cryptOwner, password, passwordLen,
269 &iWrapContext,
FALSE );
275 certObjectData, certObjectDataLen );
288 status = length = checkObjectEncoding( certObjectData,
292 sMemConnect( &stream, certObjectData, certObjectDataLen );
293 readSequence( &stream, NULL );
294 status = readSequence( &stream, NULL );
295 sMemDisconnect( &stream );
308 sMemConnect( &stream, certObjectData, certDataSize );
309 readSequence( &stream, NULL );
310 status = readCMSheader( &stream, certBagOIDinfo,
315 status = readCMSheader( &stream, certOIDinfo,
325 sMemDisconnect( &stream );
328 status = importCertFromStream( &stream, iDataCert, cryptOwner,
329 CRYPT_ICERTTYPE_DATAONLY,
331 sMemDisconnect( &stream );
341 IN_BUFFER( passwordLen )
const void *password,
343 IN_BUFFER( keyObjectDataLen )
const void *keyObjectData,
357 assert(
isReadPtr( password, passwordLen ) );
358 assert(
isReadPtr( keyObjectData, keyObjectDataLen) );
359 assert( ( label == NULL && labelLength == 0 ) || \
368 keyObjectDataLen < MAX_INTLENGTH_SHORT );
369 REQUIRES( ( label == NULL && labelLength == 0 ) || \
371 labelLength > 0 && labelLength < MAX_INTLENGTH_SHORT ) );
379 cryptOwner, password, passwordLen,
380 &iWrapContext,
FALSE );
417 keyObjectDataLen, NULL, 0, iCryptContext,
450 int iterationCount,
status;
454 maxNoPkcs12objects ) );
456 REQUIRES( maxNoPkcs12objects >= 1 && \
457 maxNoPkcs12objects < MAX_INTLENGTH_SHORT );
462 for( iterationCount = 0;
471 status = pkcs12ReadObject( stream, &localPkcs12Info,
472 isEncryptedCert, errorInfo );
477 isCertificate =
TRUE;
480 status = findObjectEntryLocation( &pkcs12infoPtr, pkcs12info,
483 NULL : localPkcs12Info.id,
485 localPkcs12Info.
flags );
488 pkcs12freeObjectEntry( isCertificate ? \
494 "No more room in keyset data to add further "
495 "PKCS #12 items" ) );
499 "Couldn't reconcile ID-less object with existing "
500 "PKCS #12 objects" ) );
505 pkcs12ObjectInfoPtr = isCertificate ? &pkcs12infoPtr->
certInfo : \
507 if( pkcs12ObjectInfoPtr->data != NULL )
509 pkcs12freeObjectEntry( isCertificate ? \
513 "Multiple conflicting %s found in keyset",
514 isCertificate ?
"certificates" :
"keys" ) );
516 copyObjectInfo( pkcs12infoPtr, &localPkcs12Info, isCertificate );
531 int iterationCount,
status;
535 maxNoPkcs12objects ) );
538 REQUIRES( maxNoPkcs12objects >= 1 && \
539 maxNoPkcs12objects < MAX_INTLENGTH_SHORT );
540 REQUIRES( endPos > 0 && endPos > stell( stream ) && \
544 memset( pkcs12info, 0,
sizeof(
PKCS12_INFO ) * maxNoPkcs12objects );
551 for( status =
CRYPT_OK, iterationCount = 0;
556 int isEncrypted, noEOCs = 0;
580 status = isEncrypted = \
581 readCMSheader( stream, keyDataOIDinfo,
588 "Invalid PKCS #12 object header" ) );
595 innerEndPos = stell( stream ) +
length;
608 status = readSequenceI( stream, &innerLength );
612 innerEndPos = stell( stream ) + innerLength;
623 status = getStreamObjectLength( stream, &innerLength );
626 innerEndPos = stell( stream ) + innerLength;
653 "Couldn't get PKCS #12 object length information" ) );
658 status = readObjects( stream, pkcs12info, maxNoPkcs12objects,
659 innerEndPos, isEncrypted, errorInfo );
670 for( i = 0; i < noEOCs; i++ )
672 const int value = checkEOC( stream );
687 if( ( status = checkEOC( stream ) ) ==
TRUE )
688 status = checkEOC( stream );
709 #pragma message( "#############################" )
710 #pragma message( "Building PKCS #12 key-cracker" )
711 #pragma message( "#############################" )
713 static int keyCrack(
const void *encData,
const int length )
738 memcpy( data,
"\x6F\xA0\x7E\xA5\x65\x00\x65\x6C", 8 );
739 for( i = 0; i < 8; i++ )
740 data[ i ] ^= i[
"\x06\x0B\x2A\x86\x48\x86\xF7\x0D" ];
742 memcpy( key,
"PKCS#12PKCS#12", 14 );
743 memset( key + 14, 0, 5 );
748 for( i = 0; i < 256; i++ )
752 printf(
"Trying keys %02X xx.\n", i );
754 while( keyPtr[ 0 ] == i )
774 memcpy( data, (
const BYTE * ) encData + 8, 8 );
780 ( (
const BYTE * ) encData + 8 ),
782 DEBUG_DUMP(
"crack_result", (
const BYTE * ) encData + 8,
789 if( data[ 0 ] == 0x06 && \
790 !memcmp( data,
"\x06\x0B\x2A\x86\x48\x86\xF7\x0D", 8 ) )
792 printf(
"Found at %02X %02X %02X %02X %02X.\n",
793 keyPtr[ 0 ], keyPtr[ 1 ], keyPtr[ 2 ],
794 keyPtr[ 3 ], keyPtr[ 4 ] );
798 for( keyIndex = 4; keyIndex >= 0; keyIndex++ )
800 keyPtr[ keyIndex ]++;
801 if( keyPtr[ keyIndex ] > 0 )
806 printf(
"Trying keys %02X %02X.\n",
807 keyPtr[ 0 ], keyPtr[ 1 ] );
837 #ifdef USE_CERTIFICATES
848 assert(
isReadPtr( keyID, keyIDlength ) );
849 assert( ( auxInfo == NULL && auxInfoMaxLength == 0 ) || \
850 isReadPtr( auxInfo, auxInfoMaxLength ) );
858 keyIDtype == CRYPT_IKEYID_KEYID || \
859 keyIDtype == CRYPT_IKEYID_PGPKEYID || \
860 keyIDtype == CRYPT_IKEYID_ISSUERID );
863 REQUIRES( ( auxInfo == NULL && *auxInfoLength == 0 ) || \
864 ( auxInfo != NULL && \
865 *auxInfoLength > 0 && \
866 *auxInfoLength < MAX_INTLENGTH_SHORT ) );
877 "PKCS #12 keysets only support private-key reads" ) );
886 if( keyIDtype == CRYPT_IKEYID_KEYID || \
887 keyIDtype == CRYPT_IKEYID_PGPKEYID || \
888 keyIDtype == CRYPT_IKEYID_ISSUERID || \
889 ( keyIDlength == 6 && !
strCompare( keyID,
"[none]", 6 ) ) )
891 pkcs12infoPtr = pkcs12FindEntry( keysetInfoPtr->keyData,
892 keysetInfoPtr->keyDataNoObjects,
898 pkcs12infoPtr = pkcs12FindEntry( keysetInfoPtr->keyData,
899 keysetInfoPtr->keyDataNoObjects,
900 keyIDtype, keyID, keyIDlength );
902 if( pkcs12infoPtr == NULL )
906 "No information present for this ID" ) );
908 if( pkcs12infoPtr->
keyInfo.data == NULL )
913 "No private key data present for this ID" ) );
922 return( attributeCopyParams( auxInfo, auxInfoMaxLength,
923 auxInfoLength, pkcs12infoPtr->label,
928 if( auxInfo == NULL )
933 #ifdef USE_CERTIFICATES
934 if( pkcs12infoPtr->
certInfo.data != NULL )
937 const int certDataSize = certObjectInfo->
payloadSize;
946 sMemConnect( &stream,
947 (
BYTE * ) certObjectInfo->data + \
950 status = importCertFromStream( &stream, &iDataCert,
951 keysetInfoPtr->ownerHandle,
952 CRYPT_ICERTTYPE_DATAONLY,
954 sMemDisconnect( &stream );
958 BYTE certDataBuffer[ 2048 + 8 ], *certData = certDataBuffer;
965 if( certDataSize > 2048 )
967 if( certDataSize >= MAX_INTLENGTH_SHORT )
969 if( ( certData =
clAlloc(
"getItemFunction", \
970 certDataSize ) ) == NULL )
974 (
BYTE * ) certObjectInfo->data + \
978 status = importCertificate( certObjectInfo,
979 keysetInfoPtr->ownerHandle,
980 auxInfo, *auxInfoLength, certData,
981 certDataSize, &iDataCert );
982 zeroise( certData, certDataSize );
983 if( certData != certDataBuffer )
984 clFree(
"getItemFunction", certData );
990 "Couldn't recreate certificate from stored certificate "
1003 CRYPT_IATTRIBUTE_CERTKEYALGO );
1010 status = importPrivateKey( &pkcs12infoPtr->
keyInfo,
1011 keysetInfoPtr->ownerHandle, auxInfo, *auxInfoLength,
1016 pkcs12infoPtr->label : NULL,
1024 "Couldn't unwrap and import private key" ) );
1027 #ifdef USE_CERTIFICATES
1041 "Couldn't attach certificate to key" ) );
1065 keysetInfoPtr->getItemFunction = getItemFunction;