-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_request.go
More file actions
75 lines (68 loc) · 1.7 KB
/
Copy pathmodel_request.go
File metadata and controls
75 lines (68 loc) · 1.7 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
/*
* Copyright (c) 2024-2026 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/
package pki
import (
"bytes"
"crypto"
"crypto/x509"
"fmt"
"os"
)
type Request struct {
Key crypto.Signer
Csr *x509.CertificateRequest
}
func (c *Request) SaveKey(filepath string) error {
if c == nil || c.Key == nil {
return fmt.Errorf("no private key provided")
}
b, err := MarshalKeyPEM(c.Key)
if err != nil {
return fmt.Errorf("marshal private key: %w", err)
}
err = os.WriteFile(filepath, b, 0600)
if err != nil {
return fmt.Errorf("save key to '%s': %w", filepath, err)
}
return nil
}
func (c *Request) SaveCert(filepath string) error {
if c == nil || c.Csr == nil {
return fmt.Errorf("no certificate request provided")
}
b, err := MarshalCsrPEM(*c.Csr)
if err != nil {
return fmt.Errorf("marshal certificate request: %w", err)
}
err = os.WriteFile(filepath, b, 0644)
if err != nil {
return fmt.Errorf("save certificate request to '%s': %w", filepath, err)
}
return nil
}
func (c *Request) LoadKey(filepath string) error {
b, err := os.ReadFile(filepath)
if err != nil {
return fmt.Errorf("load private key from '%s': %w", filepath, err)
}
if bytes.Contains(b, pemEndLine) {
c.Key, err = UnmarshalKeyPEM(b)
} else {
c.Key, err = UnmarshalKeyDER(b)
}
return err
}
func (c *Request) LoadCert(filepath string) error {
b, err := os.ReadFile(filepath)
if err != nil {
return fmt.Errorf("load certificate request from '%s': %w", filepath, err)
}
if bytes.Contains(b, pemEndLine) {
c.Csr, err = UnmarshalCsrPEM(b)
} else {
c.Csr, err = UnmarshalCsrDER(b)
}
return err
}