我正在编写一个需要处理内存中的私钥的快速应用程序。由于此类对象的敏感性,在解除分配对象时需要清除键(也称为写入所有零),并且内存无法分页到磁盘(通常使用 mlock() 完成)。
在 Objective-C 中,你可以提供一个自定义的 CFAllocator 对象,它允许你使用自己的函数来分配/解除分配/重新分配对象使用的内存。
因此,一种解决方案是在objective-c中实现一个"SecureData"对象,该对象使用自定义CFAllocator(也在objective-c中)在内部创建一个NSMutableData对象。
但是,我有什么方法可以为纯 swift 对象(例如,结构体或 [UInt8])提供自己的自定义内存分配函数?或者有没有更好的、"适当"的方式来在 swift 中实现这样的安全内存?
如果你想完全控制你自己分配的内存区域,你可以使用 UnsafePointer
和 co:
// allocate enough memory for ten Ints
var ump = UnsafeMutablePointer<Int>.alloc(10)
// memory is in an uninitialized raw state
// initialize that memory with Int objects
// (here, from a collection)
ump.initializeFrom(reverse(0..<10))
// memory property gives you access to the underlying value
ump.memory // 9
// UnsafeMutablePointer acts like an IndexType
ump.successor().memory // 8
// and it has a subscript, but it's not a CollectionType
ump[3] // = 6
// wrap it in an UnsafeMutableBufferPointer to treat it
// like a collection (or UnsafeBufferPointer if you don't
// need to be able to alter the values)
let col = UnsafeMutableBufferPointer(start: ump, count: 10)
col[3] = 99
println(",".join(map(col,toString)))
// prints 9,8,7,99,5,4,3,2,1,0
ump.destroy(10)
// now the allocated memory is back in a raw state
// you could re-allocate it...
ump.initializeFrom(0..<10)
ump.destroy(10)
// when you're done, deallocate the memory
ump.dealloc(10)
您还可以UnsafePointer
指向其他内存,例如由某些 C API 提供的内存。
UnsafePointer
可以传递到 C 函数中,这些函数采用指向连续内存块的指针。 因此,出于您的目的,您可以将此指针传递到类似 mlock
的函数中:
let count = 10
let ump = UnsafeMutablePointer.allocate<Int>(count)
mlock(ump, UInt(sizeof(Int) * count))
// initialize, use, and destroy the memory
munlock(ump, UInt(sizeof(Int) * count))
ump.dealloc(count)
您甚至可以保存自己的自定义类型:
struct MyStruct {
let a: Int
let b: Int
}
var pointerToStruct = UnsafeMutablePointer<MyStruct>.alloc(1)
pointerToStruct.initialize(MyStruct(a: 1, b: 2))
pointerToStruct.memory.b // 2
pointerToStruct.destroy()
pointerToStruct.dealloc(1)
但是,请注意,如果使用类,甚至是数组或字符串(或包含它们的结构)执行此操作,则内存中将保留的只是指向这些对象分配和拥有的其他内存的指针。 如果这对您很重要(即您正在对此内存执行一些特殊操作,例如在您的示例中保护它),这可能不是您想要的。
因此,要么需要使用固定大小的对象,要么进一步使用 UnsafePointer
来保存指向更多内存区域的指针。 如果它们不需要动态调整大小,那么只需分配一个不安全的指针(可能包装在集合接口的UnsafeBufferPointer
中)就可以做到这一点。
如果您需要更多动态行为,下面是一个集合的非常简单的实现,可以根据需要调整大小,可以对其进行增强以涵盖专用内存处理逻辑:
// Note this is a class not a struct, so it does NOT have value semantics,
// changing a copy changes all copies.
public class UnsafeCollection<T> {
private var _len: Int = 0
private var _buflen: Int = 0
private var _buf: UnsafeMutablePointer<T> = nil
public func removeAll(keepCapacity: Bool = false) {
_buf.destroy(_len)
_len = 0
if !keepCapacity {
_buf.dealloc(_buflen)
_buflen = 0
_buf = nil
}
}
public required init() { }
deinit { self.removeAll(keepCapacity: false) }
public var count: Int { return _len }
public var isEmpty: Bool { return _len == 0 }
}
满足MutableCollectionType
的要求(即 CollectionType
加上可分配的下标):
extension UnsafeCollection: MutableCollectionType {
typealias Index = Int
public var startIndex: Int { return 0 }
public var endIndex: Int { return _len }
public subscript(idx: Int) -> T {
get {
precondition(idx < _len)
return _buf[idx]
}
set(newElement) {
precondition(idx < _len)
let ptr = _buf.advancedBy(idx)
ptr.destroy()
ptr.initialize(newElement)
}
}
typealias Generator = IndexingGenerator<UnsafeCollection>
public func generate() -> Generator {
return Generator(self)
}
}
ExtensibleCollectionType
,允许动态增长:
extension UnsafeCollection: ExtensibleCollectionType {
public func reserveCapacity(n: Index.Distance) {
if n > _buflen {
let newBuf = UnsafeMutablePointer<T>.alloc(n)
newBuf.moveInitializeBackwardFrom(_buf, count: _len)
_buf.dealloc(_buflen)
_buf = newBuf
_buflen = n
}
}
public func append(x: T) {
if _len == _buflen {
reserveCapacity(Int(Double(_len) * 1.6) + 1)
}
_buf.advancedBy(_len++).initialize(x)
}
public func extend<S: SequenceType where S.Generator.Element == T>
(newElements: S) {
var g = newElements.generate()
while let x: T = g.next() {
self.append(x)
}
}
}
这个问题很老,但对于那些登陆这里的人来说:从iOS 10开始,您可以使用安全隔区安全地存储私钥。它的工作方式是所有需要解密的操作都在安全隔区内执行,因此您不必担心类的运行时挂钩或内存泄漏。
看看这里: https://developer.apple.com/documentation/security/certificate_key_and_trust_services/keys/storing_keys_in_the_secure_enclave