BER to DER conversion

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

BER to DER conversion

Lincoln-9
Hello all,

   Does OpenSSL have a routine to convert BER encoded
blob into DER encoded blob?

  I need this because I am integrating OpenSSL with a
HSM which exports RSA public key to BER blob, but
OpenSSL internal routines expect DER encoded blob.

   If you can point me to the right location, I'd
highly appreciate it!

Thanks,
Dr. Wu


               
____________________________________________________
Sell on Yahoo! Auctions – no fees. Bid on great items.  
http://auctions.yahoo.com/
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: BER to DER conversion

Dr. Stephen Henson
On Mon, Jul 11, 2005, Lincoln wrote:

> Hello all,
>
>    Does OpenSSL have a routine to convert BER encoded
> blob into DER encoded blob?
>
>   I need this because I am integrating OpenSSL with a
> HSM which exports RSA public key to BER blob, but
> OpenSSL internal routines expect DER encoded blob.
>

Err no it doesn't. OpenSSL accepts BER just fine.

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
Development Mailing List                       [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: BER to DER conversion

Lincoln-9
Steve,

 Thanks for your point. However, I think you
misinterpreted my problem.

I know BER is a superset of DER, so we can use DER in
places where BER is expected.

However, my case is reversed. I am trying to use BER
to a place where DER is expected.

 Specifically,

 The following section of code within
X509_PUBKEY_set() extracts public key in DER format by
calling i2d_PublicKey(pkey,&p), then pass the buffer s
to
M_ASN1_BIT_STRING_set(pk->public_key,s,i)

--------------------------------------------------
if ((i=i2d_PublicKey(pkey,NULL)) <= 0) goto err;
if ((s=(unsigned char *)OPENSSL_malloc(i+1)) == NULL)
        {
        X509err(X509_F_X509_PUBKEY_SET,ERR_R_MALLOC_FAILURE);
        goto err;
        }
p=s;
i2d_PublicKey(pkey,&p);
if (!M_ASN1_BIT_STRING_set(pk->public_key,s,i)) goto
err;
-------------------------------------------------------

Now, I replaced the above code segment with

if (custom_exportRSAPublicKey(publickeyhandle, &s, &i)
== -1) goto err;

if (!M_ASN1_BIT_STRING_set(pk->public_key,s,i)) goto
err;

  Where custom_exportRSAPublicKey exports the public
key designated by publickeyhandle to HSM
into BER blob, then I pass the BER blob to
M_ASN1_BIT_STRING_set(pk->public_key,s,i).

 It appears that BER is not interpreted correctly
where DER is expected.
Because the Modulus and Exponent are switched:

Does using the latest openssl solve this problem?
Or do you know how to work around this problem?

Thanks for your help, Dr. Steve!

resulting CSR:
Certificate Request:
    Data:
        Version: 0 (0x0)
        Subject: C=US, ST=CA, L=Milpitas, O=Array
Netwroks, OU=Dev,
CN=Hailin_host/emailAddress=[hidden email]
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
            RSA Public Key: (17 bit)
                Modulus (17 bit): 65537 (0x10001)
                Exponent:
                   
00:b5:a6:53:26:17:b7:98:1a:22:fa:b3:a0:fa:8a:
                   
64:77:9b:56:e8:45:c6:8f:77:37:e5:80:c7:b1:7c:
                   
a2:74:c1:f2:52:b7:3e:07:4a:49:8a:95:2b:bb:4c:
                   
54:3b:40:14:f3:ec:9d:23:f0:c1:df:e8:b3:e4:9e:
                   
cd:bf:da:00:70:f8:5e:75:db:05:dc:ab:62:e5:b0:
                   
15:26:9e:e7:8f:a8:2d:4f:fc:c7:8b:91:a6:1f:fe:
                   
cc:71:6a:1a:d2:70:fd:16:26:8f:10:f5:8a:8e:12:
                   
bf:3e:16:99:dd:94:3d:fd:27:03:78:87:75:2d:dc:
                    cd:92:9a:14:9a:f7:56:c7:f9
        Attributes:
            a0:00
    Signature Algorithm: md5WithRSAEncryption
       
b5:a6:53:26:17:b7:98:1a:22:fa:b3:a0:fa:8a:64:77:9b:56:
       
e8:45:c6:8f:77:37:e5:80:c7:b1:7c:a2:74:c1:f2:52:b7:3e:
       
07:4a:49:8a:95:2b:bb:4c:54:3b:40:14:f3:ec:9d:23:f0:c1:
       
df:e8:b3:e4:9e:cd:bf:da:00:70:f8:5e:75:db:05:dc:ab:62:
       
e5:b0:15:26:9e:e7:8f:a8:2d:4f:fc:c7:8b:91:a6:1f:fe:cc:
       
71:6a:1a:d2:70:fd:16:26:8f:10:f5:8a:8e:12:bf:3e:16:99:
       
dd:94:3d:fd:27:03:78:87:75:2d:dc:cd:92:9a:14:9a:f7:56:
        c7:f9


===========================================================
openssl-0.9.7d/crypto/asn1/x_pubkey.c

int X509_PUBKEY_set(X509_PUBKEY **x, EVP_PKEY *pkey)
        {
        int ok=0;
        X509_PUBKEY *pk;
        X509_ALGOR *a;
        ASN1_OBJECT *o;
        unsigned char *s,*p = NULL;
        int i;

        if (x == NULL) return(0);

        if ((pk=X509_PUBKEY_new()) == NULL) goto err;
        a=pk->algor;

        /* set the algorithm id */
        if ((o=OBJ_nid2obj(pkey->type)) == NULL) goto err;
        ASN1_OBJECT_free(a->algorithm);
        a->algorithm=o;

        /* Set the parameter list */
        if (!pkey->save_parameters || (pkey->type ==
EVP_PKEY_RSA))
                {
                if ((a->parameter == NULL) ||
                        (a->parameter->type != V_ASN1_NULL))
                        {
                        ASN1_TYPE_free(a->parameter);
                        a->parameter=ASN1_TYPE_new();
                        a->parameter->type=V_ASN1_NULL;
                        }
                }
        else
#ifndef OPENSSL_NO_DSA
                if (pkey->type == EVP_PKEY_DSA)
                {
                unsigned char *pp;
                DSA *dsa;

                dsa=pkey->pkey.dsa;
                dsa->write_params=0;
                ASN1_TYPE_free(a->parameter);
                i=i2d_DSAparams(dsa,NULL);
                if ((p=(unsigned char *)OPENSSL_malloc(i)) == NULL)
goto err;
                pp=p;
                i2d_DSAparams(dsa,&pp);
                a->parameter=ASN1_TYPE_new();
                a->parameter->type=V_ASN1_SEQUENCE;
                a->parameter->value.sequence=ASN1_STRING_new();
                ASN1_STRING_set(a->parameter->value.sequence,p,i);
                OPENSSL_free(p);
                }
        else
#endif
                {
       
X509err(X509_F_X509_PUBKEY_SET,X509_R_UNSUPPORTED_ALGORITHM);
                goto err;
                }

        if ((i=i2d_PublicKey(pkey,NULL)) <= 0) goto err;
        if ((s=(unsigned char *)OPENSSL_malloc(i+1)) == NULL)
                {
       
X509err(X509_F_X509_PUBKEY_SET,ERR_R_MALLOC_FAILURE);
                goto err;
                }
        p=s;
        i2d_PublicKey(pkey,&p);
        if (!M_ASN1_BIT_STRING_set(pk->public_key,s,i)) goto
err;
        /* Set number of unused bits to zero */
        pk->public_key->flags&=
~(ASN1_STRING_FLAG_BITS_LEFT|0x07);
        pk->public_key->flags|=ASN1_STRING_FLAG_BITS_LEFT;

        OPENSSL_free(s);

#if 0
        CRYPTO_add(&pkey->references,1,CRYPTO_LOCK_EVP_PKEY);
        pk->pkey=pkey;
#endif

        if (*x != NULL)
                X509_PUBKEY_free(*x);

        *x=pk;
        pk=NULL;

        ok=1;
err:
        if (pk != NULL) X509_PUBKEY_free(pk);
        return(ok);
        }


--- "Dr. Stephen Henson" <[hidden email]> wrote:

> On Mon, Jul 11, 2005, Lincoln wrote:
>
> > Hello all,
> >
> >    Does OpenSSL have a routine to convert BER
> encoded
> > blob into DER encoded blob?
> >
> >   I need this because I am integrating OpenSSL
> with a
> > HSM which exports RSA public key to BER blob, but
> > OpenSSL internal routines expect DER encoded blob.
>
> >
>
> Err no it doesn't. OpenSSL accepts BER just fine.
>
> 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
> Development Mailing List                      
> [hidden email]
> Automated List Manager                          
> [hidden email]
>



               
____________________________________________________
Sell on Yahoo! Auctions – no fees. Bid on great items.  
http://auctions.yahoo.com/
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

RE: BER to DER conversion

JoelKatz

>  Thanks for your point. However, I think you
> misinterpreted my problem.

        Actually, you have misinterpret your own problem. ;)

