/
encode.go
53 lines (45 loc) · 1.51 KB
/
encode.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
// Copyright 2019, Oath Inc.
// Licensed under the terms of the Apache License 2.0. Please see LICENSE file in project root for terms.
package sshcert
import (
"fmt"
"time"
"github.com/theparanoids/crypki/proto"
"golang.org/x/crypto/ssh"
)
var supportAlgoNames = map[string]struct{}{
ssh.KeyAlgoRSA: {},
ssh.KeyAlgoDSA: {},
ssh.KeyAlgoECDSA256: {},
ssh.KeyAlgoECDSA384: {},
ssh.KeyAlgoECDSA521: {},
ssh.KeyAlgoSKECDSA256: {},
ssh.KeyAlgoED25519: {},
ssh.KeyAlgoSKED25519: {},
}
// DecodeRequest process the SSHCertificateSigningRequest and returns an (unsigned) SSH certificate.
func DecodeRequest(req *proto.SSHCertificateSigningRequest, sshCertType uint32) (*ssh.Certificate, error) {
publicKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(req.GetPublicKey()))
if err != nil {
return nil, fmt.Errorf("bad public key: %v", err)
}
if _, ok := supportAlgoNames[publicKey.Type()]; !ok {
return nil, fmt.Errorf("bad public key type: %v", publicKey.Type())
}
// Backdate start time by one hour as the current system clock may be ahead of other running systems.
start := uint64(time.Now().Unix())
end := start + req.GetValidity()
start -= 3600
return &ssh.Certificate{
KeyId: req.GetKeyId(),
CertType: sshCertType,
ValidPrincipals: req.GetPrincipals(),
Key: publicKey,
ValidAfter: start,
ValidBefore: end,
Permissions: ssh.Permissions{
Extensions: req.GetExtensions(),
CriticalOptions: req.GetCriticalOptions(),
},
}, nil
}