Hi Andreas, I hope this thread will help other newbies like me :)
Below you will find sort of pseudo code. By stating "get the signature
hash and sign with your card" you mean that I should get digest from
variable "signature" and sign it on smartcard ? This signed and
encrypted hash needs to be added to signature either, but how ?
public void Sign()
{
Pkcs12Store ks = new Pkcs12Store(fs, CERT_PASSW.ToCharArray());
string alias = null;
foreach (string al in ks.Aliases) {
if (ks.IsKeyEntry(al) && ks.GetKey(al).Key.IsPrivate) {
alias = al;
break;
}
}
ICipherParameters key = ks.GetKey(alias).Key;
// get signing certificate
X509Certificate signer = LocateSigner();
// get basic OCSP response: is signer cert valid ?
byte[] ocsp = GetBasicOCSPResponse(signer, ks);
PdfPKCS7 pk7 = new PdfPKCS7(key, chain, null, "SHA1", null, false);
IDigest messageDigest = DigestUtilities.GetDigest("SHA1");
Stream data = sap.RangeStream;
byte[] buf = new byte[8192];
int n;
while ((n = data.Read(buf, 0, buf.Length)) > 0) {
messageDigest.BlockUpdate(buf, 0, n);
}
byte[] hash = new byte[messageDigest.GetDigestSize()];
messageDigest.DoFinal(hash, 0);
DateTime cal = DateTime.Now;
byte[] sh = pk7.GetAuthenticatedAttributeBytes(hash, cal, ocsp);
pk7.Update(sh, 0, sh.length);
byte[] signature = pk7.GetEncodedPKCS7(hash, cal, null, ocsp);
}
> Hi again,
>
> don't mess upm the different hashes :
>
> 1. hash : The one that's calculated over the PKCS7 signature itself (
especially the authenticated attributes ). This one is going to be
encrypted by the private key.
>
> 2. the hash of the PDF byte range : This one will end up in a
authenticated attribute in the PKCS7 signature.
>
> So the first hash can only be calcuated _after_ the construction of
the PKCS7 signature structure, espacially the authenticated attributes.
One of these attributes is the 'second hash', some other attributes may
be the OCSP response.
>
> The OCSP call can be done completely unrelated to the signature
creation, you just have to know the certificate you are going to use and
could do the call to the CA. So the generation sequence may look like
this :
>
> - select the certificate to use for signing
> - make the OSCP call
> - create a signature structure
> - add the OCSP response as an attribute
> - prepare the PDF for signing
> - calculate the byte range hash and add it as an attribute ( the
second hash )
> - get the signature hash and sign it with our card
> - close the PDF
>
> Don't be confused by some old styled descriptions where there are no
authenticated attributes present in a signature. In this case there is
only one hash involved, but this isn't used for PDF signing.
>
> Greetings
>
> Andreas
>
> ----- Original Message ----
> From: Whatever <[email protected]>
> To: [email protected]
> Sent: Wed, April 7, 2010 1:09:16 PM
> Subject: Re: [iText-questions] OCSP digest
>
> When using smartcard then it works differently. There is an external
> digest that is encrypted. This digest is a an hash across the PDF
byte
> range. For that purpose iText exposes method SetExternalDigest. Now I
> being confused: if hash is already calculated and signed then it's
> impossible to add any OCSP response to PdfPKCS7 object, right ? This
> will invalidate signature. If second digest is calculated across
> signature itself then what method must be used to retrieve those bytes
?
> And what private key will be used to encrypt it, is it digital id or
> must it be done on smartcard ?
>
> > Sorry, but due to other requirements we do most of the crypto
stuff
> outside of iText and so I'm not very familiar with the method you
> mentioned.
> > But a short look atv the code tells me that the 'second digest' is
the
> digest that is build across the PDF byte range that becomes an
> authenticated attribute. I guess the word 'second' denotes the
contrast
> to the 'first' digest that is calculated across the signature itself
and
> will be encrypted by the private key.
> >
> > Good luck
> >
> > Andreas
> >
> >
> > ----- Original Message ----
> > From: Whatever <[email protected]>
> > To: [email protected]
> > Sent: Wed, April 7, 2010 8:16:45 AM
> > Subject: Re: [iText-questions] OCSP digest
> >
> > Andreas, thank you for your thoughts. I need to include revocation
> > information before signing on smartcard. Haven't figured out how to
> > achieve this. PDF reference says "... it must be obtained before
the
> > computation of the digital signature". How to embed OCSP response
> before
> > creating signature ? That is the question. Examples available use
> > digital id for doing that.
> >
> > iText's implementation won't include OCSP response unless there is
a
> > magical second digest argument supplied when calling
GetEncodedPKCS7.
> > That magical digest is calculated over signature appearance and
there
> is
> > no RSA data involved. If I use smartcard then there is RSA data
> involved
> > and I can't use iText implementation, can I ? Am I wrong ?
> >
> > > Hi,
> > >
> > > your assumption seems to hold true : No one seems to be able to
> aswer
> > the question, but I'll try to help you a bit further :
> > >
> > > The first thing to mention is that PDF expects the the OCSP
> response
> > to be available at signing time. So you do the OCSP call _before_
the
> > hashing. And then there is no magic in putting the signed attribute
> into
> > the signature. There was a thread on this list regarding this
topic
> > about a year ago ...
> > >
> > > Second point ( deriving from the first ) the OCSP response is
> required
> > to have a validity period longer than zero. The OCSP information is
> > ignored if the signing time of the signature isn't within the
> validity
> > period of the OCSP response. And that's where we got stuck : The
CAs
> at
> > hand return a OCSP response with a validity period of zero. So this
> > topic is deferred until our own test CA got a responder with a
> positive
> > validity .... currently we didn't got a useful OCSP response in
our
> > signatures.
> > >
> > > But if you got a CA fitting the validity period requirement I
would
> be
> > eager to test our code with it ;-)
> > >
> > > As I remember vaguely there are some more strangenesses around
PDF
> and
> > OCSP. I would recommend to check the list's archive for it ...
> > >
> > > Good luck
> > >
> > > Andreas
> > >
> > > ----- Original Message ----
> > > From: Whatever <[email protected]>
> > > To: Post all your questions about iText here
> > <[email protected]>
> > > Sent: Tue, April 6, 2010 1:18:51 PM
> > > Subject: Re: [iText-questions] OCSP digest
> > >
> > > Is there really nobody who is able to answer the question ?
> > >
> > > My ultimate goal is to include OCSP response. I am using external
> > digest
> > > from smartcard. I have OCSP responder certificate that is issued
by
> > the
> > > same CA that issued signing certificate. I can't include basic
OCSP
> > > response in the signed data because it is already signed and hash
> is
> > > encrypted (external digest).
> > >
> > > May be itext doesn't allow all that and I need to staple the
> signing
> > > certificate with OCSP response
> > > (http://en.wikipedia.org/wiki/OCSP_Stapling) ?
> > >
> > > > Hi!
> > > >
> > > > I am reading RFC 2560 regarding OCSP response... My question is
> how
> > > is
> > > > the second
> > > > digest calculated when supplying first argument to
> GetEncodedPKCS7
> > ?
> > > Is
> > > > it hash over
> > > > all BasicOCSPResponse bytes ?
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/