@@ -768,7 +768,7 @@ static psa_algorithm_t psa_key_policy_algorithm_intersection(
768768 psa_algorithm_t alg1 ,
769769 psa_algorithm_t alg2 )
770770{
771- /* Common case: the policy only allows alg . */
771+ /* Common case: both sides actually specify the same policy . */
772772 if ( alg1 == alg2 )
773773 return ( alg1 );
774774 /* If the policies are from the same hash-and-sign family, check
@@ -786,27 +786,34 @@ static psa_algorithm_t psa_key_policy_algorithm_intersection(
786786 return ( 0 );
787787}
788788
789+ static int psa_key_algorithm_permits ( psa_algorithm_t policy_alg ,
790+ psa_algorithm_t requested_alg )
791+ {
792+ /* Common case: the policy only allows requested_alg. */
793+ if ( requested_alg == policy_alg )
794+ return ( 1 );
795+ /* If policy_alg is a hash-and-sign with a wildcard for the hash,
796+ * and requested_alg is the same hash-and-sign family with any hash,
797+ * then requested_alg is compliant with policy_alg. */
798+ if ( PSA_ALG_IS_HASH_AND_SIGN ( requested_alg ) &&
799+ PSA_ALG_SIGN_GET_HASH ( policy_alg ) == PSA_ALG_ANY_HASH )
800+ {
801+ return ( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
802+ ( requested_alg & ~PSA_ALG_HASH_MASK ) );
803+ }
804+ /* If it isn't permitted, it's forbidden. */
805+ return ( 0 );
806+ }
807+
789808/** Test whether a policy permits an algorithm.
790809 *
791810 * The caller must test usage flags separately.
792811 */
793812static int psa_key_policy_permits ( const psa_key_policy_t * policy ,
794813 psa_algorithm_t alg )
795814{
796- /* Common case: the policy only allows alg. */
797- if ( alg == policy -> alg )
798- return ( 1 );
799- /* If policy->alg is a hash-and-sign with a wildcard for the hash,
800- * and alg is the same hash-and-sign family with any hash,
801- * then alg is compliant with policy->alg. */
802- if ( PSA_ALG_IS_HASH_AND_SIGN ( alg ) &&
803- PSA_ALG_SIGN_GET_HASH ( policy -> alg ) == PSA_ALG_ANY_HASH )
804- {
805- return ( ( policy -> alg & ~PSA_ALG_HASH_MASK ) ==
806- ( alg & ~PSA_ALG_HASH_MASK ) );
807- }
808- /* If it isn't permitted, it's forbidden. */
809- return ( 0 );
815+ return ( psa_key_algorithm_permits ( policy -> alg , alg ) ||
816+ psa_key_algorithm_permits ( policy -> alg2 , alg ) );
810817}
811818
812819/** Restrict a key policy based on a constraint.
@@ -827,10 +834,15 @@ static psa_status_t psa_restrict_key_policy(
827834{
828835 psa_algorithm_t intersection_alg =
829836 psa_key_policy_algorithm_intersection ( policy -> alg , constraint -> alg );
837+ psa_algorithm_t intersection_alg2 =
838+ psa_key_policy_algorithm_intersection ( policy -> alg2 , constraint -> alg2 );
830839 if ( intersection_alg == 0 && policy -> alg != 0 && constraint -> alg != 0 )
831840 return ( PSA_ERROR_INVALID_ARGUMENT );
841+ if ( intersection_alg2 == 0 && policy -> alg2 != 0 && constraint -> alg2 != 0 )
842+ return ( PSA_ERROR_INVALID_ARGUMENT );
832843 policy -> usage &= constraint -> usage ;
833844 policy -> alg = intersection_alg ;
845+ policy -> alg2 = intersection_alg2 ;
834846 return ( PSA_SUCCESS );
835847}
836848
0 commit comments