Наследование: PemObjectGenerator
Пример #1
0
        private static PemObject CreatePemObject(object obj, string algorithm, char[] password, SecureRandom random)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (algorithm == null)
            {
                throw new ArgumentNullException("algorithm");
            }
            if (password == null)
            {
                throw new ArgumentNullException("password");
            }
            if (random == null)
            {
                throw new ArgumentNullException("random");
            }
            if (obj is AsymmetricCipherKeyPair)
            {
                return(MiscPemGenerator.CreatePemObject(((AsymmetricCipherKeyPair)obj).Private, algorithm, password, random));
            }
            string text = null;

            byte[] array = null;
            if (obj is AsymmetricKeyParameter)
            {
                AsymmetricKeyParameter asymmetricKeyParameter = (AsymmetricKeyParameter)obj;
                if (asymmetricKeyParameter.IsPrivate)
                {
                    string str;
                    array = MiscPemGenerator.EncodePrivateKey(asymmetricKeyParameter, out str);
                    text  = str + " PRIVATE KEY";
                }
            }
            if (text == null || array == null)
            {
                throw new PemGenerationException("Object type not supported: " + obj.GetType().FullName);
            }
            string text2 = Platform.ToUpperInvariant(algorithm);

            if (text2 == "DESEDE")
            {
                text2 = "DES-EDE3-CBC";
            }
            int num = text2.StartsWith("AES-") ? 16 : 8;

            byte[] array2 = new byte[num];
            random.NextBytes(array2);
            byte[] content = PemUtilities.Crypt(true, array, password, text2, array2);
            IList  list    = Platform.CreateArrayList(2);

            list.Add(new PemHeader("Proc-Type", "4,ENCRYPTED"));
            list.Add(new PemHeader("DEK-Info", text2 + "," + Hex.ToHexString(array2)));
            return(new PemObject(text, list, content));
        }
Пример #2
0
        public PemObject Generate()
        {
            PemObject result;

            try
            {
                if (this.algorithm != null)
                {
                    result = MiscPemGenerator.CreatePemObject(this.obj, this.algorithm, this.password, this.random);
                }
                else
                {
                    result = MiscPemGenerator.CreatePemObject(this.obj);
                }
            }
            catch (IOException exception)
            {
                throw new PemGenerationException("encoding exception", exception);
            }
            return(result);
        }
Пример #3
0
        private static PemObject CreatePemObject(object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException("obj");
            }
            if (obj is AsymmetricCipherKeyPair)
            {
                return(MiscPemGenerator.CreatePemObject(((AsymmetricCipherKeyPair)obj).Private));
            }
            if (obj is PemObject)
            {
                return((PemObject)obj);
            }
            if (obj is PemObjectGenerator)
            {
                return(((PemObjectGenerator)obj).Generate());
            }
            string type;

            byte[] content;
            if (obj is X509Certificate)
            {
                type = "CERTIFICATE";
                try
                {
                    content = ((X509Certificate)obj).GetEncoded();
                    goto IL_16F;
                }
                catch (CertificateEncodingException ex)
                {
                    throw new IOException("Cannot Encode object: " + ex.ToString());
                }
            }
            if (obj is X509Crl)
            {
                type = "X509 CRL";
                try
                {
                    content = ((X509Crl)obj).GetEncoded();
                    goto IL_16F;
                }
                catch (CrlException ex2)
                {
                    throw new IOException("Cannot Encode object: " + ex2.ToString());
                }
            }
            if (obj is AsymmetricKeyParameter)
            {
                AsymmetricKeyParameter asymmetricKeyParameter = (AsymmetricKeyParameter)obj;
                if (asymmetricKeyParameter.IsPrivate)
                {
                    string str;
                    content = MiscPemGenerator.EncodePrivateKey(asymmetricKeyParameter, out str);
                    type    = str + " PRIVATE KEY";
                }
                else
                {
                    type    = "PUBLIC KEY";
                    content = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(asymmetricKeyParameter).GetDerEncoded();
                }
            }
            else if (obj is IX509AttributeCertificate)
            {
                type    = "ATTRIBUTE CERTIFICATE";
                content = ((X509V2AttributeCertificate)obj).GetEncoded();
            }
            else if (obj is Pkcs10CertificationRequest)
            {
                type    = "CERTIFICATE REQUEST";
                content = ((Pkcs10CertificationRequest)obj).GetEncoded();
            }
            else
            {
                if (!(obj is Org.BouncyCastle.Asn1.Cms.ContentInfo))
                {
                    throw new PemGenerationException("Object type not supported: " + obj.GetType().FullName);
                }
                type    = "PKCS7";
                content = ((Org.BouncyCastle.Asn1.Cms.ContentInfo)obj).GetEncoded();
            }
IL_16F:
            return(new PemObject(type, content));
        }