2 // AccountCloudParameters.c
6 #include "SOSAccountPriv.h"
7 #include <SecureObjectSync/SOSTransportKeyParameter.h>
9 // Cloud Paramters encode/decode
12 static size_t der_sizeof_cloud_parameters(SecKeyRef publicKey
, CFDataRef paramters
, CFErrorRef
* error
)
14 size_t public_key_size
= der_sizeof_public_bytes(publicKey
, error
);
15 size_t parameters_size
= der_sizeof_data_or_null(paramters
, error
);
17 return ccder_sizeof(CCDER_CONSTRUCTED_SEQUENCE
, public_key_size
+ parameters_size
);
20 static uint8_t* der_encode_cloud_parameters(SecKeyRef publicKey
, CFDataRef paramters
, CFErrorRef
* error
,
21 const uint8_t* der
, uint8_t* der_end
)
23 uint8_t* original_der_end
= der_end
;
25 return ccder_encode_constructed_tl(CCDER_CONSTRUCTED_SEQUENCE
, original_der_end
, der
,
26 der_encode_public_bytes(publicKey
, error
, der
,
27 der_encode_data_or_null(paramters
, error
, der
, der_end
)));
30 static const uint8_t* der_decode_cloud_parameters(CFAllocatorRef allocator
,
31 CFIndex algorithmID
, SecKeyRef
* publicKey
,
32 CFDataRef
*parameters
,
34 const uint8_t* der
, const uint8_t* der_end
)
36 const uint8_t *sequence_end
;
37 der
= ccder_decode_sequence_tl(&sequence_end
, der
, der_end
);
38 der
= der_decode_public_bytes(allocator
, algorithmID
, publicKey
, error
, der
, sequence_end
);
39 der
= der_decode_data_or_null(allocator
, parameters
, error
, der
, sequence_end
);
45 bool SOSAccountPublishCloudParameters(SOSAccountRef account
, CFErrorRef
* error
){
47 CFIndex cloud_der_len
= der_sizeof_cloud_parameters(
49 account
->user_key_parameters
,
51 CFMutableDataRef cloudParameters
=
52 CFDataCreateMutableWithScratch(kCFAllocatorDefault
, cloud_der_len
);
54 if (der_encode_cloud_parameters(account
->user_public
, account
->user_key_parameters
, error
,
55 CFDataGetMutableBytePtr(cloudParameters
),
56 CFDataGetMutablePastEndPtr(cloudParameters
)) != NULL
) {
58 CFErrorRef changeError
= NULL
;
59 if (SOSTrasnportKeyParameterPublishCloudParameters(account
->key_transport
, cloudParameters
, error
)) {
62 SOSCreateErrorWithFormat(kSOSErrorSendFailure
, changeError
, error
, NULL
,
63 CFSTR("update parameters key failed [%@]"), cloudParameters
);
65 CFReleaseSafe(changeError
);
67 SOSCreateError(kSOSErrorEncodeFailure
, CFSTR("Encoding parameters failed"), NULL
, error
);
70 CFReleaseNull(cloudParameters
);
75 bool SOSAccountRetrieveCloudParameters(SOSAccountRef account
, SecKeyRef
*newKey
,
77 CFDataRef
*newParameters
, CFErrorRef
* error
) {
78 const uint8_t *parse_end
= der_decode_cloud_parameters(kCFAllocatorDefault
, kSecECDSAAlgorithmID
,
79 newKey
, newParameters
, error
,
80 CFDataGetBytePtr(derparms
), CFDataGetPastEndPtr(derparms
));
82 if (parse_end
== CFDataGetPastEndPtr(derparms
)) return true;