> I know BER is a superset of DER, so we can use DER in
> places where BER is expected.

        Exactly.

> However, my case is reversed. I am trying to use BER
> to a place where DER is expected.

        This is fine, so long as you simply convert the BER to DER before using it.
You can do this as easily as reading the thing in to any function that
expects DER or BER and writing it back out as DER.

>  Specifically,
>
>  The following section of code within
> X509_PUBKEY_set() extracts public key in DER format by
> calling i2d_PublicKey(pkey,&p), then pass the buffer s
> to
> M_ASN1_BIT_STRING_set(pk->public_key,s,i)
>
> --------------------------------------------------
> if ((i=i2d_PublicKey(pkey,NULL)) <= 0) goto err;
> if ((s=(unsigned char *)OPENSSL_malloc(i+1)) == NULL)
> {
> X509err(X509_F_X509_PUBKEY_SET,ERR_R_MALLOC_FAILURE);
> goto err;
> }
> p=s;
> i2d_PublicKey(pkey,&p);
> if (!M_ASN1_BIT_STRING_set(pk->public_key,s,i)) goto
> err;
> -------------------------------------------------------
>
> Now, I replaced the above code segment with
>
> if (custom_exportRSAPublicKey(publickeyhandle, &s, &i)
> == -1) goto err;
>
> if (!M_ASN1_BIT_STRING_set(pk->public_key,s,i)) goto
> err;
>
>   Where custom_exportRSAPublicKey exports the public
> key designated by publickeyhandle to HSM
> into BER blob, then I pass the BER blob to
> M_ASN1_BIT_STRING_set(pk->public_key,s,i).
>
>  It appears that BER is not interpreted correctly
> where DER is expected.
> Because the Modulus and Exponent are switched:

        This is not a DER/BER issue. Where you put the modules and exponent has
