示例#1
0
        public RsaWithRsaParameterKey GetKeys(string routingToken, OwnCertificate ownCert)
        {
            //var participantRepo = new ParticipantRepository(new UnitOfWork(new InteropContext()));
            //var publicKeyString = participantRepo.GetPublicKey(routingToken);
            var publicKeyString =
                "<RSAKeyValue><Modulus>ks+L8kWHiBwiPw4zJcZwIkeGrhNP0fI6LohybpGjNoZSf4bZ1hXrgLiWoklA2QY7CD7hPbW2d1cLVK7VOAYqAtyIdrchG6AVSWg2ul90QT/BgvNFcBqf9xuS3l25t1OimUcj47/hPx2Nu9NMMMpGhqp6PR2pEwjvMAxHgW7BzOM=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
            var fromModulus  = publicKeyString.IndexOf("<Modulus>") + "<Modulus>".Length;
            var toModulus    = publicKeyString.LastIndexOf("</Modulus>");
            var modulus      = publicKeyString.Substring(fromModulus, toModulus - fromModulus);
            var fromExponent = publicKeyString.IndexOf("<Exponent>") + "<Exponent>".Length;
            var toExponent   = publicKeyString.LastIndexOf("</Exponent>");
            var exponent     = publicKeyString.Substring(fromExponent, toExponent - fromExponent);

            var rsaParam = new RSAParameters();

            rsaParam.Modulus  = Convert.FromBase64String(modulus);
            rsaParam.Exponent = Convert.FromBase64String(exponent);

            var rsaParamKey = new RsaWithRsaParameterKey();

            rsaParamKey.PublicKey  = rsaParam;
            rsaParamKey.PrivateKey = ownCert.PrivateKey;

            return(rsaParamKey);
        }
示例#2
0
        // Опис: Метод за вчитување на сертификат
        // Влезни параметри: ILogger logger
        // Излезни параметри: OwnCertificate модел
        public OwnCertificate LoadOwnCertificate(ILogger logger)
        {
            OwnCertificate output   = new OwnCertificate();
            string         certPath = AppSettings.Get <string>("MyCertificatePath");
            string         certPass = AppSettings.Get <string>("MyCertificatePassword");
            var            certUser = AppSettings.Get <string>("MyCertificateName");
            X509Store      store    = new X509Store(StoreName.My, StoreLocation.LocalMachine);

            store.Open(OpenFlags.ReadOnly);


            bool getFromStore = Boolean.Parse(AppSettings.Get <string>("CertificateFromStore"));

            try
            {
                if (getFromStore)
                {
                    output.Certificate = store.Certificates
                                         .Find(X509FindType.FindBySubjectName, certUser, false)
                                         .OfType <X509Certificate2>()
                                         .First();
                }
                else
                {
                    output.Certificate = new X509Certificate2(certPath, certPass, X509KeyStorageFlags.Exportable);
                }
            }
            catch (Exception e)
            {
                //LogHelper.WriteInNLoc("B", "WE", e.Message + "=====" + pass, "Request_" + DateTime.Now, "Info");
                logger.Error("LoadOwnCertificate", e);
                logger.Info(e.Message + "=====" + certUser + "====" + StoreLocation.LocalMachine, "Request");
            }

            StringBuilder builder = new StringBuilder();

            builder.AppendLine(Convert.ToBase64String(output.Certificate.Export(X509ContentType.Cert), Base64FormattingOptions.InsertLineBreaks));
            var stringBuilder = builder.ToString();

            output.CertString = stringBuilder;
            output.PublicKey  = output.Certificate.PublicKey.Key.ToXmlString(false);
            var rsa = (RSACryptoServiceProvider)output.Certificate.PrivateKey;

            output.PrivateKey = rsa.ExportParameters(true);
            return(output);
        }
