mirror of
https://github.com/Luzifer/go-dhparam.git
synced 2024-11-08 23:30:02 +00:00
Add additional errors for Decode (#4)
Co-authored-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
348aee6368
commit
a42823ee10
1 changed files with 14 additions and 0 deletions
14
dh.go
14
dh.go
|
@ -4,10 +4,17 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/asn1"
|
"encoding/asn1"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"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
|
// DH contains a prime (P) and a generator (G) number representing the DH parameters
|
||||||
type DH struct {
|
type DH struct {
|
||||||
P *big.Int
|
P *big.Int
|
||||||
|
@ -16,7 +23,14 @@ type DH struct {
|
||||||
|
|
||||||
// Decode reads a DH parameters struct from its PEM data
|
// Decode reads a DH parameters struct from its PEM data
|
||||||
func Decode(pemData []byte) (*DH, error) {
|
func Decode(pemData []byte) (*DH, error) {
|
||||||
|
if pemData == nil || len(pemData) == 0 {
|
||||||
|
return nil, ErrNoPem
|
||||||
|
}
|
||||||
|
|
||||||
blk, _ := pem.Decode(pemData)
|
blk, _ := pem.Decode(pemData)
|
||||||
|
if blk == nil {
|
||||||
|
return nil, ErrInvalidPem
|
||||||
|
}
|
||||||
|
|
||||||
out := &DH{}
|
out := &DH{}
|
||||||
if _, err := asn1.Unmarshal(blk.Bytes, out); err != nil {
|
if _, err := asn1.Unmarshal(blk.Bytes, out); err != nil {
|
||||||
|
|
Loading…
Reference in a new issue