-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds signing subcommands to updater
- Loading branch information
Showing
5 changed files
with
264 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package pkg | ||
|
||
import ( | ||
"crypto" | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/sha256" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"fmt" | ||
) | ||
|
||
// Sign signs a message using a private key. | ||
func Sign(privateKey []byte, message string) ([]byte, error) { | ||
block, _ := pem.Decode(privateKey) | ||
if block == nil { | ||
return nil, fmt.Errorf("failed to decode PEM block containing private key") | ||
} | ||
|
||
key, err := x509.ParsePKCS8PrivateKey(block.Bytes) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse private key: %v", err) | ||
} | ||
|
||
hasher := sha256.New() | ||
hasher.Write([]byte(message)) | ||
hashed := hasher.Sum(nil) | ||
|
||
signature, err := rsa.SignPKCS1v15(rand.Reader, key.(*rsa.PrivateKey), crypto.SHA256, hashed) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to sign message: %v", err) | ||
} | ||
|
||
return signature, nil | ||
} | ||
|
||
// Verify verifies a message using a public key and a signature. | ||
func Verify(publicKey []byte, message string, signature []byte) error { | ||
block, _ := pem.Decode(publicKey) | ||
if block == nil { | ||
return fmt.Errorf("failed to decode PEM block containing public key") | ||
} | ||
|
||
key, err := x509.ParsePKIXPublicKey(block.Bytes) | ||
if err != nil { | ||
return fmt.Errorf("failed to parse public key: %v", err) | ||
} | ||
|
||
hasher := sha256.New() | ||
hasher.Write([]byte(message)) | ||
hashed := hasher.Sum(nil) | ||
|
||
err = rsa.VerifyPKCS1v15(key.(*rsa.PublicKey), crypto.SHA256, hashed, signature) | ||
if err != nil { | ||
return fmt.Errorf("failed to verify signature: %v", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package pkg_test | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
|
||
"github.com/input-output-hk/catalyst-ci/tools/updater/pkg" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Signing", func() { | ||
var privKey []byte | ||
var pubKey []byte | ||
|
||
BeforeEach(func() { | ||
privateKeyRaw, err := rsa.GenerateKey(rand.Reader, 2048) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
privateKeyDer, err := x509.MarshalPKCS8PrivateKey(privateKeyRaw) | ||
Expect(err).ToNot(HaveOccurred()) | ||
privateKeyBlock := &pem.Block{ | ||
Type: "RSA PRIVATE KEY", | ||
Bytes: privateKeyDer, | ||
} | ||
privKey = pem.EncodeToMemory(privateKeyBlock) | ||
|
||
publicKey := &privateKeyRaw.PublicKey | ||
publicKeyDer, err := x509.MarshalPKIXPublicKey(publicKey) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
publicKeyBlock := &pem.Block{ | ||
Type: "PUBLIC KEY", | ||
Bytes: publicKeyDer, | ||
} | ||
pubKey = pem.EncodeToMemory(publicKeyBlock) | ||
}) | ||
|
||
Describe("Sign", func() { | ||
When("signing a message", func() { | ||
When("The private key is valid", func() { | ||
It("returns a signature", func() { | ||
signature, err := pkg.Sign(privKey, "message") | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(signature).ToNot(BeNil()) | ||
}) | ||
}) | ||
|
||
When("The private key is invalid", func() { | ||
It("returns an error", func() { | ||
signature, err := pkg.Sign([]byte("invalid"), "message") | ||
Expect(err).To(HaveOccurred()) | ||
Expect(signature).To(BeNil()) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
Describe("Verify", func() { | ||
When("verifying a message", func() { | ||
var message string | ||
var signature []byte | ||
|
||
BeforeEach(func() { | ||
message = "message" | ||
|
||
var err error | ||
signature, err = pkg.Sign(privKey, message) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
|
||
When("The public key and signature are valid", func() { | ||
It("returns nil", func() { | ||
err := pkg.Verify(pubKey, message, signature) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
}) | ||
|
||
When("The public key is invalid", func() { | ||
It("returns an error", func() { | ||
err := pkg.Verify([]byte("invalid"), message, signature) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
|
||
When("The signature is invalid", func() { | ||
It("returns an error", func() { | ||
err := pkg.Verify(pubKey, message, []byte("invalid")) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |