| 1 | # JsonWebSignature2020 |
| 2 | |
| 3 | This module extends the [iron-verifiable-credentials library](https://github.com/filip26/iron-verifiable-credentials), |
| 4 | which we use in conjunction with [titanium-ld](https://github.com/filip26/titanium-json-ld/) with an implementation for |
| 5 | the [JsonWebSignature2020](https://www.w3.org/community/reports/credentials/CG-FINAL-lds-jws2020-20220721) crypto suite. |
| 6 | |
| 7 | ## Technical aspects |
| 8 | |
| 9 | This implementation is actually mostly glue code between the `iron-verifiable-credentials` lib and the |
| 10 | well-known [Nimbus JOSE lib](https://connect2id.com/products/nimbus-jose-jwt), as all cryptographic primitives are taken |
| 11 | from Nimbus. |
| 12 | |
| 13 | VerifiableCredentials and VerifiablePresentations are processed as JSON(-LD) objects, so some familiarity with JSON-LD |
| 14 | is required. |
| 15 | The entrypoint into the cryptographic suite is the `Vc` class, which allows signing/issuing and verifying JSON-LD |
| 16 | structures. The following samples use explicit types for clarity. These are just some illustrative examples, please |
| 17 | check the `IssuerTests` and the `VerifierTests` for more comprehensive explanations. |
| 18 | |
| 19 | ### Sign a VC |
| 20 | |
| 21 | ```java |
| 22 | JwsSignature2020Suite suite=new JwsSignature2020Suite(JacksonJsonLd.createObjectMapper()); |
| 23 | JsonObject vc=createVcAsJsonLd(); |
| 24 | JWK keyPair=createKeyPairAsJwk(); |
| 25 | JsonWebKeyPair signKeys=new JsonWebKeyPair(id,type,controller,keyPair); |
| 26 | |
| 27 | var options=suite.createOptions() |
| 28 | .created(Instant.now()) |
| 29 | .verificationMethod(signKeys) // embeds the proof |
| 30 | .purpose(URI.create("https://w3id.org/security#assertionMethod")); |
| 31 | |
| 32 | Issuer signedVc=Vc.sign(vc,signKeys,options); |
| 33 | |
| 34 | JsonObject compacted=IssuerCompat.compact(signedVc); |
| 35 | ``` |
| 36 | |
| 37 | ### Verify a VC |
| 38 | |
| 39 | ```java |
| 40 | JwsSignature2020Suite suite=new JwsSignature2020Suite(JacksonJsonLd.createObjectMapper()); |
| 41 | JsonObject vc=readSignedVc(); |
| 42 | Verifier result=Vc.verify(vc,suite); |
| 43 | |
| 44 | try{ |
| 45 | result.isValid(); |
| 46 | }catch(VerificationError error){ |
| 47 | //handle |
| 48 | } |
| 49 | ``` |
| 50 | |
| 51 | ## Limitations & Known Issues |
| 52 | |
| 53 | Java 17 [dropped support](https://connect2id.com/products/nimbus-jose-jwt/examples/jwt-with-es256k-signature) for |
| 54 | the `secp256k1` curve. Alternatively, the BouncyCastle JCA provider could be used. |
| 55 | For this implementation, we chose to forego this at the benefit of a smaller library footprint. There is plenty of other |
| 56 | curves to choose from. |
| 57 | |
| 58 | On a similar note, support for Octet Keypairs (`"OKP"`) has not yet been added to the standard Java JCA, thus an |
| 59 | additional dependency `tink` is needed, |
| 60 | check [here](https://connect2id.com/products/nimbus-jose-jwt/examples/jwk-generation#okp) for details. If that is not |
| 61 | acceptable to you, please add a dependency exclusion to your build script. |
| 62 | |
| 63 | `iron-verifiable-credentials` is not 100% agnostic toward its crypto suites, for example there is |
| 64 | a [hard-coded context](https://github.com/filip26/iron-verifiable-credentials/blob/82d13326c5f64a0f38c75d417ffc263febfd970d/src/main/java/com/apicatalog/vc/processor/Issuer.java#L122) |
| 65 | added to the compacted JSON-LD, which is incorrect. It doesn't negatively impact the resulting JSON-LD, other than |
| 66 | possibly affecting processing times, but unfortunately it also makes it impossible to add more contexts, such |
| 67 | as https://w3id.org/security/suites/jws-2020/v1. We mitigated this with |
| 68 | the [`IssuerCompatibilty.java`](./src/main/java/org/eclipse/edc/security/signature/jws2020/IssuerCompatibility.java), |
| 69 | which should be |
| 70 | used |
| 71 | for compaction. |