forked from google/certificate-transparency-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logid.go
89 lines (79 loc) · 2.97 KB
/
logid.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
// Copyright 2017 Google LLC. 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.
// Package logid provides a type and accompanying helpers for manipulating log IDs.
package logid
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"log"
)
// LogID is a unique identifier for a CT Log derived from its public key as described by RFC6962
// sect. 3.2. Since it is derived from a SHA-256 hash, its length is fixed at 32 bytes.
type LogID [sha256.Size]byte
// FromBytes returns a LogID copied from the supplied byte slice.
func FromBytes(bytes []byte) (LogID, error) {
var logID LogID
if len(bytes) != sha256.Size {
return logID, fmt.Errorf("FromBytes(%x): want %d bytes, got %d", bytes, sha256.Size, len(bytes))
}
copy(logID[:], bytes)
return logID, nil
}
// FromB64 returns a LogID from parsing the supplied base64-encoded Log ID.
func FromB64(logIDB64 string) (LogID, error) {
buf, err := base64.StdEncoding.DecodeString(logIDB64)
if err != nil {
return LogID{}, err
}
return FromBytes(buf[:])
}
// FromB64OrDie returns a LogID from parsing supplied base64-encoded data that we assert is
// already well-formed, so it 'cannot fail'.
func FromB64OrDie(logIDB64 string) LogID {
logID, err := FromB64(logIDB64)
if err != nil {
log.Fatalf("FromB64(%q): %v", logIDB64, err)
}
return logID
}
// FromPubKeyB64 takes a base64 encoded DER public key, and converts it into
// a LogID, as defined in RFC6962 - i.e. the SHA-256 hash of the base64 decoded
// bytes of the log's public key.
func FromPubKeyB64(pubKeyB64 string) (LogID, error) {
pkBytes, err := base64.StdEncoding.DecodeString(pubKeyB64)
if err != nil {
return LogID{}, fmt.Errorf("error decoding public key %q from base64: %s", pubKeyB64, err)
}
return LogID(sha256.Sum256(pkBytes)), nil
}
// FromPubKeyB64OrDie takes a base64 encoded DER public key, and converts it
// into a LogID, as defined in RFC6962 - i.e. the sha256 hash of the base64
// decoded bytes of the log's public key. This is for data that we assert is
// already well-formed, so it 'cannot fail'.
func FromPubKeyB64OrDie(pubKeyB64 string) LogID {
logID, err := FromPubKeyB64(pubKeyB64)
if err != nil {
log.Fatalf("FromPubKeyB64(%q): %v", pubKeyB64, err)
}
return logID
}
// Bytes returns the raw bytes of the LogID, as a slice.
func (l LogID) Bytes() []byte {
return l[:]
}
// String base64-encodes a LogID for ease of debugging.
func (l LogID) String() string {
return fmt.Sprintf("logid:[%s]", base64.StdEncoding.EncodeToString(l.Bytes()))
}