Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit 0ff9d3c
Author: Junjie Gao <[email protected]>
Date:   Mon Jan 8 16:18:37 2024 +0800

    test: add unit test

    Signed-off-by: Junjie Gao <[email protected]>

commit 43f94bb
Author: Junjie Gao <[email protected]>
Date:   Tue Jan 2 10:10:22 2024 +0800

    fix: test

    Signed-off-by: Junjie Gao <[email protected]>

commit 1b83aa0
Author: Junjie Gao <[email protected]>
Date:   Tue Jan 2 10:08:55 2024 +0800

    fix: resolve comment

    Signed-off-by: Junjie Gao <[email protected]>

commit 5854500
Author: Junjie Gao <[email protected]>
Date:   Tue Dec 26 16:03:54 2023 +0800

    fix: remove cms

    Signed-off-by: Junjie Gao <[email protected]>

commit 5614972
Author: Junjie Gao <[email protected]>
Date:   Tue Dec 26 09:29:22 2023 +0800

    feat: added cms

    Signed-off-by: Junjie Gao <[email protected]>

commit 548c448
Author: Junjie Gao <[email protected]>
Date:   Mon Dec 25 09:56:57 2023 +0800

    fix: remove common errors

    Signed-off-by: Junjie Gao <[email protected]>

commit 9e8a32e
Author: Junjie Gao <[email protected]>
Date:   Mon Dec 25 09:55:36 2023 +0800

    fix: update

    Signed-off-by: Junjie Gao <[email protected]>

commit bc72eb6
Author: Junjie Gao <[email protected]>
Date:   Mon Dec 25 09:54:16 2023 +0800

    fix: update

    Signed-off-by: Junjie Gao <[email protected]>

commit aee8248
Author: Junjie Gao <[email protected]>
Date:   Mon Dec 25 09:51:59 2023 +0800

    fix: resolve comments

    Signed-off-by: Junjie Gao <[email protected]>

commit 49dd7a6
Author: Junjie Gao <[email protected]>
Date:   Wed Dec 20 17:38:31 2023 +0800

    fix: resolve comments

    Signed-off-by: Junjie Gao <[email protected]>

commit 148e0a9
Author: Junjie Gao <[email protected]>
Date:   Thu Dec 7 16:53:24 2023 +0800

    fix: update comment

    Signed-off-by: Junjie Gao <[email protected]>

commit 9f6e6b0
Author: Junjie Gao <[email protected]>
Date:   Thu Dec 7 16:47:10 2023 +0800

    fix: update test

    Signed-off-by: Junjie Gao <[email protected]>

commit 65bc051
Author: Junjie Gao <[email protected]>
Date:   Thu Dec 7 16:45:16 2023 +0800

    fix: optimize test

    Signed-off-by: Junjie Gao <[email protected]>

commit afe0aec
Author: Junjie Gao <[email protected]>
Date:   Thu Dec 7 16:03:10 2023 +0800

    fix: add license

    Signed-off-by: Junjie Gao <[email protected]>

commit aebb36d
Author: Junjie Gao <[email protected]>
Date:   Thu Dec 7 15:41:07 2023 +0800

    test: add unit test

    Signed-off-by: Junjie Gao <[email protected]>

commit 69d5c49
Author: Junjie Gao <[email protected]>
Date:   Wed Nov 29 16:52:49 2023 +0800

    Squashed commit of the following:

    commit 9240650
    Merge: 0c1ec3b 4198690
    Author: Junjie Gao <[email protected]>
    Date:   Wed Aug 9 17:07:34 2023 +0800

        Merge pull request #1 from JeyJeyGao/feat/ans1

        feat: convert BER to DER

    commit 4198690
    Author: Junjie Gao <[email protected]>
    Date:   Wed Aug 9 09:14:29 2023 +0800

        fix: simplify code

        Signed-off-by: Junjie Gao <[email protected]>

    commit 75ce02d
    Author: Junjie Gao <[email protected]>
    Date:   Mon Aug 7 20:33:08 2023 +0800

        fix: added Conetent method for value interface

        Signed-off-by: Junjie Gao <[email protected]>

    commit 7b823a9
    Author: Junjie Gao <[email protected]>
    Date:   Mon Aug 7 08:54:37 2023 +0800

        fix: update code

        Signed-off-by: Junjie Gao <[email protected]>

    commit 41ecec6
    Author: Junjie Gao <[email protected]>
    Date:   Sun Aug 6 17:33:19 2023 +0800

        fix: remove recusive call for encode()

        Signed-off-by: Junjie Gao <[email protected]>

    commit 8f1a2af
    Author: Junjie Gao <[email protected]>
    Date:   Fri Aug 4 13:40:09 2023 +0800

        fix: remove unused value

        Signed-off-by: Junjie Gao <[email protected]>

    commit 9b6a0c5
    Author: Junjie Gao <[email protected]>
    Date:   Thu Aug 3 20:25:22 2023 +0800

        fix: update code

        Signed-off-by: Junjie Gao <[email protected]>

    commit 91a3691
    Author: Junjie Gao <[email protected]>
    Date:   Thu Aug 3 20:11:28 2023 +0800

        fix: create pointer instead of value to improve performance

        Signed-off-by: Junjie Gao <[email protected]>

    commit 1465e3e
    Author: Junjie Gao <[email protected]>
    Date:   Thu Aug 3 20:04:44 2023 +0800

        fix: update code

        Signed-off-by: Junjie Gao <[email protected]>

    commit 6524a9c
    Author: Junjie Gao <[email protected]>
    Date:   Thu Aug 3 19:53:27 2023 +0800

        fix: update variable naming

        Signed-off-by: Junjie Gao <[email protected]>

    commit 6cfbd9c
    Author: Junjie Gao <[email protected]>
    Date:   Thu Aug 3 19:47:39 2023 +0800

        fix: update code

        Signed-off-by: Junjie Gao <[email protected]>

    commit b9c73bd
    Author: Junjie Gao <[email protected]>
    Date:   Thu Aug 3 17:56:52 2023 +0800

        fix: update to use rawContent instead of expectedLen

        Signed-off-by: Junjie Gao <[email protected]>

    commit 3c99402
    Author: Junjie Gao <[email protected]>
    Date:   Thu Aug 3 16:45:09 2023 +0800

        fix: update comment

        Signed-off-by: Junjie Gao <[email protected]>

    commit f4dc95f
    Author: Junjie Gao <[email protected]>
    Date:   Thu Aug 3 16:41:57 2023 +0800

        fix: resolve comment

        Signed-off-by: Junjie Gao <[email protected]>

    commit f916316
    Author: Junjie Gao <[email protected]>
    Date:   Thu Aug 3 16:40:37 2023 +0800

        fix: update code

        Signed-off-by: Junjie Gao <[email protected]>

    commit 22afdf8
    Author: Junjie Gao <[email protected]>
    Date:   Thu Aug 3 16:34:34 2023 +0800

        fix: resolve comment

        Signed-off-by: Junjie Gao <[email protected]>

    commit edb729c
    Author: Junjie Gao <[email protected]>
    Date:   Thu Aug 3 16:32:47 2023 +0800

        fix: resolve comment

        Signed-off-by: Junjie Gao <[email protected]>

    commit a8ba0ff
    Author: Junjie Gao <[email protected]>
    Date:   Thu Aug 3 16:26:29 2023 +0800

        fix: update code

        Signed-off-by: Junjie Gao <[email protected]>

    commit bc18cae
    Author: Junjie Gao <[email protected]>
    Date:   Thu Aug 3 16:14:57 2023 +0800

        fix: resolve comments

        Signed-off-by: Junjie Gao <[email protected]>

    commit 643f388
    Author: Junjie Gao <[email protected]>
    Date:   Thu Aug 3 09:17:39 2023 +0800

        fix: update comment

        Signed-off-by: Junjie Gao <[email protected]>

    commit b5d5131
    Author: Junjie Gao <[email protected]>
    Date:   Thu Aug 3 09:15:23 2023 +0800

        fix: expectedLen == 0 should continue

        Signed-off-by: Junjie Gao <[email protected]>

    commit 2345740
    Author: Junjie Gao <[email protected]>
    Date:   Wed Aug 2 13:01:38 2023 +0800

        fix: added copyright

        Signed-off-by: Junjie Gao <[email protected]>

    commit 936ba2b
    Author: Junjie Gao <[email protected]>
    Date:   Wed Aug 2 11:36:02 2023 +0800

        fix: remove recusive decoding

        Signed-off-by: Junjie Gao <[email protected]>

    commit 4fd944a
    Author: Junjie Gao <[email protected]>
    Date:   Tue Aug 1 21:50:10 2023 +0800

        fix: remove readOnlySlice

        Signed-off-by: Junjie Gao <[email protected]>

    commit efa7575
    Author: Junjie Gao <[email protected]>
    Date:   Tue Aug 1 09:38:57 2023 +0800

        fix: update decodeIdentifier function name

        Signed-off-by: Junjie Gao <[email protected]>

    commit cbce4c1
    Author: Junjie Gao <[email protected]>
    Date:   Tue Aug 1 09:25:34 2023 +0800

        fix: update code

        Signed-off-by: Junjie Gao <[email protected]>

    commit 45480e5
    Author: Junjie Gao <[email protected]>
    Date:   Mon Jul 31 21:22:20 2023 +0800

        fix: update code

        Signed-off-by: Junjie Gao <[email protected]>

    commit b3de155
    Author: Junjie Gao <[email protected]>
    Date:   Mon Jul 31 20:51:48 2023 +0800

        fix: set non-exportable type

        Signed-off-by: Junjie Gao <[email protected]>

    commit 5dea9e5
    Author: Junjie Gao <[email protected]>
    Date:   Mon Jul 31 20:44:50 2023 +0800

        feat: asn.1 first version

        Signed-off-by: Junjie Gao <[email protected]>

    Signed-off-by: Junjie Gao <[email protected]>

Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Jan 8, 2024
1 parent f1a72e8 commit 3d022f1
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 7 deletions.
2 changes: 1 addition & 1 deletion internal/crypto/cms/encoding/ber/ber.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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(*bytes.Buffer) error
EncodeMetadata(Writer) error

