Re: [openssl-users] base64 decode in C

2015-03-19 Thread Prashant Bapat
Hi,

My problem is if the string that I want to decode start with null
terminated char (as in a SSH pubkey), the base64_decode does not return
anything at all. The encoded string itself does not have null terminated
chars but rather the decoded result. The buffer being returned is empty.
How to overcome this ?

Also my C is relatively rusty, it would be great help if someone could look
look at my code and suggest what I'm doing wrong.

Also, I tried to use the EVP_DecodeBlock function with same result. It
decodes everything except the SSH pubkey.

Thanks.
--Prashant

On 18 March 2015 at 22:56, Scott Neugroschl scot...@xypro.com wrote:

  I believe the SSH pubkey is binary data, not ASCII, so strlen() will not
 work on it if it has embedded NUL chars.

 As Dave Thompson suggested, instead of strlen(), use the length returned
 from BIO_read.





 *From:* openssl-users [mailto:openssl-users-boun...@openssl.org] *On
 Behalf Of *Prashant Bapat
 *Sent:* Wednesday, March 18, 2015 8:08 AM
 *To:* openssl-users
 *Subject:* Re: [openssl-users] base64 decode in C



 Hi Dave and Walter,



 Thanks for our reply.



 I'm not doing anything different for the ssh pubkey. I'm able to decode it
 using the openssl enc -base64 -d -A command. But not using the C program.



 Attaching my entire code here. After getting the base64 decoded I'm
 calculating the MD5 sum and printing it. This works for a regular string
 but not for SSH pubkey.



 Thanks again.



 --Prashant



 On 18 March 2015 at 18:04, Walter H. walte...@mathemainzel.info wrote:

 Hi,

 before calling this function,
 remove any whitespace;

 Walter



 ___
 openssl-users mailing list
 To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users



 ___
 openssl-users mailing list
 To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users


___
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users


Re: [openssl-users] base64 decode in C

2015-03-19 Thread Prashant Bapat
Hey I got this. Sorry I was confused with the length. BIO_read returns the
length which I was able to use to get the MD5. Problem solved!!!

Many thanks.
--Pashant

On 19 March 2015 at 14:24, Prashant Bapat prash...@apigee.com wrote:

 Hi,

 My problem is if the string that I want to decode start with null
 terminated char (as in a SSH pubkey), the base64_decode does not return
 anything at all. The encoded string itself does not have null terminated
 chars but rather the decoded result. The buffer being returned is empty.
 How to overcome this ?

 Also my C is relatively rusty, it would be great help if someone could
 look look at my code and suggest what I'm doing wrong.

 Also, I tried to use the EVP_DecodeBlock function with same result. It
 decodes everything except the SSH pubkey.

 Thanks.
 --Prashant

 On 18 March 2015 at 22:56, Scott Neugroschl scot...@xypro.com wrote:

  I believe the SSH pubkey is binary data, not ASCII, so strlen() will
 not work on it if it has embedded NUL chars.

 As Dave Thompson suggested, instead of strlen(), use the length returned
 from BIO_read.





 *From:* openssl-users [mailto:openssl-users-boun...@openssl.org] *On
 Behalf Of *Prashant Bapat
 *Sent:* Wednesday, March 18, 2015 8:08 AM
 *To:* openssl-users
 *Subject:* Re: [openssl-users] base64 decode in C



 Hi Dave and Walter,



 Thanks for our reply.



 I'm not doing anything different for the ssh pubkey. I'm able to decode
 it using the openssl enc -base64 -d -A command. But not using the C
 program.



 Attaching my entire code here. After getting the base64 decoded I'm
 calculating the MD5 sum and printing it. This works for a regular string
 but not for SSH pubkey.



 Thanks again.



 --Prashant



 On 18 March 2015 at 18:04, Walter H. walte...@mathemainzel.info wrote:

 Hi,

 before calling this function,
 remove any whitespace;

 Walter



 ___
 openssl-users mailing list
 To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users



 ___
 openssl-users mailing list
 To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users



