Maint: internal/openvpn/parse package

- Parse PEM key data for Cyberghost and VPNUnlimited
- Add more unit tests
This commit is contained in:
Quentin McGaw (desktop)
2021-09-08 16:40:19 +00:00
parent 9afe455635
commit 65ace12def
11 changed files with 321 additions and 212 deletions

View 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
}