📄 key_rw.c
字号:
const PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
writeUint32( stream, BN_num_bits( &rsaKey->rsaParam_n ) );
writeBignumInteger16Ubits( stream, &rsaKey->rsaParam_e );
return( writeBignumInteger16Ubits( stream, &rsaKey->rsaParam_n ) );
}
static int writeSsh2RsaPublicKey( STREAM *stream,
const CONTEXT_INFO *contextInfoPtr )
{
const PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
writeUint32( stream, sizeofString32( 7 ) + \
sizeofBignumInteger32( &rsaKey->rsaParam_e ) + \
sizeofBignumInteger32( &rsaKey->rsaParam_n ) );
writeString32( stream, "ssh-rsa", 7 );
writeBignumInteger32( stream, &rsaKey->rsaParam_e );
return( writeBignumInteger32( stream, &rsaKey->rsaParam_n ) );
}
static int writeSsh2DlpPublicKey( STREAM *stream,
const CONTEXT_INFO *contextInfoPtr )
{
const PKC_INFO *dsaKey = contextInfoPtr->ctxPKC;
/* SSHv2 uses PKCS #3 rather than X9.42-style DH keys, so we have to
treat this algorithm type specially */
if( contextInfoPtr->capabilityInfo->cryptAlgo == CRYPT_ALGO_DH )
{
writeUint32( stream, sizeofString32( 6 ) + \
sizeofBignumInteger32( &dsaKey->dlpParam_p ) + \
sizeofBignumInteger32( &dsaKey->dlpParam_g ) );
writeString32( stream, "ssh-dh", 6 );
writeBignumInteger32( stream, &dsaKey->dlpParam_p );
return( writeBignumInteger32( stream, &dsaKey->dlpParam_g ) );
}
writeUint32( stream, sizeofString32( 7 ) + \
sizeofBignumInteger32( &dsaKey->dlpParam_p ) + \
sizeofBignumInteger32( &dsaKey->dlpParam_q ) + \
sizeofBignumInteger32( &dsaKey->dlpParam_g ) + \
sizeofBignumInteger32( &dsaKey->dlpParam_y ) );
writeString32( stream, "ssh-dss", 7 );
writeBignumInteger32( stream, &dsaKey->dlpParam_p );
writeBignumInteger32( stream, &dsaKey->dlpParam_q );
writeBignumInteger32( stream, &dsaKey->dlpParam_g );
return( writeBignumInteger32( stream, &dsaKey->dlpParam_y ) );
}
/* Write PGP public keys */
int writePgpRsaPublicKey( STREAM *stream, const CONTEXT_INFO *contextInfoPtr )
{
const PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
sputc( stream, PGP_VERSION_OPENPGP );
writeUint32Time( stream, rsaKey->pgpCreationTime );
sputc( stream, PGP_ALGO_RSA );
writeBignumInteger16Ubits( stream, &rsaKey->rsaParam_n );
return( writeBignumInteger16Ubits( stream, &rsaKey->rsaParam_e ) );
}
int writePgpDlpPublicKey( STREAM *stream, const CONTEXT_INFO *contextInfoPtr )
{
const PKC_INFO *dlpKey = contextInfoPtr->ctxPKC;
const CRYPT_ALGO_TYPE cryptAlgo = contextInfoPtr->capabilityInfo->cryptAlgo;
sputc( stream, PGP_VERSION_OPENPGP );
writeUint32Time( stream, dlpKey->pgpCreationTime );
sputc( stream, ( cryptAlgo == CRYPT_ALGO_DSA ) ? \
PGP_ALGO_DSA : PGP_ALGO_ELGAMAL );
writeBignumInteger16Ubits( stream, &dlpKey->dlpParam_p );
if( cryptAlgo == CRYPT_ALGO_DSA )
writeBignumInteger16Ubits( stream, &dlpKey->dlpParam_q );
writeBignumInteger16Ubits( stream, &dlpKey->dlpParam_g );
return( writeBignumInteger16Ubits( stream, &dlpKey->dlpParam_y ) );
}
/* Umbrella public-key write functions */
static int writePublicKeyRsaFunction( STREAM *stream,
const CONTEXT_INFO *contextInfoPtr,
const KEYFORMAT_TYPE formatType,
const char *accessKey )
{
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isReadPtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
/* Make sure that we really intended to call this function */
if( strcmp( accessKey, "public" ) )
return( CRYPT_ERROR_PERMISSION );
switch( formatType )
{
case KEYFORMAT_CERT:
return( writeRsaSubjectPublicKey( stream, contextInfoPtr ) );
case KEYFORMAT_SSH1:
return( writeSsh1RsaPublicKey( stream, contextInfoPtr ) );
case KEYFORMAT_SSH2:
return( writeSsh2RsaPublicKey( stream, contextInfoPtr ) );
case KEYFORMAT_PGP:
return( writePgpRsaPublicKey( stream, contextInfoPtr ) );
}
assert( NOTREACHED );
return( CRYPT_ERROR ); /* Get rid of compiler warning */
}
static int writePublicKeyDlpFunction( STREAM *stream,
const CONTEXT_INFO *contextInfoPtr,
const KEYFORMAT_TYPE formatType,
const char *accessKey )
{
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isReadPtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
/* Make sure that we really intended to call this function */
if( strcmp( accessKey, "public" ) )
return( CRYPT_ERROR_PERMISSION );
switch( formatType )
{
case KEYFORMAT_CERT:
return( writeDlpSubjectPublicKey( stream, contextInfoPtr ) );
case KEYFORMAT_SSH2:
return( writeSsh2DlpPublicKey( stream, contextInfoPtr ) );
case KEYFORMAT_PGP:
return( writePgpDlpPublicKey( stream, contextInfoPtr ) );
}
assert( NOTREACHED );
return( CRYPT_ERROR ); /* Get rid of compiler warning */
}
/****************************************************************************
* *
* Read Private Keys *
* *
****************************************************************************/
/* Read private key components. This function assumes that the public
portion of the context has already been set up */
static int readRsaPrivateKey( STREAM *stream, CONTEXT_INFO *contextInfoPtr )
{
PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
int status;
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
/* Read the header and key components */
readSequence( stream, NULL );
if( peekTag( stream ) == MAKE_CTAG( 0 ) )
/* Erroneously written in older code */
readConstructed( stream, NULL, 0 );
if( peekTag( stream ) == MAKE_CTAG_PRIMITIVE( 0 ) )
{
readBignumTag( stream, &rsaKey->rsaParam_n, 0 );
readBignumTag( stream, &rsaKey->rsaParam_e, 1 );
}
if( peekTag( stream ) == MAKE_CTAG_PRIMITIVE( 2 ) )
readBignumTag( stream, &rsaKey->rsaParam_d, 2 );
readBignumTag( stream, &rsaKey->rsaParam_p, 3 );
status = readBignumTag( stream, &rsaKey->rsaParam_q, 4 );
if( peekTag( stream ) == MAKE_CTAG_PRIMITIVE( 5 ) )
{
readBignumTag( stream, &rsaKey->rsaParam_exponent1, 5 );
readBignumTag( stream, &rsaKey->rsaParam_exponent2, 6 );
status = readBignumTag( stream, &rsaKey->rsaParam_u, 7 );
}
return( status );
}
static int readRsaPrivateKeyOld( STREAM *stream, CONTEXT_INFO *contextInfoPtr )
{
PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
/* Read the header and key components */
readOctetStringHole( stream, NULL, DEFAULT_TAG );
readSequence( stream, NULL );
readShortInteger( stream, NULL );
readBignum( stream, &rsaKey->rsaParam_n );
readBignum( stream, &rsaKey->rsaParam_e );
readBignum( stream, &rsaKey->rsaParam_d );
readBignum( stream, &rsaKey->rsaParam_p );
readBignum( stream, &rsaKey->rsaParam_q );
readBignum( stream, &rsaKey->rsaParam_exponent1 );
readBignum( stream, &rsaKey->rsaParam_exponent2 );
return( readBignum( stream, &rsaKey->rsaParam_u ) );
}
static int readDlpPrivateKey( STREAM *stream, CONTEXT_INFO *contextInfoPtr )
{
PKC_INFO *dlpKey = contextInfoPtr->ctxPKC;
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
/* Read the header and key components */
if( peekTag( stream ) == BER_SEQUENCE )
{
/* Erroneously written in older code */
readSequence( stream, NULL );
return( readBignumTag( stream, &dlpKey->dlpParam_x, 0 ) );
}
return( readBignum( stream, &dlpKey->dlpParam_x ) );
}
/* Read PGP private key components. This function assumes that the public
portion of the context has already been set up */
static int readPgpRsaPrivateKey( STREAM *stream, CONTEXT_INFO *contextInfoPtr )
{
PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
int status;
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
/* Read the PGP private key information */
status = readBignumInteger16Ubits( stream, &rsaKey->rsaParam_d,
MIN_PKCSIZE_BITS,
bytesToBits( PGP_MAX_MPISIZE ) );
if( cryptStatusOK( status ) )
status = readBignumInteger16Ubits( stream, &rsaKey->rsaParam_p,
MIN_PKCSIZE_BITS / 2,
bytesToBits( PGP_MAX_MPISIZE ) );
if( cryptStatusOK( status ) )
status = readBignumInteger16Ubits( stream, &rsaKey->rsaParam_q,
MIN_PKCSIZE_BITS / 2,
bytesToBits( PGP_MAX_MPISIZE ) );
if( cryptStatusOK( status ) )
status = readBignumInteger16Ubits( stream, &rsaKey->rsaParam_u,
MIN_PKCSIZE_BITS / 2,
bytesToBits( PGP_MAX_MPISIZE ) );
return( status );
}
static int readPgpDlpPrivateKey( STREAM *stream, CONTEXT_INFO *contextInfoPtr )
{
PKC_INFO *dlpKey = contextInfoPtr->ctxPKC;
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
/* Read the PGP private key information */
return( readBignumInteger16Ubits( stream, &dlpKey->dlpParam_x, 155,
bytesToBits( PGP_MAX_MPISIZE ) ) );
}
/* Umbrella private-key read functions */
static int readPrivateKeyRsaFunction( STREAM *stream,
CONTEXT_INFO *contextInfoPtr,
const KEYFORMAT_TYPE formatType )
{
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
switch( formatType )
{
case KEYFORMAT_PRIVATE:
return( readRsaPrivateKey( stream, contextInfoPtr ) );
case KEYFORMAT_PRIVATE_OLD:
return( readRsaPrivateKeyOld( stream, contextInfoPtr ) );
case KEYFORMAT_PGP:
return( readPgpRsaPrivateKey( stream, contextInfoPtr ) );
}
assert( NOTREACHED );
return( CRYPT_ERROR ); /* Get rid of compiler warning */
}
static int readPrivateKeyDlpFunction( STREAM *stream,
CONTEXT_INFO *contextInfoPtr,
const KEYFORMAT_TYPE formatType )
{
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
switch( formatType )
{
case KEYFORMAT_PRIVATE:
return( readDlpPrivateKey( stream, contextInfoPtr ) );
case KEYFORMAT_PGP:
return( readPgpDlpPrivateKey( stream, contextInfoPtr ) );
}
assert( NOTREACHED );
return( CRYPT_ERROR ); /* Get rid of compiler warning */
}
/****************************************************************************
* *
* Write Private Keys *
* *
****************************************************************************/
/* Write private keys */
static int writeRsaPrivateKey( STREAM *stream,
const CONTEXT_INFO *contextInfoPtr )
{
const PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
int length = sizeofBignum( &rsaKey->rsaParam_p ) + \
sizeofBignum( &rsaKey->rsaParam_q );
/* Add the length of any optional components that may be present */
#ifdef OLD_MODE /* Erroneously written in older code */
if( !BN_is_zero( &rsaKey->rsaParam_n ) )
length += sizeofBignum( &rsaKey->rsaParam_n ) + \
sizeofBignum( &rsaKey->rsaParam_e );
if( !BN_is_zero( &rsaKey->rsaParam_d ) )
length += sizeofBignum( &rsaKey->rsaParam_d );
#endif /* 1 */
if( !BN_is_zero( &rsaKey->rsaParam_exponent1 ) )
{
length += sizeofBignum( &rsaKey->rsaParam_exponent1 ) + \
sizeofBignum( &rsaKey->rsaParam_exponent2 ) + \
sizeofBignum( &rsaKey->rsaParam_u );
}
/* Write the the PKC fields */
#ifdef OLD_MODE /* Erroneously written in older code */
writeSequence( stream,
( int ) sizeofObject( length ) + \
sizeofShortInteger( BN_num_bits( &rsaKey->rsaParam_n ) ) );
writeConstructed( stream, length, 0 );
if( !BN_is_zero( &rsaKey->rsaParam_n ) )
{
writeBignumTag( stream, &rsaKey->rsaParam_n, 0 );
writeBignumTag( stream, &rsaKey->rsaParam_e, 1 );
}
if( !BN_is_zero( &rsaKey->rsaParam_d ) )
writeBignumTag( stream, &rsaKey->rsaParam_d, 2 );
writeBignumTag( stream, &rsaKey->rsaParam_p, 3 );
writeBignumTag( stream, &rsaKey->rsaParam_q, 4 );
if( !BN_is_zero( &rsaKey->rsaParam_exponent1 ) )
{
writeBignumTag( stream, &rsaKey->rsaParam_exponent1, 5 );
writeBignumTag( stream, &rsaKey->rsaParam_exponent2, 6 );
writeBignumTag( stream, &rsaKey->rsaParam_u, 7 );
}
return( writeShortInteger( stream, BN_num_bits( &rsaKey->rsaParam_n ),
DEFAULT_TAG ) );
#else
writeSequence( stream, length );
writeBignumTag( stream, &rsaKey->rsaParam_p, 3 );
if( BN_is_zero( &rsaKey->rsaParam_exponent1 ) )
return( writeBignumTag( stream, &rsaKey->rsaParam_q, 4 ) );
writeBignumTag( stream, &rsaKey->rsaParam_q, 4 );
writeBignumTag( stream, &rsaKey->rsaParam_exponent1, 5 );
writeBignumTag( stream, &rsaKey->rsaParam_exponent2, 6 );
return( writeBignumTag( stream, &rsaKey->rsaParam_u, 7 ) );
#endif /* 1 */
}
static int writeRsaPrivateKeyOld( STREAM *stream,
const CONTEXT_INFO *contextInfoPtr )
{
const PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
const int length = sizeofShortInteger( 0 ) + \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -