Commit 23ecc5de authored by Sietse Ringers's avatar Sietse Ringers

feat: switch to cbor for binary messages

parent 613bc394
......@@ -5,19 +5,19 @@ import (
"fmt"
"strings"
"github.com/fxamacker/cbor"
"github.com/go-errors/errors"
"github.com/jinzhu/gorm"
)
type metaObjectIdentifier string
func (oi *metaObjectIdentifier) UnmarshalBinary(data []byte) error {
*oi = metaObjectIdentifier(data)
return nil
func (oi *metaObjectIdentifier) UnmarshalCBOR(data []byte) error {
return cbor.Unmarshal(data, (*string)(oi))
}
func (oi metaObjectIdentifier) MarshalBinary() (data []byte, err error) {
return []byte(oi), nil
func (oi metaObjectIdentifier) MarshalCBOR() (data []byte, err error) {
return cbor.Marshal(string(oi), cbor.EncOptions{})
}
// SchemeManagerIdentifier identifies a scheme manager. Equal to its ID. For example "irma-demo".
......
......@@ -384,41 +384,41 @@ func (s *Server) handleRevocationMessage(
) (int, []byte) {
if (noun == "updatefrom" || noun == "updatelatest") && method == http.MethodGet {
if len(args) != 2 {
return server.GobResponse(nil, server.RemoteError(server.ErrorInvalidRequest, "GET "+noun+" expects 2 url arguments"))
return server.BinaryResponse(nil, server.RemoteError(server.ErrorInvalidRequest, "GET "+noun+" expects 2 url arguments"))
}
i, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return server.GobResponse(nil, server.RemoteError(server.ErrorMalformedInput, err.Error()))
return server.BinaryResponse(nil, server.RemoteError(server.ErrorMalformedInput, err.Error()))
}
cred := irma.NewCredentialTypeIdentifier(args[0])
if noun == "updatefrom" {
return server.GobResponse(s.handleGetUpdateFrom(cred, i))
return server.BinaryResponse(s.handleGetUpdateFrom(cred, i))
} else {
return server.GobResponse(s.handleGetUpdateLatest(cred, i))
return server.BinaryResponse(s.handleGetUpdateLatest(cred, i))
}
}
if noun == "update" && method == http.MethodPost {
if len(args) != 1 {
return server.GobResponse(nil, server.RemoteError(server.ErrorInvalidRequest, "POST update expects 1 url argument"))
return server.BinaryResponse(nil, server.RemoteError(server.ErrorInvalidRequest, "POST update expects 1 url argument"))
}
cred := irma.NewCredentialTypeIdentifier(args[0])
var update *revocation.Update
if err := irma.UnmarshalBinary(message, update); err != nil {
return server.GobResponse(nil, server.RemoteError(server.ErrorMalformedInput, err.Error()))
if err := irma.UnmarshalValidateBinary(message, update); err != nil {
return server.BinaryResponse(nil, server.RemoteError(server.ErrorMalformedInput, err.Error()))
}
return server.GobResponse(s.handlePostUpdate(cred, update))
return server.BinaryResponse(s.handlePostUpdate(cred, update))
}
if noun == "issuancerecord" && method == http.MethodPost {
if len(args) != 2 {
return server.GobResponse(nil, server.RemoteError(server.ErrorInvalidRequest, "POST issuancercord expects 2 url arguments"))
return server.BinaryResponse(nil, server.RemoteError(server.ErrorInvalidRequest, "POST issuancercord expects 2 url arguments"))
}
cred := irma.NewCredentialTypeIdentifier(args[0])
counter, err := strconv.ParseUint(args[1], 10, 64)
if err != nil {
return server.GobResponse(nil, server.RemoteError(server.ErrorMalformedInput, err.Error()))
return server.BinaryResponse(nil, server.RemoteError(server.ErrorMalformedInput, err.Error()))
}
return server.GobResponse(s.handlePostIssuanceRecord(cred, counter, message))
return server.BinaryResponse(s.handlePostIssuanceRecord(cred, counter, message))
}
return server.GobResponse(nil, server.RemoteError(server.ErrorInvalidRequest, ""))
return server.BinaryResponse(nil, server.RemoteError(server.ErrorInvalidRequest, ""))
}
......@@ -2,8 +2,6 @@ package irma
import (
"bytes"
"encoding"
"encoding/gob"
"encoding/json"
"net/url"
"strconv"
......@@ -12,6 +10,7 @@ import (
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/fxamacker/cbor"
"github.com/go-errors/errors"
"github.com/privacybydesign/gabi"
)
......@@ -144,31 +143,11 @@ func UnmarshalValidateBinary(data []byte, dest interface{}) error {
}
func MarshalBinary(message interface{}) ([]byte, error) {
var bts []byte
var err error
if m, ok := message.(encoding.BinaryMarshaler); ok {
bts, err = m.MarshalBinary()
if err != nil {
return nil, err
}
} else {
var buf bytes.Buffer
if err = gob.NewEncoder(&buf).Encode(message); err != nil {
return nil, err
}
bts = buf.Bytes()
}
return bts, nil
return cbor.Marshal(message, cbor.EncOptions{})
}
func UnmarshalBinary(data []byte, dst interface{}) error {
if u, ok := dst.(encoding.BinaryUnmarshaler); ok {
return u.UnmarshalBinary(data)
}
if err := gob.NewDecoder(bytes.NewBuffer(data)).Decode(dst); err != nil {
return err
}
return nil
return cbor.Unmarshal(data, dst)
}
func (err *RemoteError) Error() string {
......
......@@ -5,6 +5,7 @@ import (
"fmt"
"time"
"github.com/fxamacker/cbor"
"github.com/go-errors/errors"
"github.com/hashicorp/go-multierror"
"github.com/jinzhu/gorm"
......@@ -90,7 +91,7 @@ type (
Attr *RevocationAttribute
Issued int64
ValidUntil int64
RevokedAt int64 // 0 if not currently revoked
RevokedAt int64 `json:",omitempty"` // 0 if not currently revoked
}
// TODO
......@@ -679,12 +680,12 @@ func (RevocationAttribute) GormDataType(dialect gorm.Dialect) string {
}
}
func (i *RevocationAttribute) GobEncode() ([]byte, error) {
return MarshalBinary((*big.Int)(i))
func (i *RevocationAttribute) MarshalCBOR() ([]byte, error) {
return cbor.Marshal((*big.Int)(i), cbor.EncOptions{})
}
func (i *RevocationAttribute) GobDecode(data []byte) error {
return UnmarshalBinary(data, (*big.Int)(i))
func (i *RevocationAttribute) UnmarshalCBOR(data []byte) error {
return cbor.Unmarshal(data, (*big.Int)(i))
}
func (hash eventHash) Value() (driver.Value, error) {
......
......@@ -101,7 +101,7 @@ func JsonResponse(v interface{}, err *irma.RemoteError) (int, []byte) {
return encodeValOrError(v, err, json.Marshal)
}
func GobResponse(v interface{}, err *irma.RemoteError) (int, []byte) {
func BinaryResponse(v interface{}, err *irma.RemoteError) (int, []byte) {
return encodeValOrError(v, err, irma.MarshalBinary)
}
......
......@@ -4,7 +4,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"io"
"io/ioutil"
......@@ -131,7 +131,7 @@ func (transport *HTTPTransport) log(prefix string, message interface{}, binary b
if !binary {
Logger.Tracef("transport: %s: %s", prefix, str)
} else {
Logger.Tracef("transport: %s (base64): %s", prefix, base64.RawStdEncoding.EncodeToString([]byte(str)))
Logger.Tracef("transport: %s (hex): %s", prefix, hex.EncodeToString([]byte(str)))
}
}
......
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