-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
infoData.go
46 lines (38 loc) · 1.11 KB
/
infoData.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
package grpc
import (
"crypto/x509"
"encoding/pem"
"errors"
"fmt"
"regexp"
)
func parseUUID(commonName string) string {
m := regexp.MustCompile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
return m.FindString(commonName)
}
type infoData struct {
CertificateCommonName string
CertificateCommonNameID string
}
func (d infoData) String() string {
return fmt.Sprintf("certificateCommonName=%v, certificateCommonNameID=%v", d.CertificateCommonName, d.CertificateCommonNameID)
}
// PrepareSignRequest can create a CertificateRequest struct with all the required data filled
func getInfoData(csr []byte) (infoData, error) {
csrBlock, _ := pem.Decode(csr)
if csrBlock == nil {
return infoData{}, errors.New("pem not found")
}
certificateRequest, err := x509.ParseCertificateRequest(csrBlock.Bytes)
if err != nil {
return infoData{}, err
}
err = certificateRequest.CheckSignature()
if err != nil {
return infoData{}, err
}
return infoData{
CertificateCommonName: certificateRequest.Subject.CommonName,
CertificateCommonNameID: parseUUID(certificateRequest.Subject.CommonName),
}, nil
}