nothing to do with whether you're using DER or BER. DER and BER are binary
standards. They say, for example, how to encode a large integer, but not
which large integer to put where.

> Does using the latest openssl solve this problem?
> Or do you know how to work around this problem?

        The problem has nothing to do with DER or BER but with what format you use
to encode the private key itself. This is a format that layers on top of DER
or BER. If you are using your own format, you will need to write your own
code to parse it.

        DS


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

RE: BER to DER conversion

Lincoln-9
  Thanks, David.

  I am stuck exact at how to convert BER to DER.
If I can convert BER to DER, then my problem is
solved.

Dr. Steve said there is no such conversion routine in
OpenSSL...

--- David Schwartz <[hidden email]> wrote:

>
> >  Thanks for your point. However, I think you
> > misinterpreted my problem.
>
> Actually, you have misinterpret your own problem.
> ;)
>
> > I know BER is a superset of DER, so we can use DER
> in
> > places where BER is expected.
>
> Exactly.
>
> > However, my case is reversed. I am trying to use
> BER
> > to a place where DER is expected.
>
> This is fine, so long as you simply convert the BER
> to DER before using it.
> You can do this as easily as reading the thing in to
> any function that
> expects DER or BER and writing it back out as DER.
>
> >  Specifically,
> >
> >  The following section of code within
> > X509_PUBKEY_set() extracts public key in DER
> format by
> > calling i2d_PublicKey(pkey,&p), then pass the
> buffer s
> > to
> > M_ASN1_BIT_STRING_set(pk->public_key,s,i)
> >
> > --------------------------------------------------
> > if ((i=i2d_PublicKey(pkey,NULL)) <= 0) goto err;
> > if ((s=(unsigned char *)OPENSSL_malloc(i+1)) ==
> NULL)
> > {
> >
>
X509err(X509_F_X509_PUBKEY_SET,ERR_R_MALLOC_FAILURE);
> > goto err;
> > }
> > p=s;
> > i2d_PublicKey(pkey,&p);
> > if (!M_ASN1_BIT_STRING_set(pk->public_key,s,i))
> goto
> > err;
> >
>
-------------------------------------------------------

