@@ -18,7 +18,7 @@ use std::str::FromStr;
1818use uuid:: Uuid ;
1919use aes_gcm:: KeyInit ;
2020
21- use crate :: { auth:: { MobileMeDelegateResponse , TokenProvider } , ids:: CompactECKey , keychain:: KeychainClient , mmcs:: { get_headers, get_mmcs, put_authorize_body, put_mmcs, AuthorizedOperation , MMCSConfig , PreparedPut } , mmcsp:: FordChunk , pcs:: { PCSKey , PCSPrivateKey , PCSService , PCSShareProtection } , prepare_put, util:: { base64_decode, base64_encode, decode_hex, decode_uleb128, encode_hex, encode_uleb128, gzip_normal, kdf_ctr_hmac, rfc6637_unwrap_key, REQWEST } , FileContainer , OSConfig , PushError } ;
21+ use crate :: { auth:: { MobileMeDelegateResponse , TokenProvider } , ids:: CompactECKey , keychain:: KeychainClient , mmcs:: { get_headers, get_mmcs, put_authorize_body, put_mmcs, AuthorizedOperation , MMCSConfig , PreparedPut } , mmcsp:: FordChunk , pcs:: { PCSKey , PCSKeys , PCSPrivateKey , PCSService , PCSShareProtection } , prepare_put, util:: { base64_decode, base64_encode, decode_hex, decode_uleb128, encode_hex, encode_uleb128, gzip_normal, kdf_ctr_hmac, rfc6637_unwrap_key, REQWEST } , FileContainer , OSConfig , PushError } ;
2222
2323fn undelimit_response ( resp : & mut impl Read ) -> Vec < Vec < u8 > > {
2424 let mut response: Vec < Vec < u8 > > = vec ! [ ] ;
@@ -43,15 +43,15 @@ pub struct FetchedRecords {
4343}
4444
4545impl FetchedRecords {
46- pub fn get_record < R : CloudKitRecord > ( & self , record_id : & str , key : Option < & PCSKeys > ) -> R {
46+ pub fn get_record < R : CloudKitRecord > ( & self , record_id : & str , key : Option < & PCSZoneConfig > ) -> R {
4747 self . responses . iter ( ) . find_map ( |response| {
4848 let r = response. record_retrieve_response . as_ref ( ) . expect ( "No retrieve response?" ) . record . as_ref ( ) . expect ( "No record?" ) ;
4949 if r. record_identifier . as_ref ( ) . expect ( "No record id?" ) . value . as_ref ( ) . expect ( "No identifier" ) . name . as_ref ( ) . expect ( "No name?" ) == record_id {
5050 let got_type = r. r#type . as_ref ( ) . expect ( "no TYpe" ) . name . as_ref ( ) . expect ( "No ta" ) ;
5151 if got_type. as_str ( ) != R :: record_type ( ) {
5252 panic ! ( "Wrong record type, got {} expected {}" , got_type, R :: record_type( ) ) ;
5353 }
54- let key = key. map ( |k| pcs_key_for_record ( r, k) . expect ( "PCS key failed" ) ) ;
54+ let key = key. map ( |k| pcs_keys_for_record ( r, k) . expect ( "PCS key failed" ) ) ;
5555 Some ( R :: from_record_encrypted ( & r. record_field , key. as_ref ( ) . map ( |k| ( k, r. record_identifier . as_ref ( ) . unwrap ( ) ) ) ) )
5656 } else { None }
5757 } ) . expect ( "No record found?" )
@@ -100,14 +100,16 @@ pub trait CloudKitOp {
100100 }
101101}
102102
103- pub fn pcs_key_for_record ( record : & Record , keys : & PCSKeys ) -> Result < PCSKey , PushError > {
103+ pub fn pcs_keys_for_record ( record : & Record , keys : & PCSZoneConfig ) -> Result < PCSKeys , PushError > {
104104 let Some ( protection) = & record. protection_info else {
105105 let Some ( pcskey) = & record. pcs_key else { panic ! ( "No PCS Key??" ) } ;
106- let keys = keys. default_record_keys . iter ( ) . find ( |i| i. key_id ( ) . ok ( ) . map ( |id| pcskey == & id[ ..pcskey. len ( ) ] ) . unwrap_or ( false ) ) ;
106+ if !keys. default_record_keys . iter ( ) . any ( |i| i. key_id ( ) . ok ( ) . map ( |id| pcskey == & id[ ..pcskey. len ( ) ] ) . unwrap_or ( false ) ) {
107+ return Err ( PushError :: PCSRecordKeyMissing ) ;
108+ }
107109
108- return Ok ( keys. ok_or ( PushError :: PCSRecordKeyMissing ) ? . clone ( ) )
110+ return Ok ( PCSKeys ( keys. default_record_keys . clone ( ) ) )
109111 } ;
110- Ok ( keys. decode_record_protection ( protection) ?. remove ( 0 ) )
112+ Ok ( PCSKeys ( keys. decode_record_protection ( protection) ?) )
111113}
112114
113115pub struct UploadAssetOperation ( pub cloudkit_proto:: AssetUploadTokenRetrieveRequest ) ;
@@ -191,7 +193,7 @@ impl CloudKitOp for SaveRecordOperation {
191193
192194impl SaveRecordOperation {
193195 // new with a *custom* record protection entry
194- pub fn new_protected < R : CloudKitRecord > ( id : RecordIdentifier , record : R , key : & PCSKeys , update : Option < String > ) -> ( Self , String ) {
196+ pub fn new_protected < R : CloudKitRecord > ( id : RecordIdentifier , record : R , key : & PCSZoneConfig , update : Option < String > ) -> ( Self , String ) {
195197 // create a key for this record
196198 let record_protection = PCSShareProtection :: create ( & key. zone_keys [ 0 ] , & [ ] ) . unwrap ( ) ;
197199 let der = rasn:: der:: encode ( & record_protection) . unwrap ( ) ;
@@ -219,7 +221,7 @@ impl SaveRecordOperation {
219221 } ) , tag)
220222 }
221223
222- pub fn new < R : CloudKitRecord > ( id : RecordIdentifier , record : R , key : Option < & PCSKeys > , update : bool ) -> Self {
224+ pub fn new < R : CloudKitRecord > ( id : RecordIdentifier , record : R , key : Option < & PCSZoneConfig > , update : bool ) -> Self {
223225 Self ( cloudkit_proto:: RecordSaveRequest {
224226 record : Some ( cloudkit_proto:: Record {
225227 record_identifier : Some ( id. clone ( ) ) ,
@@ -244,14 +246,14 @@ pub struct FetchedRecord {
244246}
245247
246248impl FetchedRecord {
247- pub fn get_record < R : CloudKitRecord > ( & self , key : Option < & PCSKeys > ) -> R {
249+ pub fn get_record < R : CloudKitRecord > ( & self , key : Option < & PCSZoneConfig > ) -> R {
248250 let r = self . response . record_retrieve_response . as_ref ( ) . expect ( "No retrieve response?" ) . record . as_ref ( ) . expect ( "No record?" ) ;
249251
250252 let got_type = r. r#type . as_ref ( ) . expect ( "no TYpe" ) . name . as_ref ( ) . expect ( "No ta" ) ;
251253 if got_type. as_str ( ) != R :: record_type ( ) {
252254 panic ! ( "Wrong record type, got {} expected {}" , got_type, R :: record_type( ) ) ;
253255 }
254- let key = key. map ( |k| pcs_key_for_record ( r, k) . expect ( "no PCS key" ) ) ;
256+ let key = key. map ( |k| pcs_keys_for_record ( r, k) . expect ( "no PCS key" ) ) ;
255257 R :: from_record_encrypted ( & r. record_field , key. as_ref ( ) . map ( |k| ( k, r. record_identifier . as_ref ( ) . unwrap ( ) ) ) )
256258 }
257259
@@ -793,14 +795,14 @@ pub struct QueryResult<T: CloudKitRecord> {
793795}
794796
795797#[ derive( Clone ) ]
796- pub struct PCSKeys {
798+ pub struct PCSZoneConfig {
797799 zone_keys : Vec < CompactECKey < Private > > ,
798800 zone_protection_tag : Option < String > ,
799801 default_record_keys : Vec < PCSKey > ,
800802 pub record_prot_tag : Option < String > ,
801803}
802804
803- impl PCSKeys {
805+ impl PCSZoneConfig {
804806
805807 fn decode_record_protection ( & self , protection : & ProtectionInfo ) -> Result < Vec < PCSKey > , PushError > {
806808 let record_protection: PCSShareProtection = rasn:: der:: decode ( protection. protection_info ( ) ) . expect ( "Bad record protection?" ) ;
@@ -819,7 +821,7 @@ pub struct CloudKitOpenContainer<'t, T: AnisetteProvider> {
819821 container : CloudKitContainer < ' t > ,
820822 pub user_id : String ,
821823 pub client : Arc < CloudKitClient < T > > ,
822- pub keys : Mutex < HashMap < String , PCSKeys > > ,
824+ pub keys : Mutex < HashMap < String , PCSZoneConfig > > ,
823825}
824826
825827impl < ' t , T : AnisetteProvider > Deref for CloudKitOpenContainer < ' t , T > {
@@ -850,7 +852,7 @@ impl<'t, T: AnisetteProvider> CloudKitOpenContainer<'t, T> {
850852 cached_keys. remove ( & zone_name) ;
851853 }
852854
853- pub async fn get_zone_encryption_config ( & self , zone : & cloudkit_proto:: RecordZoneIdentifier , client : & KeychainClient < T > , pcs_service : & PCSService < ' _ > ) -> Result < PCSKeys , PushError > {
855+ pub async fn get_zone_encryption_config ( & self , zone : & cloudkit_proto:: RecordZoneIdentifier , client : & KeychainClient < T > , pcs_service : & PCSService < ' _ > ) -> Result < PCSZoneConfig , PushError > {
854856 let mut cached_keys = self . keys . lock ( ) . await ;
855857 let zone_name = zone. value . as_ref ( ) . unwrap ( ) . name ( ) . to_string ( ) ;
856858 if let Some ( key) = cached_keys. get ( & zone_name) {
@@ -888,7 +890,7 @@ impl<'t, T: AnisetteProvider> CloudKitOpenContainer<'t, T> {
888890
889891 let ( _parent_key, keys) = zone_protection. decrypt_with_keychain ( & data, pcs_service) ?;
890892
891- let mut keys = PCSKeys {
893+ let mut keys = PCSZoneConfig {
892894 zone_keys : keys,
893895 zone_protection_tag : zone. protection_info . as_ref ( ) . unwrap ( ) . protection_info_tag . clone ( ) ,
894896 default_record_keys : vec ! [ ] ,
0 commit comments