Beispiel #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(CreatePemObject(((AsymmetricCipherKeyPair)obj).Private, algorithm, password, random));
        }
        string text = null;

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

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

        byte[] array2 = new byte[num];
        random.NextBytes(array2);
        byte[] content = PemUtilities.Crypt(encrypt: 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));
    }
Beispiel #2
0
    private object ReadPrivateKey(PemObject pemObject)
    {
        string text = pemObject.Type.Substring(0, pemObject.Type.Length - "PRIVATE KEY".Length).Trim();

        byte[]      array      = pemObject.Content;
        IDictionary dictionary = Platform.CreateHashtable();

        foreach (PemHeader header in pemObject.Headers)
        {
            dictionary[header.Name] = header.Value;
        }
        string a = (string)dictionary["Proc-Type"];

        if (a == "4,ENCRYPTED")
        {
            if (pFinder == null)
            {
                throw new PasswordException("No password finder specified, but a password is required");
            }
            char[] password = pFinder.GetPassword();
            if (password == null)
            {
                throw new PasswordException("Password is null, but a password is required");
            }
            string   text2      = (string)dictionary["DEK-Info"];
            string[] array2     = text2.Split(',');
            string   dekAlgName = array2[0].Trim();
            byte[]   iv         = Hex.Decode(array2[1].Trim());
            array = PemUtilities.Crypt(encrypt: false, array, password, dekAlgName, iv);
        }
        try
        {
            Asn1Sequence           instance = Asn1Sequence.GetInstance(array);
            AsymmetricKeyParameter publicParameter;
            AsymmetricKeyParameter asymmetricKeyParameter;
            switch (text)
            {
            case "RSA":
            {
                if (instance.Count != 9)
                {
                    throw new PemException("malformed sequence in RSA private key");
                }
                RsaPrivateKeyStructure instance2 = RsaPrivateKeyStructure.GetInstance(instance);
                publicParameter        = new RsaKeyParameters(isPrivate: false, instance2.Modulus, instance2.PublicExponent);
                asymmetricKeyParameter = new RsaPrivateCrtKeyParameters(instance2.Modulus, instance2.PublicExponent, instance2.PrivateExponent, instance2.Prime1, instance2.Prime2, instance2.Exponent1, instance2.Exponent2, instance2.Coefficient);
                break;
            }

            case "DSA":
            {
                if (instance.Count != 6)
                {
                    throw new PemException("malformed sequence in DSA private key");
                }
                DerInteger    derInteger  = (DerInteger)instance[1];
                DerInteger    derInteger2 = (DerInteger)instance[2];
                DerInteger    derInteger3 = (DerInteger)instance[3];
                DerInteger    derInteger4 = (DerInteger)instance[4];
                DerInteger    derInteger5 = (DerInteger)instance[5];
                DsaParameters parameters  = new DsaParameters(derInteger.Value, derInteger2.Value, derInteger3.Value);
                asymmetricKeyParameter = new DsaPrivateKeyParameters(derInteger5.Value, parameters);
                publicParameter        = new DsaPublicKeyParameters(derInteger4.Value, parameters);
                break;
            }

            case "EC":
            {
                ECPrivateKeyStructure instance3 = ECPrivateKeyStructure.GetInstance(instance);
                AlgorithmIdentifier   algID     = new AlgorithmIdentifier(X9ObjectIdentifiers.IdECPublicKey, instance3.GetParameters());
                PrivateKeyInfo        keyInfo   = new PrivateKeyInfo(algID, instance3.ToAsn1Object());
                asymmetricKeyParameter = PrivateKeyFactory.CreateKey(keyInfo);
                DerBitString publicKey = instance3.GetPublicKey();
                if (publicKey != null)
                {
                    SubjectPublicKeyInfo keyInfo2 = new SubjectPublicKeyInfo(algID, publicKey.GetBytes());
                    publicParameter = PublicKeyFactory.CreateKey(keyInfo2);
                }
                else
                {
                    publicParameter = ECKeyPairGenerator.GetCorrespondingPublicKey((ECPrivateKeyParameters)asymmetricKeyParameter);
                }
                break;
            }

            case "ENCRYPTED":
            {
                char[] password2 = pFinder.GetPassword();
                if (password2 == null)
                {
                    throw new PasswordException("Password is null, but a password is required");
                }
                return(PrivateKeyFactory.DecryptKey(password2, EncryptedPrivateKeyInfo.GetInstance(instance)));
            }

            case "":
                return(PrivateKeyFactory.CreateKey(PrivateKeyInfo.GetInstance(instance)));

            default:
                throw new ArgumentException("Unknown key type: " + text, "type");
            }
            return(new AsymmetricCipherKeyPair(publicParameter, asymmetricKeyParameter));
        }
        catch (IOException ex)
        {
            throw ex;
        }
        catch (Exception ex2)
        {
            throw new PemException("problem creating " + text + " private key: " + ex2.ToString());
        }
    }