logicalis / asn1 Goto Github PK
View Code? Open in Web Editor NEWAsn.1 BER and DER encoding library for golang.
License: MIT License
Asn.1 BER and DER encoding library for golang.
License: MIT License
Hi,
Does anyone have any larger examples of the struct creation from an ASN1 Definition. I am 70% through a very large example and I am 100% sure I have got it wrong.
If anyone is able to advise please on how they would translate the following (I have trimmed allot to capture the key points)
ASN.1 Formal Description
INIncoming ::= SET{
triggerData0 [5] IMPLICIT TriggerData OPTIONAL
}
TriggerData ::= SEQUENCE{
triggerDetectionPoint [0] IMPLICIT TriggerDetectionPoint,
sCPAddress CHOICE{
co-located [2] IMPLICIT NULL,
}
I translated to :
type TriggerData struct {
TriggerDetectionPoint []byte asn1:"implicit,tag:0"
//
SCPAddress interface{} `asn1:"implicit,choice:SCPAddress"` //
}
type INIncoming struct {
Triggerdata0 TriggerData asn1:"implicit,optional,tag:5"
/
}
ctx.AddChoice("SCPAddress", []asn1.Choice{
{
Type: CoLocated,
Options: "implicit,tag:2"
},
{
Type: PointCodeAndSubSystemNumber,
Options: "implicit,tag:3",
},
{
Type: globalTitle,
Options: "implicit,tag:5",
},
{
Type: globalTitleAndSubSystemNumber,
Options: "implicit,tag:5",
},
})
Kind regards,
I have another example I don't know how to add. My format description shows (shortened example)
UMTSGSMPLMNCallDataRecord ::= SEQUENCE{
callModule CHOICE{
transit [0] IMPLICIT Transit,
mSOriginating [1] IMPLICIT MSOriginating
}
}
Transit ::= SET{
tAC [0] IMPLICIT TAC OPTIONAL,
callIdentificationNumber [1] IMPLICIT CallIDNumber OPTIONAL
}
I translated this to ....
ctx.AddChoice("CallModule", []asn1.Choice{
{
Type: reflect.TypeOf(Transit),
Options: "implicit,tag:0",
},
{
Type: MSOriginating,
Options: "implicit,tag:1",
}
}
type Transit struct {
TAC []byte asn1:"implicit,optional,tag:0"
// OCTET STRING (SIZE 3..4)
CallIDNumber []byte asn1:"implicit,optional,tag:1"
// OCTET STRING (SIZE 3)
}
But the compiler doesn't like that arrangement. 'type Transit is not an expression'
Any pointers please would be helpful.
Currently, the Decode
and DecodeWithOptions
methods require an object to be passed where the decoded data will be put in place.
However, if the top-level element is a CHOICE
, the instance should be created by using the context.
I don't know the implementation well, but if you don't have time to take a look at this, please let me know, I could give it a shot.
F.e.
Fulfillment ::= CHOICE {
preimage PreimageFulfillment ,
prefix PrefixFulfillment,
threshold ThresholdFulfillment,
rsaSha256 RsaSha256Fulfillment,
ed25519 Ed25519Fulfillment
}
Currently universal and explicit tagging are supported, but not implicit tagging.
Hello,
I am new to the go and apologize in advance if I am asking a silly question.
I have downloaded and trying run 'go build' and I seem to be able to successfully build. But I do not see any executable to run. Do we have an application to test this library with takes ber/der files as input?
Consider this spec:
Fulfillment ::= CHOICE {
preimageSha256 [0] PreimageFulfillment ,
prefixSha256 [1] PrefixFulfillment,
thresholdSha256 [2] ThresholdFulfillment,
rsaSha256 [3] RsaSha256Fulfillment,
ed25519Sha256 [4] Ed25519Sha512Fulfillment
}
ThresholdFulfillment ::= SEQUENCE {
subfulfillments SET OF Fulfillment,
subconditions SET OF Condition
}
So in order to get a SET, we need the set
ASN1 tag on the struct field. But we must also indicate what Go types are possible for the Fulfillment
interface.
However, with the following code, asn1 tries to match the CHOICE type (some struct that implements the Fulfillment
interface) against the SubFulfillments
field, which of course does not match the []Fulfillment
type.
type FfThresholdSha256 struct {
SubFulfillments []Fulfillment `asn1:"tag:0,explicit,set,choice:fulfillment"`
SubConditions []*Condition `asn1:"tag:1,explicit,set,choice:condition"`
}
Thus, asn1 should recognize that when a choice it put on a SET or a SEQUENCE, it should match the elements inside the set or the sequence instead of that field itself.
The specific error you get is
invalid Go type '[]cryptoconditions.Fulfillment' for choice 'fulfillment'
I'm trying to encode an ed25519.PublicKey
(type PublicKey []byte
) and I get the following panic.
For some reason, asn1 seems to expect a []uint8
and does not accept a []byte
.
panic: interface conversion: interface is ed25519.PublicKey, not []uint8
goroutine 1 [running]:
panic(0x4c0160, 0xc420016140)
/usr/lib/go/src/runtime/panic.go:500 +0x1a1
github.com/PromonLogicalis/asn1.(*Context).encodeOctetString(0xc42000e4e0, 0x4bdb00, 0xc42000e4c0, 0x97, 0x40e518, 0x30, 0x4cb5e0, 0x1, 0xc420010420)
/home/steven/gocode/src/github.com/PromonLogicalis/asn1/types.go:188 +0x273
github.com/PromonLogicalis/asn1.(*Context).(github.com/PromonLogicalis/asn1.encodeOctetString)-fm(0x4bdb00, 0xc42000e4c0, 0x97, 0x4bfaa0, 0x4bdb00, 0x0, 0x0, 0x0)
/home/steven/gocode/src/github.com/PromonLogicalis/asn1/encode.go:127 +0x48
github.com/PromonLogicalis/asn1.(*Context).encodeValue(0xc42000e4e0, 0x4bdb00, 0xc42000e4c0, 0x97, 0xc42000e500, 0x1, 0x1, 0x1)
/home/steven/gocode/src/github.com/PromonLogicalis/asn1/encode.go:139 +0x131
github.com/PromonLogicalis/asn1.(*Context).encode(0xc42000e4e0, 0x4bdb00, 0xc42000e4c0, 0x97, 0xc42000e500, 0x30, 0x0, 0x556c01)
/home/steven/gocode/src/github.com/PromonLogicalis/asn1/encode.go:59 +0xd5
github.com/PromonLogicalis/asn1.(*Context).EncodeWithOptions(0xc42000e4e0, 0x4bdb00, 0xc42000e4c0, 0x0, 0x0, 0xc42000e4c0, 0x18, 0x18, 0x1ce8e2fed22701, 0xc42000e4c0)
/home/steven/gocode/src/github.com/PromonLogicalis/asn1/encode.go:28 +0xa4
github.com/PromonLogicalis/asn1.Encode(0x4bdb00, 0xc42000e4c0, 0xc42000e4c0, 0x4bdb00, 0xc42000e4c0, 0xc420016100, 0x40)
/home/steven/gocode/src/github.com/PromonLogicalis/asn1/asn1.go:42 +0x191
main.main()
/home/steven/gocode/src/github.com/me/repo/main.go:18 +0x1cc
exit status 2
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.