-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial commit (extract from jsonld-signatures). #1
Merged
+250
−55
Merged
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
6b06803
Initial commit (extract from jsonld-signatures).
dmitrizagidulin f0c9cba
Add jsonld dep.
dmitrizagidulin db535a1
Update copyright dates.
dmitrizagidulin a07db20
Rename test directory.
dmitrizagidulin ce99808
Text-decode the Uint8Array returned from base64url decoding.
dmitrizagidulin 40afb2c
Update to latest jsonld / jsigs deps.
dmitrizagidulin 680335f
Remove node-forge and env.js script.
dmitrizagidulin 8864b5a
Update to latest jsigs release, remove node-forge.
dmitrizagidulin e443e87
Update lib/JwsLinkedDataSignature.js (fix format)
dmitrizagidulin 3654abd
Add note about contexts.
dmitrizagidulin 7b79d80
Fix jws creation.
dmitrizagidulin 258ed66
Update version in readme.
dmitrizagidulin 9f05fa2
Add test note.
davidlehn 3936c47
Remove unused karma babel support.
davidlehn bf04d64
Add editor files.
davidlehn 6353c4e
Fix test dir name.
davidlehn ebe2617
Update changelog.
davidlehn 2bf931e
Update badges.
davidlehn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
/*! | ||
* Copyright (c) 2020-2021 Digital Bazaar, Inc. All rights reserved. | ||
*/ | ||
import jsonld from 'jsonld'; | ||
import jsigs from 'jsonld-signatures'; | ||
const {LinkedDataSignature} = jsigs.suites; | ||
import {encode, decode} from 'base64url-universal'; | ||
|
||
export class JwsLinkedDataSignature extends LinkedDataSignature { | ||
/** | ||
* @param type {string} Provided by subclass. | ||
* @param alg {string} JWS alg provided by subclass. | ||
* @param [LDKeyClass] {LDKeyClass} provided by subclass or subclass | ||
* overrides `getVerificationMethod`. | ||
* | ||
* @param [verificationMethod] {string} A key id URL to the paired public key. | ||
* | ||
* This parameter is required for signing: | ||
* | ||
* @param [signer] {function} an optional signer. | ||
* | ||
* Advanced optional parameters and overrides: | ||
* | ||
* @param [proof] {object} a JSON-LD document with options to use for | ||
* the `proof` node (e.g. any other custom fields can be provided here | ||
* using a context different from security-v2). | ||
* @param [date] {string|Date} signing date to use if not passed. | ||
* @param [key] {LDKeyPair} an optional crypto-ld KeyPair. | ||
* @param [useNativeCanonize] {boolean} true to use a native canonize | ||
* algorithm. | ||
*/ | ||
constructor({ | ||
type, alg, LDKeyClass, verificationMethod, signer, key, proof, | ||
date, useNativeCanonize | ||
} = {}) { | ||
super({type, verificationMethod, proof, date, useNativeCanonize}); | ||
this.alg = alg; | ||
this.LDKeyClass = LDKeyClass; | ||
this.signer = signer; | ||
if(key) { | ||
if(verificationMethod === undefined) { | ||
const publicKey = key.export({publicKey: true}); | ||
this.verificationMethod = publicKey.id; | ||
} | ||
this.key = key; | ||
if(typeof key.signer === 'function') { | ||
this.signer = key.signer(); | ||
} | ||
if(typeof key.verifier === 'function') { | ||
this.verifier = key.verifier(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param verifyData {Uint8Array}. | ||
* @param proof {object} | ||
* | ||
* @returns {Promise<{object}>} the proof containing the signature value. | ||
*/ | ||
async sign({verifyData, proof}) { | ||
if(!(this.signer && typeof this.signer.sign === 'function')) { | ||
throw new Error('A signer API has not been specified.'); | ||
} | ||
// JWS header | ||
const header = { | ||
alg: this.alg, | ||
b64: false, | ||
crit: ['b64'] | ||
}; | ||
|
||
/* | ||
+-------+-----------------------------------------------------------+ | ||
| "b64" | JWS Signing Input Formula | | ||
+-------+-----------------------------------------------------------+ | ||
| true | ASCII(BASE64URL(UTF8(JWS Protected Header)) || '.' || | | ||
| | BASE64URL(JWS Payload)) | | ||
| | | | ||
| false | ASCII(BASE64URL(UTF8(JWS Protected Header)) || '.') || | | ||
| | JWS Payload | | ||
+-------+-----------------------------------------------------------+ | ||
*/ | ||
|
||
// create JWS data and sign | ||
const encodedHeader = encode(JSON.stringify(header)); | ||
|
||
const data = _createJws({encodedHeader, verifyData}); | ||
|
||
const signature = await this.signer.sign({data}); | ||
|
||
// create detached content signature | ||
const encodedSignature = encode(signature); | ||
proof.jws = encodedHeader + '..' + encodedSignature; | ||
return proof; | ||
} | ||
|
||
/** | ||
* @param verifyData {Uint8Array}. | ||
* @param verificationMethod {object}. | ||
* @param document {object} the document the proof applies to. | ||
* @param proof {object} the proof to be verified. | ||
* @param purpose {ProofPurpose} | ||
* @param documentLoader {function} | ||
* @param expansionMap {function} | ||
* | ||
* @returns {Promise<{boolean}>} Resolves with the verification result. | ||
*/ | ||
async verifySignature({verifyData, verificationMethod, proof}) { | ||
if(!(proof.jws && typeof proof.jws === 'string' && | ||
proof.jws.includes('.'))) { | ||
throw new TypeError('The proof does not include a valid "jws" property.'); | ||
} | ||
// add payload into detached content signature | ||
const [encodedHeader, /*payload*/, encodedSignature] = proof.jws.split('.'); | ||
|
||
let header; | ||
try { | ||
const utf8decoder = new TextDecoder(); | ||
header = JSON.parse(utf8decoder.decode(decode(encodedHeader))); | ||
} catch(e) { | ||
throw new Error('Could not parse JWS header; ' + e); | ||
} | ||
if(!(header && typeof header === 'object')) { | ||
throw new Error('Invalid JWS header.'); | ||
} | ||
|
||
// confirm header matches all expectations | ||
if(!(header.alg === this.alg && header.b64 === false && | ||
Array.isArray(header.crit) && header.crit.length === 1 && | ||
header.crit[0] === 'b64') && Object.keys(header).length === 3) { | ||
throw new Error( | ||
`Invalid JWS header parameters for ${this.type}.`); | ||
} | ||
|
||
// do signature verification | ||
const signature = decode(encodedSignature); | ||
|
||
const data = _createJws({encodedHeader, verifyData}); | ||
|
||
let {verifier} = this; | ||
if(!verifier) { | ||
const key = await this.LDKeyClass.from(verificationMethod); | ||
verifier = key.verifier(); | ||
} | ||
return verifier.verify({data, signature}); | ||
} | ||
|
||
async assertVerificationMethod({verificationMethod}) { | ||
if(!jsonld.hasValue(verificationMethod, 'type', this.requiredKeyType)) { | ||
throw new Error( | ||
`Invalid key type. Key type must be "${this.requiredKeyType}".`); | ||
} | ||
} | ||
|
||
async getVerificationMethod({proof, documentLoader}) { | ||
if(this.key) { | ||
return this.key.export({publicKey: true}); | ||
} | ||
|
||
const verificationMethod = await super.getVerificationMethod( | ||
{proof, documentLoader}); | ||
await this.assertVerificationMethod({verificationMethod}); | ||
return verificationMethod; | ||
} | ||
|
||
async matchProof({proof, document, purpose, documentLoader, expansionMap}) { | ||
if(!await super.matchProof( | ||
{proof, document, purpose, documentLoader, expansionMap})) { | ||
return false; | ||
} | ||
// NOTE: When subclassing this suite: Extending suites will need to check | ||
// for the presence their contexts here and in sign() | ||
|
||
if(!this.key) { | ||
// no key specified, so assume this suite matches and it can be retrieved | ||
return true; | ||
} | ||
|
||
const {verificationMethod} = proof; | ||
|
||
// only match if the key specified matches the one in the proof | ||
if(typeof verificationMethod === 'object') { | ||
return verificationMethod.id === this.key.id; | ||
} | ||
return verificationMethod === this.key.id; | ||
} | ||
} | ||
|
||
/** | ||
* Creates the bytes ready for signing. | ||
* | ||
* @param {string} encodedHeader - base64url encoded JWT header. | ||
* @param {Uint8Array} verifyData - Payload to sign/verify. | ||
* @returns {Uint8Array} A combined byte array for signing. | ||
*/ | ||
function _createJws({encodedHeader, verifyData}) { | ||
const encodedHeaderBytes = new TextEncoder().encode(encodedHeader + '.'); | ||
|
||
// concatenate the two uint8arrays | ||
const data = new Uint8Array(encodedHeaderBytes.length + verifyData.length); | ||
data.set(encodedHeaderBytes, 0); | ||
data.set(verifyData, encodedHeaderBytes.length); | ||
return data; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/*! | ||
* Copyright (c) 2020 Digital Bazaar, Inc. All rights reserved. | ||
* Copyright (c) 2020-2021 Digital Bazaar, Inc. All rights reserved. | ||
*/ | ||
export {Example} from './Example.js'; | ||
export {JwsLinkedDataSignature} from './JwsLinkedDataSignature.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/*! | ||
* Copyright (c) 2020-2021 Digital Bazaar, Inc. All rights reserved. | ||
*/ | ||
import chai from 'chai'; | ||
chai.should(); | ||
const {expect} = chai; | ||
|
||
import {JwsLinkedDataSignature} from '../../'; | ||
|
||
describe('JwsLinkedDataSignature', () => { | ||
describe('constructor', () => { | ||
it('should exist', async () => { | ||
const ex = new JwsLinkedDataSignature({type: 'ExampleType'}); | ||
|
||
expect(ex).to.exist; | ||
}); | ||
}); | ||
}); | ||
davidlehn marked this conversation as resolved.
Show resolved
Hide resolved
|
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: Extending suites will need to check for their contexts here and in
sign
. We probably want a note about that somewhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a note - does that work?