📄 key_rd.c
字号:
RSAPARAM_MIN_EXP1, RSAPARAM_MAX_EXP1,
&rsaKey->rsaParam_n, 5 );
if( cryptStatusOK( status ) )
status = readBignumTag( stream, &rsaKey->rsaParam_exponent2,
RSAPARAM_MIN_EXP2, RSAPARAM_MAX_EXP2,
&rsaKey->rsaParam_n, 6 );
if( cryptStatusOK( status ) )
status = readBignumTag( stream, &rsaKey->rsaParam_u,
RSAPARAM_MIN_U, RSAPARAM_MAX_U,
&rsaKey->rsaParam_n, 7 );
}
return( status );
}
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
static int readRsaPrivateKeyOld( INOUT STREAM *stream,
INOUT CONTEXT_INFO *contextInfoPtr )
{
PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
int status;
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
REQUIRES( contextInfoPtr->type == CONTEXT_PKC && \
contextInfoPtr->capabilityInfo->cryptAlgo == CRYPT_ALGO_RSA );
/* Read the header and key components */
readOctetStringHole( stream, NULL, 7 * MIN_PKCSIZE, DEFAULT_TAG );
readSequence( stream, NULL );
readShortInteger( stream, NULL );
status = readBignum( stream, &rsaKey->rsaParam_n,
RSAPARAM_MIN_N, RSAPARAM_MAX_N,
NULL );
if( cryptStatusOK( status ) )
status = readBignum( stream, &rsaKey->rsaParam_e,
RSAPARAM_MIN_E, RSAPARAM_MAX_E,
&rsaKey->rsaParam_n );
if( cryptStatusOK( status ) )
status = readBignum( stream, &rsaKey->rsaParam_d,
RSAPARAM_MIN_D, RSAPARAM_MAX_D,
&rsaKey->rsaParam_n );
if( cryptStatusOK( status ) )
status = readBignum( stream, &rsaKey->rsaParam_p,
RSAPARAM_MIN_P, RSAPARAM_MAX_P,
&rsaKey->rsaParam_n );
if( cryptStatusOK( status ) )
status = readBignum( stream, &rsaKey->rsaParam_q,
RSAPARAM_MIN_Q, RSAPARAM_MAX_Q,
&rsaKey->rsaParam_n );
if( cryptStatusOK( status ) )
status = readBignum( stream, &rsaKey->rsaParam_exponent1,
RSAPARAM_MIN_EXP1, RSAPARAM_MAX_EXP1,
&rsaKey->rsaParam_n );
if( cryptStatusOK( status ) )
status = readBignum( stream, &rsaKey->rsaParam_exponent2,
RSAPARAM_MIN_EXP2, RSAPARAM_MAX_EXP2,
&rsaKey->rsaParam_n );
if( cryptStatusOK( status ) )
status = readBignum( stream, &rsaKey->rsaParam_u,
RSAPARAM_MIN_U, RSAPARAM_MAX_U,
&rsaKey->rsaParam_n );
return( status );
}
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
static int readDlpPrivateKey( INOUT STREAM *stream,
INOUT CONTEXT_INFO *contextInfoPtr )
{
PKC_INFO *dlpKey = contextInfoPtr->ctxPKC;
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
REQUIRES( contextInfoPtr->type == CONTEXT_PKC && \
( contextInfoPtr->capabilityInfo->cryptAlgo == CRYPT_ALGO_DH || \
contextInfoPtr->capabilityInfo->cryptAlgo == CRYPT_ALGO_DSA || \
contextInfoPtr->capabilityInfo->cryptAlgo == CRYPT_ALGO_ELGAMAL ) );
/* Read the key components */
if( peekTag( stream ) == BER_SEQUENCE )
{
/* Erroneously written in older code */
readSequence( stream, NULL );
return( readBignumTag( stream, &dlpKey->dlpParam_x,
DLPPARAM_MIN_X, DLPPARAM_MAX_X,
&dlpKey->dlpParam_p, 0 ) );
}
return( readBignum( stream, &dlpKey->dlpParam_x,
DLPPARAM_MIN_X, DLPPARAM_MAX_X,
&dlpKey->dlpParam_p ) );
}
#ifdef USE_ECC
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
static int readEccPrivateKey( INOUT STREAM *stream,
INOUT CONTEXT_INFO *contextInfoPtr )
{
PKC_INFO *dlpKey = contextInfoPtr->ctxPKC;
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
REQUIRES( contextInfoPtr->type == CONTEXT_PKC && \
contextInfoPtr->capabilityInfo->cryptAlgo == CRYPT_ALGO_ECDSA );
/* Read the key components */
return( readBignum( stream, &dlpKey->dlpParam_x,
ECCPARAM_MIN_X, ECCPARAM_MAX_X,
&dlpKey->dlpParam_p ) );
}
#endif /* USE_ECC */
/* Read PGP private key components. This function assumes that the public
portion of the context has already been set up */
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
static int readPgpRsaPrivateKey( INOUT STREAM *stream,
INOUT CONTEXT_INFO *contextInfoPtr )
{
PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
int status;
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
REQUIRES( contextInfoPtr->type == CONTEXT_PKC && \
contextInfoPtr->capabilityInfo->cryptAlgo == CRYPT_ALGO_RSA );
/* Read the PGP private key information */
status = readBignumInteger16Ubits( stream, &rsaKey->rsaParam_d,
bytesToBits( RSAPARAM_MIN_D ),
bytesToBits( RSAPARAM_MAX_D ),
&rsaKey->rsaParam_n );
if( cryptStatusOK( status ) )
status = readBignumInteger16Ubits( stream, &rsaKey->rsaParam_p,
bytesToBits( RSAPARAM_MIN_P ),
bytesToBits( RSAPARAM_MAX_P ),
&rsaKey->rsaParam_n );
if( cryptStatusOK( status ) )
status = readBignumInteger16Ubits( stream, &rsaKey->rsaParam_q,
bytesToBits( RSAPARAM_MIN_Q ),
bytesToBits( RSAPARAM_MAX_Q ),
&rsaKey->rsaParam_n );
if( cryptStatusOK( status ) )
status = readBignumInteger16Ubits( stream, &rsaKey->rsaParam_u,
bytesToBits( RSAPARAM_MIN_U ),
bytesToBits( RSAPARAM_MAX_U ),
&rsaKey->rsaParam_n );
return( status );
}
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
static int readPgpDlpPrivateKey( INOUT STREAM *stream,
INOUT CONTEXT_INFO *contextInfoPtr )
{
PKC_INFO *dlpKey = contextInfoPtr->ctxPKC;
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
REQUIRES( contextInfoPtr->type == CONTEXT_PKC && \
( contextInfoPtr->capabilityInfo->cryptAlgo == CRYPT_ALGO_DSA || \
contextInfoPtr->capabilityInfo->cryptAlgo == CRYPT_ALGO_ELGAMAL ) );
/* Read the PGP private key information */
return( readBignumInteger16Ubits( stream, &dlpKey->dlpParam_x,
bytesToBits( DLPPARAM_MIN_X ),
bytesToBits( DLPPARAM_MAX_X ),
&dlpKey->dlpParam_p ) );
}
/* Umbrella private-key read functions */
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
static int readPrivateKeyRsaFunction( INOUT STREAM *stream,
INOUT CONTEXT_INFO *contextInfoPtr,
IN_ENUM( KEYFORMAT ) \
const KEYFORMAT_TYPE formatType )
{
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
REQUIRES( contextInfoPtr->type == CONTEXT_PKC && \
contextInfoPtr->capabilityInfo->cryptAlgo == CRYPT_ALGO_RSA );
REQUIRES( formatType > KEYFORMAT_NONE && formatType < KEYFORMAT_LAST );
switch( formatType )
{
case KEYFORMAT_PRIVATE:
return( readRsaPrivateKey( stream, contextInfoPtr ) );
case KEYFORMAT_PRIVATE_OLD:
return( readRsaPrivateKeyOld( stream, contextInfoPtr ) );
#ifdef USE_PGP
case KEYFORMAT_PGP:
return( readPgpRsaPrivateKey( stream, contextInfoPtr ) );
#endif /* USE_PGP */
}
retIntError();
}
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
static int readPrivateKeyDlpFunction( INOUT STREAM *stream,
INOUT CONTEXT_INFO *contextInfoPtr,
IN_ENUM( KEYFORMAT ) \
const KEYFORMAT_TYPE formatType )
{
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
REQUIRES( contextInfoPtr->type == CONTEXT_PKC && \
( contextInfoPtr->capabilityInfo->cryptAlgo == CRYPT_ALGO_DH || \
contextInfoPtr->capabilityInfo->cryptAlgo == CRYPT_ALGO_DSA || \
contextInfoPtr->capabilityInfo->cryptAlgo == CRYPT_ALGO_ELGAMAL ) );
REQUIRES( formatType > KEYFORMAT_NONE && formatType < KEYFORMAT_LAST );
switch( formatType )
{
case KEYFORMAT_PRIVATE:
return( readDlpPrivateKey( stream, contextInfoPtr ) );
#ifdef USE_PGP
case KEYFORMAT_PGP:
return( readPgpDlpPrivateKey( stream, contextInfoPtr ) );
#endif /* USE_PGP */
}
retIntError();
}
#ifdef USE_ECC
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
static int readPrivateKeyEccFunction( INOUT STREAM *stream,
INOUT CONTEXT_INFO *contextInfoPtr,
IN_ENUM( KEYFORMAT ) \
const KEYFORMAT_TYPE formatType )
{
assert( isWritePtr( stream, sizeof( STREAM ) ) );
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
REQUIRES( contextInfoPtr->type == CONTEXT_PKC && \
contextInfoPtr->capabilityInfo->cryptAlgo == CRYPT_ALGO_ECDSA );
REQUIRES( formatType > KEYFORMAT_NONE && formatType < KEYFORMAT_LAST );
switch( formatType )
{
case KEYFORMAT_PRIVATE:
return( readEccPrivateKey( stream, contextInfoPtr ) );
}
retIntError();
}
#endif /* USE_ECC */
/****************************************************************************
* *
* Read DL Values *
* *
****************************************************************************/
/* Unlike the simpler RSA PKC, DL-based PKCs produce a pair of values that
need to be encoded as structured data. The following two functions
perform this en/decoding. SSH assumes that DLP values are two fixed-size
blocks of 20 bytes so we can't use the normal read/write routines to
handle these values */
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 3, 4, 5 ) ) \
static int decodeDLValuesFunction( IN_BUFFER( bufSize ) const BYTE *buffer,
IN_LENGTH_SHORT_MIN( 32 ) const int bufSize,
OUT BIGNUM *value1,
OUT BIGNUM *value2,
const BIGNUM *maxRange,
IN_ENUM( CRYPT_FORMAT ) \
const CRYPT_FORMAT_TYPE formatType )
{
STREAM stream;
int status;
assert( isReadPtr( buffer, bufSize ) );
assert( isWritePtr( value1, sizeof( BIGNUM ) ) );
assert( isWritePtr( value2, sizeof( BIGNUM ) ) );
assert( isReadPtr( maxRange, sizeof( BIGNUM ) ) );
REQUIRES( bufSize >= 32 && bufSize < MAX_INTLENGTH_SHORT );
REQUIRES( formatType > CRYPT_FORMAT_NONE && \
formatType < CRYPT_FORMAT_LAST );
sMemConnect( &stream, buffer, bufSize );
/* Read the DL components from the buffer and make sure that they're
valid, i.e. that they're in the range [1...maxRange - 1] */
switch( formatType )
{
case CRYPT_FORMAT_CRYPTLIB:
readSequence( &stream, NULL );
status = readBignum( &stream, value1, DLPPARAM_MIN_R,
CRYPT_MAX_PKCSIZE, maxRange );
if( cryptStatusOK( status ) )
status = readBignum( &stream, value2, DLPPARAM_MIN_S,
CRYPT_MAX_PKCSIZE, maxRange );
break;
#ifdef USE_PGP
case CRYPT_FORMAT_PGP:
status = readBignumInteger16Ubits( &stream, value1, DLPPARAM_MIN_R,
bytesToBits( CRYPT_MAX_PKCSIZE ),
maxRange );
if( cryptStatusOK( status ) )
status = readBignumInteger16Ubits( &stream, value2, DLPPARAM_MIN_S,
bytesToBits( CRYPT_MAX_PKCSIZE ),
maxRange );
break;
#endif /* USE_PGP */
#ifdef USE_SSH
case CRYPT_IFORMAT_SSH:
status = extractBignum( value1, buffer, 20, DLPPARAM_MIN_R,
20, maxRange, FALSE );
if( cryptStatusOK( status ) )
status = extractBignum( value2, buffer + 20, 20, DLPPARAM_MIN_S,
20, maxRange, FALSE );
break;
#endif /* USE_SSH */
default:
retIntError();
}
/* Clean up */
sMemDisconnect( &stream );
return( status );
}
/****************************************************************************
* *
* Context Access Routines *
* *
****************************************************************************/
STDC_NONNULL_ARG( ( 1 ) ) \
void initKeyRead( INOUT CONTEXT_INFO *contextInfoPtr )
{
const CRYPT_ALGO_TYPE cryptAlgo = contextInfoPtr->capabilityInfo->cryptAlgo;
PKC_INFO *pkcInfo = contextInfoPtr->ctxPKC;
assert( isWritePtr( contextInfoPtr, sizeof( CONTEXT_INFO ) ) );
REQUIRES_V( contextInfoPtr->type == CONTEXT_PKC );
/* Set the access method pointers */
pkcInfo->calculateKeyIDFunction = calculateKeyID;
if( isDlpAlgo( cryptAlgo ) )
{
pkcInfo->readPublicKeyFunction = readPublicKeyDlpFunction;
pkcInfo->readPrivateKeyFunction = readPrivateKeyDlpFunction;
pkcInfo->decodeDLValuesFunction = decodeDLValuesFunction;
return;
}
#ifdef USE_ECC
if( isEccAlgo( cryptAlgo ) )
{
pkcInfo->readPublicKeyFunction = readPublicKeyEccFunction;
pkcInfo->readPrivateKeyFunction = readPrivateKeyEccFunction;
return;
}
#endif /* USE_ECC */
pkcInfo->readPublicKeyFunction = readPublicKeyRsaFunction;
pkcInfo->readPrivateKeyFunction = readPrivateKeyRsaFunction;
}
#else
STDC_NONNULL_ARG( ( 1 ) ) \
void initKeyRead( INOUT CONTEXT_INFO *contextInfoPtr )
{
}
#endif /* USE_PKC */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -