]> Cypherpunks.ru repositories - ucspi.git/blob - x509.go
Initial commit
[ucspi.git] / x509.go
1 /*
2 ucspi -- UCSPI-related utilities
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 package ucspi
19
20 import (
21         "crypto/x509"
22         "encoding/pem"
23         "errors"
24         "io/ioutil"
25 )
26
27 func CertificateFromFile(p string) (b []byte, c *x509.Certificate, err error) {
28         var data []byte
29         data, err = ioutil.ReadFile(p)
30         if err != nil {
31                 return
32         }
33         block, data := pem.Decode(data)
34         if block == nil {
35                 err = errors.New("can not decode PEM")
36                 return
37         }
38         if block.Type != "CERTIFICATE" {
39                 err = errors.New("non CERTIFICATE found in PEM")
40                 return
41         }
42         b = block.Bytes
43         c, err = x509.ParseCertificate(b)
44         return
45 }
46
47 func PrivateKeyFromFile(p string) (prv interface{}, err error) {
48         var data []byte
49         data, err = ioutil.ReadFile(p)
50         if err != nil {
51                 return
52         }
53         block, data := pem.Decode(data)
54         if block == nil {
55                 err = errors.New("can not decode PEM")
56                 return
57         }
58         data = block.Bytes
59         switch block.Type {
60         case "PRIVATE KEY":
61                 prv, err = x509.ParsePKCS8PrivateKey(data)
62         case "EC PRIVATE KEY":
63                 prv, err = x509.ParseECPrivateKey(data)
64         default:
65                 err = errors.New("non PRIVATE KEY found in PEM")
66         }
67         return
68 }
69
70 func CertPoolFromFile(p string) (pool *x509.CertPool, err error) {
71         var data []byte
72         data, err = ioutil.ReadFile(p)
73         if err != nil {
74                 return
75         }
76         pool = x509.NewCertPool()
77         var block *pem.Block
78         for len(data) > 0 {
79                 block, data = pem.Decode(data)
80                 if block == nil {
81                         err = errors.New("can not decode PEM")
82                         return
83                 }
84                 if block.Type != "CERTIFICATE" {
85                         err = errors.New("non CERTIFICATE found in PEM")
86                         return
87                 }
88                 var ca *x509.Certificate
89                 ca, err = x509.ParseCertificate(block.Bytes)
90                 if err != nil {
91                         return
92                 }
93                 pool.AddCert(ca)
94         }
95         return
96 }