-
Notifications
You must be signed in to change notification settings - Fork 94
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 #43 from LINE-Client/feature/ecdsa
Change JWT verification from RSA to ECDSA
- Loading branch information
Showing
34 changed files
with
799 additions
and
216 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,100 @@ | ||
// | ||
// ECDRA.swift | ||
// | ||
// Copyright (c) 2016-present, LINE Corporation. All rights reserved. | ||
// | ||
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use, | ||
// copy and distribute this software in source code or binary form for use | ||
// in connection with the web services and APIs provided by LINE Corporation. | ||
// | ||
// As with any software that integrates with the LINE Corporation platform, your use of this software | ||
// is subject to the LINE Developers Agreement [http://terms2.line.me/LINE_Developers_Agreement]. | ||
// This copyright notice shall be included in all copies or substantial portions of the software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// | ||
|
||
import Foundation | ||
import CommonCrypto | ||
|
||
/// Namespace for ECDRA related things. | ||
struct ECDSA {} | ||
|
||
/// ECDRA Digest Algorithms. | ||
extension ECDSA { | ||
enum Curve: String, Decodable { | ||
case P256 = "P-256" | ||
case P384 = "P-384" | ||
case P521 = "P-521" | ||
|
||
var signatureOctetLength: Int { | ||
return coordinateOctetLength * 2 | ||
} | ||
|
||
// Standards for Efficient Cryptography Group SEC 1: | ||
// Elliptic Curve Cryptography | ||
// http://www.secg.org/sec1-v2.pdf | ||
var coordinateOctetLength: Int { | ||
switch self { | ||
case .P256: | ||
return 32 | ||
case .P384: | ||
return 48 | ||
case .P521: | ||
return 66 | ||
} | ||
} | ||
} | ||
|
||
enum Algorithm: CryptoAlgorithm { | ||
case sha1, sha224, sha256, sha384, sha512 | ||
|
||
var length: CC_LONG { | ||
switch self { | ||
case .sha1: return CC_LONG(CC_SHA1_DIGEST_LENGTH) | ||
case .sha224: return CC_LONG(CC_SHA224_DIGEST_LENGTH) | ||
case .sha256: return CC_LONG(CC_SHA256_DIGEST_LENGTH) | ||
case .sha384: return CC_LONG(CC_SHA384_DIGEST_LENGTH) | ||
case .sha512: return CC_LONG(CC_SHA512_DIGEST_LENGTH) | ||
} | ||
} | ||
|
||
var signatureAlgorithm: SecKeyAlgorithm { | ||
switch self { | ||
case .sha1: return .ecdsaSignatureMessageX962SHA1 | ||
case .sha224: return .ecdsaSignatureMessageX962SHA224 | ||
case .sha256: return .ecdsaSignatureMessageX962SHA256 | ||
case .sha384: return .ecdsaSignatureMessageX962SHA384 | ||
case .sha512: return .ecdsaSignatureMessageX962SHA512 | ||
} | ||
} | ||
|
||
var encryptionAlgorithm: SecKeyAlgorithm { | ||
Log.fatalError("ECDSA should be only used for signing purpose.") | ||
} | ||
|
||
var digest: CryptoDigest { | ||
switch self { | ||
case .sha1: return CC_SHA1 | ||
case .sha224: return CC_SHA224 | ||
case .sha256: return CC_SHA256 | ||
case .sha384: return CC_SHA384 | ||
case .sha512: return CC_SHA512 | ||
} | ||
} | ||
|
||
var curve: Curve { | ||
switch self { | ||
case .sha1, .sha224: Log.fatalError("Too simple SHA algorithm. Not supported.") | ||
case .sha256: return .P256 | ||
case .sha384: return .P384 | ||
case .sha512: return .P521 | ||
} | ||
} | ||
} | ||
} |
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,49 @@ | ||
// | ||
// CryptoAlgorithm.swift | ||
// | ||
// Copyright (c) 2016-present, LINE Corporation. All rights reserved. | ||
// | ||
// You are hereby granted a non-exclusive, worldwide, royalty-free license to use, | ||
// copy and distribute this software in source code or binary form for use | ||
// in connection with the web services and APIs provided by LINE Corporation. | ||
// | ||
// As with any software that integrates with the LINE Corporation platform, your use of this software | ||
// is subject to the LINE Developers Agreement [http://terms2.line.me/LINE_Developers_Agreement]. | ||
// This copyright notice shall be included in all copies or substantial portions of the software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
// | ||
|
||
import Foundation | ||
import CommonCrypto | ||
|
||
typealias CryptoDigest = ( | ||
_ data: UnsafeRawPointer?, | ||
_ length: CC_LONG, | ||
_ md: UnsafeMutablePointer<UInt8>?) -> UnsafeMutablePointer<UInt8>? | ||
|
||
/// Represents an algorithm used in crypto. | ||
protocol CryptoAlgorithm { | ||
var length: CC_LONG { get } | ||
var signatureAlgorithm: SecKeyAlgorithm { get } | ||
var encryptionAlgorithm: SecKeyAlgorithm { get } | ||
var digest: CryptoDigest { get } | ||
} | ||
|
||
extension Data { | ||
|
||
/// Calculate the digest with a given algorithm. | ||
/// | ||
/// - Parameter algorithm: The algorithm be used. It should provice a digest hash method at least. | ||
/// - Returns: The digest data. | ||
func digest(using algorithm: CryptoAlgorithm) -> Data { | ||
var hash = [UInt8](repeating: 0, count: Int(algorithm.length)) | ||
withUnsafeBytes { _ = algorithm.digest($0, CC_LONG(count), &hash) } | ||
return Data(bytes: 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
Oops, something went wrong.