-
Notifications
You must be signed in to change notification settings - Fork 2
/
x509certificate.go
124 lines (97 loc) · 2.63 KB
/
x509certificate.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package models
import (
"crypto/x509"
"database/sql/driver"
"encoding/base64"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"strings"
)
// --------------------------------------------
type X509Certificate x509.Certificate
func (c *X509Certificate) String() string {
res, err := c.MarshalJSON()
if err != nil {
return ""
}
certString := strings.ReplaceAll(string(res), "\"", "")
return string(certString)
}
func (c *X509Certificate) MarshalJSON() ([]byte, error) {
data := []byte{}
if c != nil {
pemCert := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: c.Raw})
data = make([]byte, base64.StdEncoding.EncodedLen(len(pemCert)))
base64.StdEncoding.Encode(data, pemCert)
return json.Marshal(string(data))
}
return json.Marshal(data)
}
func (c *X509Certificate) UnmarshalJSON(data []byte) error {
var decodedCert []byte
err := json.Unmarshal(data, &decodedCert)
if err != nil {
return err
}
certBlock, _ := pem.Decode(decodedCert)
if certBlock != nil {
certificate, err := x509.ParseCertificate(certBlock.Bytes)
if err != nil {
return err
}
*c = X509Certificate(*certificate)
return nil
}
return fmt.Errorf("missing cert block")
}
// --------------------------------------------
type X509CertificateRequest x509.CertificateRequest
func (c *X509CertificateRequest) MarshalJSON() ([]byte, error) {
data := []byte{}
if c != nil {
pemCert := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE REQUEST", Bytes: c.Raw})
data = make([]byte, base64.StdEncoding.EncodedLen(len(pemCert)))
base64.StdEncoding.Encode(data, pemCert)
return json.Marshal(string(data))
}
return json.Marshal(data)
}
func (c *X509CertificateRequest) UnmarshalJSON(data []byte) error {
var decodedCert []byte
err := json.Unmarshal(data, &decodedCert)
if err != nil {
return err
}
certBlock, _ := pem.Decode(decodedCert)
if certBlock != nil {
certificate, err := x509.ParseCertificateRequest(certBlock.Bytes)
if err != nil {
return err
}
*c = X509CertificateRequest(*certificate)
return nil
}
return fmt.Errorf("missing cert block")
}
// --------------------------------------------
func (X509Certificate) GormDataType() string {
return "text"
}
func (c *X509Certificate) Scan(value interface{}) error {
crtString, ok := value.(string)
if !ok {
return errors.New(fmt.Sprint("Failed to unmarshal JSONB value:", value))
}
crtBytes := []byte(fmt.Sprintf("\"%s\"", crtString))
err := json.Unmarshal(crtBytes, &c)
if err != nil {
return err
}
return nil
}
// Value return json value, implement driver.Valuer interface
func (c X509Certificate) Value() (driver.Value, error) {
return c.String(), nil
}