> >
> > Now, I replaced the above code segment with
> >
> > if (custom_exportRSAPublicKey(publickeyhandle, &s,
> &i)
> > == -1) goto err;
> >
> > if (!M_ASN1_BIT_STRING_set(pk->public_key,s,i))
> goto
> > err;
> >
> >   Where custom_exportRSAPublicKey exports the
> public
> > key designated by publickeyhandle to HSM
> > into BER blob, then I pass the BER blob to
> > M_ASN1_BIT_STRING_set(pk->public_key,s,i).
> >
> >  It appears that BER is not interpreted correctly
> > where DER is expected.
> > Because the Modulus and Exponent are switched:
>
> This is not a DER/BER issue. Where you put the
> modules and exponent has
> nothing to do with whether you're using DER or BER.
> DER and BER are binary
> standards. They say, for example, how to encode a
> large integer, but not
> which large integer to put where.
>
> > Does using the latest openssl solve this problem?
> > Or do you know how to work around this problem?
>
> The problem has nothing to do with DER or BER but
> with what format you use
> to encode the private key itself. This is a format
> that layers on top of DER
> or BER. If you are using your own format, you will
> need to write your own
> code to parse it.
>
> DS
>
>
>
______________________________________________________________________
> OpenSSL Project                                
> http://www.openssl.org
> Development Mailing List                      
> [hidden email]
> Automated List Manager                          
> [hidden email]
>


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

RE: BER to DER conversion

JoelKatz

>   Thanks, David.
>
>   I am stuck exact at how to convert BER to DER.
> If I can convert BER to DER, then my problem is
> solved.
>
> Dr. Steve said there is no such conversion routine in
> OpenSSL...

        That is not your program. If that were the problem, the solution would
simply be to read your data in and then write it back out. Your problem is
that you have two incompatible layouts for a key, both layered over BER/DER.

        BER and DER are binary formats for laying out things like integers and
strings. They don't specify which part of a private key should come first.

        Let me explain it another way, saying you have a private key in BER/DER is
like saying you have a private key in ASCII. A program that supports private
keys in ASCII won't necessarily support any given private key just because
it is also ASCII. It has to also be a format on top of ASCII that the
program supports.

        So you have a key in BER/DER. Great. But is it in a format OpenSSL
supports? The answer is no. And that's not a BER/DER problem, that's a
problem one layer higher.

        Someone who can read books can't necessarily read books in Latin. And the
problem is not the color of the paper, so copying the books onto white paper
won't make them any more readable.

        When we talk about a private key in DER format, we don't just mean anything
that encodes a private key in DER some way. We mean that there is some
specific standard layered on top of DER that they key is in, and there are
multiple such formats, which is why OpenSSL has many ways to read in a
private key even though it's in DER.

        So forget all about the BER/DER thing. You have a key in a format OpenSSL
doesn't support, so you need to find out what format it is in and perhaps
write your own code to read it in. You can make use of OpenSSLs BER/DER
decode logic just as the existing functions to read in keys in formats
layered on top of BER/DER do.

        Again, OpenSSL can read in BER or DER, it doesn't care. It can only write
out in DER. The only difference between BER and DER is that BER has more
than one way to encode some things and DER only allows one.

        DS


______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
Development Mailing List                       [hidden email]
Automated List Manager                           [hidden email]