コード例 #1
0
        private static X509Certificate2 GetCertificate()
        {
            if (_signingCertificate == null)
            {
                var certificateKey = ConfigurationManager.AppSettings["GovTalkCertKey"];

                _signingCertificate = GovTalkHelpers.GetSigningCertificate(certificateKey);
                if (_signingCertificate == null)
                {
                    throw new Exception("Could not find signing certificate");
                }
            }

            return(_signingCertificate);
        }
コード例 #2
0
        /// <summary>
        /// Builds key exchange request xml with generated keypair's public key and calls key exchange subcontract to receive providers public key
        /// </summary>S
        /// <param name="fileName"></param>
        /// <param name="subContractId">Key exchange subcontractId given by service provider</param>
        /// <param name="signingCert">Signing certificate</param>
        public void PerformExchange(String fileName, String subContractId, X509Certificate2 signingCert)
        {
            if (GeneratedPublicKey == null)
            {
                GenerateKeyPair();
            }

            var publicKeyXml = GeneratedPublicKey.ToXml();

            var signId = GovTalkHelpers.GenerateSignId();

            var firstChild = (XmlElement)publicKeyXml.FirstChild;

            firstChild.SignXml(signingCert, signId);

            var xmlText = GovTalkHelpers.GetXmlFile(fileName);

            var requestNode = GovTalkHelpers.ComposeRequestXmlWithParamObjects(xmlText, subContractId, publicKeyXml);

            var keyExchangeResponse = GovTalkCallApi.GetResponse(requestNode.OuterXml);

            var keyExchangeDoc = new XmlDocument();

            keyExchangeDoc.LoadXml(keyExchangeResponse);

            //var logFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XmlLogs", $"keyExch_{DateTime.Now:yyyy.MM.dd_hh.mm.ss}.xml");
            //var fileName = HttpContext.Current.Server.MapPath($"~/XmlLogs/keyExch_{DateTime.Now:yyyy.MM.dd_hh.mm.ss}.xml");

            //using (var file = File.CreateText(logFileName))
            //    keyExchangeDoc.Save(file);

            var resultStatus = GovTalkHelpers.GetResponseStatus(keyExchangeDoc);

            if (resultStatus.Code != "14")
            {
                var message = $"KeyExchange error [{resultStatus.Code}] {resultStatus.Message}";
                throw new Exception(message);
            }

            ReceivedPublicKey = new ECPublicKey();
            ReceivedPublicKey.LoadXml(keyExchangeResponse);

            KeyId = ReceivedPublicKey.Id;
        }
コード例 #3
0
        public static XmlElement LoadXml(String method, bool encrypt, IDictionary <String, Object> @params, String resultContract)
        {
            var methodKey = $"GovTalk_{method}";

            var methodFilePath = ConfigurationManager.AppSettings[methodKey];

            var xmlText = GovTalkHelpers.GetXmlFile(methodFilePath);

            foreach (var pair in @params)
            {
                var key   = String.Concat("$!{", pair.Key, "}");
                var value = Convert.ToString(pair.Value);

                if (pair.Value is DateTime)
                {
                    value = XmlConvert.ToString((DateTime)pair.Value, XmlDateTimeSerializationMode.Local);
                }

                xmlText = xmlText.Replace(key, value);
            }

            var requestDoc = new XmlDocument();

            requestDoc.LoadXml(xmlText);

            var signId      = GovTalkHelpers.GenerateSignId();
            var certificate = GetCertificate();

            var requestElement = (XmlElement)requestDoc.GetSubnode("Request");

            requestElement.SignXml(certificate, signId);

            var paramsElement = (XmlElement)requestElement.GetSubnode("Parameters");

            var contractName = resultContract;

            if (String.IsNullOrWhiteSpace(contractName))
            {
                contractName = paramsElement.FirstChild.Name;
            }

            if (encrypt)
            {
                var encKey = GetEncryptKey();
                paramsElement.EncryptXml(encKey.EncKey, encKey.KeyId);
            }

            var response = GetResponse(requestDoc.OuterXml);

            //Initialize response xml document by response string returned by service api
            var responseDoc = new XmlDocument();

            responseDoc.LoadXml(response);

            //Deserialize 'ResultStatus' node of response xml into corresponding object
            var status = GovTalkHelpers.GetResponseStatus(responseDoc);

            if (status.Code != "14")     //14 is OK
            {
                if (status.Code == "31") //TODO: 31 not found records, temporary
                {
                    return(null);
                }

                throw new Exception($"{status.Code} - {status.Message}");
            }

            var resultNode = (XmlElement)responseDoc.GetSubnode(contractName);

            if (resultNode != null && encrypt)
            {
                var encKey = GetEncryptKey();
                resultNode.DecryptXml(encKey.EncKey);
            }

            var responseNode = (XmlElement)responseDoc.GetSubnode("Response");

            if (!responseNode.ValidateSignature())
            {
                throw new Exception("Signature is not valid");
            }

            if (resultNode != null)
            {
                return(resultNode);
            }

            return((XmlElement)responseDoc.FirstChild);
        }