[openssl-users] base64 decode in C

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

[openssl-users] base64 decode in C

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.
"AAAAB3NzaC1yc2EAAAADAQABAAABAQC/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
Reply | Threaded
Open this post in threaded view
|

Re: [openssl-users] base64 decode in C

Dave Thompson-5
> From: openssl-users On Behalf Of Prashant Bapat
> Sent: Wednesday, March 18, 2015 03:37

> I'm trying to use the base64 decode function in C. <snip>

> 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.

It returns pointer to a buffer containing the pubkey, but no indication of
the length. You don't show the caller, but if the caller tries to treat it as a
C string with strlen()fputs() etc that won't work because it isn't a C string.
A C string can't contain any zero-bits aka null byte, and SSH pubkey contains
many of them; it even starts with *three* consecutive nulls, which
makes it appear to be empty if treated as a C string.

Use the length returned from (successful) BIO_read (b64bio,...).

> This decodes fine.
> "dGhpcyBpcyBhd2Vzb21lCg=="  : "this is awesome"

Actually that decodes as "this is awesome" PLUS A NEWLINE, or in
C source notation "this is awesome\n". Yes that matters.


_______________________________________________
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users
Reply | Threaded
Open this post in threaded view
|

Re: [openssl-users] base64 decode in C

Walter H.
In reply to this post by Prashant Bapat
Hi,

before calling this function,
remove any whitespace;

Walter



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

smime.p7s (7K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [openssl-users] base64 decode in C

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. <[hidden email]> 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

base64.c (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [openssl-users] base64 decode in C

Ken Goldman-2
As someone already posted, you can't use strlen on an array that is not
a string.

On 3/18/2015 11:08 AM, 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.
>
>
>


_______________________________________________
openssl-users mailing list
To unsubscribe: https://mta.openssl.org/mailman/listinfo/openssl-users
Reply | Threaded
Open this post in threaded view
|

Re: [openssl-users] base64 decode in C

Jakob Bohm-7
In reply to this post by Prashant Bapat
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
Reply | Threaded
Open this post in threaded view
|

Re: [openssl-users] base64 decode in C

Walter H.
In reply to this post by Prashant Bapat
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;


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

smime.p7s (7K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: [openssl-users] base64 decode in C

Scott Neugroschl-2
In reply to this post by Prashant Bapat

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:[hidden email]] 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. <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

Re: [openssl-users] base64 decode in C

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 <[hidden email]> 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:[hidden email]] 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. <[hidden email]> 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
Reply | Threaded
Open this post in threaded view
|

Re: [openssl-users] base64 decode in C

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 <[hidden email]> 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 <[hidden email]> 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:[hidden email]] 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. <[hidden email]> 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