Importing EC Private Key

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

Importing EC Private Key

Smith, Bill (Tridium)
I have an EC private key that I'm trying to import with the code:

          InputStreamReader isr = new InputStreamReader(importFile.getInputStream());
          PEMParser pr = new PEMParser(isr);
          try
          {
            Object obj = pr.readObject();
            ...

The EC private key (P-256) is in a file that looks something like (not a real key):

-----BEGIN EC PRIVATE KEY-----
NTlaFw0xOTAxMTUxOTU3NTlaMDIxCzAJBgNVBAgMAnVzMRAwDgYDVQQKDAd0cmlk
jmGT9DLeFemvlmR1zTA0FdcSAG2gCgYIKoZIzj0DAQehRANCAATNXYa06ykwhxuy
BPO0aVOH/PR0+el1baMWsOcwCgYIKoZIzj0EAwIDSAAwRQIhAN5W7FNF3hzDoQtT
OTU3NTla
-----END EC PRIVATE KEY-----

When it gets to the pr.readObject() line, I get

org.bouncycastle.openssl.PEMException: problem creating EC private key: java.lang.NullPointerException
   at org.bouncycastle.openssl.PEMParser$KeyPairParser.parseObject(Unknown Source)
   at org.bouncycastle.openssl.PEMParser.readObject(Unknown Source)

Is there a way to use pemparser to read an EC key?

Regards,
Bill

Reply | Threaded
Open this post in threaded view
|

Re: Importing EC Private Key

David Hook-3

It'll read EC keys, there's tests for it. I'd have to see the EC key to
work out what the issue is. At a guess it may have a curve OID
in it that isn't being recognized properly - the size of the "similar
encoding" would suggest parameters are not included.

Regards,

David

On 16/01/18 07:45, Smith, Bill (Tridium) wrote:

> I have an EC private key that I'm trying to import with the code:
>
>           InputStreamReader isr = new InputStreamReader(importFile.getInputStream());
>           PEMParser pr = new PEMParser(isr);
>           try
>           {
>             Object obj = pr.readObject();
>             ...
>
> The EC private key (P-256) is in a file that looks something like (not a real key):
>
> -----BEGIN EC PRIVATE KEY-----
> NTlaFw0xOTAxMTUxOTU3NTlaMDIxCzAJBgNVBAgMAnVzMRAwDgYDVQQKDAd0cmlk
> jmGT9DLeFemvlmR1zTA0FdcSAG2gCgYIKoZIzj0DAQehRANCAATNXYa06ykwhxuy
> BPO0aVOH/PR0+el1baMWsOcwCgYIKoZIzj0EAwIDSAAwRQIhAN5W7FNF3hzDoQtT
> OTU3NTla
> -----END EC PRIVATE KEY-----
>
> When it gets to the pr.readObject() line, I get
>
> org.bouncycastle.openssl.PEMException: problem creating EC private key: java.lang.NullPointerException
>    at org.bouncycastle.openssl.PEMParser$KeyPairParser.parseObject(Unknown Source)
>    at org.bouncycastle.openssl.PEMParser.readObject(Unknown Source)
>
> Is there a way to use pemparser to read an EC key?
>
> Regards,
> Bill
>
>


Reply | Threaded
Open this post in threaded view
|

RE: Importing EC Private Key

Smith, Bill (Tridium)
Here is the actual private key. (It's a test key so...)

-----BEGIN EC PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgvYiiubZYNO1WXXi3
jmGT9DLeFemvlmR1zTA0FdcSAG2gCgYIKoZIzj0DAQehRANCAATNXYa06ykwhxuy
Dg+q6zsVqOLk9LtXz/1fzf9AkAVm9lBMTZAh+FRfregBgl08LATztGlTh/z0dPnp
dW2jFrDn
-----END EC PRIVATE KEY-----


-----Original Message-----
From: David Hook [mailto:[hidden email]]
Sent: Monday, January 15, 2018 7:17 PM
To: Smith, Bill (Tridium) <[hidden email]>; [hidden email]
Subject: Re: [dev-crypto] Importing EC Private Key


It'll read EC keys, there's tests for it. I'd have to see the EC key to work out what the issue is. At a guess it may have a curve OID in it that isn't being recognized properly - the size of the "similar encoding" would suggest parameters are not included.

Regards,

David

On 16/01/18 07:45, Smith, Bill (Tridium) wrote:

> I have an EC private key that I'm trying to import with the code:
>
>           InputStreamReader isr = new InputStreamReader(importFile.getInputStream());
>           PEMParser pr = new PEMParser(isr);
>           try
>           {
>             Object obj = pr.readObject();
>             ...
>
> The EC private key (P-256) is in a file that looks something like (not a real key):
>
> -----BEGIN EC PRIVATE KEY-----
> NTlaFw0xOTAxMTUxOTU3NTlaMDIxCzAJBgNVBAgMAnVzMRAwDgYDVQQKDAd0cmlk
> jmGT9DLeFemvlmR1zTA0FdcSAG2gCgYIKoZIzj0DAQehRANCAATNXYa06ykwhxuy
> BPO0aVOH/PR0+el1baMWsOcwCgYIKoZIzj0EAwIDSAAwRQIhAN5W7FNF3hzDoQtT
> OTU3NTla
> -----END EC PRIVATE KEY-----
>
> When it gets to the pr.readObject() line, I get
>
> org.bouncycastle.openssl.PEMException: problem creating EC private key: java.lang.NullPointerException
>    at org.bouncycastle.openssl.PEMParser$KeyPairParser.parseObject(Unknown Source)
>    at org.bouncycastle.openssl.PEMParser.readObject(Unknown Source)
>
> Is there a way to use pemparser to read an EC key?
>
> Regards,
> Bill
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Importing EC Private Key

David Hook-3

Ah, okay... it's missing the public key field. Normally the encoding
contains both the private key and its
associated public key. The public key field is optional though (at least
from an ASN.1 point of view).

Unusual, but I guess not entirely unexpected.

Regards,

David

On 16/01/18 11:22, Smith, Bill (Tridium) wrote:

> Here is the actual private key. (It's a test key so...)
>
> -----BEGIN EC PRIVATE KEY-----
> MIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgvYiiubZYNO1WXXi3
> jmGT9DLeFemvlmR1zTA0FdcSAG2gCgYIKoZIzj0DAQehRANCAATNXYa06ykwhxuy
> Dg+q6zsVqOLk9LtXz/1fzf9AkAVm9lBMTZAh+FRfregBgl08LATztGlTh/z0dPnp
> dW2jFrDn
> -----END EC PRIVATE KEY-----
>
>
> -----Original Message-----
> From: David Hook [mailto:[hidden email]]
> Sent: Monday, January 15, 2018 7:17 PM
> To: Smith, Bill (Tridium) <[hidden email]>; [hidden email]
> Subject: Re: [dev-crypto] Importing EC Private Key
>
>
> It'll read EC keys, there's tests for it. I'd have to see the EC key to work out what the issue is. At a guess it may have a curve OID in it that isn't being recognized properly - the size of the "similar encoding" would suggest parameters are not included.
>
> Regards,
>
> David
>
> On 16/01/18 07:45, Smith, Bill (Tridium) wrote:
>> I have an EC private key that I'm trying to import with the code:
>>
>>           InputStreamReader isr = new InputStreamReader(importFile.getInputStream());
>>           PEMParser pr = new PEMParser(isr);
>>           try
>>           {
>>             Object obj = pr.readObject();
>>             ...
>>
>> The EC private key (P-256) is in a file that looks something like (not a real key):
>>
>> -----BEGIN EC PRIVATE KEY-----
>> NTlaFw0xOTAxMTUxOTU3NTlaMDIxCzAJBgNVBAgMAnVzMRAwDgYDVQQKDAd0cmlk
>> jmGT9DLeFemvlmR1zTA0FdcSAG2gCgYIKoZIzj0DAQehRANCAATNXYa06ykwhxuy
>> BPO0aVOH/PR0+el1baMWsOcwCgYIKoZIzj0EAwIDSAAwRQIhAN5W7FNF3hzDoQtT
>> OTU3NTla
>> -----END EC PRIVATE KEY-----
>>
>> When it gets to the pr.readObject() line, I get
>>
>> org.bouncycastle.openssl.PEMException: problem creating EC private key: java.lang.NullPointerException
>>    at org.bouncycastle.openssl.PEMParser$KeyPairParser.parseObject(Unknown Source)
>>    at org.bouncycastle.openssl.PEMParser.readObject(Unknown Source)
>>
>> Is there a way to use pemparser to read an EC key?
>>
>> Regards,
>> Bill
>>
>>