How to read a SecretKey from a p12 file?

Viewed 433

I'm trying to read the SecretKey from a p12 file, but it is not created in Bouncy Castle. A interesting thing to note is that file doesn't have any certificate and the class System.Security.Cryptography.X509Certificates can't read anything.

  public void Pkcs12Pfx2()
    {
        Pfx bag = Pfx.GetInstance(File.ReadAllBytes(path));
        ContentInfo info = bag.AuthSafe;
        MacData mData = bag.MacData;
        DigestInfo dInfo = mData.Mac;
        AlgorithmIdentifier algId = dInfo.AlgorithmID;
        byte[] salt = mData.GetSalt();
        int itCount = mData.IterationCount.IntValue;

        Asn1OctetString content = Asn1OctetString.GetInstance(info.Content);
        AuthenticatedSafe authSafe = AuthenticatedSafe.GetInstance(content.GetOctets());
        ContentInfo[] c = authSafe.GetContentInfo();

        foreach (ContentInfo ci in c)
        {
            DerObjectIdentifier oid = ci.ContentType;

            byte[] octetsCi = null;
            if (oid.Equals(PkcsObjectIdentifiers.Data))
            {
                octetsCi = Asn1OctetString.GetInstance(ci.Content).GetOctets();
            }

            if (octetsCi != null)
            {
                Asn1Sequence seqCi = Asn1Sequence.GetInstance(octetsCi);

                foreach (Asn1Sequence subSeq in seqCi)
                {
                    SafeBag bagCi = SafeBag.GetInstance(subSeq);

                    foreach (var item in Asn1Sequence.GetInstance(bagCi.BagValue))
                    {
                        if (item.Equals(PkcsObjectIdentifiers.Pkcs8ShroudedKeyBag))
                        {

                        }
                        else
                        {
                            PrivateKeyInfo pki = new PrivateKeyInfo(algId, (Asn1Encodable)item);
                            var epki = EncryptedPrivateKeyInfoFactory.CreateEncryptedPrivateKeyInfo("PBEWITHSHA1AND3-KEYTRIPLEDES-CBC", password.ToCharArray(), salt, itCount, pki);

                            var aa = epki.ToAsn1Object();
                        }
                    }
                }
            }
        }

The object epki contains the information of the SecretKey, but it still encrypted, or encoded. How can I decode that information?

1 Answers

If you just look to open the file and get the key it is much more simple using the Pkcs12Store class. You provide the file and the file password to open it. Then once you create the object you have a property Aliases will list aliases for all entries in the file. Then you just call the 'GetKey` method with an alias name to get the key for the entry.

Here is a sample of how to read the key for the first entry.

 Pkcs12Store pkcs12Store = new Pkcs12Store(new FileStream("test.p12", FileMode.Open), "password".ToCharArray());
 AsymmetricKeyEntry key = pkcs12Store.GetKey(pkcs12Store.Aliases.Cast<string>().First());
 RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)key.Key; // here you should check what kind of key it is to then access the specific key parameters
Related