Skip to content

Commit

Permalink
fix: added Conetent method for value interface
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Aug 7, 2023
1 parent 7b823a9 commit 75ce02d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
24 changes: 20 additions & 4 deletions internal/encoding/asn1/asn1.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,17 @@ var (

// value represents an ASN.1 value.
type value interface {
// Encode encodes the value to the value writer in DER.
Encode(*bytes.Buffer) error
// EncodeMetadata encodes the identifier and length in DER to the buffer.
EncodeMetadata(*bytes.Buffer) error

// EncodedLen returns the length in bytes of the encoded data.
EncodedLen() int

// Content returns the content of the value.
// For primitive values, it returns the content octets.
// For constructed values, it returns nil because the content is
// the data of all members.
Content() []byte
}

// ConvertToDER converts BER-encoded ASN.1 data structures to DER-encoded.
Expand All @@ -49,9 +55,17 @@ func ConvertToDER(ber []byte) ([]byte, error) {
// get the total length from the root value and allocate a buffer
buf := bytes.NewBuffer(make([]byte, 0, flatValues[0].EncodedLen()))
for _, v := range flatValues {
if err = v.Encode(buf); err != nil {
if err = v.EncodeMetadata(buf); err != nil {
return nil, err
}

if v.Content() != nil {
// primitive value
_, err = buf.Write(v.Content())
if err != nil {
return nil, err
}
}
}

return buf.Bytes(), nil
Expand All @@ -60,7 +74,9 @@ func ConvertToDER(ber []byte) ([]byte, error) {
// decode decodes BER-encoded ASN.1 data structures.
//
// r is the input byte slice.
// The returned value is the flat ASN.1 values slice.
// The returned value, which is the flat slice of ASN.1 values, contains the
// nodes from a depth-first traversal. To get the DER of `r`, encode the values
// in the returned slice in order.
func decode(r []byte) ([]value, error) {
// prepare the first value
identifier, content, r, err := decodeMetadata(r)
Expand Down
11 changes: 8 additions & 3 deletions internal/encoding/asn1/constructed.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ type constructedValue struct {
rawContent []byte // the raw content of BER
}

// Encode encodes the constructed value to the value writer in DER.
func (v constructedValue) Encode(w *bytes.Buffer) error {
// EncodeMetadata encodes the constructed value to the value writer in DER.
func (v *constructedValue) EncodeMetadata(w *bytes.Buffer) error {
_, err := w.Write(v.identifier)
if err != nil {
return err
Expand All @@ -36,6 +36,11 @@ func (v constructedValue) Encode(w *bytes.Buffer) error {
}

// EncodedLen returns the length in bytes of the encoded data.
func (v constructedValue) EncodedLen() int {
func (v *constructedValue) EncodedLen() int {
return len(v.identifier) + encodedLengthSize(v.length) + v.length
}

// Content returns the content of the value.
func (v *constructedValue) Content() []byte {
return nil
}
14 changes: 9 additions & 5 deletions internal/encoding/asn1/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,24 @@ type primitiveValue struct {
content []byte
}

// Encode encodes the primitive value to the value writer in DER.
func (v primitiveValue) Encode(w *bytes.Buffer) error {
// EncodeMetadata encodes the primitive value to the value writer in DER.
func (v *primitiveValue) EncodeMetadata(w *bytes.Buffer) error {
_, err := w.Write(v.identifier)
if err != nil {
return err
}
if err = encodeLength(w, len(v.content)); err != nil {
return err
}
_, err = w.Write(v.content)
return err
return nil
}

// EncodedLen returns the length in bytes of the encoded data.
func (v primitiveValue) EncodedLen() int {
func (v *primitiveValue) EncodedLen() int {
return len(v.identifier) + encodedLengthSize(len(v.content)) + len(v.content)
}

// Content returns the content of the value.
func (v *primitiveValue) Content() []byte {
return v.content
}

0 comments on commit 75ce02d

Please sign in to comment.