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

TLS LogRootV1 Format #1037

Merged
merged 9 commits into from
Mar 9, 2018
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: 0 additions & 1 deletion crypto/data_formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
)

// This file contains struct specific mappings and data structures.
// TODO(gdbelvin): remove data-structure specific operations.

// Constants used as map keys when building input for ObjectHash. They must not be changed
// as this will change the output of hashRoot()
Expand Down
186 changes: 119 additions & 67 deletions trillian.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions trillian.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ import "google/protobuf/any.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/timestamp.proto";


// LogRootFormat specifies the fields that are covered by the
// SignedLogRoot signature, as well as their ordering and formats.
enum LogRootFormat {
LOG_ROOT_FORMAT_UNKNOWN = 0;
LOG_ROOT_FORMAT_V1 = 1;
}

// MapRootFormat specifies the fields that are covered by the
// SignedMapRoot signature, as well as their ordering and formats.
enum MapRootFormat {
MAP_ROOT_FORMAT_UNKNOWN = 0;
MAP_ROOT_FORMAT_V1 = 1;
}


// What goes in here?
// Things which are exposed through the public trillian APIs.

Expand Down
87 changes: 87 additions & 0 deletions types/logroot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// Copyright 2018 Google Inc. All Rights Reserved.
//
// 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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a package comment in exactly one of the files in types ? (Some stricter linters check for this.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

// Package types defines serialization and parsing functions for SignedLogRoot
// and SignedMapRoot fields.
package types

import (
"encoding/binary"
"fmt"

"github.com/google/certificate-transparency-go/tls"

"github.com/google/trillian"
)

// LogRootV1 holds the TLS-deserialization of the following structure
// (described in RFC5246 section 4 notation):
// struct {
// uint64 tree_size;
// opaque root_hash<0..128>;
// uint64 timestamp_nanos;
// uint64 revision;
// opaque metadata<0..65535>;
// } LogRootV1;
type LogRootV1 struct {
TreeSize uint64
RootHash []byte `tls:"minlen:0,maxlen:128"`
TimestampNanos uint64
Revision uint64
Metadata []byte `tls:"minlen:0,maxlen:65535"`
}

// LogRoot holds the TLS-deserialization of the following structure
// (described in RFC5246 section 4 notation):
// enum { v1(1), (65535)} Version;
// struct {
// Version version;
// select(version) {
// case v1: LogRootV1;
// }
// } LogRoot;
type LogRoot struct {
Version tls.Enum `tls:"size:2"`
V1 *LogRootV1 `tls:"selector:Version,val:1"`
}

// UnmarshalBinary verifies that logRootBytes is a TLS serialized LogRoot, has
// the LOG_ROOT_FORMAT_V1 tag, and populates the caller with the deserialized
// *LogRootV1.
func (l *LogRootV1) UnmarshalBinary(logRootBytes []byte) error {
if logRootBytes == nil {
return fmt.Errorf("nil log root")
}
version := binary.BigEndian.Uint16(logRootBytes)
if version != uint16(trillian.LogRootFormat_LOG_ROOT_FORMAT_V1) {
return fmt.Errorf("invalid LogRoot.Version: %v, want %v",
version, trillian.LogRootFormat_LOG_ROOT_FORMAT_V1)
}

var logRoot LogRoot
if _, err := tls.Unmarshal(logRootBytes, &logRoot); err != nil {
return err
}

*l = *logRoot.V1
return nil
}

// MarshalBinary returns a canonical TLS serialization of LogRoot.
func (l *LogRootV1) MarshalBinary() ([]byte, error) {
return tls.Marshal(LogRoot{
Version: tls.Enum(trillian.LogRootFormat_LOG_ROOT_FORMAT_V1),
V1: l,
})
}
Loading