forked from keys-pub/keys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyring.go
More file actions
137 lines (123 loc) · 4.02 KB
/
Copy pathkeyring.go
File metadata and controls
137 lines (123 loc) · 4.02 KB
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
127
128
129
130
131
132
133
134
135
136
137
package keys
import (
"encoding/json"
"time"
"github.com/keys-pub/keys/keyring"
"github.com/pkg/errors"
)
const (
certificateItemType string = "cert-key-x509v3"
)
// NewX25519KeyItem creates keyring item for X25519Key.
func NewX25519KeyItem(key *X25519Key) *keyring.Item {
item := keyring.NewItem(key.ID().String(), key.PrivateKey()[:], string(X25519), time.Now())
return item
}
// AsX25519Key returns X25519Key for keyring Item.
// If item is EdX25519Key returns converted to X25519Key.
func AsX25519Key(item *keyring.Item) (*X25519Key, error) {
switch item.Type {
case string(X25519):
bk := NewX25519KeyFromPrivateKey(Bytes32(item.Data))
return bk, nil
case string(EdX25519):
sk, err := AsEdX25519Key(item)
if err != nil {
return nil, err
}
return sk.X25519Key(), nil
default:
return nil, errors.Errorf("item type %s != %s", item.Type, string(X25519))
}
}
// NewEdX25519KeyItem creates keyring item for EdX25519Key.
func NewEdX25519KeyItem(key *EdX25519Key) *keyring.Item {
item := keyring.NewItem(key.ID().String(), key.PrivateKey()[:], string(EdX25519), time.Now())
return item
}
// AsEdX25519Key returns EdX25519Key for keyring Item.
func AsEdX25519Key(item *keyring.Item) (*EdX25519Key, error) {
if item.Type != string(EdX25519) {
return nil, errors.Errorf("item type %s != %s", item.Type, string(EdX25519))
}
b := item.Data
if len(b) != 64 {
return nil, errors.Errorf("invalid number of bytes for ed25519 private key")
}
key := NewEdX25519KeyFromPrivateKey(Bytes64(b))
return key, nil
}
// NewEdX25519PublicKeyItem creates keyring item for EdX25519PublicKey.
func NewEdX25519PublicKeyItem(publicKey *EdX25519PublicKey) *keyring.Item {
item := keyring.NewItem(publicKey.ID().String(), publicKey.Bytes()[:], string(EdX25519Public), time.Now())
return item
}
// AsEdX25519PublicKey returns EdX25519PublicKey for keyring Item.
func AsEdX25519PublicKey(item *keyring.Item) (*EdX25519PublicKey, error) {
switch item.Type {
case string(EdX25519Public):
b := item.Data
if len(b) != 32 {
return nil, errors.Errorf("invalid number of bytes for ed25519 public key")
}
key := NewEdX25519PublicKey(Bytes32(b))
return key, nil
case string(EdX25519):
sk, err := AsEdX25519Key(item)
if err != nil {
return nil, err
}
return sk.PublicKey(), nil
default:
return nil, errors.Errorf("invalid item type for edx25519 public key: %s", item.Type)
}
}
// NewX25519PublicKeyItem creates keyring item for X25519PublicKey.
func NewX25519PublicKeyItem(publicKey *X25519PublicKey) *keyring.Item {
item := keyring.NewItem(publicKey.ID().String(), publicKey.Bytes()[:], string(X25519Public), time.Now())
return item
}
// AsX25519PublicKey returns X25519PublicKey for keyring Item.
func AsX25519PublicKey(item *keyring.Item) (*X25519PublicKey, error) {
switch item.Type {
case string(X25519Public):
b := item.Data
if len(b) != 32 {
return nil, errors.Errorf("invalid number of bytes for x25519 public key")
}
key := NewX25519PublicKey(Bytes32(b))
return key, nil
case string(X25519):
bk, err := AsX25519Key(item)
if err != nil {
return nil, err
}
return bk.PublicKey(), nil
default:
return nil, errors.Errorf("invalid item type for box public key: %s", item.Type)
}
}
type certFormat struct {
Private string `json:"private"`
Public string `json:"public"`
}
// NewCertificateKeyItem creates an Item for a certificate private key.
func NewCertificateKeyItem(id string, certKey *CertificateKey) *keyring.Item {
b, err := json.Marshal(certFormat{Private: certKey.private, Public: certKey.public})
if err != nil {
panic(err)
}
item := keyring.NewItem(id, b, certificateItemType, time.Now())
return item
}
// AsCertificateKey returns CertificateKey for keyring Item.
func AsCertificateKey(item *keyring.Item) (*CertificateKey, error) {
if item.Type != certificateItemType {
return nil, errors.Errorf("item type %s != %s", item.Type, certificateItemType)
}
var cert certFormat
if err := json.Unmarshal(item.Data, &cert); err != nil {
return nil, err
}
return NewCertificateKey(cert.Private, cert.Public)
}