forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.go
126 lines (107 loc) · 3.49 KB
/
sign.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyright 2017 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.
package proto
import (
"fmt"
"time"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/util"
)
var _ Cerealizable = (*SignS)(nil)
type SignS struct {
Timestamp uint32
Type SignType
// Src holds the required metadata to verify the signature. The format is "STRING: METADATA".
// The prefix consists of "STRING: " and is required to match the regex "^\w+\: ".
// There are no format restrictions on the metadata.
Src common.RawBytes
Signature common.RawBytes
}
func NewSignS(type_ SignType, src common.RawBytes) *SignS {
return &SignS{Type: type_, Src: src}
}
func (s *SignS) Copy() *SignS {
if s == nil {
return nil
}
return &SignS{
Timestamp: s.Timestamp,
Type: s.Type,
Src: append(common.RawBytes(nil), s.Src...),
Signature: append(common.RawBytes(nil), s.Signature...),
}
}
// SetTimestamp sets the timestamp.
func (s *SignS) SetTimestamp(ts time.Time) {
s.Timestamp = util.TimeToSecs(ts)
}
// Time returns the timestamp. If the receiver is nil, the zero value is returned.
func (s *SignS) Time() time.Time {
if s != nil {
return util.SecsToTime(s.Timestamp)
}
return time.Time{}
}
// Pack serializes the signature metadata including the signature.
func (s *SignS) Pack() common.RawBytes {
return s.pack(nil, true)
}
// SigInput serializes the signature metadata to the signature input
// including the provided message. If setTimestamp is set, the timestamp of
// the signature metadata is updated to the current time, before creating
// the signature input. It should be true when signing to provide a recent
// timestamp. When verifying, it should be false to guarantee the same
// produced input.
func (s *SignS) SigInput(msg common.RawBytes, setTimestamp bool) common.RawBytes {
if setTimestamp {
s.SetTimestamp(time.Now())
}
return s.pack(msg, false)
}
// pack appends the type, src, signature (if needed) and timestamp fields to msg
func (s *SignS) pack(msg common.RawBytes, inclSig bool) common.RawBytes {
msg = append(common.RawBytes(nil), msg...)
msg = append(msg, common.RawBytes(s.Type.String())...)
msg = append(msg, s.Src...)
if inclSig {
msg = append(msg, s.Signature...)
}
t := make(common.RawBytes, 4)
common.Order.PutUint32(t, s.Timestamp)
return append(msg, t...)
}
func (s *SignS) ProtoId() ProtoIdType {
return Sign_TypeID
}
func (s *SignS) String() string {
return fmt.Sprintf("SignType: %s Timestamp: %s SignSrc: %s Signature: %s", s.Type,
util.TimeToString(s.Time()), s.Src, s.Signature)
}
var _ Cerealizable = (*SignedBlobS)(nil)
type SignedBlobS struct {
Blob common.RawBytes
Sign *SignS
}
func (sbs *SignedBlobS) Pack() common.RawBytes {
var raw common.RawBytes
raw = append(raw, sbs.Blob...)
raw = append(raw, sbs.Sign.Pack()...)
return raw
}
func (sbs *SignedBlobS) ProtoId() ProtoIdType {
return SignedBlob_TypeID
}
func (sbs *SignedBlobS) String() string {
return fmt.Sprintf("Blob: %s Sign: %s", sbs.Blob[:20], sbs.Sign)
}