How to separate ECC Public Key from keypairs and convert it to EVP_PKEY?

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

How to separate ECC Public Key from keypairs and convert it to EVP_PKEY?

cellecial-2

Hi,

I write some code to generate an ECC certificate, it works partly.
The certificate can be parsed by IE but prompt "signature corrupt".
So I dump the cert using X509_print_fp(),here is the information:

/*=============================================*/
Certificate:
    Data:
        Version: 1 (0x0)
        Serial Number: 0 (0x0)
    Signature Algorithm: ecdsa-with-SHA1
        Issuer: C=AU, ST=SS, L=LL, O=LONG, OU=DEV, [hidden email]
        Validity
            Not Before: Jan 29 07:39:02 2013 GMT
            Not After : Jan 29 07:39:02 2014 GMT
        Subject: C=AU, ST=SS, L=LL, O=LONG, OU=DEV, [hidden email]
        Subject Public Key Info:
            Public Key Algorithm: id-ecPublicKey
                Public-Key: (256 bit)
                pub:
                    04:c6:f8:32:9a:99:ff:8f:66:f4:05:57:33:86:b1:
                    6d:18:2b:71:38:35:67:f9:37:b6:3b:e7:fc:26:dc:
                    9b:bc:40:76:64:53:ef:b4:1d:18:24:79:bc:93:ab:
                    2f:5d:50:4c:63:fb:e6:4c:d5:2e:44:8b:f2:05:e9:
                    4c:ca:4c:3d:49
                Field Type: prime-field
                Prime:
                    00:ff:ff:ff:ff:00:00:00:01:00:00:00:00:00:00:
                    00:00:00:00:00:00:ff:ff:ff:ff:ff:ff:ff:ff:ff:
                    ff:ff:ff
                A:  
                    00:ff:ff:ff:ff:00:00:00:01:00:00:00:00:00:00:
                    00:00:00:00:00:00:ff:ff:ff:ff:ff:ff:ff:ff:ff:
                    ff:ff:fc
                B:  
                    5a:c6:35:d8:aa:3a:93:e7:b3:eb:bd:55:76:98:86:
                    bc:65:1d:06:b0:cc:53:b0:f6:3b:ce:3c:3e:27:d2:
                    60:4b
                Generator (uncompressed):
                    04:6b:17:d1:f2:e1:2c:42:47:f8:bc:e6:e5:63:a4:
                    40:f2:77:03:7d:81:2d:eb:33:a0:f4:a1:39:45:d8:
                    98:c2:96:4f:e3:42:e2:fe:1a:7f:9b:8e:e7:eb:4a:
                    7c:0f:9e:16:2b:ce:33:57:6b:31:5e:ce:cb:b6:40:
                    68:37:bf:51:f5
                Order:
                    00:ff:ff:ff:ff:00:00:00:00:ff:ff:ff:ff:ff:ff:
                    ff:ff:bc:e6:fa:ad:a7:17:9e:84:f3:b9:ca:c2:fc:
                    63:25:51
                Cofactor:  1 (0x1)
                Seed:
                    c4:9d:36:08:86:e7:04:93:6a:66:78:e1:13:9d:26:
                    b7:81:9f:7e:90
    Signature Algorithm: ecdsa-with-SHA1
         30:45:02:20:1f:7a:53:12:6f:7f:79:f8:8a:f8:15:dd:f0:3a:
         b4:cd:4e:46:1d:f3:bd:89:53:33:88:ab:c6:dc:7f:d8:4b:33:
         02:21:00:ca:87:43:6b:35:8f:44:db:ee:56:2a:52:4a:86:8c:
         f4:14:b3:ae:71:49:e5:1f:94:67:a5:2a:e2:c9:27:22:90

/*=============================================*/

As you can see, there are some extra info in "Public Key Info" section.
I thought I know the reason, here is my origin code snippet:

/*=============================================*/
EVP_PKEY *pkey;
.......
ret = EC_KEY_generate_key(ecdsakey);
if (ret != 1)
 goto ERR;
 

ret = EVP_PKEY_assign_EC_KEY(pkey, ecdsakey);
if (ret != 1)
 goto ERR;

......
if (X509_PUBKEY_set(&cert->cert_info->key, pkey) != 1)
 goto ERR;

......
if (X509_sign(cert, pkey, digest) == 0)
 goto ERR;
/*=============================================*/


I think I shouldn't pass the privatekey to X509_PUBKEY_set().
So I start to separate public key from private key and convert it to EVP_KEY, but I fail.

Here is my code:

/*=============================================*/
EC_KEY *ecdsakey;
EC_GROUP *ecgroup;
unsigned int degree;
int ret = -1;
int plen = 0;
unsigned char *pp = NULL, *qq = NULL;
EVP_PKEY *pubkey = NULL;
EVP_PKEY *privkey = NULL;

ecdsakey = EC_KEY_new();
if (ecdsakey == NULL)
 return -1;

ecgroup = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1);
if (ecgroup == NULL)
 return -1;

ret = EC_KEY_set_group(ecdsakey, ecgroup);
if (ret == 0)
 return -1;
  
EC_GROUP_free(ecgroup);

ret = EC_KEY_generate_key(ecdsakey);
if (ret != 1)
 return -1; 

ret = EVP_PKEY_assign_EC_KEY(pkey, ecdsakey);
if (ret != 1)
 return -2;

/******  get (EVP_PKEY *privkey), it works fine *****/

plen = i2d_PrivateKey(pkey, NULL);
if (plen <= 0)
 return -3;

pp = (unsigned char *)malloc(plen);
qq = pp;
plen = i2d_PrivateKey(pkey, &pp);
if (plen <= 0)
 return -3;

privkey = d2i_PrivateKey(EVP_PKEY_EC,NULL,&qq,plen);
if (privkey == NULL)
 return -4;

 

/****** want to get (EVP_PKEY *pubkey),but it returns NULL ******/
plen = i2d_PublicKey(pkey, NULL);
if (plen <= 0)
 return -3;

pp = (unsigned char *)malloc(plen+1);
qq = pp;
plen = i2d_PublicKey(pkey, &pp);
if (plen <= 0)
 return -3;

pubkey = d2i_PublicKey(EVP_PKEY_EC,NULL,&qq,plen);
if (pubkey == NULL)
{
 ERR_print_errors_fp(stderr);
 return -4;
}
/*=============================================*/

I know the public key is included in the private key but I don't know how to separate it.
I read apps/req.c,it just calls X509_REQ_set_pubkey(req,pkey),which pkey is still the private key. I wonder why it works normally.
I guess I misunderstand something,so I turn to your help.

Thanks for any hints.

Reply | Threaded
Open this post in threaded view
|

Re: How to separate ECC Public Key from keypairs and convert it to EVP_PKEY?

Dr. Stephen Henson
On Wed, Jan 30, 2013, cellecial wrote:

> Hi,
>
> I write some code to generate an ECC certificate, it works partly.
> The certificate can be parsed by IE but prompt "signature corrupt".

Are you sure that version of Windows supports ECC cerrificates?

> So I dump the cert using X509_print_fp(),here is the information:
>
> /*=============================================*/
> Certificate:
>     Data:
>         Version: 1 (0x0)
>         Serial Number: 0 (0x0)
>     Signature Algorithm: ecdsa-with-SHA1
>         Issuer: C=AU, ST=SS, L=LL, O=LONG, OU=DEV,
> CN=CA/emailAddress=[hidden email]
>         Validity
>             Not Before: Jan 29 07:39:02 2013 GMT
>             Not After : Jan 29 07:39:02 2014 GMT
>         Subject: C=AU, ST=SS, L=LL, O=LONG, OU=DEV,
> CN=CA/emailAddress=[hidden email]
>         Subject Public Key Info:
>             Public Key Algorithm: id-ecPublicKey
>                 Public-Key: (256 bit)
>                 pub:
>                     04:c6:f8:32:9a:99:ff:8f:66:f4:05:57:33:86:b1:
>                     6d:18:2b:71:38:35:67:f9:37:b6:3b:e7:fc:26:dc:
>                     9b:bc:40:76:64:53:ef:b4:1d:18:24:79:bc:93:ab:
>                     2f:5d:50:4c:63:fb:e6:4c:d5:2e:44:8b:f2:05:e9:
>                     4c:ca:4c:3d:49
>                 Field Type: prime-field
>                 Prime:
>                     00:ff:ff:ff:ff:00:00:00:01:00:00:00:00:00:00:
>                     00:00:00:00:00:00:ff:ff:ff:ff:ff:ff:ff:ff:ff:
>                     ff:ff:ff
>                 A:
>                     00:ff:ff:ff:ff:00:00:00:01:00:00:00:00:00:00:
>                     00:00:00:00:00:00:ff:ff:ff:ff:ff:ff:ff:ff:ff:
>                     ff:ff:fc
>                 B:
>                     5a:c6:35:d8:aa:3a:93:e7:b3:eb:bd:55:76:98:86:
>                     bc:65:1d:06:b0:cc:53:b0:f6:3b:ce:3c:3e:27:d2:
>                     60:4b
>                 Generator (uncompressed):
>                     04:6b:17:d1:f2:e1:2c:42:47:f8:bc:e6:e5:63:a4:
>                     40:f2:77:03:7d:81:2d:eb:33:a0:f4:a1:39:45:d8:
>                     98:c2:96:4f:e3:42:e2:fe:1a:7f:9b:8e:e7:eb:4a:
>                     7c:0f:9e:16:2b:ce:33:57:6b:31:5e:ce:cb:b6:40:
>                     68:37:bf:51:f5
>                 Order:
>                     00:ff:ff:ff:ff:00:00:00:00:ff:ff:ff:ff:ff:ff:
>                     ff:ff:bc:e6:fa:ad:a7:17:9e:84:f3:b9:ca:c2:fc:
>                     63:25:51
>                 Cofactor:  1 (0x1)
>                 Seed:
>                     c4:9d:36:08:86:e7:04:93:6a:66:78:e1:13:9d:26:
>                     b7:81:9f:7e:90
>     Signature Algorithm: ecdsa-with-SHA1
>          30:45:02:20:1f:7a:53:12:6f:7f:79:f8:8a:f8:15:dd:f0:3a:
>          b4:cd:4e:46:1d:f3:bd:89:53:33:88:ab:c6:dc:7f:d8:4b:33:
>          02:21:00:ca:87:43:6b:35:8f:44:db:ee:56:2a:52:4a:86:8c:
>          f4:14:b3:ae:71:49:e5:1f:94:67:a5:2a:e2:c9:27:22:90
>
> /*=============================================*/
> As you can see, there are some extra info in "Public Key Info" section.
> I thought I know the reason, here is my origin code snippet:
>

That extra info is the default way the EC key is currently encoded by OpenSSL:
explicitly giving all the EC curve details.

If you want it to just use the curve name instead you have to call:

EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);

Steve.
--
Dr Stephen N. Henson. OpenSSL project core developer.
Commercial tech support now available see: http://www.openssl.org
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]
Reply | Threaded
Open this post in threaded view
|

Re: How to separate ECC Public Key from keypairs and convert it to EVP_PKEY?

Ryan Hurst-3
FYI ECC was added to Windows VISTA,

Ryan Hurst


Sent from my phone, please forgive the brevity.

On Jan 30, 2013, at 5:51 AM, "Dr. Stephen Henson" <[hidden email]> wrote:

On Wed, Jan 30, 2013, cellecial wrote:

Hi,

I write some code to generate an ECC certificate, it works partly.
The certificate can be parsed by IE but prompt "signature corrupt".

Are you sure that version of Windows supports ECC cerrificates?

So I dump the cert using X509_print_fp(),here is the information:

/*=============================================*/
Certificate:
   Data:
       Version: 1 (0x0)
       Serial Number: 0 (0x0)
   Signature Algorithm: ecdsa-with-SHA1
       Issuer: C=AU, ST=SS, L=LL, O=LONG, OU=DEV,
[hidden email]
       Validity
           Not Before: Jan 29 07:39:02 2013 GMT
           Not After : Jan 29 07:39:02 2014 GMT
       Subject: C=AU, ST=SS, L=LL, O=LONG, OU=DEV,
