Loading a PKCS7 SignedData structure with d2i_PKCS7_fp

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Loading a PKCS7 SignedData structure with d2i_PKCS7_fp

Fred Anderson
I have a binary file with a DER-encoded SignedData structure in it. I
can use the following command:

$ openssl pkcs7 -inform DER -in pt2.out -print_certs -text

and I get all sorts of expected information about the certificate in
the structure, which indicates to me that the file's in the right
format.

I'm trying to load the SignedData into a PKCS7, so I can use the
PKCS7_verify function to verify the signature in it. To do that, I'm
calling d2i_PKCS7_fp. I receive no errors, but my PKCS7 is still
empty.

Here's the code I'm using:

  FILE *fp = fopen("pt2.out","rb");
  PKCS7 *p7 = NULL;

  ERR_load_PKCS7_strings();
  ERR_load_X509_strings();

  p7 = d2i_PKCS7_fp(fp,NULL);

(I've also tried "p7 = d2i_PKCS7_fp(fp,&p7);" with the same results)

Printing p7->detached and p7->length both show a zero. Calling
PKCS7_verify with p7 gives a segmentation fault, while calling it with
NULL returns a 0 (failure).

I can't seem to find any real documentation on using the d2i_PKCS7_fp
function, so I suspect I'm just doing something wrong. Can someone
point me to something that gives a little explanation on using the
function, or show me an example of using it?

Thanks in advance,
Fred
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Loading a PKCS7 SignedData structure with d2i_PKCS7_fp

Dr. Stephen Henson
On Thu, Jul 07, 2005, Fred Anderson wrote:

> I have a binary file with a DER-encoded SignedData structure in it. I
> can use the following command:
>
> $ openssl pkcs7 -inform DER -in pt2.out -print_certs -text
>
> and I get all sorts of expected information about the certificate in
> the structure, which indicates to me that the file's in the right
> format.
>
> I'm trying to load the SignedData into a PKCS7, so I can use the
> PKCS7_verify function to verify the signature in it. To do that, I'm
> calling d2i_PKCS7_fp. I receive no errors, but my PKCS7 is still
> empty.
>
> Here's the code I'm using:
>
>   FILE *fp = fopen("pt2.out","rb");
>   PKCS7 *p7 = NULL;
>
>   ERR_load_PKCS7_strings();
>   ERR_load_X509_strings();
>
>   p7 = d2i_PKCS7_fp(fp,NULL);
>
> (I've also tried "p7 = d2i_PKCS7_fp(fp,&p7);" with the same results)
>
> Printing p7->detached and p7->length both show a zero. Calling
> PKCS7_verify with p7 gives a segmentation fault, while calling it with
> NULL returns a 0 (failure).
>
> I can't seem to find any real documentation on using the d2i_PKCS7_fp
> function, so I suspect I'm just doing something wrong. Can someone
> point me to something that gives a little explanation on using the
> function, or show me an example of using it?
>

If you get a non-NULL return from d2i_PKCS7_fp() the structure should be
basically OK. How are you calling PKCS7_verify?

You can perform this operation using the 'openssl smime' for example:

openssl smime -verify -inform DER -in whatever.der

Steve.
--
Dr Stephen N. Henson. Email, S/MIME and PGP keys: see homepage
OpenSSL project core developer and freelance consultant.
Funding needed! Details on homepage.
Homepage: http://www.drh-consultancy.demon.co.uk
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Loading a PKCS7 SignedData structure with d2i_PKCS7_fp

Fred Anderson
Steve,

Thanks for the reply. I was able to verify the signature using
"openssl smime" like you suggested, and the d2i_PKCS7_fp function is
returning a non-null value.

I'm thinking now that the problem may be in the BIO structures I'm
creating and passing to the verify function. Here's what I have:

  int ret;
  FILE *fp = fopen("pt2.out","rb");
  int fd = open("data.out",O_RDONLY);
  BIO *In, *Out;
  PKCS7 *p7 = NULL;


  p7 = d2i_PKCS7_fp(fp,NULL);

  In = BIO_new_fd(fd,BIO_NOCLOSE);
  Out = BIO_new(BIO_s_null());

  ret = PKCS7_verify(p7,NULL,NULL,In,Out,0);
  printf("Call returned %d\n",ret);


where 'fd' is a file descriptor to a file containing the data
(detached) for the signature I wish to verify. As the code stands, the
PKCS7_verify is segfaulting.

My apologies if this is basic stuff; I'm still a beginner with OpenSSL
coding and having a hard time finding docs to explain the usage of the
calls and how the calls all tie together.

Thanks again,
Fred

On 7/7/05, Dr. Stephen Henson <[hidden email]> wrote:

> On Thu, Jul 07, 2005, Fred Anderson wrote:
>
> > I have a binary file with a DER-encoded SignedData structure in it. I
> > can use the following command:
> >
> > $ openssl pkcs7 -inform DER -in pt2.out -print_certs -text
> >
> > and I get all sorts of expected information about the certificate in
> > the structure, which indicates to me that the file's in the right
> > format.
> >
> > I'm trying to load the SignedData into a PKCS7, so I can use the
> > PKCS7_verify function to verify the signature in it. To do that, I'm
> > calling d2i_PKCS7_fp. I receive no errors, but my PKCS7 is still
> > empty.
> >
> > Here's the code I'm using:
> >
> >   FILE *fp = fopen("pt2.out","rb");
> >   PKCS7 *p7 = NULL;
> >
> >   ERR_load_PKCS7_strings();
> >   ERR_load_X509_strings();
> >
> >   p7 = d2i_PKCS7_fp(fp,NULL);
> >
> > (I've also tried "p7 = d2i_PKCS7_fp(fp,&p7);" with the same results)
> >
> > Printing p7->detached and p7->length both show a zero. Calling
> > PKCS7_verify with p7 gives a segmentation fault, while calling it with
> > NULL returns a 0 (failure).
> >
> > I can't seem to find any real documentation on using the d2i_PKCS7_fp
> > function, so I suspect I'm just doing something wrong. Can someone
> > point me to something that gives a little explanation on using the
> > function, or show me an example of using it?
> >
>
> If you get a non-NULL return from d2i_PKCS7_fp() the structure should be
> basically OK. How are you calling PKCS7_verify?
>
> You can perform this operation using the 'openssl smime' for example:
>
> openssl smime -verify -inform DER -in whatever.der
>
> Steve.
> --
> Dr Stephen N. Henson. Email, S/MIME and PGP keys: see homepage
> OpenSSL project core developer and freelance consultant.
> Funding needed! Details on homepage.
> Homepage: http://www.drh-consultancy.demon.co.uk
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    [hidden email]
> Automated List Manager                           [hidden email]
>
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Loading a PKCS7 SignedData structure with d2i_PKCS7_fp

Dr. Stephen Henson
On Thu, Jul 07, 2005, Fred Anderson wrote:

> Steve,
>
> Thanks for the reply. I was able to verify the signature using
> "openssl smime" like you suggested, and the d2i_PKCS7_fp function is
> returning a non-null value.
>
> I'm thinking now that the problem may be in the BIO structures I'm
> creating and passing to the verify function. Here's what I have:
>
>   int ret;
>   FILE *fp = fopen("pt2.out","rb");
>   int fd = open("data.out",O_RDONLY);
>   BIO *In, *Out;
>   PKCS7 *p7 = NULL;
>
>
>   p7 = d2i_PKCS7_fp(fp,NULL);
>
>   In = BIO_new_fd(fd,BIO_NOCLOSE);
>   Out = BIO_new(BIO_s_null());
>
>   ret = PKCS7_verify(p7,NULL,NULL,In,Out,0);
>   printf("Call returned %d\n",ret);
>
>
> where 'fd' is a file descriptor to a file containing the data
> (detached) for the signature I wish to verify. As the code stands, the
> PKCS7_verify is segfaulting.
>
> My apologies if this is basic stuff; I'm still a beginner with OpenSSL
> coding and having a hard time finding docs to explain the usage of the
> calls and how the calls all tie together.
>

You might find it easier to use:

BIO *foo = BIO_new_file("filename", "rb");

which is a BIO version of "fopen".

Also you need to include a trusted certificate store which includes the CAs
you trust. If you include the flag PKCS7_NOVERIFY it wont try to verify the
signers certificates: that's not useful for anything other than debugging
because anyone could create a PKCS#7 structure it would then verify as OK.

Steve.
--
Dr Stephen N. Henson. Email, S/MIME and PGP keys: see homepage
OpenSSL project core developer and freelance consultant.
Funding needed! Details on homepage.
Homepage: http://www.drh-consultancy.demon.co.uk
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Loading a PKCS7 SignedData structure with d2i_PKCS7_fp

Fred Anderson
Thanks once again for the help, Steve. I've gotten beyond the
segmentation fault using your suggestions. Now I'm getting the
following error on the PKCS7_verify call:

error:2106906D:PKCS7 routines:PKCS7_dataInit:unknown digest type

Once again, I suspect it's something I'm doing. Given a file
"pt2.out", which has a DER-encoded SignedData, and a file "data.out",
which has the detached data whose signature is in the SignedData
structure, the following command works:

$ openssl smime -verify -inform DER -in pt2.out -content data.out -noverify
Verification successful

Right now I'm not concerned with verifying the certs, just the
signature itself.

Here's my code to (I think) do the same thing the "openssl" command
above does, and gives the error I pasted above:

main()
{
  int ret;
  unsigned long err;
  FILE *fp = fopen("pt2.out","rb");
  BIO *Out, *In;
  PKCS7 *p7 = NULL;

  ERR_load_PKCS7_strings();
  ERR_load_X509_strings();

  p7 = d2i_PKCS7_fp(fp,NULL);
  fclose(fp);
  if (p7==NULL)
  {
    printf("NULL pkcs7\n");
    return -1;
  }

  In = BIO_new_file("data.out","rb");
  Out = BIO_new_file("data2.out","wb");

  ret = PKCS7_verify(p7,NULL,NULL,In,Out,PKCS7_NOVERIFY);
  printf("Call returned %d\n",ret);
  err = ERR_get_error();
  printf("%d: %s\n",err,ERR_error_string(err,NULL));

}


All I'm trying to do right now is verify the signature, which was
created with some crypto hardware on Windows via the CryptoAPI. It's
just a proof of concept.

Any ideas on why I'm getting the "unknown digest type" error?

Thanks in advance,
Fred


On 7/7/05, Dr. Stephen Henson <[hidden email]> wrote:

> On Thu, Jul 07, 2005, Fred Anderson wrote:
>
> > Steve,
> >
> > Thanks for the reply. I was able to verify the signature using
> > "openssl smime" like you suggested, and the d2i_PKCS7_fp function is
> > returning a non-null value.
> >
> > I'm thinking now that the problem may be in the BIO structures I'm
> > creating and passing to the verify function. Here's what I have:
> >
> >   int ret;
> >   FILE *fp = fopen("pt2.out","rb");
> >   int fd = open("data.out",O_RDONLY);
> >   BIO *In, *Out;
> >   PKCS7 *p7 = NULL;
> >
> >
> >   p7 = d2i_PKCS7_fp(fp,NULL);
> >
> >   In = BIO_new_fd(fd,BIO_NOCLOSE);
> >   Out = BIO_new(BIO_s_null());
> >
> >   ret = PKCS7_verify(p7,NULL,NULL,In,Out,0);
> >   printf("Call returned %d\n",ret);
> >
> >
> > where 'fd' is a file descriptor to a file containing the data
> > (detached) for the signature I wish to verify. As the code stands, the
> > PKCS7_verify is segfaulting.
> >
> > My apologies if this is basic stuff; I'm still a beginner with OpenSSL
> > coding and having a hard time finding docs to explain the usage of the
> > calls and how the calls all tie together.
> >
>
> You might find it easier to use:
>
> BIO *foo = BIO_new_file("filename", "rb");
>
> which is a BIO version of "fopen".
>
> Also you need to include a trusted certificate store which includes the CAs
> you trust. If you include the flag PKCS7_NOVERIFY it wont try to verify the
> signers certificates: that's not useful for anything other than debugging
> because anyone could create a PKCS#7 structure it would then verify as OK.
>
> Steve.
> --
> Dr Stephen N. Henson. Email, S/MIME and PGP keys: see homepage
> OpenSSL project core developer and freelance consultant.
> Funding needed! Details on homepage.
> Homepage: http://www.drh-consultancy.demon.co.uk
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    [hidden email]
> Automated List Manager                           [hidden email]
>
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Loading a PKCS7 SignedData structure with d2i_PKCS7_fp

Dr. Stephen Henson
On Fri, Jul 08, 2005, Fred Anderson wrote:

> Thanks once again for the help, Steve. I've gotten beyond the
> segmentation fault using your suggestions. Now I'm getting the
> following error on the PKCS7_verify call:
>
> error:2106906D:PKCS7 routines:PKCS7_dataInit:unknown digest type
>

Ah, now that one is in the FAQ..

Steve.
--
Dr Stephen N. Henson. Email, S/MIME and PGP keys: see homepage
OpenSSL project core developer and freelance consultant.
Funding needed! Details on homepage.
Homepage: http://www.drh-consultancy.demon.co.uk
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: Loading a PKCS7 SignedData structure with d2i_PKCS7_fp

Fred Anderson
Indeed it was, thanks for all your help. :)

On 7/10/05, Dr. Stephen Henson <[hidden email]> wrote:

> On Fri, Jul 08, 2005, Fred Anderson wrote:
>
> > Thanks once again for the help, Steve. I've gotten beyond the
> > segmentation fault using your suggestions. Now I'm getting the
> > following error on the PKCS7_verify call:
> >
> > error:2106906D:PKCS7 routines:PKCS7_dataInit:unknown digest type
> >
>
> Ah, now that one is in the FAQ..
>
> Steve.
> --
> Dr Stephen N. Henson. Email, S/MIME and PGP keys: see homepage
> OpenSSL project core developer and freelance consultant.
> Funding needed! Details on homepage.
> Homepage: http://www.drh-consultancy.demon.co.uk
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    [hidden email]
> Automated List Manager                           [hidden email]
>
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]