Skip to content

Commit

Permalink
Align Marshaling of RelativeOID with the one of TAI
Browse files Browse the repository at this point in the history
Relates to bwesterb#5
  • Loading branch information
pohlm01 committed Sep 18, 2024
1 parent 400c835 commit 9680bf4
Showing 1 changed file with 52 additions and 30 deletions.
82 changes: 52 additions & 30 deletions mtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,9 +426,11 @@ func (p *CAParams) ActiveBatches(dt time.Time) BatchRange {
func (p *CAParams) MarshalBinary() ([]byte, error) {
// TODO add struct to I-D
var b cryptobyte.Builder
b.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(p.Issuer)
})
var issuer, err = p.Issuer.MarashalBinary()
if err != nil {
return nil, err
}
b.AddBytes(issuer)
b.AddUint16(uint16(p.PublicKey.Scheme()))
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(p.PublicKey.Bytes())
Expand Down Expand Up @@ -584,9 +586,11 @@ func (w *ValidityWindow) LabeledValdityWindow(ca *CAParams) ([]byte, error) {
var b cryptobyte.Builder
b.AddBytes([]byte("Merkle Tree Crts ValidityWindow\000"))

b.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(ca.Issuer)
})
var issuer, err = ca.Issuer.MarashalBinary()
if err != nil {
return nil, err
}
b.AddBytes(issuer)
buf, err := w.MarshalBinary()
if err != nil {
return nil, err
Expand Down Expand Up @@ -1018,9 +1022,11 @@ func (batch *Batch) hashNode(out, left, right []byte, index uint64,
var b cryptobyte.Builder

b.AddUint8(1)
b.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(batch.CA.Issuer)
})
var issuer, err = batch.CA.Issuer.MarashalBinary()
if err != nil {
return nil
}
b.AddBytes(issuer)
b.AddUint32(batch.Number)
b.AddUint64(index)
b.AddUint8(level)
Expand All @@ -1041,9 +1047,11 @@ func (batch *Batch) hashNode(out, left, right []byte, index uint64,
func (batch *Batch) hashEmpty(out []byte, index uint64, level uint8) error {
var b cryptobyte.Builder
b.AddUint8(0)
b.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(batch.CA.Issuer)
})
var issuer, err = batch.CA.Issuer.MarashalBinary()
if err != nil {
return err
}
b.AddBytes(issuer)
b.AddUint32(batch.Number)
b.AddUint64(index)
b.AddUint8(level)
Expand Down Expand Up @@ -1133,9 +1141,11 @@ func (a *AbridgedAssertion) Key(out []byte) error {
func (a *AbridgedAssertion) Hash(out []byte, batch *Batch, index uint64) error {
var b cryptobyte.Builder
b.AddUint8(2)
b.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(batch.CA.Issuer)
})
var issuer, err = batch.CA.Issuer.MarashalBinary()
if err != nil {
return nil
}
b.AddBytes(issuer)
b.AddUint32(batch.Number)
b.AddUint64(index)
buf, err := a.MarshalBinary()
Expand Down Expand Up @@ -1592,6 +1602,33 @@ func (oid *RelativeOID) UnmarshalText(text []byte) error {
return nil
}

func (oid RelativeOID) MarashalBinary() ([]byte, error) {
if len(oid) == 0 {
return nil, errors.New("can't marshal uninitialized RelativeOID")
}

var b cryptobyte.Builder
b.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
b.AddBytes(oid)
})
return b.Bytes()
}

func (oid *RelativeOID) Equal(rhs *RelativeOID) bool {
if rhs == nil {
return false
}
if len(*oid) == len(*rhs) {
for i, v := range *oid {
if v != (*rhs)[i] {
return false
}
}
return true
}
return false
}

func (tai TrustAnchorIdentifier) MarshalBinary() ([]byte, error) {
if tai.Issuer == nil || len(tai.Issuer) == 0 {
return nil, errors.New("can't marshal uninitialized TrustAnchorIdentifier")
Expand Down Expand Up @@ -1646,18 +1683,3 @@ func (tai *TrustAnchorIdentifier) unmarshal(s *cryptobyte.String) error {
tai.Issuer = oid
return nil
}

func (oid *RelativeOID) Equal(rhs *RelativeOID) bool {
if rhs == nil {
return false
}
if len(*oid) == len(*rhs) {
for i, v := range *oid {
if v != (*rhs)[i] {
return false
}
}
return true
}
return false
}

0 comments on commit 9680bf4

Please sign in to comment.