Skip to content

Commit

Permalink
feat(jwa)!: add key representable
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This feature will make the library easier to use, now there is no need parse a key to JWK, just Key just requires to support KeyRepresentable
In the future this will enable to add other well known formats like PEM and DER.
  • Loading branch information
beatt83 committed May 30, 2024
1 parent 1af633d commit 88708c5
Show file tree
Hide file tree
Showing 19 changed files with 820 additions and 739 deletions.
33 changes: 26 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,8 @@ Example:
```swift
let payload = "Hello world".data(using: .utf8)!
let key = secp256k1.Signing.PrivateKey()
let keyJWK = key.jwkRepresentation

let jws = try JWS(payload: payload, key: keyJWK)
let jws = try JWS(payload: payload, key: key)

let jwsString = jws.compactSerialization

Expand All @@ -288,6 +287,7 @@ let rsaKeyId = "Hello-keyId"
var header = DefaultJWSHeaderImpl()
header.keyID = rsaKeyId
header.algorithm = .rsa512

let keyJWK = JWK(keyType: .rsa, algorithm: "RSA512", keyID: rsaKeyId, e: rsaKeyExponent, n: rsaKeyModulus)
let jwe = try JWS(payload: payload, protectedHeader: header, key: jwk)
```
Expand Down Expand Up @@ -334,7 +334,7 @@ JWE represents encrypted content using JSON-based data structures, following the
3. **Compression Algorithms**:
- DEFLATE (zip)

Example:
Example1:

```swift
let payload = "Hello world".data(using: .utf8)!
Expand All @@ -355,6 +355,27 @@ let jwe = try JWE(compactString: compact)
let decrypted = try jwe.decrypt(recipientKey: recipientJWK)
```

Example2:

```swift
let payload = "Hello world".data(using: .utf8)!
let key = P256.Signing.PrivateKey()


let serialization = try JWE(
payload: payload,
keyManagementAlg: .a256KW,
encryptionAlgorithm: .a256GCM,
compressionAlgorithm: .zip,
recipientKey: key
)

let compact = serialization.compactSerialization()

let jwe = try JWE(compactString: compact)
let decrypted = try jwe.decrypt(recipientKey: recipientJWK)
```

If you want to add additional headers beyond the default to the JWE:

```swift
Expand Down Expand Up @@ -403,7 +424,6 @@ Example:

```swift
let key = P256.Signing.PrivateKey()
let keyJWK = key.jwkRepresentation
let mockClaims = DefaultJWTClaims(
iss: "testAlice",
sub: "Alice",
Expand All @@ -426,7 +446,6 @@ let verifiedPayload = verifiedJWT.payload

```swift
let key = Curve25519.KeyAgreement.PrivateKey()
let keyJWK = key.jwkRepresentation
let mockClaims = DefaultJWTClaims(
iss: "testAlice",
sub: "Alice",
Expand All @@ -436,7 +455,7 @@ let mockClaims = DefaultJWTClaims(
let jwt = try JWT.encrypt(
payload: payload,
protectedHeader: DefaultJWSHeaderImpl(keyManagementAlgorithm: .a128KW, encodingAlgorithm: .a128CBCHS256),
recipientKey: keyJWK
recipientKey: key
)

let jwtString = jwt.jwtString
Expand All @@ -449,7 +468,7 @@ let verifiedPayload = verifiedJWT.payload
- Standard Claims on signing a JWT

```swift
let key = JWK.testingES256Pair
let key = P256.Signing.PrivateKey()

let jwt = try JWT.signed(
payload: {
Expand Down
4 changes: 3 additions & 1 deletion Sources/JSONWebAlgorithms/CryptoError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import Foundation

/// `CryptoError` is an enumeration representing various errors that can occur in cryptographic operations.
enum CryptoError: LocalizedError {
public enum CryptoError: LocalizedError {
/// Error indicating that the initialization vector is missing for an operation that requires it.
case missingInitializationVector

Expand Down Expand Up @@ -77,4 +77,6 @@ enum CryptoError: LocalizedError {
/// - type: The key type.
/// - curve: Optional curve name, if applicable.
case cannotGenerateKeyForTypeAndCurve(type: String, curve: String?)

case keyFormatNotSupported(format: String, supportedFormats: [String])
}
13 changes: 13 additions & 0 deletions Sources/JSONWebAlgorithms/KeyManagement/JWKRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// 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 CryptoKit
import CryptoSwift
import Foundation
import JSONWebKey
import secp256k1
Expand Down Expand Up @@ -351,3 +352,15 @@ extension Curve25519.Signing.PublicKey: JWKRepresentable {
)
}
}

extension CryptoSwift.RSA: JWKRepresentable {
/// Returns the JWK representation of a `RSA` key instance.
public var jwkRepresentation: JWK {
JWK(
keyType: .rsa,
e: e.serialize(),
n: n.serialize(),
d: d?.serialize()
)
}
}
136 changes: 0 additions & 136 deletions Sources/JSONWebAlgorithms/KeyManagement/KeyRepresentable.swift

This file was deleted.

Loading

0 comments on commit 88708c5

Please sign in to comment.