Null termination after encryption.

View: New views
6 Messages — Rating Filter:   Alert me  

Null termination after encryption.

by Michael Luich-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello everyone, here's what's driving me nuts. I'm sure i'm missing something simple, but why isn't the encrypted message coming out of  " BIO_get_mem_data(out, &enc_msg); " Null terminated ?

Full code below.

Mike Luich


        fprintf (stderr, "ENTER: %s in %s, line %d.\n",
                 __FUNCTION__, __FILE__, __LINE__);


      const    EVP_CIPHER *cipher= EVP_des_ede3_cbc();
      STACK_OF(X509) *certs= sk_X509_new_null();
      X509 *tmp;
      BIO *in, *out, *b64_bio;
      PKCS7 *pkcs7;
      char *enc_msg, *ptr;

      OpenSSL_add_all_algorithms();
      ERR_load_crypto_strings();
      RAND_load_file("/dev/urandom", 1024);
     
          fprintf (stderr, "START: %s in %s, line %d.\n",
                   __FUNCTION__, __FILE__, __LINE__);
     
      in = BIO_new_mem_buf(clear_msg, -1);
      out = BIO_new(BIO_s_mem());

      if (!(tmp = PEM_read_X509(pub_cert, NULL, NULL, NULL))) {
          fprintf (stderr, "Error Opening Public Key");
      }
      sk_X509_push(certs,tmp);
      fclose (pub_cert);

      if (!(pkcs7 = PKCS7_encrypt(certs, in, cipher, PKCS7_BINARY))) {
          fprintf (stderr,"Error making PKC#7object : %s in %s, line %d.\n",
                            __FUNCTION__, __FILE__, __LINE__);
      }

      if (SMIME_write_PKCS7(out, pkcs7, in, 0) !=1)
      {
          fprintf (stderr,"Error writing SMIME : %s in %s, line %d.\n",
                            __FUNCTION__, __FILE__, __LINE__);
      }

          fprintf (stderr,"Encryption success : %s in %s, line %d.\n",
                        __FUNCTION__, __FILE__, __LINE__);
     
      BIO_get_mem_data(out, &enc_msg);

          fprintf (stderr,"Data out of bio: %s in %s, line %d.\n %s\n",
                   __FUNCTION__, __FILE__, __LINE__, enc_msg);


Re: Null termination after encryption.

by Michael S. Zick-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri August 29 2008 11:35, Michael Luich wrote:
> Hello everyone, here's what's driving me nuts. I'm sure i'm missing
> something simple, but why isn't the encrypted message coming out of  "
> BIO_get_mem_data(out, &enc_msg); " Null terminated ?
>

Because null is a valid encrypted value of a byte.

Mike

> Full code below.
>
> Mike Luich
>
>
>         fprintf (stderr, "ENTER: %s in %s, line %d.\n",
>                  __FUNCTION__, __FILE__, __LINE__);
>
>
>       const    EVP_CIPHER *cipher= EVP_des_ede3_cbc();
>       STACK_OF(X509) *certs= sk_X509_new_null();
>       X509 *tmp;
>       BIO *in, *out, *b64_bio;
>       PKCS7 *pkcs7;
>       char *enc_msg, *ptr;
>
>       OpenSSL_add_all_algorithms();
>       ERR_load_crypto_strings();
>       RAND_load_file("/dev/urandom", 1024);
>
>           fprintf (stderr, "START: %s in %s, line %d.\n",
>                    __FUNCTION__, __FILE__, __LINE__);
>
>       in = BIO_new_mem_buf(clear_msg, -1);
>       out = BIO_new(BIO_s_mem());
>
>       if (!(tmp = PEM_read_X509(pub_cert, NULL, NULL, NULL))) {
>           fprintf (stderr, "Error Opening Public Key");
>       }
>       sk_X509_push(certs,tmp);
>       fclose (pub_cert);
>
>       if (!(pkcs7 = PKCS7_encrypt(certs, in, cipher, PKCS7_BINARY))) {
>           fprintf (stderr,"Error making PKC#7object : %s in %s, line %d.\n",
>                             __FUNCTION__, __FILE__, __LINE__);
>       }
>
>       if (SMIME_write_PKCS7(out, pkcs7, in, 0) !=1)
>       {
>           fprintf (stderr,"Error writing SMIME : %s in %s, line %d.\n",
>                             __FUNCTION__, __FILE__, __LINE__);
>       }
>
>           fprintf (stderr,"Encryption success : %s in %s, line %d.\n",
>                         __FUNCTION__, __FILE__, __LINE__);
>
>       BIO_get_mem_data(out, &enc_msg);
>
>           fprintf (stderr,"Data out of bio: %s in %s, line %d.\n %s\n",
>                    __FUNCTION__, __FILE__, __LINE__, enc_msg);
>
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@...
Automated List Manager                           majordomo@...

