-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcertificate.go
543 lines (475 loc) · 16.1 KB
/
certificate.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"log"
"math/big"
"net"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"software.sslmate.com/src/go-pkcs12"
)
func convertIPsToStrings(ips []net.IP) []string {
var ipStrings []string
for _, ip := range ips {
ipStrings = append(ipStrings, ip.String())
}
return ipStrings
}
func checkRootCertAndListCerts(c *gin.Context) {
log.Println("checkRootCertAndListCerts called")
// Get root certificate info
rootCert, err := readCertificate("data/root-cert/HomeLab_Root_CA.pem")
var rootCertInfo *x509.Certificate
if err == nil {
rootCertInfo = rootCert
}
// Check for self-signed certificate
selfSignedExists := false
if _, err := os.Stat("data/certmanager-cert/selfsigned.pem"); err == nil {
selfSignedExists = true
}
// Check for homelab certificate
homelabCertExists := false
homelabCert, err := readCertificate("data/certmanager-cert/homelab_certificate_manager.pem")
var homelabCertInfo *x509.Certificate
if err == nil {
homelabCertInfo = homelabCert
homelabCertExists = true
}
files, err := os.ReadDir("./data/certs")
if err != nil {
c.String(http.StatusInternalServerError, "Unable to read certificates directory: %v", err)
return
}
type CertInfo struct {
Name string
CreatedAt time.Time
ValidUntil time.Time
SANs []string
}
certs := []CertInfo{}
for _, file := range files {
if file.Type().IsRegular() && strings.HasSuffix(file.Name(), ".pem") {
name := strings.TrimSuffix(file.Name(), ".pem")
cert, err := readCertificate(filepath.Join("./data/certs", file.Name()))
if err != nil {
log.Printf("Error reading certificate %s: %v", file.Name(), err)
continue
}
certInfo := CertInfo{
Name: name,
CreatedAt: cert.NotBefore,
ValidUntil: cert.NotAfter,
SANs: append(cert.DNSNames, convertIPsToStrings(cert.IPAddresses)...),
}
certs = append(certs, certInfo)
}
}
var rootCertDetails map[string]interface{}
if rootCert != nil {
rootCertFile := "HomeLab_Root_CA.pem"
rootCertDetails = map[string]interface{}{
"CommonName": rootCert.Subject.CommonName,
"Issuer": rootCert.Issuer,
"Subject": rootCert.Subject,
"NotBefore": rootCert.NotBefore,
"NotAfter": rootCert.NotAfter,
"Filename": rootCertFile,
}
log.Printf("Root certificate details: %+v", rootCertDetails)
} else {
log.Printf("No root certificate found.")
}
isDefaultPassword := viper.GetString("password") == hashPassword("admin")
c.HTML(http.StatusOK, "cert_list.html", gin.H{
"defaultPassword": isDefaultPassword,
"rootCertificate": rootCertInfo,
"homelabCertificate": homelabCertInfo,
"certificates": certs,
"selfSignedExists": selfSignedExists,
"homelabCertExists": homelabCertExists,
})
}
func findRootCertAndKey() (*x509.Certificate, *rsa.PrivateKey, error) {
var rootCert *x509.Certificate
var rootKey *rsa.PrivateKey
err := filepath.Walk("data/root-cert", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
rootCertBytes, err := os.ReadFile(path)
if err != nil {
return err
}
block, _ := pem.Decode(rootCertBytes)
if block == nil {
return fmt.Errorf("failed to parse PEM block")
}
if strings.Contains(block.Type, "CERTIFICATE") {
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return fmt.Errorf("failed to parse root certificate: %v", err)
}
rootCert = cert
} else if strings.Contains(block.Type, "PRIVATE KEY") {
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return fmt.Errorf("failed to parse root key: %v", err)
}
rootKey = key
}
}
return nil
})
if err != nil {
return nil, nil, err
}
if rootCert == nil || rootKey == nil {
return nil, nil, fmt.Errorf("root certificate or key not found")
}
return rootCert, rootKey, nil
}
func createCertificate(c *gin.Context) {
log.Printf("createCertificate ...")
var form struct {
CommonName string `form:"common_name" binding:"required"`
ValidityYears string `form:"validity_years" binding:"required"`
Organization string `form:"organization"`
OrganizationUnit string `form:"organization_unit"`
Country string `form:"country"`
State string `form:"state"`
Location string `form:"location"`
Email string `form:"email"`
DnsNames []string `form:"dns"`
IpAddresses []string `form:"ip"`
Overwrite string `form:"overwrite"`
}
if err := c.ShouldBind(&form); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
validityYears, err := strconv.Atoi(form.ValidityYears)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid validity period"})
return
}
// Check if a certificate with the same common name already exists
certFilePath := "data/certs/" + form.CommonName + ".pem"
if _, err := os.Stat(certFilePath); err == nil {
// Certificate already exists, prompt user for confirmation
overwrite := c.PostForm("overwrite")
if overwrite != "yes" {
c.JSON(http.StatusOK, gin.H{"error": "A certificate with the same common name already exists. Do you want to overwrite it?"})
return
}
}
serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error generating serial number"})
return
}
// Create new certificate template
certTemplate := &x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
CommonName: form.CommonName,
Organization: []string{form.Organization},
OrganizationalUnit: []string{form.OrganizationUnit},
Country: []string{form.Country},
Province: []string{form.State},
Locality: []string{form.Location},
ExtraNames: []pkix.AttributeTypeAndValue{
{
Type: []int{1, 2, 840, 113549, 1, 9, 1},
Value: form.Email,
},
},
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(validityYears, 0, 0),
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
// Generate private key and certificate
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error generating private key"})
return
}
for _, dns := range form.DnsNames {
if dns != "" {
certTemplate.DNSNames = append(certTemplate.DNSNames, dns)
}
}
log.Printf("DNS-Names: %v", certTemplate.DNSNames)
for _, ip := range form.IpAddresses {
if parsedIP := net.ParseIP(ip); parsedIP != nil {
certTemplate.IPAddresses = append(certTemplate.IPAddresses, parsedIP)
}
}
log.Printf("IP Addresses: %v", certTemplate.IPAddresses)
rootCert, rootKey, err := findRootCertAndKey()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error finding root certificate or key"})
return
}
certBytes, err := x509.CreateCertificate(rand.Reader, certTemplate, rootCert, &privateKey.PublicKey, rootKey)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error creating certificate"})
return
}
certFile, err := os.Create(certFilePath)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error creating certificate file"})
return
}
defer certFile.Close()
if err := pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: certBytes}); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error encoding certificate file"})
return
}
keyFile, err := os.Create("data/certs/" + form.CommonName + ".key")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error creating key file"})
return
}
defer keyFile.Close()
if err := pem.Encode(keyFile, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Error encoding key file"})
return
}
// Create a .pfx file using Modern.Encode
privateKeyAsCrypto := crypto.PrivateKey(privateKey) // Convert to the appropriate interface
cert, err := x509.ParseCertificate(certBytes) // Parse the raw certificate bytes
if err != nil {
c.String(http.StatusInternalServerError, "Error parsing certificate: %v", err)
return
}
pfxData, err := pkcs12.Modern.Encode(privateKeyAsCrypto, cert, []*x509.Certificate{rootCert}, "")
if err != nil {
c.String(http.StatusInternalServerError, "Error creating .pfx file: %v", err)
return
}
pfxFile, err := os.Create("data/certs/" + form.CommonName + ".pfx")
if err != nil {
c.String(http.StatusInternalServerError, "Error creating .pfx file: %v", err)
return
}
defer pfxFile.Close()
_, err = pfxFile.Write(pfxData)
if err != nil {
c.String(http.StatusInternalServerError, "Error writing .pfx file: %v", err)
return
}
c.JSON(http.StatusOK, gin.H{"message": "Certificate created successfully", "redirect": "/certificates"})
}
func downloadCertificate(c *gin.Context) {
certType := c.Param("certType")
fileName := c.Param("filename")
var filePath string
switch certType {
case "root-cert":
filePath = filepath.Join("data", "root-cert", fileName)
case "certs":
filePath = filepath.Join("data", "certs", fileName)
default:
c.String(http.StatusBadRequest, "Invalid certificate type")
return
}
if _, err := os.Stat(filePath); os.IsNotExist(err) {
c.String(http.StatusNotFound, "File not found")
return
}
c.Header("Content-Description", "File Transfer")
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", fileName))
switch {
case strings.HasSuffix(fileName, ".pem"):
c.Header("Content-Type", "application/x-pem-file")
case strings.HasSuffix(fileName, ".key"):
c.Header("Content-Type", "application/x-pem-file")
case strings.HasSuffix(fileName, ".pfx"):
c.Header("Content-Type", "application/x-pkcs12")
default:
c.Header("Content-Type", "application/octet-stream")
}
c.File(filePath)
}
func deleteCertificate(c *gin.Context) {
fileName := c.Param("filename")
baseName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
certFilePath := "./data/certs/" + fileName
keyFilePath := "./data/certs/" + baseName + ".key"
pfxFilePath := "./data/certs/" + baseName + ".pfx"
// Delete the certificate file
err := os.Remove(certFilePath)
if err != nil {
c.String(http.StatusInternalServerError, "Error deleting certificate: %v", err)
return
}
// Delete the key file
err = os.Remove(keyFilePath)
if err != nil {
log.Printf("Warning: Error deleting key file: %v", err)
// Don't return here, continue with deleting other files
}
// Delete the pfx file
err = os.Remove(pfxFilePath)
if err != nil {
log.Printf("Warning: Error deleting pfx file: %v", err)
// Don't return here, continue with deleting other files
}
// Redirect to the certificates list page after deletion
c.Redirect(http.StatusSeeOther, "/certificates")
}
func viewCertificate(c *gin.Context) {
fileName := c.Param("filename")
var filePath string
// Special case for homelab certificate manager certificate
if fileName == "homelab_certificate_manager.pem" {
filePath = "data/certmanager-cert/" + fileName
} else {
filePath = "./data/certs/" + fileName
}
if !isValidFileName(fileName) {
c.String(http.StatusBadRequest, "Invalid file name")
return
}
// Check if the file exists
if _, err := os.Stat(filePath); os.IsNotExist(err) {
c.String(http.StatusNotFound, "File not found")
return
}
// Read and decode the certificate
cert, err := readCertificate(filePath)
if err != nil {
c.String(http.StatusInternalServerError, err.Error())
return
}
// Create a decoded view of the certificate
decoded := fmt.Sprintf(
"Issuer: %s\nSubject: %s\nValidity:\n Not Before: %s\n Not After : %s\n",
cert.Issuer, cert.Subject, cert.NotBefore, cert.NotAfter,
)
// Check for SubjectAlternativeName and IP Addresses
if len(cert.DNSNames) > 0 || len(cert.IPAddresses) > 0 {
decoded += "Subject Alternative Names:\n"
for _, dns := range cert.DNSNames {
decoded += fmt.Sprintf(" DNS: %s\n", dns)
}
for _, ip := range cert.IPAddresses {
decoded += fmt.Sprintf(" IP: %s\n", ip.String())
}
}
// Return the file content and decoded view as JSON
c.JSON(http.StatusOK, gin.H{
"decoded": decoded,
})
}
func isValidFileName(fileName string) bool {
// Implement validation logic here
return !strings.Contains(fileName, "..")
}
func readCertificate(filePath string) (*x509.Certificate, error) {
content, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("error reading certificate: %v", err)
}
block, _ := pem.Decode(content)
if block == nil {
return nil, fmt.Errorf("failed to parse certificate PEM")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse certificate: %v", err)
}
return cert, nil
}
func recreateHomelabCertificate(c *gin.Context) {
// First check if root certificate exists
rootCert, rootKey, err := loadRootCertAndKey()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Root certificate not found"})
return
}
// Generate new key pair
certKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate private key"})
return
}
// Create certificate template
template := x509.Certificate{
SerialNumber: big.NewInt(time.Now().Unix()),
Subject: pkix.Name{
CommonName: "HomeLab Certificate Manager",
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
// Load DNS names and IP addresses from settings
settingsDnsNames := viper.GetStringSlice("certificate_manager_certificate.dns_names")
log.Printf("Loaded DNS Names: %v", settingsDnsNames)
// Append DNS names from settings
for _, dns := range settingsDnsNames {
if dns != "" {
template.DNSNames = append(template.DNSNames, dns)
}
}
// Append IP addresses from settings
settingsIpAddresses := viper.GetStringSlice("certificate_manager_certificate.ip_addresses")
log.Printf("Loaded IP Addresses: %v", settingsIpAddresses)
for _, ip := range settingsIpAddresses {
if parsedIP := net.ParseIP(ip); parsedIP != nil {
template.IPAddresses = append(template.IPAddresses, parsedIP)
} else {
log.Printf("Invalid IP address: %s", ip)
}
}
// Load organization details from settings
template.Subject.Organization = []string{viper.GetString("general_cert_options.organization")}
template.Subject.OrganizationalUnit = []string{viper.GetString("general_cert_options.organization_unit")}
template.Subject.Country = []string{viper.GetString("general_cert_options.country")}
template.Subject.Province = []string{viper.GetString("general_cert_options.state")}
template.Subject.Locality = []string{viper.GetString("general_cert_options.location")}
// Create certificate signed by root CA
certBytes, err := x509.CreateCertificate(rand.Reader, &template, rootCert, &certKey.PublicKey, rootKey)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create certificate"})
return
}
// Save new private key
keyFile, err := os.Create("data/certmanager-cert/homelab_certificate_manager.key")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save private key"})
return
}
defer keyFile.Close()
pem.Encode(keyFile, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(certKey)})
// Save new certificate
certFile, err := os.Create("data/certmanager-cert/homelab_certificate_manager.pem")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save certificate"})
return
}
defer certFile.Close()
pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: certBytes})
c.JSON(http.StatusOK, gin.H{"message": "Certificate recreated successfully"})
}