1
0
Fork 0
mirror of https://github.com/Luzifer/go-dhparam.git synced 2024-11-08 15:20:03 +00:00

Add additional errors for Decode (#4)

Co-authored-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
brent s. 2024-09-20 03:55:10 -04:00 committed by GitHub
parent 348aee6368
commit a42823ee10
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

14
dh.go
View file

@ -4,10 +4,17 @@ import (
"bytes"
"encoding/asn1"
"encoding/pem"
"errors"
"fmt"
"math/big"
)
// ErrNoPem is returned if pemData for the Decode function is nil or empty.
var ErrNoPem = errors.New("empty or nil bytes for PEM data")
// ErrInvalidPem is returned if pemData for the Decode function does not seem to be PEM-encoded data.
var ErrInvalidPem = errors.New("invalid bytes for PEM data; does not seem to be PEM-encoded")
// DH contains a prime (P) and a generator (G) number representing the DH parameters
type DH struct {
P *big.Int
@ -16,7 +23,14 @@ type DH struct {
// Decode reads a DH parameters struct from its PEM data
func Decode(pemData []byte) (*DH, error) {
if pemData == nil || len(pemData) == 0 {
return nil, ErrNoPem
}
blk, _ := pem.Decode(pemData)
if blk == nil {
return nil, ErrInvalidPem
}
out := &DH{}
if _, err := asn1.Unmarshal(blk.Bytes, out); err != nil {