Skip to content

Commit

Permalink
NOISSUE - Fix SANs (#71)
Browse files Browse the repository at this point in the history
* fix ipaddress

Signed-off-by: nyagamunene <[email protected]>

* fix options parameter

Signed-off-by: nyagamunene <[email protected]>

* remove ipaddress in dns names

Signed-off-by: nyagamunene <[email protected]>

* add check during ip parse

Signed-off-by: nyagamunene <[email protected]>

---------

Signed-off-by: nyagamunene <[email protected]>
  • Loading branch information
nyagamunene authored Jan 27, 2025
1 parent 91270de commit fb0da07
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions certs.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ type SubjectOptions struct {
Locality []string `json:"locality"`
StreetAddress []string `json:"street_address"`
PostalCode []string `json:"postal_code"`
DnsNames []string `json:"dns_names"`
}

type Config struct {
Expand Down
1 change: 1 addition & 0 deletions sdk/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type Options struct {
Locality []string `json:"locality"`
StreetAddress []string `json:"street_address"`
PostalCode []string `json:"postal_code"`
DnsNames []string `json:"dns_names"`
}

type Token struct {
Expand Down
14 changes: 13 additions & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"encoding/asn1"
"encoding/pem"
"math/big"
"net"
"time"

"github.com/absmach/certs/errors"
Expand Down Expand Up @@ -57,6 +58,7 @@ var (
ErrPrivKeyType = errors.New("unsupported private key type")
ErrPubKeyType = errors.New("unsupported public key type")
ErrFailedParse = errors.New("failed to parse key PEM")
ErrInvalidIP = errors.New("invalid IP address")
)

type service struct {
Expand Down Expand Up @@ -146,6 +148,15 @@ func (s *service) issue(ctx context.Context, entityID, ttl string, ipAddrs []str
}
}

var ipArray []net.IP
for _, ip := range ipAddrs {
parsedIP := net.ParseIP(ip)
if parsedIP == nil {
return Certificate{}, errors.Wrap(ErrMalformedEntity, ErrInvalidIP)
}
ipArray = append(ipArray, parsedIP)
}

template := x509.Certificate{
SerialNumber: serialNumber,
Subject: subject,
Expand All @@ -155,7 +166,8 @@ func (s *service) issue(ctx context.Context, entityID, ttl string, ipAddrs []str
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: false,
DNSNames: append(s.intermediateCA.Certificate.DNSNames, ipAddrs...),
DNSNames: append(s.intermediateCA.Certificate.DNSNames, options.DnsNames...),
IPAddresses: ipArray,
}

var privKeyBytes []byte
Expand Down

0 comments on commit fb0da07

Please sign in to comment.