Commit 0026bf71 authored by Koen van Ingen's avatar Koen van Ingen
Browse files

allow string for nonce and context in signature requests

parent 12e2d6a6
......@@ -246,10 +246,13 @@ func TestVerifyValidSig(t *testing.T) {
irmaSignedMessage := &IrmaSignedMessage{}
json.Unmarshal([]byte(irmaSignedMessageJson), irmaSignedMessage)
request := "{\"nonce\": 42, \"context\": 1337, \"message\":\"I owe you everything\",\"messageType\":\"STRING\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
request := "{\"nonce\": 42, \"context\": 1337, \"message\":\"I owe you everything\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
sigRequestJSON := []byte(request)
sigRequest := &SignatureRequest{}
json.Unmarshal(sigRequestJSON, sigRequest)
// Test marshalling of 'string' fields:
require.Equal(t, sigRequest.Nonce, big.NewInt(42))
require.Equal(t, sigRequest.Context, big.NewInt(1337))
// Test if we can verify it with the original request
sigProofResult := VerifySig(conf, irmaSignedMessage, sigRequest)
......@@ -259,8 +262,25 @@ func TestVerifyValidSig(t *testing.T) {
require.Equal(t, attributeList[0].AttributeProofStatus, PRESENT)
require.Equal(t, attributeList[0].AttributeValue, "456")
// Test if we can verify it with a request that contains strings instead of ints for nonce and context
stringRequest := "{\"nonce\": \"42\", \"context\": \"1337\", \"message\":\"I owe you everything\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
stringSigRequestJSON := []byte(stringRequest)
stringSigRequest := &SignatureRequest{}
json.Unmarshal(stringSigRequestJSON, stringSigRequest)
// Test marshalling of 'string' fields:
require.Equal(t, stringSigRequest.Nonce, big.NewInt(42))
require.Equal(t, stringSigRequest.Context, big.NewInt(1337))
// Test if we can verify it with the original request
stringSigProofResult := VerifySig(conf, irmaSignedMessage, sigRequest)
require.Equal(t, stringSigProofResult.ProofStatus, VALID)
stringAttributeList := sigProofResult.ToAttributeResultList()
require.Len(t, stringAttributeList, 1)
require.Equal(t, stringAttributeList[0].AttributeProofStatus, PRESENT)
require.Equal(t, stringAttributeList[0].AttributeValue, "456")
// Test verify against unmatched request (i.e. different nonce, context or message)
unmatched := "{\"nonce\": 42, \"context\": 1337, \"message\":\"I owe you NOTHING\",\"messageType\":\"STRING\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
unmatched := "{\"nonce\": 42, \"context\": 1337, \"message\":\"I owe you NOTHING\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
unmatchedSigRequestJSON := []byte(unmatched)
unmatchedSigRequest := &SignatureRequest{}
json.Unmarshal(unmatchedSigRequestJSON, unmatchedSigRequest)
......@@ -282,7 +302,7 @@ func TestVerifyInValidSig(t *testing.T) {
irmaSignedMessage := &IrmaSignedMessage{}
json.Unmarshal([]byte(irmaSignedMessageJson), irmaSignedMessage)
request := "{\"nonce\": 42, \"context\": 1337, \"message\":\"I owe you everything\",\"messageType\":\"STRING\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
request := "{\"nonce\": 42, \"context\": 1337, \"message\":\"I owe you everything\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
sigRequestJSON := []byte(request)
sigRequest := &SignatureRequest{}
json.Unmarshal(sigRequestJSON, sigRequest)
......@@ -304,7 +324,7 @@ func TestVerifyInValidNonce(t *testing.T) {
json.Unmarshal([]byte(irmaSignedMessageJson), irmaSignedMessage)
// Original request also has the same invalid nonce (otherwise we would get unmatched_request)
request := "{\"nonce\": 4242, \"context\": 1337, \"message\":\"I owe you everything\",\"messageType\":\"STRING\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
request := "{\"nonce\": 4242, \"context\": 1337, \"message\":\"I owe you everything\",\"content\":[{\"label\":\"Student number (RU)\",\"attributes\":[\"irma-demo.RU.studentCard.studentID\"]}]}"
sigRequestJSON := []byte(request)
sigRequest := &SignatureRequest{}
json.Unmarshal(sigRequestJSON, sigRequest)
......
......@@ -6,6 +6,7 @@ import (
"strconv"
"time"
"encoding/json"
"github.com/go-errors/errors"
)
......@@ -290,6 +291,42 @@ func (sr *SignatureRequest) GetNonce() *big.Int {
return ASN1ConvertSignatureNonce(sr.Message, sr.Nonce)
}
// Convert fields in JSON string to BigInterger if they are string
// Supply fieldnames as a slice as second argument
func convertFieldsToBigInt(jsonString []byte, fieldNames []string) ([]byte, error) {
var rawRequest map[string]interface{}
err := json.Unmarshal(jsonString, &rawRequest)
if err != nil {
return nil, err
}
for _, fieldName := range fieldNames {
field := new(big.Int)
fieldString := fmt.Sprintf("%v", rawRequest[fieldName])
field.SetString(fieldString, 10)
rawRequest[fieldName] = field
}
return json.Marshal(rawRequest)
}
// Custom Unmarshalling to support both json with string and int fields for nonce and context
// i.e. {"nonce": "42", "context": "1337", ... } and {"nonce": 42, "context": 1337, ... }
func (sr *SignatureRequest) UnmarshalJSON(b []byte) error {
type SignatureRequestTemp SignatureRequest // To avoid 'recursive unmarshalling'
fixedRequest, err := convertFieldsToBigInt(b, []string{"nonce", "context"})
var result SignatureRequestTemp
json.Unmarshal(fixedRequest, &result)
sr.DisclosureRequest = result.DisclosureRequest
sr.Message = result.Message
return err
}
// Check if Timestamp is before other Timestamp. Used for checking expiry of attributes
func (t Timestamp) Before(u Timestamp) bool {
return time.Time(t).Before(time.Time(u))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment