@@ -311,7 +311,7 @@ impl PCSPrivateKey {
311311 pub fn from_dict ( dict : & Dictionary , keychain : & KeychainClientState ) -> Self {
312312 let key = dict. get ( "v_Data" ) . expect ( "No dat?" ) . as_data ( ) . expect ( "Not data" ) ;
313313
314- let decoded: PCSPrivateKey = rasn:: der:: decode ( & key) . unwrap ( ) ;
314+ let decoded: PCSPrivateKey = rasn:: der:: decode ( & key) . expect ( "Failed to decode private key!" ) ;
315315
316316 if !decoded. verify_with_keychain ( keychain, dict. get ( "atyp" ) . expect ( "No dat?" ) . as_data ( ) . expect ( "Not data" ) ) . unwrap ( ) {
317317 panic ! ( "PCS Master key verification failed!" ) ;
@@ -559,6 +559,8 @@ impl PCSShareProtectionKeySet {
559559
560560#[ derive( AsnType , Encode , Decode ) ]
561561pub struct PCSShareProtectionIdentities {
562+ #[ rasn( tag( explicit( context, 0 ) ) ) ]
563+ symm_keys : Option < SetOf < rasn:: types:: OctetString > > ,
562564 #[ rasn( tag( explicit( context, 1 ) ) ) ]
563565 tag1 : PCSShareProtectionIdentitiesTag1 ,
564566 #[ rasn( tag( explicit( context, 2 ) ) ) ]
@@ -567,8 +569,11 @@ pub struct PCSShareProtectionIdentities {
567569
568570impl PCSShareProtection {
569571 fn signature_data ( & self ) -> PCSObjectSignature {
572+ // 5 is the version. non-exist is 1, 5 is 2, 4 is 3,
573+ // classic is 2
574+ // share is 3
570575 let data = self . attributes . iter ( ) . find ( |a| a. key == 5 ) . expect ( "No signature data" ) ;
571- rasn:: der:: decode ( & data. value ) . expect ( "failed to decode" )
576+ rasn:: der:: decode ( & data. value ) . expect ( "failed to decode signature data " )
572577 }
573578
574579 fn digest_data ( & self , objsig : & PCSObjectSignature ) -> Vec < u8 > {
@@ -577,12 +582,15 @@ impl PCSShareProtection {
577582 & self . meta [ ..] ,
578583 & objsig. unk2 . to_be_bytes ( ) ,
579584 & objsig. unk1 . to_be_bytes ( ) ,
580- & 0u32 . to_be_bytes ( ) ,
585+ & objsig . symm_key_count . unwrap_or ( 0 ) . to_be_bytes ( ) ,
581586 & objsig. public . keytype . to_be_bytes ( ) ,
582587 & objsig. public . pub_key [ ..] ,
583588 ] . concat ( ) ;
584- if let Some ( keylist) = & objsig. keylist {
585- data. extend_from_slice ( & rasn:: der:: encode ( keylist) . unwrap ( ) ) ;
589+ if let Some ( attributes) = & objsig. attributes {
590+ data. extend_from_slice ( & rasn:: der:: encode ( attributes) . unwrap ( ) ) ;
591+ }
592+ if let Some ( ec_key_list) = & objsig. ec_key_list {
593+ data. extend_from_slice ( & rasn:: der:: encode ( ec_key_list) . unwrap ( ) ) ;
586594 }
587595 data
588596 }
@@ -599,7 +607,7 @@ impl PCSShareProtection {
599607 Ok ( self . keyset . keyset . first ( ) . unwrap ( ) . decryption_key . pub_key . to_vec ( ) )
600608 }
601609
602- pub fn decrypt_with_keychain ( & self , keychain : & KeychainClientState , service : & PCSService < ' _ > ) -> Result < ( PCSKey , Vec < CompactECKey < Private > > ) , PushError > {
610+ pub fn decrypt_with_keychain ( & self , keychain : & KeychainClientState , service : & PCSService < ' _ > ) -> Result < ( Vec < PCSKey > , Vec < CompactECKey < Private > > ) , PushError > {
603611 info ! ( "Decoding with {}" , base64_encode( & self . decode_key_public( ) ?) ) ;
604612 let account = Value :: String ( base64_encode ( & self . decode_key_public ( ) ?) ) ;
605613 let item = keychain. items [ service. zone ] . keys . values ( ) . find ( |x| x. get ( "acct" ) . expect ( "No acct?" ) == & account) . ok_or ( PushError :: ShareKeyNotFound ) ?;
@@ -624,6 +632,7 @@ impl PCSShareProtection {
624632 keyset. make_checksum ( ) ;
625633
626634 let identities = PCSShareProtectionIdentities {
635+ symm_keys : None ,
627636 tag1 : Default :: default ( ) ,
628637 identities : if keys. is_empty ( ) { None } else { Some ( BTreeSet :: from_iter ( [
629638 PCSShareProtectionIdentityData {
@@ -666,10 +675,13 @@ impl PCSShareProtection {
666675 pub_key : master_ec_key. public_key ( ) . to_bytes ( master_ec_key. group ( ) , PointConversionForm :: UNCOMPRESSED , & mut num_ctx) ?. into ( ) ,
667676 } ,
668677 signature : Default :: default ( ) ,
669- keylist : if keys. is_empty ( ) { None } else { Some ( keys. iter ( ) . map ( |k| PCSKeyRef {
678+ ec_key_list : if keys. is_empty ( ) { None } else { Some ( keys. iter ( ) . map ( |k| PCSKeyRef {
670679 keytype : 3 ,
671680 pub_key : k. compress ( ) . to_vec ( ) . into ( ) ,
672- } ) . collect ( ) ) }
681+ } ) . collect ( ) ) } ,
682+ symm_key_count : None ,
683+ signature_2 : None ,
684+ attributes : None ,
673685 } ;
674686
675687 let digest_data = protection. digest_data ( & signature) ;
@@ -705,7 +717,8 @@ impl PCSShareProtection {
705717 Ok ( protection)
706718 }
707719
708- pub fn decode ( & self , key : & CompactECKey < Private > ) -> Result < ( PCSKey , Vec < CompactECKey < Private > > ) , PushError > {
720+ pub fn decode ( & self , key : & CompactECKey < Private > ) -> Result < ( Vec < PCSKey > , Vec < CompactECKey < Private > > ) , PushError > {
721+ info ! ( "Decoding share protection!" ) ;
709722 let master_key = PCSKey :: new ( key, & self . keyset . keyset . first ( ) . unwrap ( ) . ciphertext ) ?;
710723
711724 let sig = self . signature_data ( ) ;
@@ -723,7 +736,15 @@ impl PCSShareProtection {
723736 let mut verifier = Verifier :: new ( MessageDigest :: sha256 ( ) , & key) ?;
724737 verifier. update ( & digest_data) ?;
725738 if !verifier. verify ( & sig. signature . signature ) ? {
726- panic ! ( "self sig check failed" )
739+ if let Some ( past_signature) = & sig. signature_2 {
740+ let mut verifier = Verifier :: new ( MessageDigest :: sha256 ( ) , & key) ?;
741+ verifier. update ( & digest_data) ?;
742+ if !verifier. verify ( & past_signature. signature ) ? {
743+ panic ! ( "self sig 1 and 2 check failed" )
744+ }
745+ } else {
746+ panic ! ( "self sig check failed" )
747+ }
727748 }
728749
729750 let hmackey = kdf_ctr_hmac ( & master_key. 0 , "hmackey-of-masterkey" . as_bytes ( ) , & [ ] , master_key. 0 . len ( ) ) ;
@@ -747,7 +768,10 @@ impl PCSShareProtection {
747768 }
748769 }
749770
750- Ok ( ( master_key, keys) )
771+ let mut pcs_keys = vec ! [ master_key] ;
772+ pcs_keys. extend ( identities. symm_keys . unwrap_or_default ( ) . into_iter ( ) . map ( |symm| PCSKey ( symm. to_vec ( ) ) ) ) ;
773+
774+ Ok ( ( pcs_keys, keys) )
751775 }
752776}
753777
@@ -758,6 +782,13 @@ pub struct PCSObjectSignature {
758782 unk2 : u32 ,
759783 public : PCSKeyRef ,
760784 signature : PCSSignature ,
785+ // the ignore fields show up in weird situations, when there are multiple keys?
786+ #[ rasn( tag( explicit( context, 0 ) ) ) ]
787+ symm_key_count : Option < u32 > ,
788+ #[ rasn( tag( explicit( context, 1 ) ) ) ]
789+ signature_2 : Option < PCSSignature > ,
761790 #[ rasn( tag( explicit( context, 2 ) ) ) ]
762- keylist : Option < SequenceOf < PCSKeyRef > > ,
791+ ec_key_list : Option < SequenceOf < PCSKeyRef > > ,
792+ #[ rasn( tag( explicit( context, 3 ) ) ) ]
793+ attributes : Option < SequenceOf < PCSAttribute > > ,
763794}
0 commit comments