2017-07-03 184 views
2

我不能得到簽名驗證工作,就像它描述的here。我正在使用BouncyCastle.NetCore 1.8.1.3,該項目是一個.NETCoreApp 1.0。Alexa技能簽名驗證失敗

我在macOS 10.12.1上開發,運行dotnet核心1.0.4,我的服務器運行Ubuntu 16.04.2-x64運行發行版,構建爲netcore1.0和ubuntu16.04-x64應用程序。

除簽名驗證外,系統的其餘部分運行時沒有問題。

我的驗證總是返回false

這裏是我的驗證簽名和身體服務:

using System; 
using System.IO; 
using System.Text; 
using System.Threading.Tasks; 

namespace MySkill.Main.Services 
{ 
    public class CertificationValidationService : ICertificationValidationService 
    { 
     private const string Algorithm = "SHA1withRSA"; 

     public async Task<bool> IsValidSiganture(Stream body, Stream certData, string signature) 
     { 
      var pemReader = new Org.BouncyCastle.OpenSsl.PemReader(new StreamReader(certData)); 
      var cert = (Org.BouncyCastle.X509.X509Certificate)pemReader.ReadObject(); 

      using (var sr = new StreamReader(body)) 
      { 
       var content = await sr.ReadToEndAsync(); 
       var result = CheckRequestSignature(Encoding.UTF8.GetBytes(content), signature, cert); 

       return result; 
      } 
     } 

     private static bool CheckRequestSignature(byte[] bodyData, string signature, Org.BouncyCastle.X509.X509Certificate cert) 
     { 
      byte[] sig = Convert.FromBase64String(signature); 

      var pubKey = (Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters)cert.GetPublicKey(); 
      var signer = Org.BouncyCastle.Security.SignerUtilities.GetSigner(Algorithm); 
      signer.Init(false, pubKey); 
      signer.BlockUpdate(bodyData, 0, bodyData.Length); 

      return signer.VerifySignature(sig); 
     } 
    } 
} 

沒有任何人有一個提示,我在做什麼錯誤或者已經使用了不同的框架或API?

+0

1.您確定使用UTF8編碼的「內容」? 2.你有沒有嘗試一種不同的算法? – ChrisM

+0

1.是的,內容確實是UTF8。 2.我嘗試了System.Security.Cryptography.X509Certificates類。但它也不起作用。它正在崩潰dotnet。 – Duglah

回答

0

從您分享的代碼看起來是正確的。沒有看到代碼的其餘部分,我不確定你得到的簽名是否正確。你可以看看我們的C#代碼,通過認證https://github.com/sophtron/Alexa-Skill-Sophtron並與你自己比較,看看是否有任何差異。

我嘗試從.net庫RSACryptoServiceProvider簽名驗證,它從來沒有工作。所以我不得不切換到BouncyCastle.1.8.1,它爲我工作。