summary refs log tree commit diff
path: root/crypto/src/asn1/pkcs/PBES2Parameters.cs
blob: 7d8a91ffaed270144b1402d889874b6eab218fac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;

namespace Org.BouncyCastle.Asn1.Pkcs
{
    public class PbeS2Parameters
        : Asn1Encodable
    {
        private readonly KeyDerivationFunc func;
        private readonly EncryptionScheme scheme;

        public static PbeS2Parameters GetInstance(object obj)
        {
            if (obj == null)
                return null;
            PbeS2Parameters existing = obj as PbeS2Parameters;
            if (existing != null)
                return existing;
            return new PbeS2Parameters(Asn1Sequence.GetInstance(obj));
        }

        public PbeS2Parameters(KeyDerivationFunc keyDevFunc, EncryptionScheme encScheme)
        {
            this.func = keyDevFunc;
            this.scheme = encScheme;
        }

        private PbeS2Parameters(Asn1Sequence seq)
        {
            if (seq.Count != 2)
                throw new ArgumentException("Wrong number of elements in sequence", "seq");

            Asn1Sequence funcSeq = (Asn1Sequence)seq[0].ToAsn1Object();

            // TODO Not sure if this special case is really necessary/appropriate
            if (funcSeq[0].Equals(PkcsObjectIdentifiers.IdPbkdf2))
            {
                func = new KeyDerivationFunc(PkcsObjectIdentifiers.IdPbkdf2,
                    Pbkdf2Params.GetInstance(funcSeq[1]));
            }
            else
            {
                func = new KeyDerivationFunc(funcSeq);
            }

            scheme = EncryptionScheme.GetInstance(seq[1]);
        }

        public KeyDerivationFunc KeyDerivationFunc
        {
            get { return func; }
        }

        public EncryptionScheme EncryptionScheme
        {
            get { return scheme; }
        }

        public override Asn1Object ToAsn1Object()
        {
            return new DerSequence(func, scheme);
        }
    }
}