Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ber to der convertor #11

Merged
merged 8 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion internal/encoding/asn1/ber/ber.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// Reference:
// - http://luca.ntop.org/Teaching/Appunti/asn1.html
// - ISO/IEC 8825-1:2021
// - https://learn.microsoft.com/windows/win32/seccertenroll/about-introduction-to-asn-1-syntax-and-encoding
package ber

import (
Expand All @@ -32,7 +33,7 @@ import (
// value is the interface for an ASN.1 value node.
type value interface {
// EncodeMetadata encodes the identifier and length in DER to the buffer.
EncodeMetadata(Writer) error
EncodeMetadata(writer) error

// EncodedLen returns the length in bytes of the data when encoding in DER.
EncodedLen() int
Expand Down
13 changes: 7 additions & 6 deletions internal/encoding/asn1/ber/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@

package ber

import (
"io"
)
import "io"

// Writer is the interface that wraps the basic Write and WriteByte methods.
type Writer interface {
// writer is the interface that wraps the basic Write and WriteByte methods.
type writer interface {
io.Writer
io.ByteWriter
}

// encodeLength encodes length octets in DER.
// Reference: ISO/IEC 8825-1: 10.1
shizhMSFT marked this conversation as resolved.
Show resolved Hide resolved
// Reference: https://learn.microsoft.com/windows/win32/seccertenroll/about-encoded-length-and-value-bytes
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
func encodeLength(w io.ByteWriter, length int) error {
// DER restriction: short form must be used for length less than 128
if length < 0x80 {
Expand All @@ -48,14 +47,16 @@ func encodeLength(w io.ByteWriter, length int) error {
// encodedLengthSize gives the number of octets used for encoding the length
// in DER.
// Reference: ISO/IEC 8825-1: 10.1
// Reference: https://learn.microsoft.com/windows/win32/seccertenroll/about-encoded-length-and-value-bytes
JeyJeyGao marked this conversation as resolved.
Show resolved Hide resolved
func encodedLengthSize(length int) int {
if length < 0x80 {
return 1
}

lengthSize := 1
for ; length > 0; lengthSize++ {
for length > 0 {
length >>= 8
lengthSize++
}
return lengthSize
}
2 changes: 1 addition & 1 deletion internal/encoding/asn1/ber/constructed.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type constructed struct {

// EncodeMetadata encodes the identifier and length octets of constructed
// to the value writer in DER.
func (v *constructed) EncodeMetadata(w Writer) error {
func (v *constructed) EncodeMetadata(w writer) error {
_, err := w.Write(v.identifier)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/encoding/asn1/ber/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type primitive struct {

// EncodeMetadata encodes the identifier and length octets of primitive to
// the value writer in DER.
func (v *primitive) EncodeMetadata(w Writer) error {
func (v *primitive) EncodeMetadata(w writer) error {
_, err := w.Write(v.identifier)
if err != nil {
return err
Expand Down