forked from gokalkan/gokalkan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_certs.go
45 lines (35 loc) · 957 Bytes
/
load_certs.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
package gokalkan
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"github.com/olegmlsn/gokalkan/ckalkan"
)
var ErrLoadKey = errors.New("load key error")
func (cli *Client) LoadCerts() error {
for _, v := range cli.o.Certs {
if err := cli.LoadCertFromBytes(v.Cert.Raw, v.Type); err != nil {
return err
}
}
return nil
}
func (cli *Client) LoadCertFromBytes(cert []byte, certType ckalkan.CertType) (err error) {
tmpCert, err := os.CreateTemp("", "tmp.cert.*.crt")
if err != nil {
return fmt.Errorf("%w: %s", ErrLoadKey, err)
}
filename := tmpCert.Name()
defer os.Remove(filename)
defer tmpCert.Close()
written, err := io.Copy(tmpCert, bytes.NewReader(cert))
if err != nil {
return fmt.Errorf("%w: %s", ErrLoadKey, err)
}
if exp := int64(len(cert)); exp != written {
return fmt.Errorf("%w: expected %d bytes, but written %d bytes", ErrLoadKey, exp, written)
}
return cli.kc.X509LoadCertificateFromFile(filename, certType)
}