Installing pcks12 certificate in android "wrong password" bug
Asked Answered
O

4

10

When trying to import a pkcs12 certificate file into android for use with the openvpn connect app, I am prompted to input a password. This is the password relevant to this pkcs12 file. I proceed to input the correct password and am met with a "incorrect password" message.

To confirm that it is not the file that is faulty, I then tried to install the same certificate on a windows computer, where the same password was accepted and the certificate was installed without issue.

This was tested on two different smartphones running android 11 security update 2022-02-05.

Has anyone seen this issue before? I can only find similar issues online with no resolution.

Oath answered 14/4, 2022 at 14:0 Comment(0)
G
25

I had the same issue. It took me about a month to figure it out.

The tl;dr is this:

$ openssl pkcs12 -nodes < your.p12 > /tmp/certbag.pem
$ openssl pkcs12 -export -legacy -in /tmp/certbag.pem > /tmp/legacy.p12

Then use legacy.p12.

Apparently Android cannot import newer pkcs12 files. I tried this on Android 12 and Android 13. This is what man openssl-pkcs12 says for -legacy:

In the legacy mode, the default algorithm for certificate encryption is RC2_CBC or 3DES_CBC depending on whether the RC2 cipher is enabled in the build. The default algorithm for private key encryption is 3DES_CBC. If the legacy option is not specified, then the legacy provider is not loaded and the default encryption algorithm for both certificates and private keys is AES_256_CBC with PBKDF2 for key derivation.

Using openssl pkcs12 -info in my case I see this on the original .p12 file, which was created using Python's PyCryptography PKCS12 support:

MAC: sha256, Iteration 1
MAC length: 32, salt length: 8
PKCS7 Encrypted data: pbeWithSHA1And3-KeyTripleDES-CBC, Iteration 20000

And using openssl pkcs12 -info -legacy on the converted .p12 file I see this:

MAC: sha1, Iteration 2048
MAC length: 20, salt length: 8
PKCS7 Encrypted data: pbeWithSHA1And40BitRC2-CBC, Iteration 2048

The original one fails to import while the converted (legacy one) imports perfectly well.

Grillage answered 27/8, 2022 at 16:26 Comment(5)
Thanks, you saved my day. I was using XCA to export PKCS#12 files and import suddenly failed on Android devices. It appears that the OpenSSL upgrade to 3.0.x triggered this on my side and the application does not allow to export in legacy format. In case anyone else runs into this, feature request for XCA: #383.Tapley
This answer is the answer!Willawillabella
This doesnt work anymore? I get pkcs12: unable to load provider legacy Hint: use -provider-path option or OPENSSL_MODULES environment variable. 100000000A000000:error:12800067:DSO support routines:dlfcn_load:could not load the shared library:crypto/dso/dso_dlfcn.c:118:filename(/usr/lib/openssl/ossl-modules/legacy.dll): No such file or directory 100000000A000000:error:12800067:DSO support routines:DSO_load:could not load the shared library:crypto/dso/dso_lib.c:152: 100000000A000000:error:07880025:common libcrypto routines:provider_init:reason(524325):crypto/provider_core.c:904name=legacyAuriculate
Still works, as of Android 14Scribner
Thanks! But for anyone struggling like me on Ubuntu with the error, the command can be: openssl pkcs12 -in client.p12 -out certbag.pem -nodes. And then openssl pkcs12 -export -legacy -in certbag.pem > legacy.p12Celeski
A
4

In case anyone is struggling with GnuTLS certtool...

TL;DR this should work with both Android 9 & Android 12:

certtool --load-privkey client.key --load-certificate client.crt \
    --load-ca-certificate ca.crt \
    --to-p12 --outder --outfile client.p12 \
    --p12-name "A Friendly Name" \
    --hash SHA1 --pkcs-cipher 3des-pkcs12 --password YourPassword

Explanation

When creating PKCS#12 files, you have to choose MAC hash algorithm (--hash=xxx) and cipher algorithm (--pkcs-cipher=xxx). From my test, Android support is as below.

Hash Algorithm Cipher Algorithm Android 9 Android 12
(any) aes-128, aes-192, aes-256 no no
SHA384, SHA512 3des-pkcs12 no no
SHA256 3des-pkcs12 yes no
SHA1 3des-pkcs12 yes yes
SHA256 rc2-40 yes no
SHA1 rc2-40 yes yes

As can be seen above, Android 9 actually supports both SHA256 and SHA1 as MAC, but Android 12 somehow only supports SHA1.
In certtool, the default MAC hash algorithm is SHA256 even if you choose --pkcs-cipher=3des-pkcs12. Therefore you have to explicitly specify --hash=SHA1, otherwise the p12 file won't work for Android 12.

Other comments

  • Tested phones are Xperia XZ1 Compact (G8441, Android 9) and Xperia 10 ii (XQ-AU52, Android 12).
  • certtool default MAC iteration is 600000 (compared to openssl's 2048). This paranoid setting results several seconds slowness when installing .p12 files on phones & PCs. I haven't found a parameter to change this iteration (openssl 3.x specifies by -iter).
Activist answered 21/11, 2022 at 18:57 Comment(0)
B
2

PKCS12 is a encrypted container format for certificates and cryptographic keys. For encrypting the contained data multiple algorithms exists. Unfortunately not all systems processing PKCS#12 files do support all possible encryption algorithms.

When reading a PKCS#12 file by a system/program and it encounters an unsupported cryptographic algorithm you would expect an error message like "unable to read file: unknown or unsupported algorithm". Unfortunately in reality most implementations just output the generic error message "incorrect password".

Detecting the used encryption algorithm:

For detecting the used encryption algorithm execute

openssl pkcs12 -info -in example.p12

After entering the password(s) you will see the decoded data of the PKCS12 file, the encryption type can be seen by certain lines in the output.

The most recent encryption format (that is not yet supported by all programs) is used if you find a line like:

Shrouded Keybag: PBES2, PBKDF2, AES-256-CBC, Iteration 10000, PRF hmacWithSHA256

The older often called "legacy" encryption format is used if you find a line like:

Shrouded Keybag: pbeWithSHA1And3-KeyTripleDES-CBC, Iteration 1

A third even older algorithm exists. I have not found an example PKCS#12 file, but it should be output as pbeWithSHA1And40BitRC2-CBC.

Converting a PKCS#12 file to the old encryption format

Changing the encryption type used by a PKCS#12 file is pretty complicated as you have to extract all the contained keys and certificates and the reassemble everything into a new file. The necessary openssl commands are denoted here:

https://help.globalscape.com/help/archive/secureserver3/Converting_an_incompatible_PKCS_12_format_file_to_a_compatible_PKCS_12_.htm

Bastille answered 3/5, 2022 at 9:53 Comment(0)
P
2

I ran into the problem that the above solution with -legacy option did not work on an actual ubuntu/openssl with my new email certificate. Little additional problem: I had a .pfx file not a .p12 not knowing if this is the same container format with other ending?

The following workflow was a succes:

$ openssl pkcs12 -nodes < your.pfx > /home/ubuntu/certbag.pem
$ openssl pkcs12 -keypbe PBE-SHA1-3DES -certpbe PBE-SHA1-3DES -export -in /home/ubuntu/certbag.pem -out /home/ubuntu/new.pfx -name "SMIME-Cert"

Delete certbag.pem afterwards! It contains your private key without encryption!

Certificate imports now flawlessly on android 10.

Thanks to the above solution and the provided links!

Provocation answered 15/11, 2022 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.