Skip to content

Commit

Permalink
feat(jose): updated to use a new jose library
Browse files Browse the repository at this point in the history
This commit updates the code base to use this jose library instead.
  • Loading branch information
goncalo-frade-iohk committed May 31, 2024
1 parent b675d15 commit b9cb99e
Show file tree
Hide file tree
Showing 23 changed files with 160 additions and 189 deletions.
26 changes: 22 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
{
"pins" : [
{
"identity" : "joseswift",
"identity" : "cryptoswift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/niscy-eudiw/JOSESwift.git",
"location" : "https://github.com/krzyzanowskim/CryptoSwift.git",
"state" : {
"revision" : "518cedba79ef18867191811b161471298b6cb7c8",
"version" : "2.4.1-gcm"
"revision" : "c9c3df6ab812de32bae61fc0cd1bf6d45170ebf0",
"version" : "1.8.2"
}
},
{
"identity" : "jose-swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/beatt83/jose-swift.git",
"state" : {
"revision" : "1d36e208439b217e70195968d329c5e409b2be66",
"version" : "3.0.0"
}
},
{
"identity" : "secp256k1.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/GigaBitcoin/secp256k1.swift.git",
"state" : {
"revision" : "4c77c7384768acf1093d66ccaacf298d322b10b7",
"version" : "0.15.0"
}
},
{
Expand Down
8 changes: 4 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ let package = Package(
from: "5.0.1"
),
.package(
url: "https://github.com/niscy-eudiw/JOSESwift.git",
exact: "2.4.1-gcm"
url: "https://github.com/beatt83/jose-swift.git",
exact: "3.0.0"
),
],
targets: [
.target(
name: "eudi-lib-sdjwt-swift",
dependencies: [
.product(name: "SwiftyJSON", package: "swiftyjson"),
.product(name: "JOSESwift", package: "JOSESwift")
"jose-swift",
.product(name: "SwiftyJSON", package: "swiftyjson")
],
path: "Sources",
plugins: [
Expand Down
1 change: 0 additions & 1 deletion Sources/Factory/SDJWTFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
import Foundation
import SwiftyJSON
import JOSESwift

typealias ClaimSet = (value: JSON, disclosures: [Disclosure])

Expand Down
15 changes: 6 additions & 9 deletions Sources/Issuer/JWSController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,23 @@
* limitations under the License.
*/

import JOSESwift
import CryptoKit
import JSONWebAlgorithms
import JSONWebSignature
import Security

class JWSController<SecKey> {

// MARK: - Properties

var signatureAlgorithm: SignatureAlgorithm
var signatureAlgorithm: SigningAlgorithm
// SecKey Should be Data (HMAC) Or SecKey (RSA, EC)
let signer: Signer<SecKey>
let key: SecKey

// MARK: - Lifecycle

init(signingAlgorithm: SignatureAlgorithm, privateKey: SecKey) throws {
init(signingAlgorithm: SigningAlgorithm, privateKey: SecKey) throws {
self.signatureAlgorithm = signingAlgorithm
guard let signer = Signer(signingAlgorithm: signingAlgorithm, key: privateKey) else {
throw JOSESwiftError.signingFailed(description: "Failed To Create Signing Algorith \(signingAlgorithm) with key \(privateKey)")
}

self.signer = signer
self.key = privateKey
}
}
19 changes: 10 additions & 9 deletions Sources/Issuer/JWT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,38 @@
* limitations under the License.
*/
import Foundation
import JOSESwift
import JSONWebAlgorithms
import JSONWebSignature
import SwiftyJSON

public struct JWT: JWTRepresentable {

// MARK: - Properties

var header: JWSHeader
var header: JWSRegisteredFieldsHeader
var payload: JSON

// MARK: - Lifecycle

public init(header: JWSHeader, payload: JSON) throws {
public init(header: JWSRegisteredFieldsHeader, payload: JSON) throws {
guard header.algorithm?.rawValue != Keys.none.rawValue else {
throw SDJWTError.noneAsAlgorithm
}

guard SignatureAlgorithm.allCases.map({$0.rawValue}).contains(header.algorithm?.rawValue) else {
guard SigningAlgorithm.allCases.map({$0.rawValue}).contains(header.algorithm?.rawValue) else {
throw SDJWTError.macAsAlgorithm
}

self.header = header
self.payload = payload
}

public init(header: JWSHeader, kbJwtPayload: JSON) throws {
public init(header: JWSRegisteredFieldsHeader, kbJwtPayload: JSON) throws {
guard header.algorithm?.rawValue != Keys.none.rawValue else {
throw SDJWTError.noneAsAlgorithm
}

guard SignatureAlgorithm.allCases.map({$0.rawValue}).contains(header.algorithm?.rawValue) else {
guard SigningAlgorithm.allCases.map({$0.rawValue}).contains(header.algorithm?.rawValue) else {
throw SDJWTError.macAsAlgorithm
}
self.header = header
Expand All @@ -54,13 +55,13 @@ public struct JWT: JWTRepresentable {

// MARK: - Methods

func sign<KeyType>(signer: Signer<KeyType>) throws -> JWS {
func sign<KeyType>(key: KeyType) throws -> JWS {
let unsignedJWT = try self.asUnsignedJWT()
return try JWS(header: unsignedJWT.header, payload: unsignedJWT.payload, signer: signer)
return try JWS.init(payload: unsignedJWT.payload, protectedHeader: unsignedJWT.header, key: key)
}

mutating func addKBTyp() {
self.header.typ = "kb+jwt"
self.header.type = "kb+jwt"
}
}

Expand Down
13 changes: 6 additions & 7 deletions Sources/Issuer/JWTRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,25 @@
* limitations under the License.
*/
import Foundation
import JOSESwift
import JSONWebSignature
import SwiftyJSON

typealias Base64String = String
typealias UnsignedJWT = (header: JWSHeader, payload: Payload)
typealias UnsignedJWT = (header: JWSRegisteredFieldsHeader, payload: Data)

protocol JWTRepresentable {

var header: JWSHeader { get }
var header: JWSRegisteredFieldsHeader { get }
var payload: JSON { get }

func asUnsignedJWT() throws -> UnsignedJWT
func sign<KeyType>(signer: Signer<KeyType>) throws -> JWS
func sign<KeyType>(key: KeyType) throws -> JWS

init(header: JWSHeader, payload: JSON) throws
init(header: JWSRegisteredFieldsHeader, payload: JSON) throws
}

extension JWTRepresentable {
func asUnsignedJWT() throws -> UnsignedJWT {
let payload = Payload(try payload.rawData())
return(header, payload)
return(header, try payload.rawData())
}
}
35 changes: 13 additions & 22 deletions Sources/Issuer/SDJWT.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
* limitations under the License.
*/
import Foundation
import JSONWebKey
import JSONWebSignature
import JSONWebToken
import SwiftyJSON
import JOSESwift

public typealias KBJWT = JWT

Expand Down Expand Up @@ -75,7 +77,7 @@ public struct SignedSDJWT {

var delineatedCompactSerialisation: String {
let separator = "~"
let input = ([jwt.compactSerializedString] + disclosures).reduce("") { $0.isEmpty ? $1 : $0 + separator + $1 } + separator
let input = ([jwt.compactSerialization] + disclosures).reduce("") { $0.isEmpty ? $1 : $0 + separator + $1 } + separator
return DigestCreator()
.hashAndBase64Encode(
input: input
Expand All @@ -89,9 +91,9 @@ public struct SignedSDJWT {
disclosures: [Disclosure],
serializedKbJwt: String?
) throws {
self.jwt = try JWS(compactSerialization: serializedJwt)
self.jwt = try JWS(jwsString: serializedJwt)
self.disclosures = disclosures
self.kbJwt = try? JWS(compactSerialization: serializedKbJwt ?? "")
self.kbJwt = try? JWS(jwsString: serializedKbJwt ?? "")
}

private init?<KeyType>(sdJwt: SDJWT, issuersPrivateKey: KeyType) {
Expand Down Expand Up @@ -141,7 +143,7 @@ public struct SignedSDJWT {
}

private static func createSignedJWT<KeyType>(jwsController: JWSController<KeyType>, jwt: JWT) throws -> JWS {
try jwt.sign(signer: jwsController.signer)
try jwt.sign(key: jwsController.key)
}

func disclosuresToPresent(disclosures: [Disclosure]) -> Self {
Expand All @@ -151,16 +153,16 @@ public struct SignedSDJWT {
}

func toSDJWT() throws -> SDJWT {
if let kbJwtHeader = kbJwt?.header,
if let kbJwtHeader = kbJwt?.protectedHeader,
let kbJWtPayload = try? kbJwt?.payloadJSON() {
return try SDJWT(
jwt: JWT(header: jwt.header, payload: jwt.payloadJSON()),
jwt: JWT(header: jwt.protectedHeader, payload: jwt.payloadJSON()),
disclosures: disclosures,
kbJWT: JWT(header: kbJwtHeader, kbJwtPayload: kbJWtPayload))
}

return try SDJWT(
jwt: JWT(header: jwt.header, payload: jwt.payloadJSON()),
jwt: JWT(header: jwt.protectedHeader, payload: jwt.payloadJSON()),
disclosures: disclosures,
kbJWT: nil)
}
Expand All @@ -173,22 +175,11 @@ public struct SignedSDJWT {
throw SDJWTVerifierError.keyBindingFailed(description: "Failled to find holders public key")
}

guard let keyType = JWKKeyType(rawValue: jwk["kty"].stringValue) else {
guard let jwkObject = try? JSONDecoder.jwt.decode(JWK.self, from: jwk.rawData()) else {
throw SDJWTVerifierError.keyBindingFailed(description: "failled to extract key type")
}

switch keyType {
case .EC:
guard let crvType = ECCurveType(rawValue: jwk["crv"].stringValue) else {
throw SDJWTVerifierError.keyBindingFailed(description: "failled to extract curve type")
}
return ECPublicKey(crv: crvType, x: jwk["x"].stringValue, y: jwk["y"].stringValue)
case .RSA:
return RSAPublicKey(modulus: jwk["n"].stringValue, exponent: jwk["e"].stringValue)
case .OCT:
return try SymmetricKey(key: jwk["k"].rawData())
}


return jwkObject
}
}

Expand Down
6 changes: 3 additions & 3 deletions Sources/Issuer/SDJWTIssuer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
* limitations under the License.
*/
import Foundation
import JOSESwift
import JSONWebSignature
import SwiftyJSON

public class SDJWTIssuer {

/// Enum to represent the purpose of the JWT.
enum Purpose {
/// Used for JWT issuance.
case issuance(JWSHeader, ClaimSet)
case issuance(JWSRegisteredFieldsHeader, ClaimSet)
/// Used for JWT presentation.
case presentation(SignedSDJWT, [Disclosure], KBJWT?)
}
Expand All @@ -45,7 +45,7 @@ public class SDJWTIssuer {
/// - Throws: An error if there's an issue with JWT creation or signing.
///
public static func issue<KeyType>(issuersPrivateKey: KeyType,
header: JWSHeader,
header: JWSRegisteredFieldsHeader,
decoys: Int = 0,
@SDJWTBuilder buildSDJWT: () throws -> SdElement) throws -> SignedSDJWT {

Expand Down
1 change: 0 additions & 1 deletion Sources/Parser/CompactParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/
import Foundation
import JOSESwift
import SwiftyJSON

public enum SerialisationFormat {
Expand Down
5 changes: 2 additions & 3 deletions Sources/Serializer/CompactSerialiser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/
import Foundation
import JOSESwift

public class CompactSerialiser: SerialiserProtocol {

Expand All @@ -41,11 +40,11 @@ public class CompactSerialiser: SerialiserProtocol {
public extension SerialisationFormat {
func serialise(signedSDJWT: SignedSDJWT) -> String {
var output = ""
output += signedSDJWT.jwt.compactSerializedString
output += signedSDJWT.jwt.compactSerialization
output += signedSDJWT.disclosures.reduce(into: "~", { partialResult, disclosure in
partialResult += disclosure + "~"
})
output += signedSDJWT.kbJwt?.compactSerializedString ?? ""
output += signedSDJWT.kbJwt?.compactSerialization ?? ""
return output
}
}
5 changes: 2 additions & 3 deletions Sources/Serializer/EnvelopedSerialiser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
import Foundation
import SwiftyJSON
import JOSESwift

public class EnvelopedSerialiser: SerialiserProtocol {

Expand All @@ -34,11 +33,11 @@ public class EnvelopedSerialiser: SerialiserProtocol {

// MARK: - Lifecycle

public init(SDJWT: SignedSDJWT, jwTpayload: Payload, options opt: JSONSerialization.ReadingOptions = []) throws {
public init(SDJWT: SignedSDJWT, jwTpayload: Data, options opt: JSONSerialization.ReadingOptions = []) throws {
var updatedSDJWT = SDJWT
updatedSDJWT.kbJwt = nil

payload = try JSON(data: jwTpayload.data())
payload = try JSON(data: jwTpayload)
let compactSerialiser = CompactSerialiser(signedSDJWT: updatedSDJWT)
payload[Keys.sdJwt].string = compactSerialiser.serialised
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Utilities/Extensions/JWS+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

import JOSESwift
import JSONWebSignature
import SwiftyJSON

extension JWS {
func payloadJSON() throws -> JSON {
try JSON(data: self.payload.data())
try JSON(data: self.payload)
}

func iat() throws -> Int? {
Expand Down
23 changes: 0 additions & 23 deletions Sources/Utilities/Extensions/Payload+Extension.swift

This file was deleted.

Loading

0 comments on commit b9cb99e

Please sign in to comment.