RE: Null termination after encryption.

by David Schwartz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


> Hello everyone, here's what's driving me nuts.
> I'm sure i'm missing something simple, but why isn't the
> encrypted message coming out of
> " BIO_get_mem_data(out, &enc_msg); " Null terminated ?

> Mike Luich

Why should it be? It's not a string, it's a block of arbitrary data.

Besides, what would be the point in nul-terminating a block of data that may
already contains any number of zero bytes in it?

DS


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@...
Automated List Manager                           majordomo@...

Re: Null termination after encryption.

by Michael Luich-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hmm then perhaps I'm expecting the wong thing to be coming out. I'm using PKCS7_encrypt followed by SMIME_write_PKCS7. So the data is base64 encoded and in S/MIME Format.

I just want to get this in a format that I can return as a string that's null terminated. So the app can use it as a string.

Mike Luich

On Fri, Aug 29, 2008 at 12:54 PM, David Schwartz <davids@...> wrote:

> Hello everyone, here's what's driving me nuts.
> I'm sure i'm missing something simple, but why isn't the
> encrypted message coming out of
> " BIO_get_mem_data(out, &enc_msg); " Null terminated ?

> Mike Luich

Why should it be? It's not a string, it's a block of arbitrary data.

Besides, what would be the point in nul-terminating a block of data that may
already contains any number of zero bytes in it?

DS


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@...
Automated List Manager                           majordomo@...


RE: Null termination after encryption.

by David Schwartz :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message



> Hmm then perhaps I'm expecting the wong thing to be coming
> out. I'm using PKCS7_encrypt followed by SMIME_write_PKCS7.
> So the data is base64 encoded and in S/MIME Format.

> I just want to get this in a format that I can return as a
> string that's null terminated. So the app can use it as a string.

> Mike Luich

You're right, wrong explanation. The function wrote the string to the BIO,
but not the terminator (just as you wouldn't write the terminator to a
file). You then got a pointer to the data in the BIO.

The same thing would occur if you memory-mapped a file containing the
string. It wouldn't be terminated.

If you need to the data in the form of a C-style string, you need to make it
one. You could simply write a zero to the BIO, I suppose.

DS


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@...
Automated List Manager                           majordomo@...

Re: Null termination after encryption.

by Michael Luich-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ok that makes sense. kinda goes with what I had been thinking, So i just add the null character at the end of the bio before I use BIO_get_mem_data(out, &enc_msg);

now just how to do that .....
ssl bio write
mike Luich
On Fri, Aug 29, 2008 at 1:35 PM, David Schwartz <davids@...> wrote:


> Hmm then perhaps I'm expecting the wong thing to be coming
> out. I'm using PKCS7_encrypt followed by SMIME_write_PKCS7.
> So the data is base64 encoded and in S/MIME Format.

> I just want to get this in a format that I can return as a
> string that's null terminated. So the app can use it as a string.

> Mike Luich

You're right, wrong explanation. The function wrote the string to the BIO,
but not the terminator (just as you wouldn't write the terminator to a
file). You then got a pointer to the data in the BIO.

The same thing would occur if you memory-mapped a file containing the
string. It wouldn't be terminated.

If you need to the data in the form of a C-style string, you need to make it
one. You could simply write a zero to the BIO, I suppose.

DS


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@...
Automated List Manager                           majordomo@...

LightInTheBox - Buy quality products at wholesale price!