Skip to content

Commit

Permalink
Bug: nonjson: Ensure MAC address is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
zsrv committed Feb 14, 2023
1 parent ea08419 commit e555525
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
8 changes: 7 additions & 1 deletion pkg/nonjson/product_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/base64"
"errors"
"fmt"
"github.com/zsrv/supermicro-product-key/pkg/oob"
"strconv"
"time"
)
Expand Down Expand Up @@ -77,6 +78,11 @@ type ProductKey struct {
// Encode returns the encrypted, base64-encoded ProductKey associated with
// the given BMC MAC address.
func (pk *ProductKey) Encode(macAddress string) (string, error) {
macAddress, err := oob.NormalizeMACAddress(macAddress)
if err != nil {
return "", err
}

buffer := bytes.NewBuffer(make([]byte, 0, 255))
writer := bufio.NewWriterSize(buffer, 255)

Expand All @@ -85,7 +91,7 @@ func (pk *ProductKey) Encode(macAddress string) (string, error) {

// Unencrypted block

err := writer.WriteByte(pk.FormatVersion)
err = writer.WriteByte(pk.FormatVersion)
if err != nil {
return "", err
}
Expand Down
43 changes: 31 additions & 12 deletions pkg/nonjson/product_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ func TestProductKey_Encode(t *testing.T) {
type args struct {
macAddress string
}

validProductKey := fields{
FormatVersion: 0,
SoftwareIdentifier: *SoftwareIdentifiers.ALL,
SoftwareVersion: "none",
InvoiceNumber: "none",
CreationDate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
ExpirationDate: time.Unix(0, 0).UTC(),
Property: nil,
SecretData: []byte("76aa41c893a4e32a34203d8d47b0faab"),
Checksum: 171,
}

tests := []struct {
name string
fields fields
Expand All @@ -28,24 +41,30 @@ func TestProductKey_Encode(t *testing.T) {
wantErr bool
}{
{
name: "valid",
fields: fields{
FormatVersion: 0,
SoftwareIdentifier: *SoftwareIdentifiers.ALL,
SoftwareVersion: "none",
InvoiceNumber: "none",
CreationDate: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
ExpirationDate: time.Unix(0, 0).UTC(),
Property: nil,
SecretData: []byte("76aa41c893a4e32a34203d8d47b0faab"),
Checksum: 171,
},
name: "valid",
fields: validProductKey,
args: args{
macAddress: "3cecef123456",
},
want: "AAYAAAAAAAAAAAAAAAAAAExLCU/N0RxxvG7ZACnE9iyfm1zRK6acy5rtKA01mFtnuCkFSJQtmsmoAN7KVyfxVbUpwPvJNKc2tkQbezXSbITnPSKnp8i9uG+C8DB+9oISsuTL8L0v07TOOsAnrSq4fR4mAhwANTYsmoLYmpqhVDLH/VVisfqVFSZu72vTDf2rjYESalQNawzIH8qjEhS2dzDUTm4RWf122JiPTSccbg2V8b4XXLRSefvc4ctVmCVvmrWRX+Aosgn9z0VS5V1ABhitiDjBd4NK34wOoGtn0vTwdiAfjMH95U5Q+c4hCjWsUnTrlUrdH5OQgtCDGi7Nag==",
wantErr: false,
},
{
name: "mac address with invalid character",
fields: validProductKey,
args: args{
macAddress: "3cecef12345x",
},
wantErr: true,
},
{
name: "mac address too long",
fields: validProductKey,
args: args{
macAddress: "3cecef1234567",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit e555525

Please sign in to comment.