This repository has been archived by the owner on Nov 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathprofile.go
102 lines (90 loc) · 2.41 KB
/
profile.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
package kr
import (
"bytes"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"strings"
"golang.org/x/crypto/openpgp/armor"
"golang.org/x/crypto/openpgp/packet"
"golang.org/x/crypto/ssh"
)
type Profile struct {
SSHWirePublicKey []byte `json:"public_key_wire"`
Email string `json:"email"`
PGPPublicKey *[]byte `json:"pgp_pk,omitempty"`
TeamCheckpoint *TeamCheckpoint `json:"team_checkpoint,omitempty"`
}
func (p Profile) AuthorizedKeyString() (authString string, err error) {
pk, err := p.SSHPublicKey()
if err != nil {
return
}
authString = pk.Type() + " " + base64.StdEncoding.EncodeToString(p.SSHWirePublicKey) + " " + strings.Replace(p.Email, " ", "", -1)
return
}
func (p Profile) SSHPublicKey() (pk ssh.PublicKey, err error) {
return ssh.ParsePublicKey(p.SSHWirePublicKey)
}
func (p Profile) RSAPublicKey() (pk *rsa.PublicKey, err error) {
return SSHWireRSAPublicKeyToRSAPublicKey(p.SSHWirePublicKey)
}
func (p Profile) PublicKeyFingerprint() []byte {
digest := sha256.Sum256(p.SSHWirePublicKey)
return digest[:]
}
func (p Profile) IsOnTeam() bool {
return p.TeamCheckpoint != nil
}
func (p Profile) Equal(other Profile) bool {
return bytes.Equal(p.SSHWirePublicKey, other.SSHWirePublicKey) && p.Email == other.Email
}
var KRYPTONITE_ASCII_ARMOR_HEADERS = map[string]string{"Comment": "Created with Kryptonite"}
var KRYPTON_ASCII_ARMOR_HEADERS = map[string]string{"Comment": "Created with Krypton"}
func (p Profile) AsciiArmorPGPPublicKey() (s string, err error) {
if p.PGPPublicKey == nil {
err = fmt.Errorf("no pgp public key")
return
}
output := &bytes.Buffer{}
input, err := armor.Encode(output, "PGP PUBLIC KEY BLOCK", KRYPTON_ASCII_ARMOR_HEADERS)
if err != nil {
return
}
_, err = input.Write(*p.PGPPublicKey)
if err != nil {
return
}
err = input.Close()
if err != nil {
return
}
s = string(output.Bytes())
return
}
func (p Profile) PGPPublicKeySHA1Fingerprint() (s string, err error) {
if p.PGPPublicKey == nil {
err = fmt.Errorf("no pgp public key")
return
}
reader := bytes.NewReader(*p.PGPPublicKey)
for {
var pkt packet.Packet
pkt, err = packet.Read(reader)
if err != nil {
break
}
switch pkt := pkt.(type) {
case *packet.PublicKey:
digest := pkt.Fingerprint[:]
s = hex.EncodeToString(digest)
return
default:
continue
}
}
err = fmt.Errorf("no pgp public key packet found")
return
}