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

Drkey port pr4 #7

Merged
merged 7 commits into from
May 11, 2020
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
1 change: 1 addition & 0 deletions go/lib/ctrl/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ go_library(
"//go/lib/common:go_default_library",
"//go/lib/ctrl/ack:go_default_library",
"//go/lib/ctrl/cert_mgmt:go_default_library",
"//go/lib/ctrl/drkey_mgmt:go_default_library",
"//go/lib/ctrl/extn:go_default_library",
"//go/lib/ctrl/ifid:go_default_library",
"//go/lib/ctrl/path_mgmt:go_default_library",
Expand Down
26 changes: 26 additions & 0 deletions go/lib/ctrl/ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl/cert_mgmt"
"github.com/scionproto/scion/go/lib/ctrl/drkey_mgmt"
"github.com/scionproto/scion/go/lib/ctrl/path_mgmt"
"github.com/scionproto/scion/go/proto"
)
Expand Down Expand Up @@ -59,6 +60,16 @@ func NewCertMgmtPld(u proto.Cerealizable, certD *cert_mgmt.Data, ctrlD *Data) (*
return NewPld(cpld, ctrlD)
}

// NewDRKeyMgmtPld creates a new control payload, containing a new drkey_mgmt payload,
// which in turn contains the supplied Cerealizable instance.
func NewDRKeyMgmtPld(u proto.Cerealizable, drkeyD *drkey_mgmt.Data, ctrlD *Data) (*Pld, error) {
kpld, err := drkey_mgmt.NewPld(u, drkeyD)
if err != nil {
return nil, err
}
return NewPld(kpld, ctrlD)
}

func NewPldFromRaw(b common.RawBytes) (*Pld, error) {
p := &Pld{Data: &Data{}}
return p, proto.ParseFromRaw(p, b)
Expand Down Expand Up @@ -98,6 +109,21 @@ func (p *Pld) GetPathMgmt() (*path_mgmt.Pld, *Data, error) {
return pathP, p.Data, nil
}

// GetDRKeyMgmt returns the DRKeyMgmt payload and the CtrlPld's non-union Data.
// If the union type is not DRKeyMgmt, an error is returned.
func (p *Pld) GetDRKeyMgmt() (*drkey_mgmt.Pld, *Data, error) {
u, err := p.Union()
if err != nil {
return nil, nil, err
}
drkeyP, ok := u.(*drkey_mgmt.Pld)
if !ok {
return nil, nil, common.NewBasicError("Non-matching ctrl pld contents", nil,
"expected", "*drkey_mgmt.Pld", "actual", common.TypeOf(u))
}
return drkeyP, p.Data, nil
}

func (p *Pld) Len() int {
return -1
}
Expand Down
22 changes: 22 additions & 0 deletions go/lib/ctrl/drkey_mgmt/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = [
"drkey_lvl1_rep.go",
"drkey_lvl1_req.go",
"drkey_lvl2_rep.go",
"drkey_lvl2_req.go",
"drkey_mgmt.go",
],
importpath = "github.com/scionproto/scion/go/lib/ctrl/drkey_mgmt",
visibility = ["//visibility:public"],
deps = [
"//go/lib/addr:go_default_library",
"//go/lib/common:go_default_library",
"//go/lib/drkey:go_default_library",
"//go/lib/scrypto:go_default_library",
"//go/lib/util:go_default_library",
"//go/proto:go_default_library",
],
)
66 changes: 66 additions & 0 deletions go/lib/ctrl/drkey_mgmt/drkey_lvl1_rep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2018 ETH Zurich
//
// 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.

// This file contains the Go representation of first order DRKey responses.

package drkey_mgmt

import (
"fmt"
"time"

"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/drkey"
"github.com/scionproto/scion/go/lib/scrypto"
"github.com/scionproto/scion/go/lib/util"
"github.com/scionproto/scion/go/proto"
)

var _ proto.Cerealizable = (*Lvl1Rep)(nil)

// Lvl1Rep represents the lvel 1 reply from one CS to another.
type Lvl1Rep struct {
DstIARaw addr.IAInt `capnp:"dstIA"`
EpochBegin uint32
EpochEnd uint32
Cipher []byte
Nonce []byte
CertVerDst scrypto.Version
TimestampRaw uint32 `capnp:"timestamp"`
}

// DstIA returns the source ISD-AS of the DRKey.
func (c *Lvl1Rep) DstIA() addr.IA {
return c.DstIARaw.IA()
}

// ProtoId returns the proto ID.
func (c *Lvl1Rep) ProtoId() proto.ProtoIdType {
return proto.DRKeyLvl1Rep_TypeID
}

// Epoch returns the begin and end of the validity period of DRKey.
func (c *Lvl1Rep) Epoch() drkey.Epoch {
return drkey.NewEpoch(c.EpochBegin, c.EpochEnd)
}

// Timestamp returns the time when this reply was created.
func (c *Lvl1Rep) Timestamp() time.Time {
return util.SecsToTime(c.TimestampRaw)
}

