forked from DagAgren/toot-relay
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPushNotificationReceiver.swift
154 lines (125 loc) · 5.53 KB
/
PushNotificationReceiver.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import Foundation
import Security
public struct PushNotificationReceiver: Codable, Equatable, Hashable {
public let privateKeyData: Data
public let publicKeyData: Data
public let authentication: Data
}
extension PushNotificationReceiver {
public init() throws {
var error: Unmanaged<CFError>?
guard let privateKey = SecKeyCreateRandomKey([
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
kSecAttrKeySizeInBits as String: 256,
] as CFDictionary, &error) else {
throw PushNotificationReceiverErrorType.creatingKeyFailed(error?.takeRetainedValue())
}
guard let privateKeyData = SecKeyCopyExternalRepresentation(privateKey, &error) as Data? else {
throw PushNotificationReceiverErrorType.extractingPrivateKeyFailed(error?.takeRetainedValue())
}
guard let publicKey = SecKeyCopyPublicKey(privateKey) else {
throw PushNotificationReceiverErrorType.impossible
}
guard let publicKeyData = SecKeyCopyExternalRepresentation(publicKey, &error) as Data? else {
throw PushNotificationReceiverErrorType.extractingPublicKeyFailed(error?.takeRetainedValue())
}
var authentication = Data(count: 16)
try authentication.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<UInt8>) -> Void in
guard SecRandomCopyBytes(kSecRandomDefault, 16, bytes) == errSecSuccess else {
throw PushNotificationReceiverErrorType.creatingRandomDataFailed(error?.takeRetainedValue())
}
}
self.init(
privateKeyData: privateKeyData,
publicKeyData: publicKeyData,
authentication: authentication
)
}
}
extension PushNotificationReceiver {
func decrypt(payload: Data, salt: Data, serverPublicKeyData: Data) throws -> Data {
var error: Unmanaged<CFError>?
guard let privateKey = SecKeyCreateWithData(privateKeyData as CFData,[
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
kSecAttrKeyClass as String: kSecAttrKeyClassPrivate,
kSecAttrKeySizeInBits as String: 256,
] as CFDictionary, &error) else {
throw PushNotificationReceiverErrorType.restoringKeyFailed(error?.takeRetainedValue())
}
guard let serverPublicKey = SecKeyCreateWithData(serverPublicKeyData as CFData,[
kSecAttrKeyType as String: kSecAttrKeyTypeECSECPrimeRandom,
kSecAttrKeyClass as String: kSecAttrKeyClassPublic,
kSecAttrKeySizeInBits as String: 256,
] as CFDictionary, &error) else {
throw PushNotificationReceiverErrorType.creatingKeyFailed(error?.takeRetainedValue())
}
guard let sharedSecret = SecKeyCopyKeyExchangeResult(privateKey, .ecdhKeyExchangeStandard, serverPublicKey, [:] as CFDictionary, &error) as Data? else {
throw PushNotificationReceiverErrorType.keyExhangedFailed(error?.takeRetainedValue())
}
// TODO: These steps are slightly different from aes128gcm
let secondSaltInfo = "Content-Encoding: auth\0".data(using: .utf8)!
let secondSalt = deriveKey(firstSalt: authentication, secondSalt: sharedSecret, info: secondSaltInfo, length: 32)
let keyInfo = info(type: "aesgcm", clientPublicKey: publicKeyData, serverPublicKey: serverPublicKeyData)
let key = deriveKey(firstSalt: salt, secondSalt: secondSalt, info: keyInfo, length: 16)
let nonceInfo = info(type: "nonce", clientPublicKey: publicKeyData, serverPublicKey: serverPublicKeyData)
let nonce = deriveKey(firstSalt: salt, secondSalt: secondSalt, info: nonceInfo, length: 12)
let gcm = try SwiftGCM(key: key, nonce: nonce, tagSize: 16)
let clearText = try gcm.decrypt(auth: nil, ciphertext: payload)
guard clearText.count >= 2 else {
throw PushNotificationReceiverErrorType.clearTextTooShort
}
let paddingLength = Int(clearText[0]) * 256 + Int(clearText[1])
guard clearText.count >= 2 + paddingLength else {
throw PushNotificationReceiverErrorType.clearTextTooShort
}
let unpadded = clearText.suffix(from: paddingLength + 2)
return unpadded
}
private func deriveKey(firstSalt: Data, secondSalt: Data, info: Data, length: Int) -> Data {
return firstSalt.withUnsafeBytes { (firstSaltBytes: UnsafePointer<UInt8>) -> Data in
return secondSalt.withUnsafeBytes { (secondSaltBytes: UnsafePointer<UInt8>) -> Data in
return info.withUnsafeBytes { (infoBytes: UnsafePointer<UInt8>) -> Data in
// RFC5869 Extract
var context = CCHmacContext()
CCHmacInit(&context, CCHmacAlgorithm(kCCHmacAlgSHA256), firstSaltBytes, firstSalt.count)
CCHmacUpdate(&context, secondSaltBytes, secondSalt.count)
var hmac: [UInt8] = .init(repeating: 0, count: 32)
CCHmacFinal(&context, &hmac)
// RFC5869 Expand
CCHmacInit(&context, CCHmacAlgorithm(kCCHmacAlgSHA256), &hmac, hmac.count)
CCHmacUpdate(&context, infoBytes, info.count)
var one: [UInt8] = [1] // Add sequence byte. We only support short keys so this is always just 1.
CCHmacUpdate(&context, &one, 1)
CCHmacFinal(&context, &hmac)
return Data(bytes: hmac.prefix(upTo: length))
}
}
}
}
private func info(type: String, clientPublicKey: Data, serverPublicKey: Data) -> Data {
var info = Data()
info.append("Content-Encoding: ".data(using: .utf8)!)
info.append(type.data(using: .utf8)!)
info.append(0)
info.append("P-256".data(using: .utf8)!)
info.append(0)
info.append(0)
info.append(65)
info.append(clientPublicKey)
info.append(0)
info.append(65)
info.append(serverPublicKey)
return info
}
}
enum PushNotificationReceiverErrorType: Error {
case invalidKey
case impossible
case creatingKeyFailed(Error?)
case restoringKeyFailed(Error?)
case extractingPrivateKeyFailed(Error?)
case extractingPublicKeyFailed(Error?)
case creatingRandomDataFailed(Error?)
case keyExhangedFailed(Error?)
case clearTextTooShort
}