-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathJWK+Extensions.swift
153 lines (135 loc) · 5.62 KB
/
JWK+Extensions.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
//
// Copyright (c) 2022-Present, Okta, Inc. and/or its affiliates. All rights reserved.
// The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
//
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
// See the License for the specific language governing permissions and limitations under the License.
//
import Foundation
#if canImport(CommonCrypto)
import CommonCrypto
extension JWK.Algorithm {
@available(iOS 10.0, macCatalyst 13.0, tvOS 10.0, watchOS 3.0, macOS 10.12, *)
var secKeyAlgorithm: SecKeyAlgorithm? {
switch self {
case .rs256:
return .rsaSignatureMessagePKCS1v15SHA256
case .rs384:
return .rsaSignatureMessagePKCS1v15SHA384
case .rs512:
return .rsaSignatureMessagePKCS1v15SHA512
default:
return nil
}
}
var secPadding: SecPadding? {
switch self {
case .rs256:
return .PKCS1SHA256
case .rs384:
return .PKCS1SHA384
case .rs512:
return .PKCS1SHA512
default:
return nil
}
}
func digest(data: Data) -> Data? {
switch self {
case .rs256:
var digestBytes = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA256($0.baseAddress, CC_LONG(data.count), &digestBytes)
}
return Data(digestBytes)
case .rs384:
var digestBytes = [UInt8](repeating: 0, count: Int(CC_SHA384_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA384($0.baseAddress, CC_LONG(data.count), &digestBytes)
}
return Data(digestBytes)
case .rs512:
var digestBytes = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH))
data.withUnsafeBytes {
_ = CC_SHA512($0.baseAddress, CC_LONG(data.count), &digestBytes)
}
return Data(digestBytes)
default:
return nil
}
}
}
extension JWK {
var rsaData: Data? {
guard let rsaModulus = rsaModulus?.base64URLDecoded,
let rsaExponent = rsaExponent?.base64URLDecoded,
let modulusData = Data(base64Encoded: rsaModulus),
let exponentData = Data(base64Encoded: rsaExponent)
else {
return nil
}
return Data(modulus: modulusData, exponent: exponentData)
}
func publicKey() throws -> SecKey {
guard let keyData = rsaData else {
throw JWTError.invalidKey
}
if #available(iOS 10.0, macCatalyst 13.0, tvOS 10.0, watchOS 3.0, macOS 10.12, *) {
let keySize = keyData.count * 8
var errorRef: Unmanaged<CFError>?
let attributes: [CFString: Any] = [
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeyClass: kSecAttrKeyClassPublic,
kSecAttrKeySizeInBits: NSNumber(value: keySize),
kSecReturnPersistentRef: true
]
guard let publicKey: SecKey = SecKeyCreateWithData(keyData as NSData,
attributes as NSDictionary,
&errorRef)
else {
let error = errorRef?.takeRetainedValue()
let desc = error != nil ? CFErrorCopyDescription(error) : nil
let code = error != nil ? CFErrorGetCode(error) : 0
throw JWTError.cannotCreateKey(code: OSStatus(code),
description: desc as String?)
}
return publicKey
} else {
let persistKey = UnsafeMutablePointer<AnyObject?>(mutating: nil)
let addAttributes: [CFString: Any] = [
kSecClass: kSecClassKey,
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecValueData: keyData,
kSecAttrKeyClass: kSecAttrKeyTypeRSA,
kSecReturnPersistentRef: true,
kSecAttrAccessible: kSecAttrAccessibleAfterFirstUnlock
]
let addStatus = SecItemAdd(addAttributes as CFDictionary, persistKey)
guard addStatus == errSecSuccess || addStatus == errSecDuplicateItem else {
throw JWTError.cannotCreateKey(code: addStatus,
description: nil)
}
let copyAttributes: [CFString: Any] = [
kSecClass: kSecClassKey,
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeyClass: kSecAttrKeyTypeRSA,
kSecAttrAccessible: kSecAttrAccessibleAfterFirstUnlock,
kSecReturnRef: true
]
var keyRef: AnyObject?
let copyStatus = SecItemCopyMatching(copyAttributes as CFDictionary, &keyRef)
guard let publicKey = keyRef else {
throw JWTError.cannotCreateKey(code: copyStatus,
description: nil)
}
// swiftlint:disable force_cast
return publicKey as! SecKey
// swiftlint:enable force_cast
}
}
}
#endif