Digital signatures in pdf.js #1076
Comments
|
There are currently no imminent plans to implement this feature. We haven't seen it come up much so it hasn't been a priority. We're always looking for more contributors though, so we'd welcome any patches to add this feature. If you're interested in adding it feel free to stop by our IRC channel(#pdfjs irc.mozilla.org) if you have questions. Brendan |
|
Hi what is the present state on the implementation of this feature? Thanks |
|
@fermo111 Not implemented yet, but I will be glad to coach somebody who is willing to take this task. |
|
@yurydelendik I'm interested in implementing the feature of presenting digital signatures of PDF files in pdf.js. How can I contact you? |
|
@wolvz please find me at IRC irc.mozilla.org channel #pdfjs (that's simplest) or join any of our public meetings to coordinate stuff. |
|
I'm trying to implement SigWidgetAnnotation (like TextAnnotation, LinkAnnotation), in annotation.js, for supporting digital signatures in pdf.js. var contentStreamPromise = this.pdfManager.ensure(this, 'getContentStream',[]); Can someone please enlighten me on what is the best way to get file contents in annotation.js? |
|
wolvz, |
|
@yurydelendik @wolvz |
|
I am confused. What i have to do in my pdf.js library to show digital signature pdf in pdfJS |
|
@mrpandya007, the digital signature doesn't show because the developers chose to hide it until they have signature verification feature working. |
|
@wolvz, Thanx for the response but its already commented. PLease look into this,, Crystal report's generated pdf signature is not showing but DevExpress generated pdf is showing it properly |
|
Please answer me .... what i have to do.. I can give you that pdf to test |
|
There is no good support for digital signatures yet, so this will have to be implemented. |
|
How many days it will take to fix this problem of digital signature because we are dependent on your lib. |
|
It might take a long time as this feature will have to be developed by someone and there are currently higher-priority issues. |
|
@stephanrauh Thanks.
Is there a way to enable forms and show signatures? |
|
@lainosantos No, but I've found a nice walk-through how to build your own forms. The example assumes that |
|
I have more urgent matters for the moment and cannot spend time on the task, but if anyone manages to import an up-to-date pdf.js version on Angular 9, he would have my deepest thanks. |
|
@Epsiom Here you are: https://www.npmjs.com/package/ngx-extended-pdf-viewer :) |
|
@stephanrauh ...Well, except that I need to be compatible IE11 and only use the canvas directly to have a custom-made pdf viewer, so I cannot use that, sadly. |
|
I've successfully show the signatures by commenting |
|
If anyone is interested in biometric signatures: https://github.com/DennisWacom/AlphaSign You can even use certificates... |
worked for me ! thank you. |
|
Hi guys. I've been searching for a way to parsing signature information from PDF so at some point I came across this thread. Alright, if you happen to open signed-PDF with a text editor and search for a word there will be 5 lines of data for each signature attached to this document. the first line and last line has nothing much, just ignore it. on the 2nd line, data in between you can also get a Location, Timestamp of signature, and Reason on the 3rd and 4th line. My English is bad I think I talk too much, I should let the code speak. Dependency: forge.js <form id="getPDFSignature">
<input type="file" name="pdf" accept="application/pdf" required>
<button>get Signatures</button>
</form>document.getElementById('getPDFSignature').onsubmit = function (e) {
e.preventDefault();
let FR = new FileReader();
FR.onload = function (e) {
let Signatures = getPDFSignature(e.target.result);
if(Signatures){
console.log(Signatures);
}else{
alert('This document does not contains any signature.');
}
}
FR.readAsText(this.pdf.files[0]);
return false;
}
function getPDFSignature(pdf){
let Signatures = [];
let PKCS7 = pdf.split(/\n\n/).filter(d=>d.indexOf('adbe.pkcs7.detached') > -1); // search for paragraph with word "adbe.pkcs7.detached"
PKCS7.forEach(function(sig){
let sigHex = sig.split(/\n/)[1].slice(13, -1).replace(/(?:00)*$/, ''); // get signature from the second line, also remove 0 padding at the end
let p7Asn1 = forge.asn1.fromDer(hex2str(sigHex)); // convert hex to String to get Der, convert Der to ASN.1
let message = forge.pkcs7.messageFromAsn1(p7Asn1); // convert ASN.1 to Signature Message
let meta = sig.match(/\((.*?)\)/g); // [Location, Timestamp, Reason, ContactInfo]
// timestamp format from string like this -> "(D:20210114234159+07'00')"
let timestamp = `${meta[1].slice(3,7)}-${meta[1].slice(7,9)}-${meta[1].slice(9,11)}T${meta[1].slice(11,13)}:${meta[1].slice(13,15)}:${meta[1].slice(15,20)}${meta[1].slice(21,23)}`;
meta = {
Location: meta[0].slice(1,-1),
M: timestamp,
Reason: meta[2].slice(1,-1),
ContactInfo: meta[3].slice(1,-1),
};
Signatures.push({
message: message,
meta: meta
});
})
}
function hex2str(str1){
var hex = str1.toString();
var str = '';
for (var n = 0; n < hex.length; n += 2) {
str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
}
return str;
} |
|
Hi, |
|
|
@earthchie Your code snippet looks promising. Does it also verify the signature is valid? |
|
Hi, |
|
@softboy99 No, pdf.js doesn't have an API to sign a PDF file. And it doesn't display digital signatures because the team discovered it's difficult to verify the signature is valid. However, some people found out it's possible to display signatures nonetheless. What I'm interested in is the original idea: displaying valid, correctly signed signatures, and being able to detect fraud signatures. The code snippet provided by earthchie seems to point into this direction. But since I'm new to cryptography, I can't tell whether it can be used to verify the signature is valid. Does anybody following this discussion know? |
|
Hi, |
We do both sign and verify with https://verify.ink |
|
Hi, |
No, The document doesn’t leave the browser, the validation happens in browser, the signing happens in the browser, the encryption/decryption happens on the browser, the rendering happens in the browser, it’s all client side. |
If your computer cannot connect to https://verify.ink/viewer, what will happen? |
It’s possible to host the web component on ones own servers too. |
|
@softboy99 I'm not a member of the pdf.js team. I just maintain of fork of pdf.js for my own PDF viewer. However, what we can do is implement the feature in ngx-extended-pdf-viewer until it's mature. After that, we can offer the implementation to pdf.js. I can't do that alone (for lack of knowledge and lack of leisure time), so I'd appreciate help. Maybe even your help? |
Sorry for the late reply. The answer is no. If I understand correctly. There are two steps to verify a PDF signature.
but it is not as easy as it seems. In step 2, you'll need to slice some of the PDF content precisely, or else the hash will shift. Recently I also found a signed-PDF which signature is not in the 5-line format too. So the snippet I provided earlier might not work all the time. Please try this instead. (I know, the code is a mess lol. Please forgive me.) <form id="getPDFSignature">
<input type="file" name="pdf" accept="application/pdf" required>
<button>get Signatures</button>
</form>document.getElementById('getPDFSignature').onsubmit = function (e) {
e.preventDefault();
let FR = new FileReader();
FR.onload = function (e) {
let Signatures = getPDFSignature(e.target.result);
if(Signatures){
console.log(Signatures);
}else{
alert('This document does not contains any signature.');
}
}
FR.readAsText(this.pdf.files[0]);
return false;
}
function getPDFSignature(pdf){
return pdf.split(/endobj/).filter(l=>l.indexOf('adbe.pkcs7.detached')>-1).map(d=>{
let sig = {};
d = d.trim();
let date = d.match(/\/M\(D:(.*?)\)/)[1].replace("'",'').match(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(.{5})/);
let reason = d.match(/\/Reason\((.*?)\)\//);
let location = d.match(/\/Location\((.*?)\)\//);
sig.raw = d;
sig.header = d.match(/(\d) (\d) obj/)[0];
sig.ByteRange = d.match(/\/ByteRange.*?\[(.*?)\]/)[1].trim().split(' ').map(i=>+i);
sig.Date = new Date(`${date[1]}-${date[2]}-${date[3]}T${date[4]}:${date[5]}:${date[6]}${date[7]}`);
sig.Contents = d.match(/\/Contents *?\<(.*?)\>/)[1].replace(/(?:00)*$/, '');
sig.Signature = {Der: hex2str(sig.Contents)};
sig.Signature.ASN1 = forge.asn1.fromDer(sig.Signature.Der);
sig.Signature.message = forge.pkcs7.messageFromAsn1(sig.Signature.ASN1);
if(reason){
sig.Reason = reason[1];
}else{
sig.Reason = '';
}
if(location){
sig.Location = location[1];
}else{
sig.Location = '';
}
return sig;
});
}
function hex2str(str1){
var hex = str1.toString();
var str = '';
for (var n = 0; n < hex.length; n += 2) {
str += String.fromCharCode(parseInt(hex.substr(n, 2), 16));
}
return str;
}Will update ya later once I successfully validate the signature with PDF content. |




Hi! We are interested about when or if you´re going to implement the Signature Data View (Xades, Pades & Cades) in the PDF viewer (PDF.js)
Kind Regards
Alejandro Pinedo,
SOA-X
The text was updated successfully, but these errors were encountered: