Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
KeyChain.getPrivateKey(Context ,String) on Android 4.1?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Meidey  
View profile  
 More options Sep 20 2012, 3:24 am
From: Meidey <toms.med...@gmail.com>
Date: Thu, 20 Sep 2012 00:24:18 -0700 (PDT)
Local: Thurs, Sep 20 2012 3:24 am
Subject: KeyChain.getPrivateKey(Context,String) on Android 4.1?

I read that there is problem method KeyChain.getPrivateKey(Context,String)
on Android 4.1 although on older versions it works. Does anybody has
solution how to get private key on JellyBean?

Code that does not work on 4.1, but works great on older versions.

PrivateKey pk;

try {
    pk = KeyChain.getPrivateKey(context,string);
    byte[] pkByte = pk.getEncoded();
    pkBase64 = new String(Base64.encode(pkByte, Base64.NO_WRAP));


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nikolay Elenkov  
View profile   Translate to Translated (View Original)
 More options Sep 21 2012, 12:45 pm
From: Nikolay Elenkov <nikolay.elen...@gmail.com>
Date: Sat, 22 Sep 2012 01:43:21 +0900
Local: Fri, Sep 21 2012 12:43 pm
Subject: Re: [android-developers] KeyChain.getPrivateKey(Context,String) on Android 4.1?

On Thu, Sep 20, 2012 at 4:24 PM, Meidey <toms.med...@gmail.com> wrote:
> I read that there is problem method KeyChain.getPrivateKey(Context,String)
> on Android 4.1 although on older versions it works. Does anybody has
> solution how to get private key on JellyBean?

Not sure what you read, but there were (fixed in AOSP master) some bugs
affecting keys with particular characters in the key name.

http://code.google.com/p/android/issues/detail?id=34577

> Code that does not work on 4.1, but works great on older versions.

How exactly does it 'not work'? On what device and version?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Yonan  
View profile  
 More options Oct 5 2012, 1:40 am
From: James Yonan <caprifin...@gmail.com>
Date: Thu, 4 Oct 2012 22:40:33 -0700 (PDT)
Local: Fri, Oct 5 2012 1:40 am
Subject: Re: [android-developers] KeyChain.getPrivateKey(Context,String) on Android 4.1?

There is a problem in 4.1 with apps that need to create a signature using a
private key in the keychain.  In particular, apps that establish SSL client
sessions (such as OpenVPN) would use code such as this to allow an SSL
negotiation to use a client cert/key from the keychain:

import java.security.PrivateKey;
import javax.crypto.Cipher;

. . .

PrivateKey privateKey = KeyChain.getPrivateKey(context, alias);
if (privateKey) {
  byte[] data;
  Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1PADDING");
  cipher.init(Cipher.ENCRYPT_MODE, privateKey);
  byte[] signed_bytes = cipher.doFinal(data);

}

This code now fails badly on 4.1 (testing on Nexus 7) even
though KeyChain.getPrivateKey returns a non-null value for privateKey.  It
not only fails to work, but causes a segfault later when privateKey is
garbage collected.

I understand that on 4.1, private keys might be offloaded to hardware, but
still the code above should work because it's not accessing the key
directly -- it is only performing an encrypt operation using the key.

What is the proper way to do this on 4.1?


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nikolay Elenkov  
View profile  
 More options Oct 5 2012, 9:01 am
From: Nikolay Elenkov <nikolay.elen...@gmail.com>
Date: Fri, 5 Oct 2012 21:59:20 +0900
Local: Fri, Oct 5 2012 8:59 am
Subject: Re: [android-developers] KeyChain.getPrivateKey(Context,String) on Android 4.1?

Not quite. You are feeding it to the Bouncy Castle provider, which knows
nothing about native keys and thus cannot use your key. I agree that it
should be better documented, but the new OpenSSL engine supports
only signing, verifying and key import. It should work if you use the
Signature class. The segfault sounds bad though, can you reproduce
it consistently? You might want to post this on android-security as well,
a lot of the Google people responsible for this seem to monitor it.

> What is the proper way to do this on 4.1?

Does using the Signature class not work for you?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Yonan  
View profile  
 More options Oct 5 2012, 4:40 pm
From: James Yonan <caprifin...@gmail.com>
Date: Fri, 5 Oct 2012 13:40:03 -0700 (PDT)
Local: Fri, Oct 5 2012 4:40 pm
Subject: Re: [android-developers] KeyChain.getPrivateKey(Context,String) on Android 4.1?

> > I understand that on 4.1, private keys might be offloaded to hardware,
> but
> > still the code above should work because it's not accessing the key
> directly
> > -- it is only performing an encrypt operation using the key.

> Not quite. You are feeding it to the Bouncy Castle provider, which knows
> nothing about native keys and thus cannot use your key. I agree that it
> should be better documented, but the new OpenSSL engine supports
> only signing, verifying and key import. It should work if you use the
> Signature class. The segfault sounds bad though, can you reproduce
> it consistently? You might want to post this on android-security as well,
> a lot of the Google people responsible for this seem to monitor it.

Well, there's essentially two problems here...

1. KeyChain.getPrivateKey(this, alias) returns an object that segfaults
when collected by the GC.  The segfault occurs in RSA_free in libcrypto.
 This is 100% reproducible for me on Nexus 7.  This behavior has been
documented in other posts, e.g.
http://code.google.com/p/android/issues/detail?id=36545

2. You say that the new OpenSSL engine supports only signing, verifying and
key import.  That's fine, but keep in mind that the RSA signature used for
verification of an SSL session is typically generated by the cipher object
javax.crypto.Cipher.getInstance("RSA/ECB/PKCS1PADDING") acting as an
encryptor.  Can java.security.Signature replicate this behavior?  This
issue is also documented here:
http://stackoverflow.com/questions/11261774/using-android-4-1-keychain


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nikolay Elenkov  
View profile  
 More options Oct 5 2012, 10:26 pm
From: Nikolay Elenkov <nikolay.elen...@gmail.com>
Date: Sat, 6 Oct 2012 11:23:08 +0900
Local: Fri, Oct 5 2012 10:23 pm
Subject: Re: [android-developers] KeyChain.getPrivateKey(Context,String) on Android 4.1?

On Sat, Oct 6, 2012 at 5:40 AM, James Yonan <caprifin...@gmail.com> wrote:

> Well, there's essentially two problems here...

> 1. KeyChain.getPrivateKey(this, alias) returns an object that segfaults when
> collected by the GC.  The segfault occurs in RSA_free in libcrypto.  This is
> 100% reproducible for me on Nexus 7.  This behavior has been documented in
> other posts, e.g. http://code.google.com/p/android/issues/detail?id=36545

If it is indeed reproducible, it should be easy to fix. Have you tried building
latest AOSP source, there have been a number of fixes/changes in this
area? I fail to see how this is documented in the bug report though (no
steps to reproduce).

> 2. You say that the new OpenSSL engine supports only signing, verifying and
> key import.  That's fine, but keep in mind that the RSA signature used for
> verification of an SSL session is typically generated by the cipher object
> javax.crypto.Cipher.getInstance("RSA/ECB/PKCS1PADDING") acting as an
> encryptor.  Can java.security.Signature replicate this behavior?  This issue
> is also documented here:
> http://stackoverflow.com/questions/11261774/using-android-4-1-keychain

Again, are there any reasons you are using the Cipher class for signing?
I don't think this is typical at all, but if you have a particular reason
for wanting to use 'raw' RSA encryption, please say why. And, again,
this is probably more suitable for andorid-security.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Yonan  
View profile  
 More options Oct 6 2012, 1:56 pm
From: James Yonan <caprifin...@gmail.com>
Date: Sat, 6 Oct 2012 10:56:45 -0700 (PDT)
Local: Sat, Oct 6 2012 1:56 pm
Subject: Re: [android-developers] KeyChain.getPrivateKey(Context,String) on Android 4.1?

> If it is indeed reproducible, it should be easy to fix. Have you tried
> building
> latest AOSP source, there have been a number of fixes/changes in this
> area? I fail to see how this is documented in the bug report though (no
> steps to reproduce).

Steps for me on Nexus 7 were:

1. import PKCS#12 file containing root cert, client cert, and private key

2. have app obtain an alias to the client cert

3. get the private key: PrivateKey pk = KeyChain.getPrivateKey(this, alias);

4. observe segfault when pk is collected by GC

Raw RSA encryption is necessary as part of the challenge/response handshake
of an SSL/TLS negotiation, if your side of the connection is using a cert
as an authentication factor.  It's essential for VPN implementations that
layer on top of SSL/TLS transport and need to interoperate with an external
key store.  I enumerated the signature algs provided by the AndroidOpenSSL
1.0 provider and I don't see any implementation for raw RSA.  Some Java
implementations include it as "NONEwithRSA", but I don't see it here.

Yes, I'll try to move this over to android-security.  Thanks for your time.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nikolay Elenkov  
View profile  
 More options Oct 8 2012, 10:44 pm
From: Nikolay Elenkov <nikolay.elen...@gmail.com>
Date: Tue, 9 Oct 2012 11:41:43 +0900
Local: Mon, Oct 8 2012 10:41 pm
Subject: Re: [android-developers] KeyChain.getPrivateKey(Context,String) on Android 4.1?

On Sun, Oct 7, 2012 at 2:56 AM, James Yonan <caprifin...@gmail.com> wrote:

> Raw RSA encryption is necessary as part of the challenge/response handshake
> of an SSL/TLS negotiation, if your side of the connection is using a cert as
> an authentication factor.  It's essential for VPN implementations that layer
> on top of SSL/TLS transport and need to interoperate with an external key
> store.  I enumerated the signature algs provided by the AndroidOpenSSL 1.0
> provider and I don't see any implementation for raw RSA.  Some Java
> implementations include it as "NONEwithRSA", but I don't see it here.

OK, that makes sense. The current version indeed doesn't provide this, but it
looks like it has been added in AOSP master:

// Cipher
put("Cipher.RSA/ECB/NoPadding", OpenSSLCipherRawRSA.class.getName());
put("Alg.Alias.Cipher.RSA/None/NoPadding", "RSA/ECB/NoPadding");

Haven't tested it yet though. BTW, CyanogenMod 10 seems to pull from jb-dev,
so it's not in there yet either.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »