Skip to content

Conversation

@vbuch
Copy link
Contributor

@vbuch vbuch commented Aug 8, 2018

Adds signDetached() to the PKCS#7 implementation (see #279) as in my usecase it worked perfectly fine the way it was proposed 3 years ago by @fadlijuniarso. The same piece of code is being used in a number of repos on GitHub but what lead me here was the piece used in pdfsign.js so thanks to @tbocek as well.
I've also added a test to cover signDetached().

@vbuch vbuch mentioned this pull request Aug 8, 2018
@tbocek
Copy link

tbocek commented Aug 9, 2018

awesome!

Copy link
Contributor

@mattcollier mattcollier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

lib/pkcs7.js Outdated
},

/**
* Signs the content.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add @param etc to the function docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 09afde4

lib/pkcs7.js Outdated
/**
* Signs the content.
*/
baseSign: function(detached) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be _baseSign? Do we want this API to be part of the public API?

Copy link
Contributor Author

@vbuch vbuch Aug 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally correct. Fixed in 09afde4

// the content field and is optional for a ContentInfo but required here
// since signers are present
if(msg.contentInfo.value.length < 2) {
if(!content) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since contentInfo is no longer being used here, the above comment becomes irrelevant. Can it be moved to some other useful location?

Copy link
Contributor Author

@vbuch vbuch Aug 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the comment where it makes sense in 09afde4

lib/pkcs7.js Outdated
* @param detached If signing should be done in detached mode
*/
baseSign: function(detached) {
_baseSign: function(detached) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vbuch I don't know what the future holds for _baseSign. I really don't like the look of msg._baseSign(true). How do you feel about the signature being msg._baseSign({detached: true}), and for the default case, it can just be msg._baseSign()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would introduce a couple of lines of more code, but would look much better, yes. Will do it.

Copy link
Contributor Author

@vbuch vbuch Aug 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... I'm thinking, now that I see the code... baseSign could be needless in this case. As long as sing() has an optional options parameter we could call sign() and sign({detached: true}) instead of sign() and signDetached() What do you think @mattcollier ?
See: https://github.com/vbuch/forge/blob/master/lib/pkcs7.js#L352

I will also include a line in the README in the PKCS#7 section once you confirm either _baseSign(options) or sign(options).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vbuch good point. It will be up to @dlongley and @davidlehn to answer this question.

@davidlehn
Copy link
Member

I'll be a buzzkill and suggest the commit descriptions could be rewritten to remove the emoji. We haven't done that elsewhere and sparkles and ok_hand don't really mean anything in this context.

@vbuch
Copy link
Contributor Author

vbuch commented Aug 10, 2018

@davidlehn Agreed. I'm just used to gitmoji. I'll create another PR once I'm done with @mattcollier's remarks.

@davidlehn
Copy link
Member

@vbuch Huh, I didn't realize gitmoji was a "spec". I just thought emoji were random and people doing that were crazy. :-) It's not a bad idea I guess, but kind of weird to just use it for a few commits. Also seems a "spec" mapping keywords like ":bugfix:" to emoji would make lots more sense. But I'm getting off topic. Could just rewrite the commit message history on your branch and force push. Or we could just not worry about it. Sorry for the hassle and thanks for the patch.

@vbuch
Copy link
Contributor Author

vbuch commented Aug 10, 2018

@davidlehn Will do that. And what about this #605 (comment)

@davidlehn
Copy link
Member

@vbuch: I don't have strong opinions on the api, mainly because I don't know what detached signatures are used for! ;-) @dlongley? I'd say just have sign(options), and if people get confused we can add the helper function signDetached = () => sign({detached: true}).

@davidlehn
Copy link
Member

@vbuch Was the test case generated by some other app or by the new code itself? Just wondering where to look to see if the detached sig construction is correct. Not quite sure where spec text is for that.

@vbuch
Copy link
Contributor Author

vbuch commented Aug 10, 2018

What I am using detached signatures for is signing PDFs. The one in the test is generated by the code. I'm working on an actual test for its validity here https://github.com/vbuch/node-signpdf Once I'm done I could possibly have better testing for the code here thou I'm not sure more is really needed.

@davidlehn
Copy link
Member

Ok. Testing against itself is good for regressions, but hard to say if it's compatible with other tools. I'm not keen on reading that whole adobe pdf doc about this stuff. ;-) Once that options api issue is addressed will likely just merge and release this. Can fix and/or improve testing later if there are issues.

vbuch and others added 5 commits August 13, 2018 10:34
@vbuch vbuch changed the title Add signDetached() to PKCS#7 Add sign({detached: true}) to PKCS#7 Aug 13, 2018
@vbuch
Copy link
Contributor Author

vbuch commented Aug 13, 2018

@davidlehn @mattcollier @dlongley I think I did all the things we agreed on. CI passed. Seems to me this piece is good to go. 🎉

@davidlehn davidlehn merged commit d2940f4 into digitalbazaar:master Aug 14, 2018
@davidlehn
Copy link
Member

0.7.6 released with these changes. Thanks.

@dirkx
Copy link

dirkx commented Jun 12, 2020

@vbuch A bit of an odd issue - using your detached:true I am still seeing the payload in the PKCS#7.

I.e running node x.js | openssl pkcs7 -print -text shows the contents: type: pkcs7-data field in the output.

var forge = require('node-forge');
var fs = require('fs');


// openssl req -x509 -subj /CN=foo -keyout x.key -out x.crt -nodes
var certAsPem = fs.readFileSync('x.crt');
var cert = forge.pki.certificateFromPem(certAsPem)

var keyAsPem = fs.readFileSync('x.key');
var privateKey = forge.pki.privateKeyFromPem(keyAsPem);

var p7 = forge.pkcs7.createSignedData();

p7.content = forge.util.createBuffer('TEK:12321378, TEK:1278391287189, TEK:12321378, TEK:1278391287189,TEK:12321378, TEK:1278391287189,TEK:12321378, TEK:1278391287189,TEK:12321378, TEK:1278391287189,TEK:12321378, TEK:1278391287189', 'utf8');
p7.addCertificate(cert);

p7.addSigner({
  key: privateKey,
  certificate: cert,
  digestAlgorithm: forge.pki.oids.sha256,
  authenticatedAttributes: [{
    type: forge.pki.oids.contentType,
    value: forge.pki.oids.data
  }, {
    type: forge.pki.oids.messageDigest
    // value will be auto-populated at signing time
  }, {
    type: forge.pki.oids.signingTime,
    // value can also be auto-populated at signing time
    value: new Date()
  }]
});
p7.sign();

var pem = forge.pkcs7.messageToPem(p7);

// PKCS#7 Sign in detached mode.
// Includes the signature and certificate without the signed data.
//
p7.sign({detached: true});

var out = forge.pkcs7.messageToPem(p7);

console.log(out);

@vbuch
Copy link
Contributor Author

vbuch commented Jun 19, 2020

Sorry @dirkx. I won't be able to help. I see the lib has gone 2 versions ahead since my addition was released. Maybe something changed meanwhile.
I remember the output signature was always the same length, given the same key no matter the length of the content. I would think that is caused by not including the contents but rather only the md. I may be wrong. Not sure. Dont have the time to browse and check whats up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants