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

TrustStore: Implement crypto provider #3149

Merged
merged 4 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 18 additions & 1 deletion go/lib/infra/modules/trust/v2/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,35 @@ go_library(
"//go/lib/scrypto:go_default_library",
"//go/lib/scrypto/trc/v2:go_default_library",
"//go/lib/serrors:go_default_library",
"@org_golang_x_xerrors//:go_default_library",
],
)

go_test(
name = "go_default_test",
srcs = ["main_test.go"],
srcs = [
"export_test.go",
"main_test.go",
"provider_test.go",
],
data = [
"//go/lib/infra/modules/trust/v2/testdata:crypto_tar",
],
embed = [":go_default_library"],
deps = [
"//go/lib/addr:go_default_library",
"//go/lib/infra:go_default_library",
"//go/lib/infra/modules/trust/v2/internal/decoded:go_default_library",
"//go/lib/infra/modules/trust/v2/mock_v2:go_default_library",
"//go/lib/log:go_default_library",
"//go/lib/scrypto:go_default_library",
"//go/lib/scrypto/trc/v2:go_default_library",
"//go/lib/serrors:go_default_library",
"//go/lib/util:go_default_library",
"//go/lib/xtest:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
"@com_github_stretchr_testify//assert:go_default_library",
"@com_github_stretchr_testify//require:go_default_library",
"@org_golang_x_xerrors//:go_default_library",
],
)
32 changes: 32 additions & 0 deletions go/lib/infra/modules/trust/v2/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2019 Anapaya Systems
//
// 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 trust

// NewCryptoProvider allows instantiating the private cryptoProvider for
// black-box testing.
var NewCryptoProvider = newTestCryptoProvider

// newTestCryptoProvider returns a new crypto provider for testing.
func newTestCryptoProvider(db DBRead, recurser Recurser, resolver Resolver, router Router,
alwaysCacheOnly bool) CryptoProvider {

return &cryptoProvider{
db: db,
recurser: recurser,
resolver: resolver,
router: router,
alwaysCacheOnly: alwaysCacheOnly,
}
}
1 change: 1 addition & 0 deletions go/lib/infra/modules/trust/v2/internal/decoded/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ go_library(
deps = [
"//go/lib/scrypto/cert/v2:go_default_library",
"//go/lib/scrypto/trc/v2:go_default_library",
"//go/lib/serrors:go_default_library",
],
)
22 changes: 22 additions & 0 deletions go/lib/infra/modules/trust/v2/internal/decoded/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,37 @@ import (

"github.com/scionproto/scion/go/lib/scrypto/cert/v2"
"github.com/scionproto/scion/go/lib/scrypto/trc/v2"
"github.com/scionproto/scion/go/lib/serrors"
)

// ErrParse indicates that parsign failed.
var ErrParse = serrors.New("parse error")

// TRC is a container for the decoded TRC.
type TRC struct {
TRC *trc.TRC
Signed trc.Signed
Raw []byte
}

// DecodeTRC decodes the TRC.
func DecodeTRC(raw []byte) (TRC, error) {
signed, err := trc.ParseSigned(raw)
if err != nil {
return TRC{}, serrors.WithCtx(ErrParse, err, "part", "signed")
}
decoded, err := signed.EncodedTRC.Decode()
if err != nil {
return TRC{}, serrors.WithCtx(ErrParse, err, "part", "decode payload")
}
d := TRC{
TRC: decoded,
Signed: signed,
Raw: raw,
}
return d, nil
}

func (d TRC) String() string {
if d.TRC == nil {
return "<nil>"
Expand Down
52 changes: 52 additions & 0 deletions go/lib/infra/modules/trust/v2/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,53 @@ package trust_test

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/infra/modules/trust/v2/internal/decoded"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/scrypto"
"github.com/scionproto/scion/go/lib/scrypto/trc/v2"
"github.com/scionproto/scion/go/lib/xtest"
)

// tmpDir contains the generated crypto material.
var tmpDir string

type TRCDesc struct {
ISD addr.ISD
Version scrypto.Version
}

func (desc TRCDesc) File() string {
return fmt.Sprintf("ISD%d/trcs/ISD%d-V%d.trc", desc.ISD, desc.ISD, desc.Version)
}

var (
trc1v1 = TRCDesc{ISD: 1, Version: 1}

// primary ASes
ia110 = xtest.MustParseIA("1-ff00:0:110")
ia120 = xtest.MustParseIA("1-ff00:0:120")
ia130 = xtest.MustParseIA("1-ff00:0:130")
)

var (
trc2v1 = TRCDesc{ISD: 2, Version: 1}

// primary ASes
ia210 = xtest.MustParseIA("2-ff00:0:210")

// non-primary ASes
ia122 = xtest.MustParseIA("1-ff00:0:122")
)

func TestMain(m *testing.M) {
var cleanF func()
tmpDir, cleanF = xtest.MustTempDir("", "test-trust")
Expand All @@ -41,3 +77,19 @@ func TestMain(m *testing.M) {
log.Root().SetHandler(log.DiscardHandler())
os.Exit(m.Run())
}

func loadTRC(t *testing.T, desc TRCDesc) decoded.TRC {
t.Helper()
file := filepath.Join(tmpDir, desc.File())
raw, err := ioutil.ReadFile(file)
require.NoError(t, err)
signed, err := trc.ParseSigned(raw)
require.NoError(t, err)
trcObj, err := signed.EncodedTRC.Decode()
require.NoError(t, err)
return decoded.TRC{
Raw: raw,
Signed: signed,
TRC: trcObj,
}
}
16 changes: 16 additions & 0 deletions go/lib/infra/modules/trust/v2/mock_v2/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["v2.go"],
importpath = "github.com/scionproto/scion/go/lib/infra/modules/trust/v2/mock_v2",
visibility = ["//visibility:public"],
deps = [
"//go/lib/addr:go_default_library",
"//go/lib/infra/modules/trust/v2:go_default_library",
"//go/lib/infra/modules/trust/v2/internal/decoded:go_default_library",
"//go/lib/scrypto:go_default_library",
"//go/lib/scrypto/trc/v2:go_default_library",
"@com_github_golang_mock//gomock:go_default_library",
],
)
Loading