Maint: internal/openvpn/parse package
- Parse PEM key data for Cyberghost and VPNUnlimited - Add more unit tests
This commit is contained in:
27
internal/openvpn/parse/pem.go
Normal file
27
internal/openvpn/parse/pem.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package parse
|
||||
|
||||
import (
|
||||
"encoding/pem"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
errPEMDecode = errors.New("cannot decode PEM encoded block")
|
||||
)
|
||||
|
||||
func extractPEM(b []byte, name string) (encodedData string, err error) {
|
||||
name = strings.ToUpper(name) // certificate -> CERTIFICATE
|
||||
|
||||
pemBlock, _ := pem.Decode(b)
|
||||
if pemBlock == nil {
|
||||
return "", errPEMDecode
|
||||
}
|
||||
|
||||
encodedBytes := pem.EncodeToMemory(pemBlock)
|
||||
encodedData = string(encodedBytes)
|
||||
encodedData = strings.ReplaceAll(encodedData, "\n", "")
|
||||
encodedData = strings.TrimPrefix(encodedData, "-----BEGIN "+name+"-----")
|
||||
encodedData = strings.TrimSuffix(encodedData, "-----END "+name+"-----")
|
||||
return encodedData, nil
|
||||
}
|
||||
Reference in New Issue
Block a user