-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Digital Signature and Certificate verification #21247
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
base: master
Are you sure you want to change the base?
Changes from all commits
f53152d
6f2a078
24d95e9
6a8a397
85b01ac
fe81d07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1990,6 +1990,163 @@ class PDFDocument { | |
| return shadow(this, "fieldObjects", promise); | ||
| } | ||
|
|
||
| #collectSignatureFields(fields, out, visitedRefs) { | ||
| if (!Array.isArray(fields)) { | ||
| return; | ||
| } | ||
| for (const fieldRef of fields) { | ||
| if (fieldRef instanceof Ref) { | ||
| if (visitedRefs.has(fieldRef)) { | ||
| continue; | ||
| } | ||
| visitedRefs.put(fieldRef); | ||
| } | ||
| const field = this.xref.fetchIfRef(fieldRef); | ||
| if (!(field instanceof Dict)) { | ||
| continue; | ||
| } | ||
| if (field.has("Kids")) { | ||
| this.#collectSignatureFields(field.get("Kids"), out, visitedRefs); | ||
| continue; | ||
| } | ||
| if (!isName(field.get("FT"), "Sig")) { | ||
| continue; | ||
| } | ||
| const sigDict = this.xref.fetchIfRef(field.get("V")); | ||
| if (!(sigDict instanceof Dict)) { | ||
| continue; | ||
| } | ||
| const parsed = this.#parseSignatureDict(field, sigDict, fieldRef); | ||
| if (parsed) { | ||
| out.push(parsed); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // The /ByteRange of a signature that covers the whole document usually | ||
| // ends a few bytes before the file's actual end — the trailer, | ||
| // `startxref` offset and `%%EOF` marker are conventionally left outside | ||
| // the signed range. 100 bytes covers the worst case (large | ||
| // `startxref` offsets, optional whitespace) without false positives. | ||
| static #WHOLE_DOCUMENT_TAIL_FUZZ = 100; | ||
|
|
||
| #parseSignatureDict(field, sigDict, fieldRef) { | ||
| const byteRange = sigDict.get("ByteRange"); | ||
| if ( | ||
| !Array.isArray(byteRange) || | ||
| byteRange.length !== 4 || | ||
| byteRange.some(n => !Number.isInteger(n) || n < 0) | ||
| ) { | ||
| return null; | ||
| } | ||
| const contents = sigDict.get("Contents"); | ||
| if (typeof contents !== "string" || contents.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const filterName = sigDict.get("Filter"); | ||
| const filter = filterName instanceof Name ? filterName.name : null; | ||
| const subFilterName = sigDict.get("SubFilter"); | ||
| const subFilter = subFilterName instanceof Name ? subFilterName.name : null; | ||
|
|
||
| let signatureType = null; | ||
| if (subFilter === "adbe.pkcs7.detached") { | ||
| signatureType = 0; | ||
| } else if (subFilter === "adbe.pkcs7.sha1") { | ||
| signatureType = 1; | ||
| } | ||
|
|
||
| // Slice the two ByteRange byte spans out of the underlying PDF stream. | ||
| // ByteRange = [a, b, c, d] means signed bytes are [a..a+b] and [c..c+d]; | ||
| // the gap covers the /Contents hex blob itself. | ||
| const [a, b, c, d] = byteRange; | ||
| const stream = this.stream; | ||
| const data = [stream.getByteRange(a, a + b), stream.getByteRange(c, c + d)]; | ||
|
|
||
| const pkcs7 = stringToBytes(contents); | ||
|
|
||
| const t = field.get("T"); | ||
| const fieldName = typeof t === "string" ? stringToPDFString(t) : ""; | ||
| const name = sigDict.get("Name"); | ||
| const reason = sigDict.get("Reason"); | ||
| const location = sigDict.get("Location"); | ||
| const contactInfo = sigDict.get("ContactInfo"); | ||
| const m = sigDict.get("M"); | ||
|
|
||
| const refKey = fieldRef instanceof Ref ? fieldRef.toString() : "inline"; | ||
| const id = `${refKey}:${a}-${b}-${c}-${d}`; | ||
|
|
||
| const fileLength = stream.end || 0; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know if it's correct in general.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that However, looking at this now I wonder why
|
||
| const lastSignedByte = c + d; | ||
|
|
||
| return { | ||
| id, | ||
| fieldName, | ||
| signerName: typeof name === "string" ? stringToPDFString(name) : null, | ||
| reason: typeof reason === "string" ? stringToPDFString(reason) : null, | ||
| location: | ||
| typeof location === "string" ? stringToPDFString(location) : null, | ||
| contactInfo: | ||
| typeof contactInfo === "string" ? stringToPDFString(contactInfo) : null, | ||
| signingTime: typeof m === "string" ? m : null, | ||
| filter, | ||
| subFilter, | ||
| signatureType, | ||
| byteRange, | ||
| pkcs7, | ||
| data, | ||
| revisionIndex: 0, | ||
| parentId: null, | ||
| coversWholeDocument: | ||
| fileLength > 0 && | ||
| lastSignedByte >= fileLength - PDFDocument.#WHOLE_DOCUMENT_TAIL_FUZZ, | ||
| }; | ||
| } | ||
|
|
||
| get signatures() { | ||
| const promise = this.pdfManager | ||
| .ensureDoc("formInfo") | ||
| .then(async formInfo => { | ||
| if (!formInfo.hasSignatures || !formInfo.hasFields) { | ||
| return null; | ||
| } | ||
| const annotationGlobals = await this.annotationGlobals; | ||
| if (!annotationGlobals) { | ||
| return null; | ||
| } | ||
| const fields = annotationGlobals.acroForm.get("Fields"); | ||
|
|
||
| const collected = []; | ||
| this.#collectSignatureFields(fields, collected, new RefSet()); | ||
|
|
||
| // Group sub-signatures by ByteRange containment: outer revision is | ||
| // the largest covering signature (largest c + d). Sort descending, | ||
| // then point each later signature at the smallest enclosing parent | ||
| // that came before it. | ||
| collected.sort( | ||
| (a, b) => | ||
| b.byteRange[2] + b.byteRange[3] - (a.byteRange[2] + a.byteRange[3]) | ||
| ); | ||
| for (let i = 0, ii = collected.length; i < ii; i++) { | ||
| const sig = collected[i]; | ||
| sig.revisionIndex = i; | ||
| for (let j = i - 1; j >= 0; j--) { | ||
| const candidate = collected[j]; | ||
| if ( | ||
| candidate.byteRange[2] + candidate.byteRange[3] > | ||
| sig.byteRange[2] + sig.byteRange[3] | ||
| ) { | ||
| sig.parentId = candidate.id; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| return collected.length ? collected : null; | ||
| }); | ||
|
|
||
| return shadow(this, "signatures", promise); | ||
| } | ||
|
|
||
| get hasJSActions() { | ||
| const promise = this.pdfManager.ensureDoc("_parseHasJSActions"); | ||
| return shadow(this, "hasJSActions", promise); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| *.p7s | ||
| *.pkcs7spec |
Uh oh!
There was an error while loading. Please reload this page.