Skip to content

Commit

Permalink
add FromObjectIdentifer(
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz834 committed Jun 16, 2023
1 parent ebbb889 commit af2876d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/encoding/asn1/asn1.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,25 @@ func parseObjectIdentifier(bytes []byte) (s ObjectIdentifier, err error) {
return
}

var errInvalidDEROid = errors.New("invalid DER Object Identifier encoding")
var (
errInvalidDEROid = errors.New("invalid DER Object Identifier encoding")
errInvalidOID = errors.New("invalid oid")
)

type OID struct {
der []byte
}

func FromObjectIdentifer(oid ObjectIdentifier) (OID, error) {
enc, err := makeObjectIdentifier(oid)
if err != nil {
return OID{}, errInvalidOID
}
der := make([]byte, enc.Len())
enc.Encode(der)
return OID{der}, nil
}

// ParseOID parsed DER-encoded Object Identifier.
// On success, der is referenced in the OID struct.
// der must not be modified after parsing.
Expand Down
10 changes: 10 additions & 0 deletions src/encoding/asn1/asn1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1239,5 +1239,15 @@ func TestOID(t *testing.T) {
if ok && !o.Equal(v.oid) {
t.Errorf("%v: after ToObjectIdentifer, is not equal to %v", v.raw, v.oid)
}

if v.oid != nil {
oid2, err := FromObjectIdentifer(v.oid)
if err != nil {
t.Errorf("%v: failed while creating OID from ObjectIdentifier: %v", v.raw, err)
}
if !oid2.Equal(oid) {
t.Errorf("%v: OID from ObjectIdentifier is not equal to oid", v.raw)
}
}
}
}

0 comments on commit af2876d

Please sign in to comment.