[hidden email]
       Subject Public Key Info:
           Public Key Algorithm: id-ecPublicKey
               Public-Key: (256 bit)
               pub:
                   04:c6:f8:32:9a:99:ff:8f:66:f4:05:57:33:86:b1:
                   6d:18:2b:71:38:35:67:f9:37:b6:3b:e7:fc:26:dc:
                   9b:bc:40:76:64:53:ef:b4:1d:18:24:79:bc:93:ab:
                   2f:5d:50:4c:63:fb:e6:4c:d5:2e:44:8b:f2:05:e9:
                   4c:ca:4c:3d:49
               Field Type: prime-field
               Prime:
                   00:ff:ff:ff:ff:00:00:00:01:00:00:00:00:00:00:
                   00:00:00:00:00:00:ff:ff:ff:ff:ff:ff:ff:ff:ff:
                   ff:ff:ff
               A:
                   00:ff:ff:ff:ff:00:00:00:01:00:00:00:00:00:00:
                   00:00:00:00:00:00:ff:ff:ff:ff:ff:ff:ff:ff:ff:
                   ff:ff:fc
               B:
                   5a:c6:35:d8:aa:3a:93:e7:b3:eb:bd:55:76:98:86:
                   bc:65:1d:06:b0:cc:53:b0:f6:3b:ce:3c:3e:27:d2:
                   60:4b
               Generator (uncompressed):
                   04:6b:17:d1:f2:e1:2c:42:47:f8:bc:e6:e5:63:a4:
                   40:f2:77:03:7d:81:2d:eb:33:a0:f4:a1:39:45:d8:
                   98:c2:96:4f:e3:42:e2:fe:1a:7f:9b:8e:e7:eb:4a:
                   7c:0f:9e:16:2b:ce:33:57:6b:31:5e:ce:cb:b6:40:
                   68:37:bf:51:f5
               Order:
                   00:ff:ff:ff:ff:00:00:00:00:ff:ff:ff:ff:ff:ff:
                   ff:ff:bc:e6:fa:ad:a7:17:9e:84:f3:b9:ca:c2:fc:
                   63:25:51
               Cofactor:  1 (0x1)
               Seed:
                   c4:9d:36:08:86:e7:04:93:6a:66:78:e1:13:9d:26:
                   b7:81:9f:7e:90
   Signature Algorithm: ecdsa-with-SHA1
        30:45:02:20:1f:7a:53:12:6f:7f:79:f8:8a:f8:15:dd:f0:3a:
        b4:cd:4e:46:1d:f3:bd:89:53:33:88:ab:c6:dc:7f:d8:4b:33:
        02:21:00:ca:87:43:6b:35:8f:44:db:ee:56:2a:52:4a:86:8c:
        f4:14:b3:ae:71:49:e5:1f:94:67:a5:2a:e2:c9:27:22:90

/*=============================================*/
As you can see, there are some extra info in "Public Key Info" section.
I thought I know the reason, here is my origin code snippet:


That extra info is the default way the EC key is currently encoded by OpenSSL:
explicitly giving all the EC curve details.

If you want it to just use the curve name instead you have to call:

EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);

Steve.
--
Dr Stephen N. Henson. OpenSSL project core developer.
Commercial tech support now available see: http://www.openssl.org
______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    [hidden email]
Automated List Manager                           [hidden email]

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

Re: How to separate ECC Public Key from keypairs and convert it to EVP_PKEY?

cellecial-2
In reply to this post by Dr. Stephen Henson
On Wed, Jan 30, 2013 at 9:51 PM, Dr. Stephen Henson <[hidden email]> wrote:

If you want it to just use the curve name instead you have to call:

EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);

Steve.
--


It's efficacious.
After I  call  EC_KEY_set_asn1_flag() before EC_KEY_generate_key(), the self-signed certificate(ca.der) is generated successfully without prompting "signature corrupt".
Thank you, Dr. Henson and Ryan.
 
But I still have another question though it is not critical.
I use Windows 8 and IE10 now.
When I double click ca.der and install, if I choose 'Automatically select the certificate store based on the type of certificate',then the self-signed certificate will be in the 'Intermediate Certification Authorities',not 'Trusted Root Certification Authorities'.
If I choose 'Place all certificates in the following store' and select 'Trusted Root Certification Authorities', I can find the certificate in 'Trusted Root Certification Authorities' as I wish.
The automatic import activity on Win8 is a little different from it on WinXP.
Maybe it's a new OS security policy, I don't know.
 
Thank you again.