func (c *Lvl1Rep) String() string {
return fmt.Sprintf("Timestamp: %v DstIA: %v EpochBegin: %d EpochEnd: %d CertVerEnc: %d",
util.TimeToCompact(c.Timestamp()), c.DstIA(), c.EpochBegin, c.EpochEnd, c.CertVerDst)
}
69 changes: 69 additions & 0 deletions go/lib/ctrl/drkey_mgmt/drkey_lvl1_req.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright 2018 ETH Zurich
//
// 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.

// This file contains the Go representation of first order DRKey requests.

package drkey_mgmt

import (
"fmt"
"time"

"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/util"
"github.com/scionproto/scion/go/proto"
)

var _ proto.Cerealizable = (*Lvl1Req)(nil)

// Lvl1Req represents a level 1 request between certificate servers.
type Lvl1Req struct {
DstIARaw addr.IAInt `capnp:"dstIA"`
ValTimeRaw uint32 `capnp:"valTime"`
TimestampRaw uint32 `capnp:"timestamp"`
}

// NewLvl1Req creates a new level 1 request struct.
func NewLvl1Req(dstIA addr.IA, valTime uint32) Lvl1Req {
return Lvl1Req{
DstIARaw: dstIA.IAInt(),
ValTimeRaw: valTime,
TimestampRaw: uint32(time.Now().Unix()),
}
}

// DstIA returns the source ISD-AS of the requested DRKey.
func (c *Lvl1Req) DstIA() addr.IA {
return c.DstIARaw.IA()
}

// ProtoId returns the proto ID
func (c *Lvl1Req) ProtoId() proto.ProtoIdType {
return proto.DRKeyLvl1Req_TypeID
}

// ValTime returns the validity time of the requested DRKey.
func (c *Lvl1Req) ValTime() time.Time {
return util.SecsToTime(c.ValTimeRaw)
}

// Timestamp returns the time when this request was created.
func (c *Lvl1Req) Timestamp() time.Time {
return util.SecsToTime(c.TimestampRaw)
}

func (c *Lvl1Req) String() string {
return fmt.Sprintf("Timestamp: %v DstIA: %s ValTime: %v",
util.TimeToCompact(c.Timestamp()), c.DstIA(), util.TimeToCompact(c.ValTime()))
}
84 changes: 84 additions & 0 deletions go/lib/ctrl/drkey_mgmt/drkey_lvl2_rep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2018 ETH Zurich
//
// 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.

// This file contains the Go representation of first order DRKey responses.

package drkey_mgmt

import (
"fmt"
"time"

"github.com/scionproto/scion/go/lib/drkey"
"github.com/scionproto/scion/go/lib/util"
"github.com/scionproto/scion/go/proto"
)

var _ proto.Cerealizable = (*Lvl2Rep)(nil)

// Lvl2Rep encodes the level 2 key response from a CS to an endhost.
type Lvl2Rep struct {
TimestampRaw uint32 `capnp:"timestamp"`
DRKeyRaw []byte `capnp:"drkey"`
EpochBegin uint32
EpochEnd uint32
Misc []byte
}

// NewLvl2RepFromKey constructs a level 2 response from a standard level 2 key.
func NewLvl2RepFromKey(key drkey.Lvl2Key, timestamp time.Time) *Lvl2Rep {
return &Lvl2Rep{
TimestampRaw: util.TimeToSecs(timestamp),
DRKeyRaw: []byte(key.Key),
EpochBegin: util.TimeToSecs(key.Epoch.NotBefore.Time),
EpochEnd: util.TimeToSecs(key.Epoch.NotAfter.Time),
}
}

// ProtoId returns the proto ID.
func (c *Lvl2Rep) ProtoId() proto.ProtoIdType {
return proto.DRKeyLvl2Rep_TypeID
}

// Epoch returns the begin and end of the validity period of DRKey.
func (c *Lvl2Rep) Epoch() drkey.Epoch {
return drkey.NewEpoch(c.EpochBegin, c.EpochEnd)
}

// ToKey returns a drkey Lvl2 built from these values.
func (c *Lvl2Rep) ToKey(meta drkey.Lvl2Meta) drkey.Lvl2Key {

return drkey.Lvl2Key{
Lvl2Meta: drkey.Lvl2Meta{
Epoch: c.Epoch(),
SrcIA: meta.SrcIA,
DstIA: meta.DstIA,
KeyType: meta.KeyType,
Protocol: meta.Protocol,
SrcHost: meta.SrcHost,
DstHost: meta.DstHost,
},
Key: drkey.DRKey(c.DRKeyRaw),
}
}

// Timestamp returns the time when this reply was created.
func (c *Lvl2Rep) Timestamp() time.Time {
return util.SecsToTime(c.TimestampRaw)
}

func (c *Lvl2Rep) String() string {
return fmt.Sprintf("Timestamp: %v EpochBegin: %d EpochEnd: %d Misc: %v",
util.TimeToCompact(c.Timestamp()), c.EpochBegin, c.EpochEnd, c.Misc)
}
Loading