@@ -779,6 +779,156 @@ static CK_RV prvRsaContextParse( const CK_ATTRIBUTE * pxAttribute,
779779 return xResult ;
780780}
781781
782+ /**
783+ * @brief Populates attribute values for an RSA key from the mbed TLS context.
784+ */
785+ static CK_RV prvGetAttributesFromRsaContext ( CK_ATTRIBUTE * pxAttribute ,
786+ const mbedtls_rsa_context * pxRsaContext )
787+ {
788+ CK_RV xResult = CKR_OK ;
789+ int32_t lMbedTLSResult = 0 ;
790+ mbedtls_mpi * pxMpi = ( mbedtls_mpi * ) pxAttribute -> pValue ;
791+
792+ mbedtls_mpi_init ( pxMpi );
793+
794+ switch ( pxAttribute -> type )
795+ {
796+ case ( CKA_MODULUS ):
797+
798+ lMbedTLSResult = mbedtls_mpi_grow ( pxMpi , pxRsaContext -> N .n );
799+
800+ if ( lMbedTLSResult == 0 )
801+ {
802+ lMbedTLSResult = mbedtls_rsa_export ( pxRsaContext ,
803+ pxMpi , /* N */
804+ NULL , /* P */
805+ NULL , /* Q */
806+ NULL , /* D */
807+ NULL ); /* E */
808+ }
809+
810+ break ;
811+
812+ case ( CKA_PUBLIC_EXPONENT ):
813+
814+ lMbedTLSResult = mbedtls_mpi_grow ( pxMpi , pxRsaContext -> E .n );
815+
816+ if ( lMbedTLSResult == 0 )
817+ {
818+ lMbedTLSResult = mbedtls_rsa_export ( pxRsaContext ,
819+ NULL , /* N */
820+ NULL , /* P */
821+ NULL , /* Q */
822+ NULL , /* D */
823+ pxMpi ); /* E */
824+ }
825+
826+ break ;
827+
828+ case ( CKA_PRIME_1 ):
829+
830+ lMbedTLSResult = mbedtls_mpi_grow ( pxMpi , pxRsaContext -> P .n );
831+
832+ if ( lMbedTLSResult == 0 )
833+ {
834+ lMbedTLSResult = mbedtls_rsa_export ( pxRsaContext ,
835+ NULL , /* N */
836+ pxMpi , /* P */
837+ NULL , /* Q */
838+ NULL , /* D */
839+ NULL ); /* E */
840+ }
841+
842+ break ;
843+
844+ case ( CKA_PRIME_2 ):
845+
846+ lMbedTLSResult = mbedtls_mpi_grow ( pxMpi , pxRsaContext -> Q .n );
847+
848+ if ( lMbedTLSResult == 0 )
849+ {
850+ lMbedTLSResult = mbedtls_rsa_export ( pxRsaContext ,
851+ NULL , /* N */
852+ NULL , /* P */
853+ pxMpi , /* Q */
854+ NULL , /* D */
855+ NULL ); /* E */
856+ }
857+
858+ break ;
859+
860+ case ( CKA_PRIVATE_EXPONENT ):
861+
862+ lMbedTLSResult = mbedtls_mpi_grow ( pxMpi , pxRsaContext -> D .n );
863+
864+ if ( lMbedTLSResult == 0 )
865+ {
866+ lMbedTLSResult = mbedtls_rsa_export ( pxRsaContext ,
867+ NULL , /* N */
868+ NULL , /* P */
869+ NULL , /* Q */
870+ pxMpi , /* D */
871+ NULL ); /* E */
872+ }
873+
874+ break ;
875+
876+ case ( CKA_EXPONENT_1 ):
877+
878+ lMbedTLSResult = mbedtls_mpi_grow ( pxMpi , pxRsaContext -> DP .n );
879+
880+ if ( lMbedTLSResult == 0 )
881+ {
882+ lMbedTLSResult = mbedtls_rsa_export_crt ( pxRsaContext ,
883+ pxMpi , /* DP */
884+ NULL , /* DQ */
885+ NULL ); /* QP */
886+ }
887+
888+ break ;
889+
890+ case ( CKA_EXPONENT_2 ):
891+
892+ lMbedTLSResult = mbedtls_mpi_grow ( pxMpi , pxRsaContext -> DQ .n );
893+
894+ if ( lMbedTLSResult == 0 )
895+ {
896+ lMbedTLSResult = mbedtls_rsa_export_crt ( pxRsaContext ,
897+ NULL , /* DP */
898+ pxMpi , /* DQ */
899+ NULL ); /* QP */
900+ }
901+
902+ break ;
903+
904+ default :
905+
906+ /* This is the CKA_COEFFICIENT case. The type is checked in
907+ * C_GetAttributeValue. */
908+ lMbedTLSResult = mbedtls_mpi_grow ( pxMpi , pxRsaContext -> QP .n );
909+
910+ if ( lMbedTLSResult == 0 )
911+ {
912+ lMbedTLSResult = mbedtls_rsa_export_crt ( pxRsaContext ,
913+ NULL , /* DP */
914+ NULL , /* DQ */
915+ pxMpi ); /* QP */
916+ }
917+
918+ break ;
919+ }
920+
921+ if ( lMbedTLSResult != 0 )
922+ {
923+ LogError ( ( "Failed to parse RSA private key attributes: mbed TLS error = %s : %s." ,
924+ mbedtlsHighLevelCodeOrDefault ( lMbedTLSResult ),
925+ mbedtlsLowLevelCodeOrDefault ( lMbedTLSResult ) ) );
926+ xResult = CKR_FUNCTION_FAILED ;
927+ }
928+
929+ return xResult ;
930+ }
931+
782932/**
783933 * @brief Parses attribute values for a RSA Key.
784934 */
@@ -3076,6 +3226,7 @@ CK_DECLARE_FUNCTION( CK_RV, C_GetAttributeValue )( CK_SESSION_HANDLE hSession,
30763226 mbedtls_x509_crt xMbedX509Context = { 0 };
30773227 mbedtls_pk_type_t xKeyType ;
30783228 const mbedtls_ecp_keypair * pxKeyPair ;
3229+ const mbedtls_rsa_context * pxRsaContext ;
30793230 CK_KEY_TYPE xPkcsKeyType = ( CK_KEY_TYPE ) ~0UL ;
30803231 CK_OBJECT_CLASS xClass = ~0UL ;
30813232 CK_BYTE_PTR pxObjectValue = NULL ;
@@ -3294,15 +3445,6 @@ CK_DECLARE_FUNCTION( CK_RV, C_GetAttributeValue )( CK_SESSION_HANDLE hSession,
32943445
32953446 break ;
32963447
3297- case CKA_PRIVATE_EXPONENT :
3298-
3299- LogError ( ( "Failed to parse attribute. "
3300- "CKA_PRIVATE_EXPONENT is private data." ) );
3301- xResult = CKR_ATTRIBUTE_SENSITIVE ;
3302- pTemplate [ iAttrib ].ulValueLen = CK_UNAVAILABLE_INFORMATION ;
3303-
3304- break ;
3305-
33063448 case CKA_EC_PARAMS :
33073449
33083450 if ( pTemplate [ iAttrib ].pValue == NULL )
@@ -3384,6 +3526,44 @@ CK_DECLARE_FUNCTION( CK_RV, C_GetAttributeValue )( CK_SESSION_HANDLE hSession,
33843526
33853527 break ;
33863528
3529+ case CKA_MODULUS :
3530+ case CKA_PUBLIC_EXPONENT :
3531+ case CKA_PRIME_1 :
3532+ case CKA_PRIME_2 :
3533+ case CKA_PRIVATE_EXPONENT :
3534+ case CKA_EXPONENT_1 :
3535+ case CKA_EXPONENT_2 :
3536+ case CKA_COEFFICIENT :
3537+
3538+ if ( pTemplate [ iAttrib ].pValue == NULL )
3539+ {
3540+ pTemplate [ iAttrib ].ulValueLen = sizeof ( mbedtls_mpi );
3541+ }
3542+ else
3543+ {
3544+ if ( pTemplate [ iAttrib ].ulValueLen == sizeof ( mbedtls_mpi ) )
3545+ {
3546+ pxRsaContext = ( mbedtls_rsa_context * ) xKeyContext .pk_ctx ;
3547+
3548+ if ( pxRsaContext != NULL )
3549+ {
3550+ xResult = prvGetAttributesFromRsaContext ( & ( pTemplate [ iAttrib ] ),
3551+ pxRsaContext );
3552+ }
3553+ else
3554+ {
3555+ xResult = CKR_FUNCTION_FAILED ;
3556+ pTemplate [ iAttrib ].ulValueLen = CK_UNAVAILABLE_INFORMATION ;
3557+ }
3558+ }
3559+ else
3560+ {
3561+ xResult = CKR_BUFFER_TOO_SMALL ;
3562+ }
3563+ }
3564+
3565+ break ;
3566+
33873567 default :
33883568 LogError ( ( "Failed to parse attribute. Received unknown "
33893569 "attribute type." ) );
0 commit comments