B4J Question AWS SimpleDB: Signature does not match [solved]

roumei

Active Member
Licensed User
I'm experimenting with AWS SimpleDB but I can't get the required signature right. As a test, I just use the example from the AWS documentation with my own credentials:
HMAC-SHA Signature - Amazon SimpleDB

This is the error (I'm pretty sure that I used the correct Secret Access Key):
B4X:
<Response><Errors><Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.</Message></Error></Errors><RequestID>11ad6352-3299-70d1-2e16-4dc11ba239c5</RequestID></Response>


Does anyone have experience with this kind of AWS signature? Thanks!

B4X:
' Test with example from: https://docs.aws.amazon.com/AmazonSimpleDB/latest/DeveloperGuide/HMACAuth.html#REST_RESTAuth
Private Sub SDBTest
   
    Dim TimeStamp As String = SDBCreateTimeStamp
       
    Dim sStringToSign As String =    "GET\n" & _
                                    "sdb.eu-west-1.amazonaws.com\n" & _
                                    "/\n" & _
                                    "AWSAccessKeyId=" & AccessKeyID & _
                                    "&Action=PutAttributes" & _
                                    "&Attribute.1.Name=Color" & _
                                    "&Attribute.1.Value=Blue" & _
                                    "&Attribute.2.Name=Size" & _
                                    "&Attribute.2.Value=Med" & _
                                    "&Attribute.3.Name=Price" & _
                                    "&Attribute.3.Value=0014.99" & _
                                    "&DomainName=MyDomain" & _
                                    "&ItemName=Item123" & _
                                    "&SignatureMethod=HmacSHA256" & _
                                    "&SignatureVersion=2" & _
                                    "&Timestamp=" & TimeStamp & _
                                    "&Version=2009-04-15"
   
    Log("String to sign:" & CRLF & sStringToSign)
   
    ' requires Encryption lib: https://www.b4x.com/android/forum/threads/base64-and-encryption-library.6839/
    Dim m As Mac
    Dim k As KeyGenerator
    k.Initialize("HMACSHA256")
    k.KeyFromBytes(SecretAccessKey.GetBytes("UTF8"))
    m.Initialise("HMACSHA256", k.Key)
    m.Update(sStringToSign.GetBytes("UTF8"))
    Dim b() As Byte
    b = m.Sign
   
    Dim sSignature As String = su.EncodeUrl(su.EncodeBase64(b), "UTF8") ' Private su As StringUtils
    Log("Signature:" & CRLF & sSignature)
   
    Dim sRequest As String =    "https://sdb.eu-west-1.amazonaws.com/?Action=PutAttributes" & _
                                "&DomainName=MyDomain" & _
                                "&ItemName=Item123" & _
                                "&Attribute.1.Name=Color&Attribute.1.Value=Blue" & _
                                "&Attribute.2.Name=Size&Attribute.2.Value=Med" & _
                                "&Attribute.3.Name=Price&Attribute.3.Value=0014.99" & _
                                "&Version=2009-04-15" & _
                                "&Timestamp=" & TimeStamp & _
                                "&Signature=" & sSignature & _
                                "&SignatureVersion=2" & _
                                "&SignatureMethod=HmacSHA256" & _
                                "&AWSAccessKeyId=" & AccessKeyID
   
    Log("Request:" & CRLF & sRequest)
   
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(sRequest)
   
    Wait For (j) JobDone(j As HttpJob)
   
    If j.Success = True Then Log("Success")
       
    j.Release
   
End Sub

Private Sub SDBCreateTimeStamp As String
    Dim dBackupTimeZone As Double = DateTime.TimeZoneOffset
    DateTime.SetTimeZone(0)
    DateTime.DateFormat = "yyyy-MM-dd'T'HH:mm:ss'Z'"
    Dim s As String = DateTime.Date(DateTime.Now).Replace(":","%3A")
    DateTime.SetTimeZone(dBackupTimeZone)
    Return s
End Sub
 
Last edited:

roumei

Active Member
Licensed User
It seems that I made a mistake by simply following the documentation and literally adding the "\n" command. Using CRLF instead of "/n" solves the problem. The following code works.
B4X:
Dim sStringToSign As String =    "GET" & CRLF & _
                                    "sdb.eu-west-1.amazonaws.com" & CRLF & _
                                    "/" & CRLF & _
                                    "AWSAccessKeyId=" & AccessKeyID & _
                                    "&Action=PutAttributes" & _
                                    "&Attribute.1.Name=Color" & _
                                    "&Attribute.1.Value=Blue" & _
                                    "&Attribute.2.Name=Size" & _
                                    "&Attribute.2.Value=Med" & _
                                    "&Attribute.3.Name=Price" & _
                                    "&Attribute.3.Value=0014.99" & _
                                    "&DomainName=MyDomain" & _
                                    "&ItemName=Item123" & _
                                    "&SignatureMethod=HmacSHA256" & _
                                    "&SignatureVersion=2" & _
                                    "&Timestamp=" & TimeStamp & _
                                    "&Version=2009-04-15"
 
Upvote 0
Top