Beispiel #1
0
        internal static void ImportCertificate(string destination, SecureString securePassword, string base64cert)
        {
            if (string.IsNullOrEmpty(destination))
            {
                throw new ArgumentNullException("destination");
            }
            if (securePassword == null)
            {
                throw new ArgumentNullException("securePassword");
            }
            if (string.IsNullOrEmpty(base64cert))
            {
                throw new ArgumentNullException("base64cert");
            }
            ExchangeCertificateRpc exchangeCertificateRpc = new ExchangeCertificateRpc();

            exchangeCertificateRpc.ImportCert       = base64cert;
            exchangeCertificateRpc.ImportExportable = true;
            ExchangeCertificateRpcVersion exchangeCertificateRpcVersion = ExchangeCertificateRpcVersion.Version1;

            byte[] outputBlob = null;
            try
            {
                byte[] inBlob = exchangeCertificateRpc.SerializeInputParameters(ExchangeCertificateRpcVersion.Version2);
                ExchangeCertificateRpcClient2 exchangeCertificateRpcClient = new ExchangeCertificateRpcClient2(destination);
                outputBlob = exchangeCertificateRpcClient.ImportCertificate2(0, inBlob, securePassword);
                exchangeCertificateRpcVersion = ExchangeCertificateRpcVersion.Version2;
            }
            catch (RpcException)
            {
                exchangeCertificateRpcVersion = ExchangeCertificateRpcVersion.Version1;
            }
            if (exchangeCertificateRpcVersion == ExchangeCertificateRpcVersion.Version1)
            {
                try
                {
                    byte[] inBlob2 = exchangeCertificateRpc.SerializeInputParameters(exchangeCertificateRpcVersion);
                    ExchangeCertificateRpcClient exchangeCertificateRpcClient2 = new ExchangeCertificateRpcClient(destination);
                    outputBlob = exchangeCertificateRpcClient2.ImportCertificate(0, inBlob2, securePassword);
                }
                catch (RpcException e)
                {
                    ManageExchangeCertificate.ThrowLocalizedException(e, destination);
                }
            }
            ExchangeCertificateRpc exchangeCertificateRpc2 = new ExchangeCertificateRpc(exchangeCertificateRpcVersion, null, outputBlob);

            if (!string.IsNullOrEmpty(exchangeCertificateRpc2.ReturnTaskErrorString))
            {
                throw new InvalidOperationException(exchangeCertificateRpc2.ReturnTaskErrorString);
            }
        }
Beispiel #2
0
        protected override void InternalProcessRecord()
        {
            base.InternalProcessRecord();
            if (base.HasErrors)
            {
                return;
            }
            ExchangeCertificateRpc exchangeCertificateRpc = new ExchangeCertificateRpc();
            string text;

            if (this.FileData != null)
            {
                text = ImportExchangeCertificate.RemoveBase64HeaderFooter(CertificateEnroller.ToBase64String(this.FileData));
            }
            else if (this.Instance != null)
            {
                text = ImportExchangeCertificate.RemoveBase64HeaderFooter(string.Join(null, this.Instance));
            }
            else
            {
                text = ImportExchangeCertificate.RemoveBase64HeaderFooter(CertificateEnroller.ToBase64String(this.GetFileData(this.FileName)));
            }
            if (text.Length == 0)
            {
                base.WriteError(new ImportCertificateDataInvalidException(), ErrorCategory.ReadError, 0);
            }
            exchangeCertificateRpc.ImportCert        = text;
            exchangeCertificateRpc.ImportDescription = this.FriendlyName;
            exchangeCertificateRpc.ImportExportable  = this.PrivateKeyExportable;
            ExchangeCertificateRpcVersion exchangeCertificateRpcVersion = ExchangeCertificateRpcVersion.Version1;

            byte[] outputBlob = null;
            try
            {
                byte[] inBlob = exchangeCertificateRpc.SerializeInputParameters(ExchangeCertificateRpcVersion.Version2);
                ExchangeCertificateRpcClient2 exchangeCertificateRpcClient = new ExchangeCertificateRpcClient2(this.serverObject.Name);
                outputBlob = exchangeCertificateRpcClient.ImportCertificate2(0, inBlob, this.Password);
                exchangeCertificateRpcVersion = ExchangeCertificateRpcVersion.Version2;
            }
            catch (RpcException)
            {
                exchangeCertificateRpcVersion = ExchangeCertificateRpcVersion.Version1;
            }
            if (exchangeCertificateRpcVersion == ExchangeCertificateRpcVersion.Version1)
            {
                try
                {
                    byte[] inBlob2 = exchangeCertificateRpc.SerializeInputParameters(exchangeCertificateRpcVersion);
                    ExchangeCertificateRpcClient exchangeCertificateRpcClient2 = new ExchangeCertificateRpcClient(this.serverObject.Name);
                    outputBlob = exchangeCertificateRpcClient2.ImportCertificate(0, inBlob2, this.Password);
                }
                catch (RpcException e)
                {
                    ManageExchangeCertificate.WriteRpcError(e, this.serverObject.Name, new Task.TaskErrorLoggingDelegate(base.WriteError));
                }
            }
            ExchangeCertificateRpc exchangeCertificateRpc2 = new ExchangeCertificateRpc(exchangeCertificateRpcVersion, null, outputBlob);

            ExchangeCertificateRpc.OutputTaskMessages(this.serverObject, exchangeCertificateRpc2, new Task.TaskWarningLoggingDelegate(this.WriteWarning), new Task.TaskErrorLoggingDelegate(base.WriteError));
            if (exchangeCertificateRpc2.ReturnCert != null)
            {
                exchangeCertificateRpc2.ReturnCert.Identity = this.serverObject.Fqdn + "\\" + exchangeCertificateRpc2.ReturnCert.Thumbprint;
            }
            base.WriteObject(exchangeCertificateRpc2.ReturnCert);
        }