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_FAILUR E);
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=hwu@arraynetworks.net
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_FAILUR E);
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" 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
> openssl-dev@openssl.org
> Automated List Manager
> majordomo@openssl.org
>





__________________________________________________ __
Sell on Yahoo! Auctions no fees. Bid on great items.
http://auctions.yahoo.com/
__________________________________________________ ____________________
OpenSSL Project http://www.openssl.org
Development Mailing List openssl-dev@openssl.org
Automated List Manager majordomo@openssl.org