Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
3ab331ed
Commit
3ab331ed
authored
Nov 19, 2009
by
Adam Langley
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
asn1: add support for RawContent
R=rsc CC=golang-dev
https://golang.org/cl/157056
parent
cc56fc38
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
1 deletion
+46
-1
asn1.go
src/pkg/asn1/asn1.go
+20
-1
asn1_test.go
src/pkg/asn1/asn1_test.go
+26
-0
No files found.
src/pkg/asn1/asn1.go
View file @
3ab331ed
...
...
@@ -349,6 +349,11 @@ type RawValue struct {
Bytes
[]
byte
;
}
// RawContent is used to signal that the undecoded, DER data needs to be
// preserved for a struct. To use it, the first field of the struct must have
// this type. It's an error for any of the other fields to have this type.
type
RawContent
[]
byte
// Tagging
// parseTagAndLength parses an ASN.1 tag and length pair from the given offset
...
...
@@ -460,6 +465,7 @@ var (
objectIdentifierType
=
reflect
.
Typeof
(
ObjectIdentifier
{});
timeType
=
reflect
.
Typeof
(
&
time
.
Time
{});
rawValueType
=
reflect
.
Typeof
(
RawValue
{});
rawContentsType
=
reflect
.
Typeof
(
RawContent
(
nil
));
)
// invalidLength returns true iff offset + length > sliceLength, or if the
...
...
@@ -594,7 +600,7 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
if
ok
{
offset
=
initOffset
}
else
{
err
=
StructuralError
{
fmt
.
Sprintf
(
"tags don't match (%d vs %+v) %+v %s
%#v"
,
expectedTag
,
t
,
params
,
fieldType
.
Name
(),
bytes
[
offset
:
len
(
bytes
)]
)}
err
=
StructuralError
{
fmt
.
Sprintf
(
"tags don't match (%d vs %+v) %+v %s
@%d"
,
expectedTag
,
t
,
params
,
fieldType
.
Name
(),
offset
)}
}
return
;
}
...
...
@@ -662,9 +668,19 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
return
;
case
*
reflect
.
StructValue
:
structType
:=
fieldType
.
(
*
reflect
.
StructType
);
if
structType
.
NumField
()
>
0
&&
structType
.
Field
(
0
)
.
Type
==
rawContentsType
{
bytes
:=
bytes
[
initOffset
:
offset
+
t
.
length
];
val
.
Field
(
0
)
.
SetValue
(
reflect
.
NewValue
(
RawContent
(
bytes
)));
}
innerOffset
:=
0
;
for
i
:=
0
;
i
<
structType
.
NumField
();
i
++
{
field
:=
structType
.
Field
(
i
);
if
i
==
0
&&
field
.
Type
==
rawContentsType
{
continue
}
innerOffset
,
err
=
parseField
(
val
.
Field
(
i
),
innerBytes
,
innerOffset
,
parseFieldParameters
(
field
.
Tag
));
if
err
!=
nil
{
return
...
...
@@ -763,6 +779,9 @@ func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool) {
// [explicit] tag:x specifies the ASN.1 tag number; implies ASN.1 CONTEXT SPECIFIC
// default:x sets the default value for optional integer fields
//
// If the type of the first field of a structure is RawContent then the raw
// ASN1 contents of the struct will be stored in it.
//
// Other ASN.1 types are not supported; if it encounters them,
// Unmarshal returns a parse error.
func
Unmarshal
(
val
interface
{},
b
[]
byte
)
(
rest
[]
byte
,
err
os
.
Error
)
{
...
...
src/pkg/asn1/asn1_test.go
View file @
3ab331ed
...
...
@@ -354,6 +354,32 @@ func TestCertificateWithNUL(t *testing.T) {
}
}
type
rawStructTest
struct
{
Raw
RawContent
;
A
int
;
}
func
TestRawStructs
(
t
*
testing
.
T
)
{
var
s
rawStructTest
;
input
:=
[]
byte
{
0x30
,
0x03
,
0x02
,
0x01
,
0x50
};
rest
,
err
:=
Unmarshal
(
&
s
,
input
);
if
len
(
rest
)
!=
0
{
t
.
Errorf
(
"incomplete parse: %x"
,
rest
);
return
;
}
if
err
!=
nil
{
t
.
Error
(
err
);
return
;
}
if
s
.
A
!=
0x50
{
t
.
Errorf
(
"bad value for A: got %d want %d"
,
s
.
A
,
0x50
)
}
if
bytes
.
Compare
([]
byte
(
s
.
Raw
),
input
)
!=
0
{
t
.
Errorf
(
"bad value for Raw: got %x want %x"
,
s
.
Raw
,
input
)
}
}
var
derEncodedSelfSignedCert
=
Certificate
{
TBSCertificate
:
TBSCertificate
{
Version
:
0
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment