Skip to content

Commit

Permalink
feat(jwa): removing curve 448 and open ssl since it was not being used
Browse files Browse the repository at this point in the history
This will be back in future implementation, but I noticed it actually didn't support all the targets I required.
I will have to add the targets on a open ssl framework so then I can produce this work.

this commit also adds a fix: secp256k1 bug when trying to recreate uncompressed key
  • Loading branch information
beatt83 committed Jan 29, 2024
1 parent 1ab2c1f commit 0b01385
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 171 deletions.
3 changes: 0 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ let package = Package(
)
],
dependencies: [
// For `X448` support
.package(url: "https://github.com/krzyzanowskim/OpenSSL.git", .upToNextMinor(from: "3.1.4000")),
// For `secp256k1` support
.package(url: "https://github.com/GigaBitcoin/secp256k1.swift.git", .upToNextMinor(from: "0.15.0")),
// For `AES_CBC_HMAC_SHA2`, `PBES2` and RSA DER encoding support
Expand Down Expand Up @@ -78,7 +76,6 @@ let package = Package(
.target(
name: "JSONWebKey",
dependencies: [
"OpenSSL",
"CryptoSwift",
"Tools",
.product(name: "secp256k1", package: "secp256k1.swift"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public extension JWK {
is P384.KeyAgreement.PrivateKey.Type,
is P521.KeyAgreement.PrivateKey.Type,
is secp256k1.KeyAgreement.PrivateKey.Type,
is Curve25519.KeyAgreement.PrivateKey.Type,
is Curve448.KeyAgreement.PrivateKey.Type:
is Curve25519.KeyAgreement.PrivateKey.Type:

guard let d else {
throw JWK.Error.missingDComponent
Expand All @@ -45,8 +44,6 @@ public extension JWK {
return try secp256k1.KeyAgreement.PrivateKey(dataRepresentation: d, format: .uncompressed) as! T
case is Curve25519.KeyAgreement.PrivateKey.Type:
return try Curve25519.KeyAgreement.PrivateKey(rawRepresentation: d) as! T
case is Curve448.KeyAgreement.PrivateKey.Type:
return try Curve448.KeyAgreement.PrivateKey(rawRepresentation: d) as! T
default:
throw JWK.Error.notSupported
}
Expand All @@ -63,6 +60,11 @@ public extension JWK {
throw JWK.Error.missingYComponent
}
let data = x + y
print(self.keyID)
print(x.toHexString())
print(x.count)
print(y.toHexString())
print(y.count)
switch type {
case is P256.KeyAgreement.PublicKey.Type:
return try P256.KeyAgreement.PublicKey(rawRepresentation: data) as! T
Expand All @@ -80,8 +82,7 @@ public extension JWK {
throw JWK.Error.notSupported
}

case is Curve25519.KeyAgreement.PublicKey.Type,
is Curve448.KeyAgreement.PublicKey.Type:
case is Curve25519.KeyAgreement.PublicKey.Type:

guard let x else {
throw JWK.Error.missingXComponent
Expand All @@ -90,8 +91,6 @@ public extension JWK {
switch type {
case is Curve25519.KeyAgreement.PublicKey.Type:
return try Curve25519.KeyAgreement.PublicKey(rawRepresentation: data) as! T
case is Curve448.KeyAgreement.PublicKey.Type:
return try Curve448.KeyAgreement.PublicKey(rawRepresentation: data) as! T
default:
throw JWK.Error.notSupported
}
Expand Down
23 changes: 0 additions & 23 deletions Sources/JSONWebAlgorithms/KeyManagement/JWKRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,18 +177,6 @@ extension Curve25519.KeyAgreement.PrivateKey: JWKRepresentable {
}
}

extension Curve448.KeyAgreement.PrivateKey: JWKRepresentable {
/// Returns the JWK representation of a `Curve448.KeyAgreement.PrivateKey` instance.
public var jwkRepresentation: JWK {
JWK(
keyType: .octetKeyPair,
curve: .x448,
x: publicKey.rawRepresentation,
d: rawRepresentation
)
}
}

extension secp256k1.KeyAgreement.PublicKey: JWKRepresentable {
/// Returns the JWK representation of a `secp256k1.KeyAgreement.PublicKey` instance.
public var jwkRepresentation: JWK {
Expand Down Expand Up @@ -344,14 +332,3 @@ extension Curve25519.Signing.PublicKey: JWKRepresentable {
)
}
}

extension Curve448.KeyAgreement.PublicKey: JWKRepresentable {
/// Returns the JWK representation of a `Curve448.KeyAgreement.PublicKey` instance.
public var jwkRepresentation: JWK {
JWK(
keyType: .octetKeyPair,
curve: .x448,
x: rawRepresentation
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct ES256KVerifier: Verifier {
let x = key?.x,
let y = key?.y
else { throw CryptoError.notValidPublicKey }
let publicKey = try secp256k1.Signing.PublicKey(dataRepresentation: x + y, format: .uncompressed)
let publicKey = try secp256k1.Signing.PublicKey(dataRepresentation: [0x04] + x + y, format: .uncompressed)
let hash = SHA256.hash(data: data)
return try publicKey.isValidSignature(getSignature(signature), for: hash)
}
Expand Down
135 changes: 0 additions & 135 deletions Sources/JSONWebKey/Curve448.swift

This file was deleted.

23 changes: 22 additions & 1 deletion Sources/JSONWebSignature/JWS+Json.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public typealias DefaultJWSJson = JWSJson<DefaultJWSHeaderImpl, DefaultJWSHeader
/// - Parameters:
/// - P: The type of the protected header, conforming to `JWSRegisteredFieldsHeader`.
/// - H: The type of the unprotected header, conforming to `JWSRegisteredFieldsHeader`.
public struct JWSJson<P: JWSRegisteredFieldsHeader, H: JWSRegisteredFieldsHeader>: Codable {
public struct JWSJson<P: JWSRegisteredFieldsHeader, H: JWSRegisteredFieldsHeader> {

/// `Signature` represents a single signature within the `JWSJson`, including its associated headers and signature data.
public struct Signature {
Expand Down Expand Up @@ -143,6 +143,27 @@ public struct JWSJson<P: JWSRegisteredFieldsHeader, H: JWSRegisteredFieldsHeader
}
}

extension JWSJson: Codable {
enum CodingKeys: String, CodingKey {
case payload
case signatures
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(Base64URL.encode(payload), forKey: .payload)
try container.encode(signatures, forKey: .signatures)
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let payloadBase64 = try container.decode(String.self, forKey: .payload)
self.payload = try Base64URL.decode(payloadBase64)
self.signatures = try container.decode([Signature].self, forKey: .signatures)
}
}


extension JWSJson.Signature: Codable {
enum CodingKeys: String, CodingKey {
case protected
Expand Down

0 comments on commit 0b01385

Please sign in to comment.