4

Is it possible to extract the expiration date from a private key (.p12 file) without knowing the password? I used the command bellow without success:

openssl pkcs12 -in test.p12 -nokeys -nomacver
Mike Ounsworth
  • 57,707
  • 21
  • 150
  • 207
user159486
  • 43
  • 1
  • 1
  • 4
  • 3
    The option '-nokeys' in openssl is not to bypass passwords but rather "no private keys will be output". You need the password to interact with a p12. – zedman9991 Sep 20 '17 at 14:49
  • Slight correction: you are looking for the expiration date of the _certificate_. Keys themselves don't expire. – Mike Ounsworth Sep 20 '17 at 17:37
  • @MikeOunsworth: keys can and typically _should_ have defined lifetimes, and I've seen some schemes (notably HSMs) that do track them, but the PKCS8 key format (used essentially always in PKCS12) does not have dates while the X.509 cert format (ditto) does. – dave_thompson_085 Sep 21 '17 at 04:50

1 Answers1

4

From my understanding, .p12 is a very flexible file format in that a p12 created by openssl can look very different from a p12 created by java keytool, but most often the contents look like this:

P12 diagram

You need to extract the certificate, not the private key. Keys themselves don't have expiration dates, you want to extract the certificate from the p12 and look at the notAfter or validTo field.

My understanding is that if you created the p12 with a password, then the entire contents are encrypted as one blob. ie there is no way to access the only the certificates without knowing the password.

I'm not an openssl expert, but this seems consistent with this openssl command-line guide, which acts on the p12 certificate and private key together:

# Check a PKCS#12 file (.pfx or .p12)
openssl pkcs12 -info -in keyStore.p12

# Convert a PKCS#12 file (.pfx .p12) containing a private key and certificates to PEM
openssl pkcs12 -in keyStore.pfx -out keyStore.pem -nodes
Mike Ounsworth
  • 57,707
  • 21
  • 150
  • 207
  • 1
    p12 isn't normally _encrypted_ as a single blob, but usually as blobs using the same passphrase (except for Java with its storepass/keypass scheme). The 'certbag' usually if not always uses a very weak algorithm, RC2-40, which _can_ be broken, but not trivially. (p12 is MACed as a single blob, essentially always.) – dave_thompson_085 Sep 21 '17 at 04:50