rsa/pkg/inspect/pem.go

45 lines
1.0 KiB
Go
Raw Normal View History

2023-03-01 21:28:48 +00:00
package inspect
import (
"bytes"
"encoding/pem"
"fmt"
"src.lwithers.me.uk/go/rsa/pkg/pemfile"
)
// LoadPEM returns a set of information about a PEM file.
func LoadPEM(data []byte) []Info {
var (
info []Info
line int = 1
)
for {
p, rest := pem.Decode(data)
if p == nil {
return info
}
loc := fmt.Sprintf("line %d", line)
switch p.Type {
case pemfile.TypePKCS1PrivateKey:
info = append(info, PKCS1PrivateKeyInfo(loc, p.Bytes))
case pemfile.TypePKCS8PrivateKey:
info = append(info, PKCS8PrivateKeyInfo(loc, p.Bytes))
case pemfile.TypePKCS1PublicKey:
info = append(info, PKCS1PublicKeyInfo(loc, p.Bytes))
case pemfile.TypePKIXPublicKey:
info = append(info, PKIXPublicKeyInfo(loc, p.Bytes))
case pemfile.TypeX509Certificate:
info = append(info, DERCertificateInfo(loc, p.Bytes))
case pemfile.TypeX509CRL:
info = append(info, DERCRLInfo(loc, p.Bytes))
case pemfile.TypeX509CSR:
info = append(info, DERCSRInfo(loc, p.Bytes))
}
line += bytes.Count(data[:len(data)-len(rest)], []byte{'\n'})
data = rest
}
}