Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
sec_transform = None
try:
cf_dict = CFHelpers.cf_dictionary_from_pairs([(Security.kSecAttrKeyType, cipher)])
cf_key = CFHelpers.cf_data_from_bytes(key)
cf_data = CFHelpers.cf_data_from_bytes(data)
error_pointer = new(CoreFoundation, 'CFErrorRef *')
sec_key = Security.SecKeyCreateFromData(cf_dict, cf_key, error_pointer)
handle_cf_error(error_pointer)
sec_transform = Security.SecDecryptTransformCreate(sec_key, error_pointer)
handle_cf_error(error_pointer)
if cipher != Security.kSecAttrKeyTypeRC4:
Security.SecTransformSetAttribute(sec_transform, Security.kSecModeCBCKey, null(), error_pointer)
handle_cf_error(error_pointer)
Security.SecTransformSetAttribute(sec_transform, Security.kSecPaddingKey, padding, error_pointer)
handle_cf_error(error_pointer)
cf_iv = CFHelpers.cf_data_from_bytes(iv)
Security.SecTransformSetAttribute(sec_transform, Security.kSecIVKey, cf_iv, error_pointer)
handle_cf_error(error_pointer)
Security.SecTransformSetAttribute(
sec_transform,
Security.kSecTransformInputAttributeName,
cf_data,
error_pointer
)
handle_cf_error(error_pointer)
error_pointer = new(CoreFoundation, 'CFErrorRef *')
sec_key = Security.SecKeyCreateFromData(cf_dict, cf_key, error_pointer)
handle_cf_error(error_pointer)
sec_transform = Security.SecDecryptTransformCreate(sec_key, error_pointer)
handle_cf_error(error_pointer)
if cipher != Security.kSecAttrKeyTypeRC4:
Security.SecTransformSetAttribute(sec_transform, Security.kSecModeCBCKey, null(), error_pointer)
handle_cf_error(error_pointer)
Security.SecTransformSetAttribute(sec_transform, Security.kSecPaddingKey, padding, error_pointer)
handle_cf_error(error_pointer)
cf_iv = CFHelpers.cf_data_from_bytes(iv)
Security.SecTransformSetAttribute(sec_transform, Security.kSecIVKey, cf_iv, error_pointer)
handle_cf_error(error_pointer)
Security.SecTransformSetAttribute(
sec_transform,
Security.kSecTransformInputAttributeName,
cf_data,
error_pointer
)
handle_cf_error(error_pointer)
plaintext = Security.SecTransformExecute(sec_transform, error_pointer)
handle_cf_error(error_pointer)
return CFHelpers.cf_data_to_bytes(plaintext)
finally:
error_pointer = new(CoreFoundation, 'CFErrorRef *')
sec_key = Security.SecKeyCreateFromData(cf_dict, cf_key, error_pointer)
handle_cf_error(error_pointer)
sec_transform = Security.SecEncryptTransformCreate(sec_key, error_pointer)
handle_cf_error(error_pointer)
if cipher != Security.kSecAttrKeyTypeRC4:
Security.SecTransformSetAttribute(sec_transform, Security.kSecModeCBCKey, null(), error_pointer)
handle_cf_error(error_pointer)
Security.SecTransformSetAttribute(sec_transform, Security.kSecPaddingKey, padding, error_pointer)
handle_cf_error(error_pointer)
cf_iv = CFHelpers.cf_data_from_bytes(iv)
Security.SecTransformSetAttribute(sec_transform, Security.kSecIVKey, cf_iv, error_pointer)
handle_cf_error(error_pointer)
Security.SecTransformSetAttribute(
sec_transform,
Security.kSecTransformInputAttributeName,
cf_data,
error_pointer
)
handle_cf_error(error_pointer)
ciphertext = Security.SecTransformExecute(sec_transform, error_pointer)
handle_cf_error(error_pointer)
return CFHelpers.cf_data_to_bytes(ciphertext)
finally:
hash_constant,
error_pointer
)
handle_cf_error(error_pointer)
if hash_algorithm in set(['sha224', 'sha256', 'sha384', 'sha512']):
hash_length = {
'sha224': 224,
'sha256': 256,
'sha384': 384,
'sha512': 512
}[hash_algorithm]
cf_hash_length = CFHelpers.cf_number_from_integer(hash_length)
Security.SecTransformSetAttribute(
sec_transform,
Security.kSecDigestLengthAttribute,
cf_hash_length,
error_pointer
)
handle_cf_error(error_pointer)
if certificate_or_public_key.algorithm == 'rsa':
Security.SecTransformSetAttribute(
sec_transform,
Security.kSecPaddingKey,
Security.kSecPaddingPKCS1Key,
error_pointer
)
handle_cf_error(error_pointer)
cf_hash_length,
error_pointer
)
handle_cf_error(error_pointer)
if private_key.algorithm == 'rsa':
Security.SecTransformSetAttribute(
sec_transform,
Security.kSecPaddingKey,
Security.kSecPaddingPKCS1Key,
error_pointer
)
handle_cf_error(error_pointer)
cf_data = CFHelpers.cf_data_from_bytes(data)
Security.SecTransformSetAttribute(
sec_transform,
Security.kSecTransformInputAttributeName,
cf_data,
error_pointer
)
handle_cf_error(error_pointer)
cf_signature = Security.SecTransformExecute(sec_transform, error_pointer)
handle_cf_error(error_pointer)
return CFHelpers.cf_data_to_bytes(cf_signature)
finally:
if sec_transform:
CoreFoundation.CFRelease(sec_transform)
if cf_signature:
try:
error_pointer = new(CoreFoundation, 'CFErrorRef *')
sec_transform = Security.SecSignTransformCreate(private_key.sec_key_ref, error_pointer)
handle_cf_error(error_pointer)
hash_constant = {
'md5': Security.kSecDigestMD5,
'sha1': Security.kSecDigestSHA1,
'sha224': Security.kSecDigestSHA2,
'sha256': Security.kSecDigestSHA2,
'sha384': Security.kSecDigestSHA2,
'sha512': Security.kSecDigestSHA2
}[hash_algorithm]
Security.SecTransformSetAttribute(
sec_transform,
Security.kSecDigestTypeAttribute,
hash_constant,
error_pointer
)
handle_cf_error(error_pointer)
if hash_algorithm in set(['sha224', 'sha256', 'sha384', 'sha512']):
hash_length = {
'sha224': 224,
'sha256': 256,
'sha384': 384,
'sha512': 512
}[hash_algorithm]
cf_hash_length = CFHelpers.cf_number_from_integer(hash_length)
sec_transform = Security.SecEncryptTransformCreate(sec_key, error_pointer)
handle_cf_error(error_pointer)
if cipher != Security.kSecAttrKeyTypeRC4:
Security.SecTransformSetAttribute(sec_transform, Security.kSecModeCBCKey, null(), error_pointer)
handle_cf_error(error_pointer)
Security.SecTransformSetAttribute(sec_transform, Security.kSecPaddingKey, padding, error_pointer)
handle_cf_error(error_pointer)
cf_iv = CFHelpers.cf_data_from_bytes(iv)
Security.SecTransformSetAttribute(sec_transform, Security.kSecIVKey, cf_iv, error_pointer)
handle_cf_error(error_pointer)
Security.SecTransformSetAttribute(
sec_transform,
Security.kSecTransformInputAttributeName,
cf_data,
error_pointer
)
handle_cf_error(error_pointer)
ciphertext = Security.SecTransformExecute(sec_transform, error_pointer)
handle_cf_error(error_pointer)
return CFHelpers.cf_data_to_bytes(ciphertext)
finally:
if cf_dict:
CoreFoundation.CFRelease(cf_dict)
if cf_key: