@@ -34,10 +34,12 @@ pub fn handle_write(apdu: &ParsedApdu, store: &mut ObjectStore) -> ApduResponse
3434 }
3535}
3636
37- /// Handle READ commands for objects.
38- pub fn handle_read ( apdu : & ParsedApdu , store : & mut ObjectStore ) -> ApduResponse {
37+ /// Handle READ commands for objects. With `strict` (the applet 7.2
38+ /// contract, see ec::strict_ecdh_from_env) ReadObject enforces the
39+ /// symmetric key read policy; size/list/type reads are unaffected.
40+ pub fn handle_read ( apdu : & ParsedApdu , store : & mut ObjectStore , strict : bool ) -> ApduResponse {
3941 match apdu. p2 {
40- P2_DEFAULT => handle_read_object ( apdu, store) ,
42+ P2_DEFAULT => handle_read_object ( apdu, store, strict ) ,
4143 P2_SIZE => handle_read_size ( apdu, store) ,
4244 P2_LIST => handle_read_id_list ( apdu, store) ,
4345 P2_TYPE => handle_read_type ( apdu, store) ,
@@ -190,7 +192,7 @@ fn handle_write_counter(apdu: &ParsedApdu, store: &mut ObjectStore) -> ApduRespo
190192 ApduResponse :: success ( )
191193}
192194
193- fn handle_read_object ( apdu : & ParsedApdu , store : & mut ObjectStore ) -> ApduResponse {
195+ fn handle_read_object ( apdu : & ParsedApdu , store : & mut ObjectStore , strict : bool ) -> ApduResponse {
194196 let tlvs = match apdu. parse_tlvs ( ) {
195197 Ok ( t) => t,
196198 Err ( _) => return ApduResponse :: error ( SW_WRONG_DATA ) ,
@@ -238,7 +240,21 @@ fn handle_read_object(apdu: &ParsedApdu, store: &mut ObjectStore) -> ApduRespons
238240 SecureObject :: AESKey { key } => key. clone ( ) ,
239241 SecureObject :: UserID { value } => value. clone ( ) ,
240242 SecureObject :: Counter { value } => value. to_be_bytes ( ) . to_vec ( ) ,
241- SecureObject :: HMACKey { key } => key. clone ( ) ,
243+ SecureObject :: HMACKey { key, policy } => {
244+ // Applet 7.2 refuses to export a symmetric key object
245+ // unless the policy attached at creation grants
246+ // POLICY_OBJ_ALLOW_READ; an object created with no
247+ // policy attached is not readable. This is the ECDH
248+ // derive target readback contract, enforced only in
249+ // strict mode so hosts that predate it keep working.
250+ if strict
251+ && policy. map_or ( true ,
252+ |p| p & crate :: policy:: POLICY_OBJ_ALLOW_READ == 0 )
253+ {
254+ return ApduResponse :: error ( SW_COMMAND_NOT_ALLOWED ) ;
255+ }
256+ key. clone ( )
257+ }
242258 } ;
243259 ApduResponse :: success_with_tlvs ( & [ Tlv :: new ( TAG_1 , & data) ] )
244260 }
@@ -347,3 +363,82 @@ fn handle_delete(apdu: &ParsedApdu, store: &mut ObjectStore) -> ApduResponse {
347363 store. remove ( & obj_id) ;
348364 ApduResponse :: success ( )
349365}
366+
367+ #[ cfg( test) ]
368+ mod read_policy_tests {
369+ use super :: * ;
370+ use crate :: policy:: POLICY_OBJ_ALLOW_READ ;
371+
372+ fn read_apdu ( obj_id : [ u8 ; 4 ] ) -> ParsedApdu {
373+ ParsedApdu {
374+ cla : 0x80 ,
375+ ins : INS_READ ,
376+ p1 : P1_DEFAULT ,
377+ p2 : P2_DEFAULT ,
378+ data : vec ! [ TAG_1 , 0x04 , obj_id[ 0 ] , obj_id[ 1 ] , obj_id[ 2 ] , obj_id[ 3 ] ] ,
379+ le : None ,
380+ }
381+ }
382+
383+ #[ test]
384+ fn test_strict_read_hmackey_without_policy_returns_6986 ( ) {
385+ // Applet 7.2 behavior seen on SE05x hardware: an HMACKey object
386+ // created with no policy attached cannot be read back, so the
387+ // ECDH shared secret export fails unless the derive target was
388+ // created with a read policy.
389+ let mut store = ObjectStore :: new ( ) ;
390+ store. insert ( [ 0 , 0 , 0 , 0x66 ] ,
391+ SecureObject :: HMACKey { key : vec ! [ 0xAB ; 32 ] , policy : None } ) ;
392+ let resp = handle_read ( & read_apdu ( [ 0 , 0 , 0 , 0x66 ] ) , & mut store, true ) ;
393+ assert_eq ! ( resp. sw, SW_COMMAND_NOT_ALLOWED ) ;
394+ }
395+
396+ #[ test]
397+ fn test_strict_read_hmackey_policy_without_read_returns_6986 ( ) {
398+ let mut store = ObjectStore :: new ( ) ;
399+ store. insert ( [ 0 , 0 , 0 , 0x66 ] ,
400+ SecureObject :: HMACKey { key : vec ! [ 0xAB ; 32 ] , policy : Some ( 0x0014_0000 ) } ) ;
401+ let resp = handle_read ( & read_apdu ( [ 0 , 0 , 0 , 0x66 ] ) , & mut store, true ) ;
402+ assert_eq ! ( resp. sw, SW_COMMAND_NOT_ALLOWED ) ;
403+ }
404+
405+ #[ test]
406+ fn test_strict_read_hmackey_with_read_policy_succeeds ( ) {
407+ let key = vec ! [ 0xABu8 ; 32 ] ;
408+ let mut store = ObjectStore :: new ( ) ;
409+ store. insert ( [ 0 , 0 , 0 , 0x66 ] ,
410+ SecureObject :: HMACKey {
411+ key : key. clone ( ) ,
412+ policy : Some ( POLICY_OBJ_ALLOW_READ | 0x0014_0000 ) ,
413+ } ) ;
414+ let resp = handle_read ( & read_apdu ( [ 0 , 0 , 0 , 0x66 ] ) , & mut store, true ) ;
415+ assert_eq ! ( resp. sw, 0x9000 ) ;
416+ let tlvs = crate :: tlv:: parse_tlvs ( & resp. data ) . unwrap ( ) ;
417+ assert_eq ! ( tlv:: find_tlv( & tlvs, TAG_1 ) . unwrap( ) . value, key) ;
418+ }
419+
420+ #[ test]
421+ fn test_lenient_read_hmackey_without_policy_succeeds ( ) {
422+ // Hosts that predate the applet 7.2 contract keep working when
423+ // strict mode is off.
424+ let key = vec ! [ 0xABu8 ; 32 ] ;
425+ let mut store = ObjectStore :: new ( ) ;
426+ store. insert ( [ 0 , 0 , 0 , 0x66 ] ,
427+ SecureObject :: HMACKey { key : key. clone ( ) , policy : None } ) ;
428+ let resp = handle_read ( & read_apdu ( [ 0 , 0 , 0 , 0x66 ] ) , & mut store, false ) ;
429+ assert_eq ! ( resp. sw, 0x9000 ) ;
430+ }
431+
432+ #[ test]
433+ fn test_strict_read_size_of_unreadable_hmackey_succeeds ( ) {
434+ // Only the object content is policy-guarded; ReadSize must keep
435+ // working since sss_key_store_get_key sizes its buffer with it.
436+ let mut store = ObjectStore :: new ( ) ;
437+ store. insert ( [ 0 , 0 , 0 , 0x66 ] ,
438+ SecureObject :: HMACKey { key : vec ! [ 0xAB ; 32 ] , policy : None } ) ;
439+ let mut apdu = read_apdu ( [ 0 , 0 , 0 , 0x66 ] ) ;
440+ apdu. p2 = P2_SIZE ;
441+ let resp = handle_read ( & apdu, & mut store, true ) ;
442+ assert_eq ! ( resp. sw, 0x9000 ) ;
443+ }
444+ }
0 commit comments