Skip to content

Commit

Permalink
Merge pull request #2 from MojtabaHs/fix/code-typos
Browse files Browse the repository at this point in the history
Fix/code typos
  • Loading branch information
amosavian authored Oct 10, 2023
2 parents dad6265 + 422a333 commit 79e8f50
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 36 deletions.
7 changes: 5 additions & 2 deletions Sources/JWSETKit/Cryptography/Algorithms.swift
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,11 @@ public struct JSONWebKeyType: RawRepresentable, Hashable, Codable, ExpressibleBy
extension JSONWebKeyType {
static let empty: Self = ""

@available(*, deprecated, renamed: "ellipticCurve")

public static var elipticCurve: Self { ellipticCurve }
/// Elliptic Curve
public static let elipticCurve: Self = "EC"
public static let ellipticCurve: Self = "EC"

/// RSA
public static let rsa: Self = "RSA"
Expand Down Expand Up @@ -417,7 +420,7 @@ extension JSONWebSignatureAlgorithm {
case "RS", "PS":
return .rsa
case "ES", "Ed":
return .elipticCurve
return .ellipticCurve
default:
return .symmetric
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/JWSETKit/Cryptography/EC/CryptoKitAbstract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension CryptoECPublicKey {
public var storage: JSONWebValueStorage {
var result = AnyJSONWebKey()
let rawRepresentation = rawRepresentation
result.keyType = .elipticCurve
result.keyType = .ellipticCurve
result.curve = Self.curve
result.xCoordinate = rawRepresentation.prefix(rawRepresentation.count / 2)
result.yCoordinate = rawRepresentation.suffix(rawRepresentation.count / 2)
Expand Down Expand Up @@ -58,7 +58,7 @@ extension CryptoECPrivateKey {
public var storage: JSONWebValueStorage {
var result = AnyJSONWebKey()
let rawRepresentation = rawRepresentation
result.keyType = .elipticCurve
result.keyType = .ellipticCurve
result.curve = PublicKey.curve
result.xCoordinate = publicKey.rawRepresentation.prefix(rawRepresentation.count / 2)
result.yCoordinate = publicKey.rawRepresentation.suffix(rawRepresentation.count / 2)
Expand Down
24 changes: 12 additions & 12 deletions Sources/JWSETKit/Cryptography/EC/JWK-EC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ public struct JSONWebECPublicKey: MutableJSONWebKey, JSONWebValidatingKey, Senda
public func verifySignature<S, D>(_ signature: S, for data: D, using algorithm: JSONWebSignatureAlgorithm) throws where S: DataProtocol, D: DataProtocol {
// swiftformat:disable:next redundantSelf
switch (self.keyType ?? .empty, self.curve ?? .empty) {
case (JSONWebKeyType.elipticCurve, .p256):
case (JSONWebKeyType.ellipticCurve, .p256):
try P256.Signing.PublicKey.create(storage: storage)
.verifySignature(signature, for: data, using: algorithm)
case (JSONWebKeyType.elipticCurve, .p384):
case (JSONWebKeyType.ellipticCurve, .p384):
try P384.Signing.PublicKey.create(storage: storage)
.verifySignature(signature, for: data, using: algorithm)
case (JSONWebKeyType.elipticCurve, .p521):
case (JSONWebKeyType.ellipticCurve, .p521):
try P521.Signing.PublicKey.create(storage: storage)
.verifySignature(signature, for: data, using: algorithm)
case (JSONWebKeyType.elipticCurve, .ed25519):
case (JSONWebKeyType.ellipticCurve, .ed25519):
try Curve25519.Signing.PublicKey.create(storage: storage)
.verifySignature(signature, for: data, using: algorithm)
default:
Expand Down Expand Up @@ -66,16 +66,16 @@ public struct JSONWebECPrivateKey: MutableJSONWebKey, JSONWebSigningKey, Sendabl
public func signature<D>(_ data: D, using algorithm: JSONWebSignatureAlgorithm) throws -> Data where D: DataProtocol {
// swiftformat:disable:next redundantSelf
switch (self.keyType ?? .empty, self.curve ?? .empty) {
case (JSONWebKeyType.elipticCurve, .p256):
case (JSONWebKeyType.ellipticCurve, .p256):
return try P256.Signing.PrivateKey.create(storage: storage)
.signature(data, using: algorithm)
case (JSONWebKeyType.elipticCurve, .p384):
case (JSONWebKeyType.ellipticCurve, .p384):
return try P384.Signing.PrivateKey.create(storage: storage)
.signature(data, using: algorithm)
case (JSONWebKeyType.elipticCurve, .p521):
case (JSONWebKeyType.ellipticCurve, .p521):
return try P521.Signing.PrivateKey.create(storage: storage)
.signature(data, using: algorithm)
case (JSONWebKeyType.elipticCurve, .ed25519):
case (JSONWebKeyType.ellipticCurve, .ed25519):
return try Curve25519.Signing.PrivateKey.create(storage: storage)
.signature(data, using: algorithm)
default:
Expand All @@ -86,16 +86,16 @@ public struct JSONWebECPrivateKey: MutableJSONWebKey, JSONWebSigningKey, Sendabl
public func verifySignature<S, D>(_ signature: S, for data: D, using algorithm: JSONWebSignatureAlgorithm) throws where S: DataProtocol, D: DataProtocol {
// swiftformat:disable:next redundantSelf
switch (self.keyType ?? .empty, self.curve ?? .empty) {
case (JSONWebKeyType.elipticCurve, .p256):
case (JSONWebKeyType.ellipticCurve, .p256):
try P256.Signing.PublicKey.create(storage: storage)
.verifySignature(signature, for: data, using: algorithm)
case (JSONWebKeyType.elipticCurve, .p384):
case (JSONWebKeyType.ellipticCurve, .p384):
try P384.Signing.PublicKey.create(storage: storage)
.verifySignature(signature, for: data, using: algorithm)
case (JSONWebKeyType.elipticCurve, .p521):
case (JSONWebKeyType.ellipticCurve, .p521):
try P521.Signing.PublicKey.create(storage: storage)
.verifySignature(signature, for: data, using: algorithm)
case (JSONWebKeyType.elipticCurve, .ed25519):
case (JSONWebKeyType.ellipticCurve, .ed25519):
try Curve25519.Signing.PublicKey.create(storage: storage)
.verifySignature(signature, for: data, using: algorithm)
default:
Expand Down
16 changes: 11 additions & 5 deletions Sources/JWSETKit/Cryptography/KeyAccessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public struct JSONWebKeyRegisteredParameters {
/// -- not base64url-encoded) DER [ITU.X690.2008] PKIX certificate value.
public var certificateChain: [Certificate]

@available(*, deprecated, renamed: "certificateThumbprint")
public var certificateThumprint: Data? {
get { certificateThumbprint }
set { certificateThumbprint = newValue }
}

/// The "`x5t`"/"`x5t#S256`" (X.509 certificate SHA-1/256 thumbprint)
/// Header Parameter is a `base64url-encoded` SHA-1/256 thumbprint
/// (a.k.a. digest) of the `DER` encoding of the X.509 certificate [RFC5280]
Expand All @@ -112,7 +118,7 @@ public struct JSONWebKeyRegisteredParameters {
/// Note that certificate thumbprints are also sometimes known as certificate fingerprints.
///
/// Use of this Header Parameter is OPTIONAL.
public var certificateThumprint: Data?
public var certificateThumbprint: Data?

/// ECC curve or the subtype of key pair.
public var curve: JSONWebKeyCurve?
Expand Down Expand Up @@ -160,7 +166,7 @@ public struct JSONWebKeyRegisteredParameters {
\.keyType: "kty", \.keyUsage: "use", \.keyOperations: "key_ops",
\.algorithm: "alg", \.keyId: "kid",
\.certificateURL: "x5u", \.certificateChain: "x5c",
\.certificateThumprint: "x5t",
\.certificateThumbprint: "x5t",
\.curve: "crv", \.xCoordinate: "x", \.yCoordinate: "y",
\.privateKey: "d", \.modulus: "n", \.exponent: "e",
\.privateExponent: "d", \.firstPrimeFactor: "p", \.secondPrimeFactor: "q",
Expand Down Expand Up @@ -209,7 +215,7 @@ extension JSONWebKey {
@_documentation(visibility: private)
public subscript(dynamicMember keyPath: KeyPath<JSONWebKeyRegisteredParameters, Data?>) -> Data? {
switch keyPath {
case \.certificateThumprint where storage.contains(key: "x5t#S256"):
case \.certificateThumbprint where storage.contains(key: "x5t#S256"):
return storage["x5t#S256", true]
default:
return storage[stringKey(keyPath), true]
Expand Down Expand Up @@ -277,15 +283,15 @@ extension MutableJSONWebKey {
public subscript(dynamicMember keyPath: KeyPath<JSONWebKeyRegisteredParameters, Data?>) -> Data? {
get {
switch keyPath {
case \.certificateThumprint where storage.contains(key: "x5t#S256"):
case \.certificateThumbprint where storage.contains(key: "x5t#S256"):
return storage["x5t#S256", true]
default:
return storage[stringKey(keyPath), true]
}
}
set {
switch keyPath {
case \.certificateThumprint where newValue?.count == SHA256.byteCount:
case \.certificateThumbprint where newValue?.count == SHA256.byteCount:
storage["x5t#S256", true] = newValue
default:
storage[stringKey(keyPath), true] = newValue
Expand Down
2 changes: 1 addition & 1 deletion Sources/JWSETKit/Cryptography/KeyParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension AnyJSONWebKey {

// swiftformat:disable:next redundantSelf
switch (keyType, self.algorithm) {
case (.elipticCurve, _):
case (.ellipticCurve, _):
// swiftformat:disable:next redundantSelf
if self.privateKey != nil {
return try JSONWebECPrivateKey.create(storage: storage)
Expand Down
2 changes: 1 addition & 1 deletion Sources/JWSETKit/Cryptography/Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ extension JSONWebKey {
guard self.modulus != nil, self.exponent != nil else {
throw JSONWebKeyError.keyNotFound
}
case .elipticCurve:
case .ellipticCurve:
// swiftformat:disable:next redundantSelf
guard self.xCoordinate != nil, self.yCoordinate != nil else {
throw JSONWebKeyError.keyNotFound
Expand Down
12 changes: 6 additions & 6 deletions Sources/JWSETKit/Cryptography/RSA/SecKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension SecKey: JSONWebKey {
private static func createPairKey(type: JSONWebKeyType, bits length: Int) throws -> SecKey {
let keyType: CFString
switch type {
case .elipticCurve:
case .ellipticCurve:
keyType = kSecAttrKeyTypeECSECPrimeRandom
case .rsa:
keyType = kSecAttrKeyTypeRSA
Expand Down Expand Up @@ -57,7 +57,7 @@ extension SecKey: JSONWebKey {
throw JSONWebKeyError.unknownKeyType
}
switch type {
case .elipticCurve:
case .ellipticCurve:
guard let xCoordinate = key.xCoordinate, let yCoordinate = key.yCoordinate else {
throw CryptoKitError.incorrectKeySize
}
Expand Down Expand Up @@ -115,7 +115,7 @@ extension SecKey: JSONWebKey {
case kSecAttrKeyTypeRSA:
return .rsa
case kSecAttrKeyTypeEC, kSecAttrKeyTypeECSECPrimeRandom:
return .elipticCurve
return .ellipticCurve
default:
throw JSONWebKeyError.unknownKeyType
}
Expand Down Expand Up @@ -190,12 +190,12 @@ extension SecKey: JSONWebKey {
var key = AnyJSONWebKey()
switch components.count {
case 2:
key.keyType = .elipticCurve
key.keyType = .ellipticCurve
key.xCoordinate = components[0]
key.yCoordinate = components[1]
return JSONWebECPublicKey(storage: key.storage)
case 3:
key.keyType = .elipticCurve
key.keyType = .ellipticCurve
key.xCoordinate = components[0]
key.yCoordinate = components[1]
key.privateKey = components[2]
Expand All @@ -210,7 +210,7 @@ extension SecKey: JSONWebKey {
SecKeyCopyExternalRepresentation(self, &error)
} as Data
switch try keyType {
case .elipticCurve:
case .ellipticCurve:
return try Self.ecWebKey(data: keyData, isPrivateKey: isPrivateKey)
case .rsa:
return try Self.rsaWebKey(data: keyData)
Expand Down
14 changes: 10 additions & 4 deletions Sources/JWSETKit/Entities/JOSE/JOSE-JWSRegistered.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ public struct JoseHeaderJWSRegisteredParameters: JSONWebContainerParameters {
/// -- not base64url-encoded) DER [ITU.X690.2008] PKIX certificate value.
public var certificateChain: [Certificate]

@available(*, deprecated, renamed: "certificateThumbprint")
public var certificateThumprint: Data? {
get { certificateThumbprint }
set { certificateThumbprint = newValue }
}

/// The "`x5t`"/"`x5t#S256`" (X.509 certificate SHA-1/256 thumbprint)
/// Header Parameter is a `base64url-encoded` SHA-1/256 thumbprint
/// (a.k.a. digest) of the `DER` encoding of the X.509 certificate [RFC5280]
Expand All @@ -89,7 +95,7 @@ public struct JoseHeaderJWSRegisteredParameters: JSONWebContainerParameters {
/// Note that certificate thumbprints are also sometimes known as certificate fingerprints.
///
/// Use of this Header Parameter is OPTIONAL.
public var certificateThumprint: Data?
public var certificateThumbprint: Data?

/// The "`typ`" (type) Header Parameter is used by JWS applications
/// to declare the media type [IANA.MediaTypes] of this complete JWS.
Expand Down Expand Up @@ -162,7 +168,7 @@ public struct JoseHeaderJWSRegisteredParameters: JSONWebContainerParameters {
public static let keys: [PartialKeyPath<Self>: String] = [
\.algorithm: "alg", \.jsonWebKeySetUrl: "jku",
\.key: "jwk", \.keyId: "kid", \.certificateChain: "x5c",
\.certificateURL: "x5u", \.certificateThumprint: "x5t",
\.certificateURL: "x5u", \.certificateThumbprint: "x5t",
\.type: "typ", \.contentType: "cty", \.critical: "crit",
\.base64: "b64",
]
Expand Down Expand Up @@ -218,15 +224,15 @@ extension JOSEHeader {
public subscript(dynamicMember keyPath: KeyPath<JoseHeaderJWSRegisteredParameters, Data?>) -> Data? {
get {
switch keyPath {
case \.certificateThumprint where storage.contains(key: "x5t#S256"):
case \.certificateThumbprint where storage.contains(key: "x5t#S256"):
return storage["x5t#S256"]
default:
return storage[stringKey(keyPath)]
}
}
set {
switch keyPath {
case \.certificateThumprint where newValue?.count == SHA256.byteCount:
case \.certificateThumbprint where newValue?.count == SHA256.byteCount:
storage["x5t#S256"] = newValue
default:
storage[stringKey(keyPath)] = newValue
Expand Down
2 changes: 1 addition & 1 deletion Tests/JWSETKitTests/Base/StorageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class StorageTests: XCTestCase {
XCTAssertEqual(storage["http://example.com/is_root"], true)

let keys = storage.keys as [any JSONWebKey]
XCTAssertEqual(keys[0].keyType, .elipticCurve)
XCTAssertEqual(keys[0].keyType, .ellipticCurve)
XCTAssertEqual(
try P256.Signing.PublicKey.create(storage: keys[0].storage).rawRepresentation,
Data(base64Encoded: "MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D7gS2XpJFbZiItSs3m9+9Ue6GnvHw/GW2ZZaVtszggXIw==")
Expand Down
4 changes: 2 additions & 2 deletions Tests/JWSETKitTests/Entities/JOSEHeaderJWSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ final class JOSEHeaderJWSTests: XCTestCase {
XCTAssertEqual(claims.key?.storage, ecKey.storage)
XCTAssertEqual(claims.keyId, "2011-04-29")
XCTAssertEqual(claims.certificateURL, URL(string: "http://example.com/janedoe"))
XCTAssertEqual(claims.certificateThumprint, Data(base64Encoded: "We5K4CMGHXgX4urupYm/Zq2gIhm7d6MdNTEyRu+b6Ck="))
XCTAssertEqual(claims.certificateThumbprint, Data(base64Encoded: "We5K4CMGHXgX4urupYm/Zq2gIhm7d6MdNTEyRu+b6Ck="))
XCTAssertEqual(claims.certificateChain, [cert1, cert2, cert3])
XCTAssertEqual(claims.type, .jwt)
XCTAssertEqual(claims.contentType, .init(rawValue: "application/json"))
Expand All @@ -75,7 +75,7 @@ final class JOSEHeaderJWSTests: XCTestCase {
claims.key = ecKey
claims.keyId = "2011-04-29"
claims.certificateURL = URL(string: "http://example.com/janedoe")
claims.certificateThumprint = Data(base64Encoded: "We5K4CMGHXgX4urupYm/Zq2gIhm7d6MdNTEyRu+b6Ck=")
claims.certificateThumbprint = Data(base64Encoded: "We5K4CMGHXgX4urupYm/Zq2gIhm7d6MdNTEyRu+b6Ck=")
claims.certificateChain = [cert1, cert2, cert3]
claims.type = .jwt
claims.contentType = .init(rawValue: "application/json")
Expand Down

0 comments on commit 79e8f50

Please sign in to comment.