39 #define OID_PBKDF2 MKOID( "\x06\x09\x2A\x86\x48\x86\xF7\x0D\x01\x05\x0C" )
40 #define OID_PWRIKEK MKOID( "\x06\x0B\x2A\x86\x48\x86\xF7\x0D\x01\x09\x10\x03\x09" )
75 status = readSequence( stream, &length );
78 endPos = stell( stream ) +
length;
86 queryInfo->keySetupIterations = ( int ) value;
88 if( stell( stream ) < endPos && \
102 queryInfo->keySize = ( int ) value;
104 if( stell( stream ) <
endPos )
112 queryInfo->keySetupAlgo = prfAlgo;
125 int derivationInfoSize,
status;
145 saltLength = msgData.
length;
146 derivationInfoSize = ( int ) sizeofObject( saltLength ) + \
147 sizeofShortInteger( keySetupIterations );
153 (
int ) sizeofObject( derivationInfoSize ),
CTAG_KK_DA );
155 writeSequence( stream, derivationInfoSize );
156 writeOctetString( stream, salt, saltLength,
DEFAULT_TAG );
157 status = writeShortInteger( stream, keySetupIterations,
DEFAULT_TAG );
159 status = writeAlgoID( stream, prfAlgo );
203 const int algoIdInfoSize = \
206 int kekInfoSize, labelSize,
status;
209 assert(
isReadPtr( encryptedKey, encryptedKeyLength ) );
216 return( algoIdInfoSize );
224 labelSize = msgData.
length;
228 sMemOpen( &localStream, kekInfo, 128 );
231 status = writeContextCryptAlgoID( &localStream, iCryptContext );
233 kekInfoSize = stell( &localStream );
234 sMemDisconnect( &localStream );
240 sizeofObject( sizeofObject( labelSize ) ) + \
241 kekInfoSize + sizeofObject( encryptedKeyLength ),
244 writeSequence( stream, sizeofObject( labelSize ) );
245 writeOctetString( stream, label, labelSize,
DEFAULT_TAG );
246 swrite( stream, kekInfo, kekInfoSize );
247 return( writeOctetString( stream, encryptedKey, encryptedKeyLength,
266 const int startPos = stell( stream );
281 return( readCmsKek( stream, queryInfo ) );
294 status = readKeyDerivationInfo( stream, &keyDerivationQueryInfo );
297 hasDerivationInfo =
TRUE;
299 readSequence( stream, NULL );
301 status = readContextAlgoID( stream, NULL, queryInfo,
DEFAULT_TAG,
308 if( hasDerivationInfo )
310 memcpy( queryInfo->salt, keyDerivationQueryInfo.salt,
312 queryInfo->saltLength = keyDerivationQueryInfo.
saltLength;
313 queryInfo->keySetupIterations = \
314 keyDerivationQueryInfo.keySetupIterations;
315 queryInfo->keySetupAlgo = keyDerivationQueryInfo.
keySetupAlgo;
316 if( keyDerivationQueryInfo.
keySize > 0 )
323 queryInfo->keySize = keyDerivationQueryInfo.
keySize;
328 status = readOctetStringHole( stream, &queryInfo->dataLength,
332 queryInfo->dataStart = stell( stream ) -
startPos;
335 return( sSkip( stream, queryInfo->dataLength ) );
339 static
int writeCryptlibKek(
STREAM *stream,
342 const BYTE *encryptedKey,
344 const int encryptedKeyLength )
353 assert(
isReadPtr( encryptedKey, encryptedKeyLength ) );
360 return( algoIdInfoSize );
369 hasKeyDerivationInfo =
FALSE;
382 return( writeCmsKek( stream, iCryptContext, encryptedKey,
383 encryptedKeyLength ) );
391 if( hasKeyDerivationInfo )
394 status = writeKeyDerivationInfo( &localStream, iCryptContext );
396 derivationInfoSize = stell( &localStream );
397 sMemDisconnect( &localStream );
401 sMemOpen( &localStream, kekInfo, 128 );
404 status = writeCryptContextAlgoID( &localStream, iCryptContext );
406 kekInfoSize = stell( &localStream );
407 sMemDisconnect( &localStream );
413 derivationInfoSize + kekInfoSize +
414 (
int ) sizeofObject( encryptedKeyLength ),
417 if( derivationInfoSize > 0 )
418 swrite( stream, derivationInfo, derivationInfoSize );
419 swrite( stream, kekInfo, kekInfoSize );
420 return( writeOctetString( stream, encryptedKey, encryptedKeyLength,
453 status = getPgpPacketInfo( stream, queryInfo );
461 status = readPgpAlgo( stream, &queryInfo->cryptAlgo,
467 value = sgetc( stream );
468 if( value != 0 && value != 1 && value != 3 )
470 status = readPgpAlgo( stream, &queryInfo->keySetupAlgo,
479 status = sread( stream, queryInfo->salt,
PGP_SALTSIZE );
501 value = sgetc( stream );
504 queryInfo->keySetupIterations = \
505 ( 16 + ( ( long ) value & 0x0F ) ) << ( value >> 4 );
506 if( queryInfo->keySetupIterations <= 0 || \
508 return( CRYPT_ERROR_BADDATA );
520 int pgpKekCryptAlgo, pgpHashAlgo =
DUMMY_INIT, keySetupIterations;
526 REQUIRES( encryptedKey == NULL && encryptedKeyLength == 0 );
551 status = cryptlibToPgpAlgo( kekCryptAlgo, &pgpKekCryptAlgo );
553 status = cryptlibToPgpAlgo( hashAlgo, &pgpHashAlgo );
568 REQUIRES( keySetupIterations % 16 == 0 );
569 keySetupIterations /= 32;
570 while( keySetupIterations > 0 )
573 keySetupIterations >>= 1;
576 ENSURES( count >= 0 && count <= 0xFF );
583 sputc( stream, pgpKekCryptAlgo );
585 sputc( stream, pgpHashAlgo );
587 return( sputc( stream, count ) );
610 const int startPos = stell( stream );
623 readSequence( stream, NULL );
628 return( CRYPT_ERROR_BADDATA );
633 status = getStreamObjectLength( stream, &length );
636 queryInfo->iAndSStart = stell( stream ) -
startPos;
637 queryInfo->iAndSLength =
length;
638 sSkip( stream, length );
644 status = readOctetStringHole( stream, &queryInfo->dataLength,
648 queryInfo->dataStart = stell( stream ) -
startPos;
651 return( sSkip( stream, queryInfo->dataLength ) );
658 const BYTE *encryptedKey,
660 const int encryptedKeyLength,
664 const int algoIdInfoSize = \
668 assert(
isReadPtr( encryptedKey, encryptedKeyLength ) );
669 assert(
isReadPtr( auxInfo, auxInfoLength ) );
677 return( algoIdInfoSize );
680 auxInfoLength + algoIdInfoSize + \
681 (
int ) sizeofObject( encryptedKeyLength ) );
683 swrite( stream, auxInfo, auxInfoLength );
685 return( writeOctetString( stream, encryptedKey, encryptedKeyLength,
699 static
int readCryptlibKeytrans(
INOUT STREAM *stream,
702 const int startPos = stell( stream );
715 readSequence( stream, NULL );
720 return( CRYPT_ERROR_BADDATA );
723 readOctetStringTag( stream, queryInfo->keyID, &queryInfo->keyIDlength,
725 status = readAlgoID( stream, &queryInfo->cryptAlgo,
731 status = readOctetStringHole( stream, &queryInfo->dataLength,
735 queryInfo->dataStart = stell( stream ) -
startPos;
738 return( sSkip( stream, queryInfo->dataLength ) );
742 static
int writeCryptlibKeytrans(
INOUT STREAM *stream,
745 const BYTE *encryptedKey,
747 const int encryptedKeyLength,
753 const int algoIdInfoSize = \
758 assert(
isReadPtr( encryptedKey, encryptedKeyLength ) );
763 REQUIRES( auxInfo == NULL && auxInfoLength == 0 );
766 return( algoIdInfoSize );
770 CRYPT_IATTRIBUTE_KEYID );
774 (
int ) sizeofObject( msgData.
length ) + algoIdInfoSize + \
775 (
int ) sizeofObject( encryptedKeyLength ) );
779 return( writeOctetString( stream, encryptedKey, encryptedKeyLength,
799 const int startPos = stell( stream );
814 status = getPgpPacketInfo( stream, queryInfo );
817 value = sgetc( stream );
819 return( CRYPT_ERROR_BADDATA );
828 status = readPgpAlgo( stream, &queryInfo->cryptAlgo,
847 status = readInteger16Ubits( stream, NULL, &queryInfo->dataLength,
851 queryInfo->dataStart = ( stell( stream ) -
startPos ) - \
852 queryInfo->dataLength;
856 const int dataStartPos = stell( stream );
866 status = readInteger16Ubits( stream, NULL, &dummy,
MIN_PKCSIZE,
869 status = readInteger16Ubits( stream, NULL, &dummy,
MIN_PKCSIZE,
873 queryInfo->dataStart = dataStartPos -
startPos;
874 queryInfo->dataLength = stell( stream ) - dataStartPos;
884 const BYTE *encryptedKey,
886 const int encryptedKeyLength,
894 assert(
isReadPtr( encryptedKey, encryptedKeyLength ) );
899 REQUIRES( auxInfo == NULL && auxInfoLength == 0 );
910 &msgData, CRYPT_IATTRIBUTE_KEYID_OPENPGP );
914 status = cryptlibToPgpAlgo( algorithm, &pgpAlgo );
922 encryptedKeyLength ) );
925 sputc( stream, pgpAlgo );
927 writeInteger16Ubits( stream, encryptedKey, encryptedKeyLength ) :
928 swrite( stream, encryptedKey, encryptedKeyLength ) );
940 const READKEYTRANS_FUNCTION
function;
953 const WRITEKEYTRANS_FUNCTION
function;
966 const READKEK_FUNCTION
function;
979 const WRITEKEK_FUNCTION
function;
1003 if( keytransReadTable[ i ].
type == keyexType )
1004 return( keytransReadTable[ i ].
function );
1023 if( keytransWriteTable[ i ].
type == keyexType )
1024 return( keytransWriteTable[ i ].
function );
1043 if( kekReadTable[ i ].
type == keyexType )
1044 return( kekReadTable[ i ].
function );
1063 if( kekWriteTable[ i ].
type == keyexType )
1064 return( kekWriteTable[ i ].
function );