2using System.Collections.Generic;
3using System.Security.Cryptography;
4using System.Security.Cryptography.X509Certificates;
15 private const string bagTypes =
"1.2.840.113549.1.12.10.1";
16 private static readonly RandomNumberGenerator rnd = RandomNumberGenerator.Create();
18 private LinkedList<DerEncoder> stack =
null;
20 private byte[] macSalt =
null;
35 byte[] Result =
new byte[NrBytes];
59 if (this.stack is
null)
60 this.stack =
new LinkedList<DerEncoder>();
62 this.stack.AddLast(this.der);
68 if (this.stack is
null || this.stack.Last is
null)
69 throw new InvalidOperationException(
"Stack empty.");
71 byte[] Result = this.der.
ToArray();
73 this.der = this.stack.Last.Value;
74 this.stack.RemoveLast();
127 if (!(this.stack is
null) && !(this.stack.First is
null))
128 throw new InvalidOperationException(
"Stack not empty.");
140 byte[] Result = this.der.
ToArray();
146 private void AssertBegun()
148 if (this.der is
null)
149 throw new InvalidOperationException(
"Encoding not begun.");
170 private void StartSafeBag(
string OID)
179 private void EndSafeBag()
192 this.StartSafeBag(bagTypes +
".1");
193 EncodePrivateKeyInfo(this.der, Algorithm);
226 this.StartSafeBag(bagTypes +
".2");
229 EncodePrivateKeyInfo(Key, Algorithm);
230 byte[] PrivateKey = Key.
ToArray();
247 internal static byte[] FormatPassword(
string Password)
249 return Primitives.
CONCAT(Encoding.BigEndianUnicode.GetBytes(Password),
new byte[] { 0, 0 });
271 internal static byte[] PRF(
HashFunction H,
int r,
byte[] P,
byte[] S,
int n,
byte ID)
276 throw new ArgumentException(
"Must be a factor of 8.", nameof(n));
306 throw new ArgumentException(
"Hash function not supported.", nameof(H));
312 byte[] D =
new byte[v8];
315 for (i = 0; i < v8; i++)
326 byte[][] As =
new byte[c][];
328 for (i = 0; i < c; i++)
332 for (j = 0; j < r; j++)
335 byte[] B = Extend(As[i], v);
337 for (j = 0; j < i8; j += v8)
338 AddTo(I, j, B,
true);
344 Array.Resize(ref A, n);
349 private static void AddTo(
byte[] Dest,
int Offset,
byte[] Term,
bool Carry)
373 private static byte[] Extend(
byte[] Bin,
int v)
375 int Len = Bin.Length;
376 int c = (v / 8) * ((Len + v - 1) / v);
377 byte[] Result =
new byte[c];
380 for (i = 0; i < c; i += Len)
384 Array.Copy(Bin, 0, Result, i, j);
386 Array.Copy(Bin, 0, Result, i, Len);
398 this.StartSafeBag(bagTypes +
".3");
401 this.der.
OCTET_STRING(Certificate.Export(X509ContentType.Pkcs7));
Contains methods for simple hash calculations.
static byte[] ComputeHash(HashFunction Function, byte[] Data)
Computes a hash of a block of binary data.
Encodes data using the Distinguished Encoding Rules (DER), as defined in X.690
byte[] ToArray()
Converts the generated output to a byte arary.
void StartOCTET_STRING()
Starts a OCTET_STRING.
void EndSEQUENCE()
Ends the current SEQUENCE.
void EndContent(Asn1TypeClass Class)
Ends the current Content section.
void OBJECT_IDENTIFIER(string OID)
Encodes an OBJECT IDENTIFIER value.
void NULL()
Encodes an NULL value.
void EndOCTET_STRING()
Ends the current OCTET_STRING.
void StartContent(Asn1TypeClass Class)
Starts a content section.
void INTEGER(long Value)
Encodes an INTEGER value.
void StartSEQUENCE()
Starts a SEQUENCE.
void OCTET_STRING(byte[] Value)
Encodes an OCTET STRING value.
Abstract base class for password-based encryption algorithms
abstract void EncodePkcs5AlgorithmIdentifier(DerEncoder Der)
Encodes the AlgorithmIdentifier, as defined in PKCS#5 (RFC 2898).
abstract byte[] Encrypt(byte[] PlainText)
Encrypts data.
Implements pbeWithSHAAnd3-KeyTripleDES-CBC
Encodes certificates and keys into PKCS#12 or PFX files.
void ShroudedKeyBag(PasswordEncryption Encryption, SignatureAlgorithm Algorithm)
Encodes a ShroudedKeyBag (§4.2.2 RFC 7292, §6, RFC 5208)
void StartSafeContent()
Starts a block of safe content, in accordance with §5 of RFC 7292.
void Begin()
Begins PKCS#12 encoding.
static byte[] GetRandomBytes(int NrBytes)
Gets a number of random bytes.
void CertificateBag(X509Certificate2 Certificate)
Encodes a CertBag (§4.2.3 RFC 7292, §6, RFC 5208)
void ShroudedKeyBag(string Password, SignatureAlgorithm Algorithm)
Encodes a ShroudedKeyBag (§4.2.2 RFC 7292, §6, RFC 5208)
void KeyBag(SignatureAlgorithm Algorithm)
Encodes a KeyBag (§4.2.1 RFC 7292, §5, RFC 5208)
void EndSafeContent()
Ends a block of safe content, in accordance with §5 of RFC 7292.
byte[] End()
Ends PKCS#12 encoding and returns the encoded result.
void StartEncryptedSafeContent()
Starts a block of encrypted safe content, in accordance with §5 of RFC 7292.
void EndBagSet()
Ends a PKCS12BagSet
void StartBagSet()
Starts a PKCS12BagSet
PfxEncoder()
Encodes certificates and keys into PKCS#12 or PFX files.
void EndEncryptedSafeContent()
Ends a block of encrypted safe content, in accordance with §5 of RFC 7292.
Contains static functions used by different algorithms.
static byte[] CONCAT(params byte[][] OctetStrings)
Concatenates a series of octet strings.
Abstract base class for signature algorithms
abstract string PkiAlgorithmOID
Object Identity for the PKI algorithm.
abstract void ExportPrivateKey(DerEncoder Output)
Exports the private key using DER.
HashFunction
Hash method enumeration.