// EncodedLen returns the length in bytes of the data when encoding in DER.
EncodedLen() int
Expand Down
32 changes: 32 additions & 0 deletions internal/crypto/cms/encoding/ber/ber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,20 @@ func TestConvertToDER(t *testing.T) {
der: []byte{},
expectError: true,
},
{
name: "long form length greather than subsequent octets length ",
ber: []byte{
// Primitive value
// identifier
0x1f, 0xa0, 0x20,
// length
0x81, 0x09,
// content
0x01,
},
der: []byte{},
expectError: true,
},
}

for _, tt := range testData {
Expand All @@ -317,3 +331,21 @@ func TestConvertToDER(t *testing.T) {
}
}
}

func TestDecodeIdentifier(t *testing.T) {
t.Run("identifier is empty", func(t *testing.T) {
_, _, err := decodeIdentifier([]byte{})
if err == nil {
t.Errorf("decodeIdentifier() error = nil, but expect error")
}
})
}

func TestDecodeLength(t *testing.T) {
t.Run("length is empty", func(t *testing.T) {
_, _, err := decodeLength([]byte{})
if err == nil {
t.Errorf("decodeLength() error = nil, but expect error")
}
})
}
6 changes: 6 additions & 0 deletions internal/crypto/cms/encoding/ber/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import (
"io"
)

// 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
func encodeLength(w io.ByteWriter, length int) error {
Expand Down
4 changes: 1 addition & 3 deletions internal/crypto/cms/encoding/ber/constructed.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

package ber

import "bytes"

// constructed represents a value in constructed.
type constructed struct {
identifier []byte
Expand All @@ -25,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 *bytes.Buffer) error {
func (v *constructed) EncodeMetadata(w Writer) error {
_, err := w.Write(v.identifier)
if err != nil {
return err
Expand Down
56 changes: 56 additions & 0 deletions internal/crypto/cms/encoding/ber/constructed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright The Notary Project Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ber

import (
"errors"
"testing"
)

type errorWriter struct{}

func (ew *errorWriter) Write(p []byte) (n int, err error) {
return 0, errors.New("write error")
}

func (ew *errorWriter) WriteByte(c byte) error {
return errors.New("write error")
}

func TestConstructedEncodeMetadata(t *testing.T) {
tests := []struct {
name string
v constructed
wantError bool
}{
{
name: "Error case",
v: constructed{
identifier: []byte{0x30},
length: 5,
},
wantError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &errorWriter{}
err := tt.v.EncodeMetadata(w)
if (err != nil) != tt.wantError {
t.Errorf("EncodeMetadata() error = %v, wantError %v", err, tt.wantError)
}
})
}
}
4 changes: 1 addition & 3 deletions internal/crypto/cms/encoding/ber/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

package ber

import "bytes"

// primitive represents a value in primitive encoding.
type primitive struct {
identifier []byte
Expand All @@ -23,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 *bytes.Buffer) error {
func (v *primitive) EncodeMetadata(w Writer) error {
_, err := w.Write(v.identifier)
if err != nil {
return err
Expand Down
44 changes: 44 additions & 0 deletions internal/crypto/cms/encoding/ber/primitive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright The Notary Project Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package ber

import (
"testing"
)

func TestPrimitiveEncodeMetadata(t *testing.T) {
tests := []struct {
name string
v primitive
wantError bool
}{
{
name: "Error case",
v: primitive{
identifier: []byte{0x30},
},
wantError: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := &errorWriter{}
err := tt.v.EncodeMetadata(w)
if (err != nil) != tt.wantError {
t.Errorf("EncodeMetadata() error = %v, wantError %v", err, tt.wantError)
}
})
}
}

0 comments on commit 3d022f1

Please sign in to comment.