]> Cypherpunks.ru repositories - ucspi.git/blobdiff - x509.go
Raised copyright years
[ucspi.git] / x509.go
diff --git a/x509.go b/x509.go
index 8a5dd4eac910799dbb545a602184a4883fb15521..c8d9f9e1c234774f991c5127a567ce9c6da18d19 100644 (file)
--- a/x509.go
+++ b/x509.go
@@ -1,6 +1,6 @@
 /*
 ucspi -- UCSPI-related utilities
-Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2021-2022 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -30,17 +30,19 @@ func CertificateFromFile(p string) (b []byte, c *x509.Certificate, err error) {
        if err != nil {
                return
        }
-       block, data := pem.Decode(data)
-       if block == nil {
-               err = errors.New("can not decode PEM")
-               return
-       }
-       if block.Type != "CERTIFICATE" {
-               err = errors.New("non CERTIFICATE found in PEM")
-               return
+       var block *pem.Block
+       for len(data) > 0 {
+               block, data = pem.Decode(data)
+               if block == nil {
+                       continue
+               }
+               if block.Type == "CERTIFICATE" {
+                       b = block.Bytes
+                       c, err = x509.ParseCertificate(b)
+                       return
+               }
        }
-       b = block.Bytes
-       c, err = x509.ParseCertificate(b)
+       err = errors.New("no CERTIFICATE found in PEM")
        return
 }
 
@@ -50,24 +52,26 @@ func PrivateKeyFromFile(p string) (prv interface{}, err error) {
        if err != nil {
                return
        }
-       block, data := pem.Decode(data)
-       if block == nil {
-               err = errors.New("can not decode PEM")
-               return
-       }
-       data = block.Bytes
-       switch block.Type {
-       case "PRIVATE KEY":
-               prv, err = x509.ParsePKCS8PrivateKey(data)
-       case "EC PRIVATE KEY":
-               prv, err = x509.ParseECPrivateKey(data)
-       default:
-               err = errors.New("non PRIVATE KEY found in PEM")
+       var block *pem.Block
+       for len(data) > 0 {
+               block, data = pem.Decode(data)
+               if block == nil {
+                       continue
+               }
+               switch block.Type {
+               case "PRIVATE KEY":
+                       prv, err = x509.ParsePKCS8PrivateKey(block.Bytes)
+                       return
+               case "EC PRIVATE KEY":
+                       prv, err = x509.ParseECPrivateKey(block.Bytes)
+                       return
+               }
        }
+       err = errors.New("no PRIVATE KEY found in PEM")
        return
 }
 
-func CertPoolFromFile(p string) (pool *x509.CertPool, err error) {
+func CertPoolFromFile(p string) (certs []*x509.Certificate, pool *x509.CertPool, err error) {
        var data []byte
        data, err = ioutil.ReadFile(p)
        if err != nil {
@@ -90,6 +94,7 @@ func CertPoolFromFile(p string) (pool *x509.CertPool, err error) {
                if err != nil {
                        return
                }
+               certs = append(certs, ca)
                pool.AddCert(ca)
        }
        return