10 #if defined( INC_ALL )
49 #define hasReversedParams( cryptAlgo ) \
50 ( ( cryptAlgo ) == CRYPT_ALGO_DH || \
51 ( cryptAlgo ) == CRYPT_ALGO_ELGAMAL )
67 #if defined( USE_SSH )
77 int bnLength, noZeroes, i,
status;
87 noZeroes = fixedSize - bnLength;
88 REQUIRES( noZeroes >= 0 && noZeroes < fixedSize );
91 for( i = 0; i < noZeroes; i++ )
93 status = swrite( stream, buffer, bnLength );
109 static
int writeRsaSubjectPublicKey(
INOUT STREAM *stream,
112 const PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
114 sizeofBignum( &rsaKey->rsaParam_e );
125 (
int ) sizeofObject( \
126 sizeofObject( length ) + 1 ) );
130 writeBitStringHole( stream, (
int ) sizeofObject( length ),
132 writeSequence( stream, length );
134 return(
writeBignum( stream, &rsaKey->rsaParam_e ) );
138 static
int writeDlpSubjectPublicKey(
INOUT STREAM *stream,
142 const PKC_INFO *dlpKey = contextInfoPtr->ctxPKC;
143 const int parameterSize = ( int ) sizeofObject( \
145 sizeofBignum( &dlpKey->dlpParam_q ) + \
146 sizeofBignum( &dlpKey->dlpParam_g ) );
147 const int componentSize =
sizeofBignum( &dlpKey->dlpParam_y );
162 DEBUG_DIAG((
"Can't write Elgamal key due to missing q parameter" ));
170 ( int ) sizeofObject( componentSize + 1 );
173 writeSequence( stream, totalSize );
174 writeAlgoIDparam( stream, cryptAlgo, parameterSize );
177 writeSequence( stream,
sizeofBignum( &dlpKey->dlpParam_p ) + \
193 writeBitStringHole( stream, componentSize,
DEFAULT_TAG );
194 return(
writeBignum( stream, &dlpKey->dlpParam_y ) );
197 #if defined( USE_ECDH ) || defined( USE_ECDSA )
200 static
int writeEccSubjectPublicKey(
INOUT STREAM *stream,
203 const PKC_INFO *eccKey = contextInfoPtr->ctxPKC;
207 int oidTblSize, fieldSize =
DUMMY_INIT, encodedPointSize, totalSize;
221 status = getECCOidTbl( &oidTbl, &oidTblSize );
223 status = getECCFieldSize( eccKey->curveType, &fieldSize );
226 for( i = 0; i < oidTblSize && oidTbl[ i ].
oid != NULL; i++ )
228 if( oidTbl[ i ].selectionID == eccKey->curveType )
230 oid = oidTbl[ i ].
oid;
239 &eccKey->eccParam_qx, &eccKey->eccParam_qy,
252 ( int ) sizeofObject( encodedPointSize + 1 );
255 writeSequence( stream, totalSize );
262 writeBitStringHole( stream, encodedPointSize,
DEFAULT_TAG );
263 status = swrite( stream, buffer, encodedPointSize );
278 static
int writeSsh1RsaPublicKey(
INOUT STREAM *stream,
281 const PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
289 writeUint32( stream,
BN_num_bits( &rsaKey->rsaParam_n ) );
290 writeBignumInteger16Ubits( stream, &rsaKey->rsaParam_e );
291 return( writeBignumInteger16Ubits( stream, &rsaKey->rsaParam_n ) );
316 static
int writeSshRsaPublicKey(
INOUT STREAM *stream,
319 const PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
328 sizeofBignumInteger32( &rsaKey->rsaParam_e ) + \
329 sizeofBignumInteger32( &rsaKey->rsaParam_n ) );
330 writeString32( stream,
"ssh-rsa", 7 );
331 writeBignumInteger32( stream, &rsaKey->rsaParam_e );
332 return( writeBignumInteger32( stream, &rsaKey->rsaParam_n ) );
336 static
int writeSshDlpPublicKey(
INOUT STREAM *stream,
339 const PKC_INFO *dsaKey = contextInfoPtr->ctxPKC;
353 sizeofBignumInteger32( &dsaKey->dlpParam_p ) + \
354 sizeofBignumInteger32( &dsaKey->dlpParam_g ) );
355 writeString32( stream,
"ssh-dh", 6 );
356 writeBignumInteger32( stream, &dsaKey->dlpParam_p );
357 return( writeBignumInteger32( stream, &dsaKey->dlpParam_g ) );
361 sizeofBignumInteger32( &dsaKey->dlpParam_p ) + \
362 sizeofBignumInteger32( &dsaKey->dlpParam_q ) + \
363 sizeofBignumInteger32( &dsaKey->dlpParam_g ) + \
364 sizeofBignumInteger32( &dsaKey->dlpParam_y ) );
365 writeString32( stream,
"ssh-dss", 7 );
366 writeBignumInteger32( stream, &dsaKey->dlpParam_p );
367 writeBignumInteger32( stream, &dsaKey->dlpParam_q );
368 writeBignumInteger32( stream, &dsaKey->dlpParam_g );
369 return( writeBignumInteger32( stream, &dsaKey->dlpParam_y ) );
372 #if defined( USE_ECDH ) || defined( USE_ECDSA )
375 static
int writeSshEccPublicKey(
INOUT STREAM *stream,
378 const PKC_INFO *eccKey = contextInfoPtr->ctxPKC;
382 int algoNameLen, paramNameLen,
status;
391 switch( eccKey->curveType )
394 algoName =
"ecdsa-sha2-nistp256";
396 paramName =
"nistp256";
401 algoName =
"ecdsa-sha2-nistp384";
403 paramName =
"nistp384";
408 algoName =
"ecdsa-sha2-nistp521";
410 paramName =
"nistp521";
422 status = getECCFieldSize( eccKey->curveType, &fieldSize );
426 &encodedPointSize, &eccKey->eccParam_qx,
427 &eccKey->eccParam_qy, fieldSize );
436 writeString32( stream, algoName, algoNameLen );
437 writeString32( stream, paramName, paramNameLen );
438 status = writeString32( stream, buffer, encodedPointSize );
469 static
int writeSslDlpPublicKey(
INOUT STREAM *stream,
472 const PKC_INFO *dhKey = contextInfoPtr->ctxPKC;
480 writeBignumInteger16U( stream, &dhKey->dlpParam_p );
481 return( writeBignumInteger16U( stream, &dhKey->dlpParam_g ) );
484 #if defined( USE_ECDH ) || defined( USE_ECDSA )
486 static const MAP_TABLE sslCurveInfo[] = {
497 static
int getEccSslInfoTbl(
OUT const MAP_TABLE **sslInfoTblPtr,
501 assert(
isWritePtr( noSslInfoTblEntries,
sizeof(
int ) ) );
503 *sslInfoTblPtr = sslCurveInfo;
510 static
int writeSslEccPublicKey(
INOUT STREAM *stream,
513 const PKC_INFO *eccKey = contextInfoPtr->ctxPKC;
515 int curveID, sslCurveInfoNoEntries,
status;
524 status = getEccSslInfoTbl( &sslCurveInfoPtr, &sslCurveInfoNoEntries );
527 status = mapValue( eccKey->curveType, &curveID, sslCurveInfoPtr,
528 sslCurveInfoNoEntries );
532 sputc( stream, 0x03 );
533 return( writeUint16( stream, curveID ) );
553 static
int writePgpRsaPublicKey(
INOUT STREAM *stream,
556 const PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
566 writeUint32( stream, 0 );
568 writeUint32Time( stream, rsaKey->pgpCreationTime );
570 writeBignumInteger16Ubits( stream, &rsaKey->rsaParam_n );
571 return( writeBignumInteger16Ubits( stream, &rsaKey->rsaParam_e ) );
575 static
int writePgpDlpPublicKey(
INOUT STREAM *stream,
578 const PKC_INFO *dlpKey = contextInfoPtr->ctxPKC;
590 writeUint32( stream, 0 );
592 writeUint32Time( stream, dlpKey->pgpCreationTime );
595 writeBignumInteger16Ubits( stream, &dlpKey->dlpParam_p );
597 writeBignumInteger16Ubits( stream, &dlpKey->dlpParam_q );
598 writeBignumInteger16Ubits( stream, &dlpKey->dlpParam_g );
599 return( writeBignumInteger16Ubits( stream, &dlpKey->dlpParam_y ) );
606 static
int writePublicKeyRsaFunction(
INOUT STREAM *stream,
611 const char *accessKey,
613 const int accessKeyLen )
617 assert(
isReadPtr( accessKey, accessKeyLen ) );
625 if( accessKeyLen != 10 || memcmp( accessKey,
"public_key", 10 ) )
631 return( writeRsaSubjectPublicKey( stream, contextInfoPtr ) );
635 return( writeSshRsaPublicKey( stream, contextInfoPtr ) );
640 return( writeSsh1RsaPublicKey( stream, contextInfoPtr ) );
645 return( writePgpRsaPublicKey( stream, contextInfoPtr ) );
653 static
int writePublicKeyDlpFunction(
INOUT STREAM *stream,
658 const char *accessKey,
660 const int accessKeyLen )
664 assert(
isReadPtr( accessKey, accessKeyLen ) );
674 if( accessKeyLen != 10 || memcmp( accessKey,
"public_key", 10 ) )
680 return( writeDlpSubjectPublicKey( stream, contextInfoPtr ) );
684 return( writeSshDlpPublicKey( stream, contextInfoPtr ) );
689 return( writeSslDlpPublicKey( stream, contextInfoPtr ) );
694 return( writePgpDlpPublicKey( stream, contextInfoPtr ) );
701 #if defined( USE_ECDH ) || defined( USE_ECDSA )
704 static
int writePublicKeyEccFunction(
INOUT STREAM *stream,
709 const char *accessKey,
711 const int accessKeyLen )
715 assert(
isReadPtr( accessKey, accessKeyLen ) );
725 if( accessKeyLen != 10 || memcmp( accessKey,
"public_key", 10 ) )
731 return( writeEccSubjectPublicKey( stream, contextInfoPtr ) );
735 return( writeSslEccPublicKey( stream, contextInfoPtr ) );
740 return( writeSshEccPublicKey( stream, contextInfoPtr ) );
757 static
int writeRsaPrivateKey(
INOUT STREAM *stream,
760 const PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
762 sizeofBignum( &rsaKey->rsaParam_q );
771 if( !
BN_is_zero( &rsaKey->rsaParam_exponent1 ) )
773 length +=
sizeofBignum( &rsaKey->rsaParam_exponent1 ) + \
774 sizeofBignum( &rsaKey->rsaParam_exponent2 ) + \
775 sizeofBignum( &rsaKey->rsaParam_u );
779 writeSequence( stream, length );
781 if(
BN_is_zero( &rsaKey->rsaParam_exponent1 ) )
792 static
int writeRsaPrivateKeyOld(
INOUT STREAM *stream,
795 const PKC_INFO *rsaKey = contextInfoPtr->ctxPKC;
797 sizeofBignum( &rsaKey->rsaParam_n ) + \
798 sizeofBignum( &rsaKey->rsaParam_e ) + \
799 sizeofBignum( &rsaKey->rsaParam_d ) + \
800 sizeofBignum( &rsaKey->rsaParam_p ) + \
801 sizeofBignum( &rsaKey->rsaParam_q ) + \
802 sizeofBignum( &rsaKey->rsaParam_exponent1 ) + \
803 sizeofBignum( &rsaKey->rsaParam_exponent2 ) + \
804 sizeofBignum( &rsaKey->rsaParam_u );
825 (
int ) sizeofObject( \
826 sizeofObject( length ) ) );
829 writeOctetStringHole( stream, (
int ) sizeofObject( length ),
831 writeSequence( stream, length );
838 writeBignum( stream, &rsaKey->rsaParam_exponent1 );
839 writeBignum( stream, &rsaKey->rsaParam_exponent2 );
840 return(
writeBignum( stream, &rsaKey->rsaParam_u ) );
847 static
int writePrivateKeyRsaFunction(
INOUT STREAM *stream,
852 const char *accessKey,
854 const int accessKeyLen )
858 assert(
isReadPtr( accessKey, accessKeyLen ) );
866 if( accessKeyLen != 11 || memcmp( accessKey,
"private_key", 11 ) || \
874 return( writeRsaPrivateKey( stream, contextInfoPtr ) );
878 return( writeRsaPrivateKeyOld( stream, contextInfoPtr ) );
886 static
int writePrivateKeyDlpFunction(
INOUT STREAM *stream,
891 const char *accessKey,
893 const int accessKeyLen )
895 const PKC_INFO *dlpKey = contextInfoPtr->ctxPKC;
899 assert(
isReadPtr( accessKey, accessKeyLen ) );
909 if( accessKeyLen != 11 || memcmp( accessKey,
"private_key", 11 ) || \
917 return( writeShortInteger( stream, 0,
DEFAULT_TAG ) );
920 return(
writeBignum( stream, &dlpKey->dlpParam_x ) );
923 #if defined( USE_ECDH ) || defined( USE_ECDSA )
926 static
int writePrivateKeyEccFunction(
INOUT STREAM *stream,
931 const char *accessKey,
933 const int accessKeyLen )
935 const PKC_INFO *eccKey = contextInfoPtr->ctxPKC;
939 assert(
isReadPtr( accessKey, accessKeyLen ) );
947 if( accessKeyLen != 11 || memcmp( accessKey,
"private_key", 11 ) || \
952 return(
writeBignum( stream, &eccKey->eccParam_d ) );
987 const int comp1Size =
sizeofInteger( component1, component1Length );
988 const int comp2Size =
sizeofInteger( component2, component2Length );
989 const int comp3Size = ( component3 == NULL ) ? 0 : \
991 int parameterSize, componentSize, totalSize,
status;
993 assert( ( buffer == NULL && bufMaxSize == 0 ) || \
995 assert(
isWritePtr( bufSize,
sizeof(
int ) ) );
996 assert(
isReadPtr( component1, component1Length ) );
997 assert(
isReadPtr( component2, component2Length ) );
998 assert( component3 == NULL || \
999 isReadPtr( component3, component3Length ) );
1000 assert( component4 == NULL || \
1001 isReadPtr( component4, component4Length ) );
1003 REQUIRES( ( buffer == NULL && bufMaxSize == 0 ) || \
1004 ( buffer != NULL && \
1010 REQUIRES( ( component3 == NULL && component3Length == 0 ) || \
1011 ( component3 != NULL && \
1013 REQUIRES( ( component4 == NULL && component4Length == 0 ) || \
1014 ( component4 != NULL && \
1018 if( buffer != NULL )
1019 memset( buffer, 0,
min( 16, bufMaxSize ) );
1029 REQUIRES( component3 != NULL && component4 != NULL );
1031 parameterSize = ( int ) sizeofObject( comp1Size + comp2Size + \
1033 componentSize =
sizeofInteger( component4, component4Length );
1037 REQUIRES( component3 == NULL && component4 == NULL );
1040 componentSize = ( int ) sizeofObject( comp1Size + comp2Size );
1053 totalSize += ( int ) sizeofObject( componentSize + 1 );
1054 if( buffer == NULL )
1057 *bufSize = ( int ) sizeofObject( totalSize );
1062 sMemOpen( &stream, buffer, bufMaxSize );
1065 writeSequence( &stream, totalSize );
1066 writeAlgoIDparam( &stream, cryptAlgo, parameterSize );
1071 writeSequence( &stream, comp1Size + comp2Size + comp3Size );
1072 writeInteger( &stream, component1, component1Length,
DEFAULT_TAG );
1075 writeInteger( &stream, component3, component3Length,
DEFAULT_TAG );
1076 writeInteger( &stream, component2, component2Length,
DEFAULT_TAG );
1080 writeInteger( &stream, component2, component2Length,
DEFAULT_TAG );
1081 writeInteger( &stream, component3, component3Length,
DEFAULT_TAG );
1086 writeBitStringHole( &stream, componentSize,
DEFAULT_TAG );
1089 writeSequence( &stream, comp1Size + comp2Size );
1090 writeInteger( &stream, component1, component1Length,
DEFAULT_TAG );
1091 status = writeInteger( &stream, component2, component2Length,
1096 status = writeInteger( &stream, component4, component4Length,
1100 *bufSize = stell( &stream );
1103 sMemDisconnect( &stream );
1120 static
int encodeDLValuesFunction(
OUT_BUFFER( bufMaxSize, \
1121 *bufSize )
BYTE *buffer,
1123 const int bufMaxSize,
1134 assert(
isWritePtr( bufSize,
sizeof(
int ) ) );
1143 memset( buffer, 0,
min( 16, bufMaxSize ) );
1146 sMemOpen( &stream, buffer, bufMaxSize );
1149 switch( formatType )
1160 writeBignumInteger16Ubits( &stream, value1 );
1161 status = writeBignumInteger16Ubits( &stream, value2 );
1166 case CRYPT_IFORMAT_SSH:
1171 status = writeFixedBignum( &stream, value1, 20 );
1173 status = writeFixedBignum( &stream, value2, 20 );
1181 length = stell( &stream );
1182 sMemDisconnect( &stream );
1191 static
int encodeECDLValuesFunction(
OUT_BUFFER( bufMaxSize, \
1192 *bufSize )
BYTE *buffer,
1194 const int bufMaxSize,
1205 assert(
isWritePtr( bufSize,
sizeof(
int ) ) );
1214 memset( buffer, 0,
min( 16, bufMaxSize ) );
1221 if( formatType != CRYPT_IFORMAT_SSH )
1223 return( encodeDLValuesFunction( buffer, bufMaxSize, bufSize,
1224 value1, value2, formatType ) );
1226 sMemOpen( &stream, buffer, bufMaxSize );
1227 writeBignumInteger32( &stream, value1 );
1228 status = writeBignumInteger32( &stream, value2 );
1230 length = stell( &stream );
1231 sMemDisconnect( &stream );
1248 const CRYPT_ALGO_TYPE cryptAlgo = contextInfoPtr->capabilityInfo->cryptAlgo;
1249 PKC_INFO *pkcInfo = contextInfoPtr->ctxPKC;
1257 pkcInfo->writePublicKeyFunction = writePublicKeyDlpFunction;
1258 pkcInfo->writePrivateKeyFunction = writePrivateKeyDlpFunction;
1259 pkcInfo->encodeDLValuesFunction = encodeDLValuesFunction;
1263 #if defined( USE_ECDH ) || defined( USE_ECDSA )
1266 pkcInfo->writePublicKeyFunction = writePublicKeyEccFunction;
1267 pkcInfo->writePrivateKeyFunction = writePrivateKeyEccFunction;
1268 pkcInfo->encodeDLValuesFunction = encodeECDLValuesFunction;
1273 pkcInfo->writePublicKeyFunction = writePublicKeyRsaFunction;
1274 pkcInfo->writePrivateKeyFunction = writePrivateKeyRsaFunction;