@@ -5,8 +5,10 @@ import (
5
5
"crypto/x509"
6
6
"encoding/pem"
7
7
"errors"
8
- "gorm.io/gorm "
8
+ "strings "
9
9
"time"
10
+
11
+ "gorm.io/gorm"
10
12
)
11
13
12
14
// This file contains the operations for the Domain model.
@@ -33,7 +35,7 @@ func (domain *Domain) FindById(_ context.Context, db gorm.DB, id uint) error {
33
35
}
34
36
35
37
func (domain * Domain ) Create (_ context.Context , db gorm.DB ) error {
36
- err := domain .fillSSLInfo ()
38
+ err := domain .validateAndFillSSLInfo ()
37
39
if err != nil {
38
40
return err
39
41
}
@@ -42,7 +44,7 @@ func (domain *Domain) Create(_ context.Context, db gorm.DB) error {
42
44
}
43
45
44
46
func (domain * Domain ) Update (_ context.Context , db gorm.DB ) error {
45
- err := domain .fillSSLInfo ()
47
+ err := domain .validateAndFillSSLInfo ()
46
48
if err != nil {
47
49
return err
48
50
}
@@ -66,16 +68,58 @@ func (domain *Domain) Delete(_ context.Context, db gorm.DB) error {
66
68
67
69
func (domain * Domain ) UpdateSSLStatus (_ context.Context , db gorm.DB , status DomainSSLStatus ) error {
68
70
domain .SSLStatus = status
69
- tx := db .Where ("id = ?" , domain .ID ).Update ("ssl_status" , status )
71
+ tx := db .Model ( & domain ). Where ("id = ?" , domain .ID ).Update ("ssl_status" , status )
70
72
return tx .Error
71
73
}
72
74
73
- func (domain * Domain ) fillSSLInfo () error {
75
+ func (domain * Domain ) validateAndFillSSLInfo () error {
74
76
if domain == nil || domain .SSLFullChain == "" {
75
77
return nil
76
78
}
79
+
80
+ // if ssl full chain or private key is missing \n at the end , add it
81
+ if ! strings .HasSuffix (domain .SSLFullChain , "\n " ) {
82
+ domain .SSLFullChain = domain .SSLFullChain + "\n "
83
+ }
84
+ if ! strings .HasSuffix (domain .SSLPrivateKey , "\n " ) {
85
+ domain .SSLPrivateKey = domain .SSLPrivateKey + "\n "
86
+ }
87
+
88
+ // validate private key
89
+ keyBytes := []byte (domain .SSLPrivateKey )
90
+ block , _ := pem .Decode (keyBytes )
91
+ if block == nil {
92
+ return errors .New ("failed to decode SSL private key" )
93
+ }
94
+ // Attempt parsing the key as any supported private key format
95
+ isValidated := false
96
+ _ , err := x509 .ParsePKCS8PrivateKey (block .Bytes )
97
+ if err == nil {
98
+ isValidated = true // Key is valid PKCS8
99
+ }
100
+
101
+ if ! isValidated {
102
+
103
+ _ , err = x509 .ParsePKCS1PrivateKey (block .Bytes )
104
+ if err == nil {
105
+ isValidated = true // Key is valid PKCS1
106
+ }
107
+ }
108
+
109
+ if ! isValidated {
110
+ _ , err = x509 .ParseECPrivateKey (block .Bytes )
111
+ if err == nil {
112
+ isValidated = true // Key is valid EC
113
+ }
114
+ }
115
+
116
+ if ! isValidated {
117
+ return errors .New ("provided private keys is not a valid private key (RSA, PKCS8, PKCS1, or EC)" )
118
+ }
119
+
120
+ // validate full chain certificate
77
121
certBytes := []byte (domain .SSLFullChain )
78
- block , _ : = pem .Decode (certBytes )
122
+ block , _ = pem .Decode (certBytes )
79
123
if block == nil {
80
124
return errors .New ("failed to decode SSL full chain certificate" )
81
125
}
0 commit comments