2 ucspi -- UCSPI-related utilities
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 func CertificateFromFile(p string) (b []byte, c *x509.Certificate, err error) {
29 data, err = ioutil.ReadFile(p)
33 block, data := pem.Decode(data)
35 err = errors.New("can not decode PEM")
38 if block.Type != "CERTIFICATE" {
39 err = errors.New("non CERTIFICATE found in PEM")
43 c, err = x509.ParseCertificate(b)
47 func PrivateKeyFromFile(p string) (prv interface{}, err error) {
49 data, err = ioutil.ReadFile(p)
53 block, data := pem.Decode(data)
55 err = errors.New("can not decode PEM")
61 prv, err = x509.ParsePKCS8PrivateKey(data)
62 case "EC PRIVATE KEY":
63 prv, err = x509.ParseECPrivateKey(data)
65 err = errors.New("non PRIVATE KEY found in PEM")
70 func CertPoolFromFile(p string) (pool *x509.CertPool, err error) {
72 data, err = ioutil.ReadFile(p)
76 pool = x509.NewCertPool()
79 block, data = pem.Decode(data)
81 err = errors.New("can not decode PEM")
84 if block.Type != "CERTIFICATE" {
85 err = errors.New("non CERTIFICATE found in PEM")
88 var ca *x509.Certificate
89 ca, err = x509.ParseCertificate(block.Bytes)