___
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users


Re: [openssl-users] base64 decode in C

2015-03-18 Thread Walter H.

Hi,

before calling this function,
remove any whitespace;

Walter




smime.p7s
Description: S/MIME Cryptographic Signature
___
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users


Re: [openssl-users] base64 decode in C

2015-03-18 Thread Prashant Bapat
Hi Dave and Walter,

Thanks for our reply.

I'm not doing anything different for the ssh pubkey. I'm able to decode it
using the openssl enc -base64 -d -A command. But not using the C program.

Attaching my entire code here. After getting the base64 decoded I'm
calculating the MD5 sum and printing it. This works for a regular string
but not for SSH pubkey.

Thanks again.

--Prashant

On 18 March 2015 at 18:04, Walter H. walte...@mathemainzel.info wrote:

 Hi,

 before calling this function,
 remove any whitespace;

 Walter



 ___
 openssl-users mailing list
 To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users


#include openssl/md5.h
#include openssl/sha.h
#include openssl/hmac.h
#include openssl/evp.h
#include openssl/bio.h
#include openssl/buffer.h
#include string.h
#include stdio.h

char *b64_decode(unsigned char *input, int length);
char* md5_digest(char *string);

int main()
{
char *str = B3NzaC1yc2EDAQABAAABAQC/KdcFv09+f+tJK9IZ8I+L0zG7dUINClI5v8FlHJsBPSM3DDO2DpwIg/KqZKCRH9y6lEO+QAJt2DTEq/LBZcBUCdeiX1TXPFRorX+VdZigj7av/S/UHkq2EH6hfkJB3oLA5ZOZioMOAuDv1ng/DE4pRBr+KZ2oVhGjf3wa0hWi21vTZqb3s7vh+bPf6C2eUmAQJKHvFhtBK8Xx7FxN0b7igsGbk7ObwcItfMxdzkMvuiuU/UnthFVpa8wZIObFDi3MxJuf3/R+h6R1lFMvEIrU6CWRupS7Pqkm4X3qWQfhAWbdgdbD5KAk5JLA2eWIPQQA5Uay5CeH+GXz8gCa4zaz;

printf(Base64 decoded string is : %s\n, b64_decode(str, strlen(str))); // This should print binary for a ssh key.
printf(MD5 Sum of the decoded string is : %s\n, md5_digest(b64_decode(str, strlen(str;

return 0;
}

char *b64_decode(unsigned char *input, int length)
{
BIO *b64, *bmem;
char *buffer = (char *)malloc(length);
memset(buffer, 0, length);
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new_mem_buf((void*)input, length);
bmem = BIO_push(b64, bmem);
BIO_set_flags(bmem, BIO_FLAGS_BASE64_NO_NL);
BIO_read(bmem, buffer, length);
BIO_free_all(bmem);
return buffer;
}

char* md5_digest(char *string)
{
  int i;
  unsigned char result[MD5_DIGEST_LENGTH];

  // Length of MD5 signature is 32 !
  char * md5_sig = (char *) malloc(33);
  
  MD5(string, strlen(string), result);

  // output
  for(i = 0; i  MD5_DIGEST_LENGTH; i++){
sprintf( md5_sig[i*2], %02x, result[i]);
}
  return md5_sig;
}
___
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users


Re: [openssl-users] base64 decode in C

2015-03-18 Thread Jakob Bohm

Please refer to Dave Thompson's answer, it describes your problem.

On 18/03/2015 16:08, Prashant Bapat wrote:

Hi Dave and Walter,

Thanks for our reply.

I'm not doing anything different for the ssh pubkey. I'm able to 
decode it using the openssl enc -base64 -d -A command. But not using 
the C program.


Attaching my entire code here. After getting the base64 decoded I'm 
calculating the MD5 sum and printing it. This works for a regular 
string but not for SSH pubkey.


Thanks again.

--Prashant


Enjoy

Jakob
--
Jakob Bohm, CIO, Partner, WiseMo A/S.  http://www.wisemo.com
Transformervej 29, 2860 Søborg, Denmark.  Direct +45 31 13 16 10
This public discussion message is non-binding and may contain errors.
WiseMo - Remote Service Management for PCs, Phones and Embedded

___
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users


Re: [openssl-users] base64 decode in C

2015-03-18 Thread Scott Neugroschl
I believe the SSH pubkey is binary data, not ASCII, so strlen() will not work 
on it if it has embedded NUL chars.
As Dave Thompson suggested, instead of strlen(), use the length returned from 
BIO_read.


From: openssl-users [mailto:openssl-users-boun...@openssl.org] On Behalf Of 
Prashant Bapat
Sent: Wednesday, March 18, 2015 8:08 AM
To: openssl-users
Subject: Re: [openssl-users] base64 decode in C

Hi Dave and Walter,

Thanks for our reply.

I'm not doing anything different for the ssh pubkey. I'm able to decode it 
using the openssl enc -base64 -d -A command. But not using the C program.

Attaching my entire code here. After getting the base64 decoded I'm calculating 
the MD5 sum and printing it. This works for a regular string but not for SSH 
pubkey.

Thanks again.

--Prashant

On 18 March 2015 at 18:04, Walter H. 
walte...@mathemainzel.infomailto:walte...@mathemainzel.info wrote:
Hi,

before calling this function,
remove any whitespace;

Walter



___
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users

___
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users


[openssl-users] base64 decode in C

2015-03-18 Thread Prashant Bapat
Hi,

Most likely this has been answered before, please bear with me.

I'm trying to use the base64 decode function in C. Below is the function.

char *b64_decode(unsigned char *input, int length)
{
BIO *b64, *bmem;
char *buffer = (char *)malloc(length);
memset(buffer, 0, length);
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new_mem_buf((void*)input, length);
bmem = BIO_push(b64, bmem);
BIO_set_flags(bmem, BIO_FLAGS_BASE64_NO_NL);
BIO_read(bmem, buffer, length);
BIO_free_all(bmem);
return buffer;
}

This works well for simple b64 encoded strings like hello world! etc. But
when I want to b64 decode the contents of a SSH public key, it fails.
Returns nothing.

What I'm trying to get to is the SSH public key fingerprint which is the
MD5 hash of the base64 decoded part of the public key.

This decodes fine.

dGhpcyBpcyBhd2Vzb21lCg==  : this is awesome

This does not.
B3NzaC1yc2EDAQABAAABAQC/KdcFv09+f+tJK9IZ8I+L0zG7dUINClI5v8FlHJsBPSM3DDO2DpwIg/KqZKCRH9y6lEO+QAJt2DTEq/LBZcBUCdeiX1TXPFRorX+VdZigj7av/S/UHkq2EH6hfkJB3oLA5ZOZioMOAuDv1ng/DE4pRBr+KZ2oVhGjf3wa0hWi21vTZqb3s7vh+bPf6C2eUmAQJKHvFhtBK8Xx7FxN0b7igsGbk7ObwcItfMxdzkMvuiuU/UnthFVpa8wZIObFDi3MxJuf3/R+h6R1lFMvEIrU6CWRupS7Pqkm4X3qWQfhAWbdgdbD5KAk5JLA2eWIPQQA5Uay5CeH+GXz8gCa4zaz

What I'm I doing wrong ?

Btw in the command line both decode. Using echo string | openssl enc
-base64 -d -A

Any help appreciated.

Thanks in advance.

--Prashant
___
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users


Re: [openssl-users] base64 decode in C

2015-03-18 Thread Walter H.

On 18.03.2015 16:08, Prashant Bapat wrote:

printf(Base64 decoded string is : %s\n, b64_decode(str, strlen(str))); // 
This should print binary for a ssh key.
not really, because the return of b64_decode is not a C string; and the 
format specfier %s expects a C string;




smime.p7s
Description: S/MIME Cryptographic Signature
___
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users