|
| 1 | +using System; |
| 2 | + |
| 3 | +namespace Org.BouncyCastle.Asn1.BC |
| 4 | +{ |
| 5 | + /** |
| 6 | + * Carrier for the contents of a {@link javax.crypto.interfaces.PBEKey} stored |
| 7 | + * in a BCFKS keystore. |
| 8 | + * <pre> |
| 9 | + * PbkdKeyData ::= SEQUENCE { |
| 10 | + * keyAlgorithm UTF8String, |
| 11 | + * password OCTET STRING, |
| 12 | + * salt [0] IMPLICIT OCTET STRING OPTIONAL, |
| 13 | + * iterationCount [1] IMPLICIT INTEGER OPTIONAL, |
| 14 | + * encoded [2] IMPLICIT OCTET STRING OPTIONAL |
| 15 | + * } |
| 16 | + * </pre> |
| 17 | + */ |
| 18 | + public class PbkdKeyData |
| 19 | + : Asn1Encodable |
| 20 | + { |
| 21 | + public static PbkdKeyData GetInstance(object obj) |
| 22 | + { |
| 23 | + if (obj == null) |
| 24 | + return null; |
| 25 | + if (obj is PbkdKeyData pbkdKeyData) |
| 26 | + return pbkdKeyData; |
| 27 | + return new PbkdKeyData(Asn1Sequence.GetInstance(obj)); |
| 28 | + } |
| 29 | + |
| 30 | + public static PbkdKeyData GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) => |
| 31 | + new PbkdKeyData(Asn1Sequence.GetInstance(taggedObject, declaredExplicit)); |
| 32 | + |
| 33 | + public static PbkdKeyData GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) => |
| 34 | + new PbkdKeyData(Asn1Sequence.GetTagged(taggedObject, declaredExplicit)); |
| 35 | + |
| 36 | + private readonly DerUtf8String m_keyAlgorithm; |
| 37 | + private readonly Asn1OctetString m_password; |
| 38 | + private readonly Asn1OctetString m_salt; |
| 39 | + private readonly DerInteger m_iterationCount; |
| 40 | + private readonly Asn1OctetString m_encoded; |
| 41 | + |
| 42 | + public PbkdKeyData(DerUtf8String keyAlgorithm, Asn1OctetString password, Asn1OctetString salt, |
| 43 | + DerInteger iterationCount, Asn1OctetString encoded) |
| 44 | + { |
| 45 | + m_keyAlgorithm = keyAlgorithm ?? throw new ArgumentNullException(nameof(keyAlgorithm)); |
| 46 | + m_password = password ?? throw new ArgumentNullException(nameof(password)); |
| 47 | + } |
| 48 | + |
| 49 | + private PbkdKeyData(Asn1Sequence seq) |
| 50 | + { |
| 51 | + int count = seq.Count, pos = 0; |
| 52 | + if (count < 2 || count > 5) |
| 53 | + throw new ArgumentException("Bad sequence size: " + count, nameof(seq)); |
| 54 | + |
| 55 | + m_keyAlgorithm = DerUtf8String.GetInstance(seq[pos++]); |
| 56 | + m_password = Asn1OctetString.GetInstance(seq[pos++]); |
| 57 | + m_salt = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, false, Asn1OctetString.GetTagged); |
| 58 | + m_iterationCount = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, false, DerInteger.GetTagged); |
| 59 | + m_encoded = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 2, false, Asn1OctetString.GetTagged); |
| 60 | + |
| 61 | + if (pos != count) |
| 62 | + throw new ArgumentException("Unexpected elements in sequence", nameof(seq)); |
| 63 | + } |
| 64 | + |
| 65 | + public Asn1OctetString Encoded => m_encoded; |
| 66 | + |
| 67 | + public DerInteger IterationCount => m_iterationCount; |
| 68 | + |
| 69 | + public DerUtf8String KeyAlgorithm => m_keyAlgorithm; |
| 70 | + |
| 71 | + public Asn1OctetString Password => m_password; |
| 72 | + |
| 73 | + public Asn1OctetString Salt => m_salt; |
| 74 | + |
| 75 | + public override Asn1Object ToAsn1Object() |
| 76 | + { |
| 77 | + Asn1EncodableVector v = new Asn1EncodableVector(5); |
| 78 | + v.Add(m_keyAlgorithm, m_password); |
| 79 | + v.AddOptionalTagged(false, 0, m_salt); |
| 80 | + v.AddOptionalTagged(false, 1, m_iterationCount); |
| 81 | + v.AddOptionalTagged(false, 2, m_encoded); |
| 82 | + return new DerSequence(v); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments