예제 #1
0
        internal override DeserializeResponse Deserialize(string payload)
        {
            try
            {
                using (new MethodLogger(s_logger))
                {
                    var dr = base.Deserialize(payload);

                    var doc = XDocument.Parse(payload);
                    var xph = new XPathHelper(doc, Namespaces);

                    Response.Bank = new BankParams();

                    Response.OrderId = xph.GetOrderID()?.Value;
                    Response.TimestampBankParameter = ParseTimestamp(xph.GetTimestampBankParameter()?.Value);

                    if (dr.HasError)
                    {
                        return(dr);
                    }

                    var decryptedOd = DecryptOrderData(xph);
                    var deflatedOd  = Decompress(decryptedOd);
                    var strResp     = Encoding.UTF8.GetString(deflatedOd);
                    var hpbrod      = XDocument.Parse(strResp);

                    s_logger.LogDebug("Order data:\n{orderData}", hpbrod.ToString());

                    var r = new XPathHelper(hpbrod, Namespaces);

                    if (r.GetAuthenticationPubKeyInfoX509Data() != null || r.GetEncryptionPubKeyInfoX509Data() != null)
                    {
                        throw new DeserializationException("X509 not supported yet", payload);
                    }

                    if (r.GetAuthenticationPubKeyInfoPubKeyValue() != null)
                    {
                        if (!Enum.TryParse <AuthVersion>(r.GetAuthenticationPubKeyInfoAuthenticationVersion()?.Value,
                                                         out var authVersion))
                        {
                            throw new DeserializationException(
                                      "unknown authentication version for bank's authentication key");
                        }

                        var modulus =
                            Convert.FromBase64String(r.GetAuthenticationPubKeyInfoModulus()?.Value);
                        var exponent =
                            Convert.FromBase64String(r.GetAuthenticationPubKeyInfoExponent()?.Value);

                        var authPubKeyParams = new RSAParameters {
                            Exponent = exponent, Modulus = modulus
                        };
                        var authPubKey = RSA.Create();
                        authPubKey.ImportParameters(authPubKeyParams);
                        Response.Bank.AuthKeys = new AuthKeyPair
                        {
                            PublicKey = authPubKey,
                            Version   = authVersion
                        };
                    }
                    else
                    {
                        throw new DeserializationException($"{XmlNames.AuthenticationPubKeyInfo} missing", payload);
                    }

                    if (r.GetEncryptionPubKeyInfoPubKeyValue() != null)
                    {
                        if (!Enum.TryParse <CryptVersion>(r.GetEncryptionPubKeyInfoEncryptionVersion()?.Value,
                                                          out var encryptionVersion))
                        {
                            throw new DeserializationException("unknown encryption version for bank's encryption key");
                        }

                        var modulus =
                            Convert.FromBase64String(r.GetEncryptionPubKeyInfoModulus()?.Value);
                        var exponent =
                            Convert.FromBase64String(r.GetEncryptionPubKeyInfoExponent()?.Value);

                        var cryptPubKeyParams = new RSAParameters {
                            Exponent = exponent, Modulus = modulus
                        };
                        var cryptPubKey = RSA.Create();
                        cryptPubKey.ImportParameters(cryptPubKeyParams);
                        Response.Bank.CryptKeys = new CryptKeyPair
                        {
                            PublicKey = cryptPubKey,
                            Version   = encryptionVersion
                        };
                    }
                    else
                    {
                        throw new DeserializationException($"{XmlNames.EncryptionPubKeyInfo} missing", payload);
                    }

                    s_logger.LogDebug("Bank authentication key digest: {digest}",
                                      CryptoUtils.Print(Response.Bank?.AuthKeys?.Digest));
                    s_logger.LogDebug("Bank encryption key digest: {digest}",
                                      CryptoUtils.Print(Response.Bank?.CryptKeys?.Digest));

                    return(dr);
                }
            }
            catch (EbicsException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new DeserializationException($"can't deserialize {OrderType} response", ex, payload);
            }
        }