示例#3
0
        public OwnCertificate LoadOwnCertificate(ILogger logger)
        {
            OwnCertificate output   = new OwnCertificate();
            string         certPath = AppSettings.Get <string>("MyCertificate");
            string         certPass = AppSettings.Get <string>("MyCertificatePassword");

            try
            {
                output.Certificate = new X509Certificate2(certPath, certPass, X509KeyStorageFlags.Exportable);
            }
            catch (Exception e)
            {
                //LogHelper.WriteInNLoc("B", "WE", e.Message + "=====" + pass, "Request_" + DateTime.Now, "Info");
                logger.Info(e.Message + "=====" + certPath + "====" + certPass, "Request");
            }
            output.PublicKey = output.Certificate.PublicKey.Key.ToXmlString(false);
            var rsa = (RSACryptoServiceProvider)output.Certificate.PrivateKey;

            output.PrivateKey = rsa.ExportParameters(true);
            return(output);
        }
示例#4
0
 public SoapMessage CreateMimResponseMsg(SoapMessage mimMsg, string soapBody, string mimeType, string sessionKey, string iVector, OwnCertificate ownCert)
 {
     return(new SoapMessage()
     {
         Header = new Header()
         {
             MimHeader = new MimHeader()
             {
                 id = "Header",
                 Consumer = mimMsg.Header.MimHeader.Consumer,
                 Provider = AppSettings.Get <string>("ParticipantCode"),
                 RoutingToken = mimMsg.Header.MimHeader.RoutingToken,
                 Service = mimMsg.Header.MimHeader.Service,
                 ServiceMethod = mimMsg.Header.MimHeader.ServiceMethod,
                 TransactionId = mimMsg.Header.MimHeader.TransactionId,
                 Dir = "Response",
                 PublicKey = ownCert.PublicKey,
                 MimeType = mimeType,
                 TimeStamp = DateTime.Now,
                 CorrelationID = String.Empty,
                 CallType = mimMsg.Header.MimHeader.CallType,
                 Signature = new MimSignature()
             },
             MimAdditionalHeader = new MimAdditionalHeader()
             {
                 Status = "200",
                 StatusMessage = "OK",
                 ProviderEndpointUrl = String.Empty,
                 ExternalEndpointUrl = String.Empty,
                 WebServiceUrl = String.Empty
             },
             CryptoHeader = new CryptoHeader()
             {
                 Key = sessionKey,
                 InitializationVector = iVector,
                 FormatValue = "AES"
             }
         },
         Body = new Body()
         {
             MimBody = new MimBody()
             {
                 id = "Body",
                 Message = soapBody
             }
         }
     });
 }
示例#5
0
 public SoapMessage CreateMimRequestMsg(UrlSegment urlSegments, string transactionID, string soapAction, string soapBody, string sessionKey, string iVector, string soapMethodName, OwnCertificate ownCert)
 {
     return(new SoapMessage()
     {
         Header = new Header()
         {
             MimHeader = new MimHeader()
             {
                 id = "Header",
                 Consumer = urlSegments.Consumer,
                 Provider = String.Empty,
                 RoutingToken = urlSegments.RoutingToken,
                 Service = urlSegments.Service,
                 ServiceMethod = soapMethodName,
                 //TransactionId = Guid.NewGuid().ToString(),
                 TransactionId = transactionID,
                 Dir = "Request",
                 PublicKey = ownCert.PublicKey,
                 MimeType = String.Empty,
                 TimeStamp = DateTime.Now,
                 CorrelationID = String.Empty,
                 CallType = urlSegments.Async ? MimHeaderCallType.asynchronous : MimHeaderCallType.synchronous,
                 Signature = new MimSignature()
             },
             MimAdditionalHeader = new MimAdditionalHeader()
             {
                 Status = String.Empty,
                 StatusMessage = String.Empty,
                 ProviderEndpointUrl = String.Empty,
                 ExternalEndpointUrl = String.Empty,
                 WebServiceUrl = String.Empty
             },
             CryptoHeader = new CryptoHeader()
             {
                 Key = sessionKey,
                 InitializationVector = iVector,
                 FormatValue = "AES"
             }
         },
         Body = new Body()
         {
             MimBody = new MimBody()
             {
                 id = "Body",
                 Message = soapBody
             }
         }
     });
 }
示例#6
0
        public string CreateMimSignedXmlMsg(SoapMessage mimMsg, OwnCertificate ownCert, ILogger _logger)
        {
            var doc = CreateMimXmlMsg(mimMsg);

            return(SignXml(doc, ownCert.Certificate, _logger));
        }