Skip to main content
 首页 » 编程设计

python之使用 Python 中的 Security.framework 类型

2025年05月04日15mfryf

我正在编写一个 Python 脚本,它通过 ctypes 从 OS X 上的 Security.framework 调用函数。

我能够在 Python 中制作 CFDictionaryRef 和 CFStringRef,并将字符串存储在字典中,但现在我要添加 Security.framework 查询对象(kSecClass,kSecMatchLimit, kSecReturnAttributes, …) 我遇到以下崩溃:

objc[28201]: Method cache corrupted. This may be a message to an invalid object, or a memory error somewhere else. 
objc[28201]: receiver 0x7fff724fcdf8, SEL 0x7fff8d814fe5, isa 0x7fff724f5f90, cache 0x7fff724f5fa0, buckets 0x7fff8842ffe3, mask 0x5, occupied 0x0, wrap bucket 0x7fff8842ffe3 
objc[28201]: receiver 0 bytes, buckets 0 bytes 
objc[28201]: selector 'hash' 
objc[28201]: isa '(null)' 
objc[28201]: Method cache corrupted. 
zsh: illegal hardware instruction  python kc.py 

我试过用包含对象的 ctypes 数组调用 CFDictionaryCreate(),然后调用 CFMutableDictionaryCreate() CFDictionarySetValue。见下文。

import ctypes 
import ctypes.util 
from ctypes import CFUNCTYPE 
 
security = ctypes.CDLL(ctypes.util.find_library('Security')) 
foundation = ctypes.CDLL(ctypes.util.find_library('Foundation')) 
 
CFDictionaryRef = ctypes.c_void_p 
CFMutableDictionaryRef = ctypes.c_void_p 
CFTypeRef = ctypes.c_void_p 
CFIndex = ctypes.c_long 
 
CFDictionaryCreate = CFUNCTYPE(CFDictionaryRef, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, CFIndex, ctypes.c_void_p, ctypes.c_void_p)(('CFDictionaryCreate', foundation)) 
CFDictionaryCreateMutable = CFUNCTYPE(CFMutableDictionaryRef, CFIndex, ctypes.c_void_p, ctypes.c_void_p)(('CFDictionaryCreateMutable', foundation)) 
CFDictionarySetValue = CFUNCTYPE(CFMutableDictionaryRef, ctypes.c_void_p, ctypes.c_void_p)(('CFDictionarySetValue', foundation)) 
 
def createCFDictionary(items): 
    """ 
    Create a CFDictionaryRef from items = [(CFTypeRef, CFTypeRef), ...] 
    """ 
    # Create C arrays for the keys and values (which are cast as CFTypeRefs). 
    keys_array = (CFTypeRef * len(items))(*[ctypes.cast(k, CFTypeRef) for k, v in items]) 
    values_array = (CFTypeRef * len(items))(*[ctypes.cast(v, CFTypeRef) for k, v in items]) 
 
    return CFDictionaryCreate(None, 
                              keys_array, values_array, len(items), 
                              foundation.kCFTypeDictionaryKeyCallBacks, foundation.kCFTypeDictionaryValueCallBacks) 
 
# 1. Create a CFDictionary with the items in it (crashes). 
query = createCFDictionary([(security.kSecClass, security.kSecClassCertificate)]) 
 
 
# 2a. Create a CFMutableDictionary. 
query = CFDictionaryCreateMutable(0, foundation.kCFTypeDictionaryKeyCallBacks, foundation.kCFTypeDictionaryValueCallBacks) 
 
# 2b. Add items to it (crashes). 
CFDictionarySetValue(query, security.kSecClass, security.kSecClassCertificate) 

如果这是一个更简单的解决方案,我很乐意使用 PyObjC 而不是 ctypes;然而,我对它不太熟悉,并且无法导入 Security.framework。

OS X 10.9.2、Python 2.7.5(包含在 OS X 中)。

请您参考如下方法:

我正在研究案例 #2,因为它看起来更简单。

您的 CFDictionarySetValue 缺少 dict 参数。修复该问题后,security.kSecClass 似乎实际上返回了一个函数引用,这很糟糕。我认为您需要:

ctypes.c_void_p.in_dll(foundation, 'kSecClass') 

这导致将正确的值传递给函数(如 lldb 中所示)。

不知何故,kCFTypeDictionaryKeyCallBacks 结构仍然不适用于该机制,可能是因为这些符号实际上指向一个结构,而不是指向结构的指针。我想不出任何方法让 ctypes 返回符号代表的实际地址(这是你想传递给 CFDictionaryCreateMutable 的地址)而不是那个符号的值(0,因为这是该结构的第一个成员)。

您可以自己构建回调,这应该更容易,因为您可以在 ctypes 中定义结构类型,然后只获取所需的函数。