+ if (csp()->guid() != keychain->csp()->guid()) {
+ // Prepare wrapping key to be usable in target keychain's CSP.
+ CssmClient::WrapKey exportWrapKey(csp(), CSSM_ALGID_NONE);
+ CssmClient::Key exportedWrappingKey(exportWrapKey(wrappingKey));
+ CssmClient::UnwrapKey importUnwrapKey(keychain->csp(), CSSM_ALGID_NONE);
+ CssmClient::Key importedWrappingKey(importUnwrapKey(exportedWrappingKey, KeySpec(CSSM_KEYUSE_UNWRAP, 0)));
+ unwrap.key(importedWrappingKey);
+ } else {
+ // Wrapping key can be used directly, because source and target CSPs are the same.
+ unwrap.key(wrappingKey);
+ }