-
Notifications
You must be signed in to change notification settings - Fork 1
/
sshcert.go
61 lines (50 loc) · 1.58 KB
/
sshcert.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
package main
import (
"crypto"
"encoding/pem"
"fmt"
"github.com/awnumar/memguard"
"github.com/openpubkey/openpubkey/examples/ssh/sshcert"
"github.com/openpubkey/openpubkey/pktoken/clientinstance"
"golang.org/x/crypto/ssh"
)
func generateSshCert(token []byte, signer crypto.Signer, cic *clientinstance.Claims, issuer string) ([]byte, []byte, error) {
// Use the commitment nonce to complete the OIDC flow and get an ID token from the provider
// idToken, err := Op.RequestTokens(context.Background(), string(nonce))
idToken := memguard.NewBufferFromBytes(token)
defer idToken.Destroy()
// Sign over the payload from the ID token and client instance claims
cicToken, err := cic.Sign(signer, algo, idToken.Bytes())
if err != nil {
return nil, nil, fmt.Errorf("error creating cic token: %w", err)
}
pkt, err := OidcAuth(idToken, cicToken, &oidcPiperOP{
OIDCIssuer: issuer,
})
if err != nil {
return nil, nil, err
}
cert, err := sshcert.New(pkt, []string{})
if err != nil {
return nil, nil, err
}
sshSigner, err := ssh.NewSignerFromSigner(signer)
if err != nil {
return nil, nil, err
}
signerMas, err := ssh.NewSignerWithAlgorithms(sshSigner.(ssh.AlgorithmSigner), []string{ssh.KeyAlgoECDSA256})
if err != nil {
return nil, nil, err
}
sshCert, err := cert.SignCert(signerMas)
if err != nil {
return nil, nil, err
}
certBytes := ssh.MarshalAuthorizedKey(sshCert)
seckeySsh, err := ssh.MarshalPrivateKey(signer, "openpubkey cert")
if err != nil {
return nil, nil, err
}
seckeySshBytes := pem.EncodeToMemory(seckeySsh)
return seckeySshBytes, certBytes, nil
}