-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #285 from omise/feature/MIT-2470
Release v5.1.0 - Add support for new 3DS SDK
- Loading branch information
Showing
49 changed files
with
1,938 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import Foundation | ||
import CommonCrypto | ||
|
||
enum CryptoError: Error { | ||
case invalidKeyLength | ||
case creationError(Int) | ||
case updateError(Int) | ||
case finalError(Int) | ||
} | ||
|
||
// swiftlint:disable:next function_parameter_count function_body_length | ||
func cryptData( | ||
_ dataIn: Data, | ||
operation: CCOperation, // kCCEncrypt, kCCDecrypt | ||
mode: CCMode, // kCCModeECB, kCCModeCBC, etc. | ||
algorithm: CCAlgorithm, // kCCAlgorithmAES, kCCAlgorithmDES, etc. | ||
padding: CCPadding, // ccNoPadding, ccPKCS7Padding | ||
keyLength: size_t, | ||
iv: Data?, | ||
key: Data | ||
) throws -> Data { | ||
guard key.count == keyLength else { | ||
throw CryptoError.invalidKeyLength | ||
} | ||
|
||
var cryptor: CCCryptorRef? | ||
var status = CCCryptorCreateWithMode(operation, | ||
mode, | ||
algorithm, | ||
padding, | ||
iv?.withUnsafeBytes { $0.baseAddress }, | ||
key.withUnsafeBytes { $0.baseAddress }, | ||
keyLength, | ||
nil, | ||
0, | ||
0, // tweak XTS mode, numRounds | ||
0, // CCModeOptions | ||
&cryptor) | ||
|
||
if status != kCCSuccess { | ||
throw CryptoError.creationError(Int(status)) | ||
} | ||
|
||
guard let cryptor = cryptor else { | ||
throw CryptoError.creationError(Int(status)) | ||
} | ||
|
||
defer { | ||
CCCryptorRelease(cryptor) | ||
} | ||
|
||
let dataOutLength = CCCryptorGetOutputLength(cryptor, dataIn.count, true) | ||
var dataOut = Data(count: dataOutLength) | ||
var dataOutMoved = 0 | ||
|
||
status = dataOut.withUnsafeMutableBytes { dataOutPointer in | ||
dataIn.withUnsafeBytes { dataInPointer -> CCCryptorStatus in | ||
guard let dataInPointerBaseAddress = dataInPointer.baseAddress, | ||
let dataOutPointerBaseAddress = dataOutPointer.baseAddress else { | ||
return Int32(kCCParamError) | ||
} | ||
return CCCryptorUpdate( | ||
cryptor, | ||
dataInPointerBaseAddress, | ||
dataIn.count, | ||
dataOutPointerBaseAddress, | ||
dataOutLength, | ||
&dataOutMoved | ||
) | ||
} | ||
} | ||
|
||
if status != kCCSuccess { | ||
throw CryptoError.updateError(Int(status)) | ||
} | ||
|
||
var dataOutMovedFinal = 0 | ||
status = dataOut.withUnsafeMutableBytes { dataOutPointer in | ||
guard let dataOutPointerBaseAddress = dataOutPointer.baseAddress else { | ||
return Int32(kCCParamError) | ||
} | ||
|
||
return CCCryptorFinal( | ||
cryptor, | ||
dataOutPointerBaseAddress.advanced(by: dataOutMoved), | ||
dataOutLength - dataOutMoved, | ||
&dataOutMovedFinal | ||
) | ||
} | ||
|
||
if status != kCCSuccess { | ||
throw CryptoError.finalError(Int(status)) | ||
} | ||
|
||
dataOut.count = dataOutMoved + dataOutMovedFinal | ||
|
||
return dataOut | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Foundation | ||
|
||
extension String { | ||
var pemCertificate: String { | ||
self | ||
.replacingOccurrences(of: "-----BEGIN CERTIFICATE-----", with: "") | ||
.replacingOccurrences(of: "-----END CERTIFICATE-----", with: "") | ||
.replacingOccurrences(of: "\r\n", with: "") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Foundation | ||
import CommonCrypto | ||
|
||
extension String { | ||
public var sha512: Data { | ||
let data = Data(self.utf8) | ||
var hash = [UInt8](repeating: 0, count: Int(CC_SHA512_DIGEST_LENGTH)) | ||
|
||
data.withUnsafeBytes { | ||
_ = CC_SHA512($0.baseAddress, CC_LONG(data.count), &hash) | ||
} | ||
|
||
return Data(hash) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import Foundation | ||
|
||
public struct NetceteraConfig { | ||
public let id: String | ||
public let deviceInfoEncryptionAlg: String | ||
public let deviceInfoEncryptionEnc: String | ||
public let deviceInfoEncryptionCertPem: String | ||
public let directoryServerId: String | ||
public let key: String | ||
public let messageVersion: String | ||
} | ||
|
||
extension NetceteraConfig: Decodable { | ||
/// Mapping keys to encode/decode JSON string | ||
private enum CodingKeys: String, CodingKey { | ||
case id = "identifier" | ||
case deviceInfoEncryptionAlg = "device_info_encryption_alg" | ||
case deviceInfoEncryptionEnc = "device_info_encryption_enc" | ||
case deviceInfoEncryptionCertPem = "device_info_encryption_cert_pem" | ||
case directoryServerId = "directory_server_id" | ||
case key | ||
case messageVersion = "message_version" | ||
} | ||
} |
Oops, something went wrong.