doudu4282 2013-11-16 05:04
浏览 77
已采纳

使用GO进行AWS SNS签名验证

I want to implement AWS SNS signature verification in GO. Here is the signature verification tutorial provided by AWS.

However, there are some points I can not get it.

7: Generate the derived hash value of the Amazon SNS message. Submit the Amazon SNS message, in canonical format, to the same hash function used to generate the signature.

How to derived the hash value? Which hash function should I use?

8: Generate the asserted hash value of the Amazon SNS message. The asserted hash value is the result of using the public key value (from step 3) to decrypt the signature delivered with the Amazon SNS message.

How to get the asserted hash value?

Here is my code, I have a struct for notification:

type Notification struct {
    Message          string
    MessageId        string
    Signature        string
    SignatureVersion string
    SigningCertURL   string
    SubscribeURL     string
    Subject          string
    Timestamp        string
    TopicArn         string
    Type             string
    UnsubscribeURL   string
}

and I've already generated the canonical string:

    signString := fmt.Sprintf(`Message
%v
MessageId
%v`, self.Message, self.MessageId)

    if self.Subject != "" {
        signString = signString + fmt.Sprintf(`
Subject
%v`, self.Subject)
    }

    signString = signString + fmt.Sprintf(`
Timestamp
%v
TopicArn
%v
Type
%v`, self.Timestamp, self.TopicArn, self.Type)

Decode signature from base64

signed, err := base64.StdEncoding.DecodeString(self.Signature)

Get the certificate from .pem

resp, _ := http.Get(self.SigningCertURL)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
p, _ := pem.Decode(body)
cert, err := x509.ParseCertificate(p.Bytes)

Now, how can I verify the signature with my canonical string? Is the following code right?

cert.CheckSignature(x509.SHA1WithRSA, signed, []byte(signString))

I always get crypto/rsa: verification error from above code.

Thanks!

  • 写回答

1条回答 默认 最新

  • dtyqflrr775518 2016-02-24 23:55
    关注

    I know this is a really old question, but I had the same problems as the reporter so I took a day to figure this out with the assistance of AWS. I have open sourced my work as an external library, now available here.

    You can use it like this (notificationJson is a JSON string):

    import (
      "encoding/json"
      "fmt"
    
      "github.com/robbiet480/go.sns"
    )
    
    var notificationPayload sns.Payload
    err := json.Unmarshal([]byte(notificationJson), &notificationPayload)
    if err != nil {
      fmt.Print(err)
    }
    verifyErr := notificationPayload.VerifyPayload()
    if verifyErr != nil {
      fmt.Print(verifyErr)
    }
    fmt.Print("Payload is valid!")
    

    Thanks for your initial work on this lazywei, I based my library on your above code!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败