Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat: added offline license support

=dd5105379670
diff --git a/backend-go/internal/services/license/keys/license_public_key.pem b/backend-go/internal/services/license/keys/license_public_key.pem
new file mode 100644
index 0000000000..0cda06f3cf
--- /dev/null
+++ b/backend-go/internal/services/license/keys/license_public_key.pem
@@ -0,0 +1,8 @@
+-----BEGIN RSA PUBLIC KEY-----
+MIIBCgKCAQEApchBY3BXTu4zWGBguB7nM/pjpVLY3V7VGZOAxmR5ueQTJOwiGM13
+5HN3EM9fDlQnZu9VSc0OFqRM/bUeUaI1oLPE6WzTHjdHyKjDI/S+TLx3VGEsvhM1
+uukZpYX+3KX2w4wzRHBaBWyglFy0CVNth9UJhhpD+KKfv7dzcRmsbyoUWi9wGfJu
+wLYCwaCwZRXIt1sLGmMncPz14vfwdnm2a5Tj1Jbt0GTyBl+1/ZqLbO6SsslLg2G+
+o7FfGS9z8OUTkvDdu16qxL+p2wCEFZMnOz5BB4oakuT2gS9iOO2l5AOPcT4WzPzy
+PYbX3d7cN9BkOY9I5z0cX4wzqHjQTvGNLQIDAQAB
+-----END RSA PUBLIC KEY-----
\ No newline at end of file
diff --git a/backend-go/internal/services/license/license.go b/backend-go/internal/services/license/license.go
index d75d7a7f43..ae210c24ab 100644
--- a/backend-go/internal/services/license/license.go
+++ b/backend-go/internal/services/license/license.go
@@ -2,9 +2,15 @@ package license
import (
"context"
+ "crypto"
+ "crypto/rsa"
+ "crypto/sha256"
+ "crypto/x509"
"database/sql"
+ _ "embed"
"encoding/base64"
"encoding/json"
+ "encoding/pem"
"errors"
"fmt"
"log/slog"
@@ -17,6 +23,9 @@ import (
"github.com/infisical/api/internal/database/pg"
)
+//go:embed keys/license_public_key.pem
+var licensePublicKeyPEM []byte
+
const (
cloudLoginPath = "/api/auth/v1/license-server-login"
onPremLoginPath = "/api/auth/v1/license-login"
@@ -176,19 +185,74 @@ func isOfflineKey(key string) bool {
return contents.Signature != "" && len(contents.License) > 0
}
-// loadOfflineLicense decodes a base64 offline license, checks expiration, and loads features.
+// verifySignatureWithKey verifies an RSA-SHA256 signature using the provided public key.
+func verifySignatureWithKey(data []byte, signatureBase64 string, pubKey *rsa.PublicKey) error {
+ signature, err := base64.StdEncoding.DecodeString(signatureBase64)
+ if err != nil {
+ return fmt.Errorf("decoding signature: %w", err)
+ }
+
+ hash := sha256.Sum256(data)
+
+ if err := rsa.VerifyPKCS1v15(pubKey, crypto.SHA256, hash[:], signature); err != nil {
+ return fmt.Errorf("signature verification failed: %w", err)
+ }
+
+ return nil
+}
+
+// parseLicensePublicKey parses the embedded license public key.
+func parseLicensePublicKey() (*rsa.PublicKey, error) {
+ block, _ := pem.Decode(licensePublicKeyPEM)
+ if block == nil {
+ return nil, errors.New("failed to parse PEM block containing public key")
+ }
+
+ pubKey, err := x509.ParsePKCS1PublicKey(block.Bytes)
+ if err != nil {
+ return nil, fmt.Errorf("parsing public key: %w", err)
+ }
+
+ return pubKey, nil
+}
+
+// verifyOfflineLicenseSignature verifies the RSA-SHA256 signature of the license.
+// This matches the Node.js implementation in backend/src/lib/crypto/signing.ts.
+func verifyOfflineLicenseSignature(licenseJSON []byte, signatureBase64 string) error {
+ pubKey, err := parseLicensePublicKey()
+ if err != nil {
+ return err
+ }
+
+ return verifySignatureWithKey(licenseJSON, signatureBase64, pubKey)
+}
+
+// loadOfflineLicense decodes a base64 offline license, verifies signature, checks expiration, and loads features.
func (s *Service) loadOfflineLicense(key string) error {
decoded, err := base64.StdEncoding.DecodeString(key)
if err != nil {
return fmt.Errorf("decoding offline license: %w", err)
}
- var contents OfflineLicenseContents
- if err := json.Unmarshal(decoded, &contents); err != nil {
+ // Parse to get raw license JSON for signature verification.
+ var rawContents struct {
+ License json.RawMessage `json:"license"`
+ Signature string `json:"signature"`
+ }
+ if err := json.Unmarshal(decoded, &rawContents); err != nil {
return fmt.Errorf("parsing offline license: %w", err)
}
- // TODO: verify RSA signature against embedded public key.
+ // Verify RSA-SHA256 signature (matches Node.js verifyOfflineLicense).
+ if err := verifyOfflineLicenseSignature(rawContents.License, rawContents.Signature); err != nil {
+ return fmt.Errorf("offline license verification failed: %w", err)
+ }
+
+ // Parse the license contents.
+ var contents OfflineLicenseContents
+ if err := json.Unmarshal(decoded, &contents); err != nil {
+ return fmt.Errorf("parsing offline license contents: %w", err)
+ }
// Check termination date.
if contents.License.TerminatesAt != nil {
diff --git a/backend-go/internal/services/license/license_test.go b/backend-go/internal/services/license/license_test.go
new file mode 100644
index 0000000000..11c4018c7d
--- /dev/null
+++ b/backend-go/internal/services/license/license_test.go
@@ -0,0 +1,153 @@
+package license
+
+import (
+ "crypto"
+ "crypto/rand"
+ "crypto/rsa"
+ "crypto/sha256"
+ "encoding/base64"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+// generateTestKeyPair creates an RSA keypair for testing signature verification.
+func generateTestKeyPair(t *testing.T) (*rsa.PrivateKey, *rsa.PublicKey) {
+ t.Helper()
+ privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
+ require.NoError(t, err)
+ return privateKey, &privateKey.PublicKey
+}
+
+// signTestLicense signs license JSON with the test private key.
+func signTestLicense(t *testing.T, privateKey *rsa.PrivateKey, licenseJSON []byte) string {
+ t.Helper()
+ hash := sha256.Sum256(licenseJSON)
+ signature, err := rsa.SignPKCS1v15(rand.Reader, privateKey, crypto.SHA256, hash[:])
+ require.NoError(t, err)
+ return base64.StdEncoding.EncodeToString(signature)
+}
+
+func TestVerifySignatureWithKey_ValidSignature(t *testing.T) {
+ t.Parallel()
+
+ privateKey, publicKey := generateTestKeyPair(t)
+
+ tests := []struct {
+ name string
+ licenseJSON string
+ }{
+ {
+ name: "simple license",
+ licenseJSON: `{"issuedTo":"test","licenseId":"123"}`,
+ },
+ {
+ name: "license with features",
+ licenseJSON: `{"issuedTo":"Acme Corp","licenseId":"lic-456","features":{"secretsManagement":true}}`,
+ },
+ {
+ name: "license with termination date",
+ licenseJSON: `{"issuedTo":"test","licenseId":"789","terminatesAt":"2030-01-01T00:00:00Z"}`,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ t.Parallel()
+ signature := signTestLicense(t, privateKey, []byte(tt.licenseJSON))
+ err := verifySignatureWithKey([]byte(tt.licenseJSON), signature, publicKey)
+ require.NoError(t, err, "valid signature should be accepted")
+ })
+ }
+}
+
+func TestVerifySignatureWithKey_RejectsInvalidSignature(t *testing.T) {
+ t.Parallel()
+
+ _, publicKey := generateTestKeyPair(t)
+
+ tests := []struct {
+ name string
+ licenseJSON string
+ signature string
+ }{
+ {
+ name: "empty signature",
+ licenseJSON: `{"issuedTo":"test","licenseId":"123"}`,
+ signature: "",
+ },
+ {
+ name: "garbage signature",
+ licenseJSON: `{"issuedTo":"test","licenseId":"123"}`,
+ signature: "bm90YXZhbGlkc2lnbmF0dXJl", // "notavalidsignature" in base64
+ },
+ {
+ name: "valid base64 but wrong signature",
+ licenseJSON: `{"issuedTo":"forged","features":{}}`,
+ signature: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ t.Parallel()
+ err := verifySignatureWithKey([]byte(tt.licenseJSON), tt.signature, publicKey)
+ require.Error(t, err, "invalid signature should be rejected")
+ })
+ }
+}
+
+func TestVerifySignatureWithKey_RejectsTamperedData(t *testing.T) {
+ t.Parallel()
+
+ privateKey, publicKey := generateTestKeyPair(t)
+
+ // Sign original license
+ originalJSON := `{"issuedTo":"test","licenseId":"123"}`
+ signature := signTestLicense(t, privateKey, []byte(originalJSON))
+
+ // Try to verify with tampered data
+ tamperedJSON := `{"issuedTo":"hacker","licenseId":"123"}`
+ err := verifySignatureWithKey([]byte(tamperedJSON), signature, publicKey)
+ require.Error(t, err, "tampered data should fail signature verification")
+ assert.Contains(t, err.Error(), "signature verification failed")
+}
+
+func TestVerifySignatureWithKey_RejectsWrongKey(t *testing.T) {
+ t.Parallel()
+
+ // Sign with one keypair
+ privateKey1, _ := generateTestKeyPair(t)
+ licenseJSON := `{"issuedTo":"test","licenseId":"123"}`
+ signature := signTestLicense(t, privateKey1, []byte(licenseJSON))
+
+ // Verify with different keypair
+ _, publicKey2 := generateTestKeyPair(t)
+ err := verifySignatureWithKey([]byte(licenseJSON), signature, publicKey2)
+ require.Error(t, err, "signature from different key should be rejected")
+}
+
+func TestVerifyOfflineLicenseSignature_PublicKeyLoaded(t *testing.T) {
+ t.Parallel()
+
+ require.NotEmpty(t, licensePublicKeyPEM, "license public key should be embedded")
+ assert.Contains(t, string(licensePublicKeyPEM), "BEGIN RSA PUBLIC KEY", "should be RSA public key PEM")
+
+ // Verify the key is parseable
+ pubKey, err := parseLicensePublicKey()
+ require.NoError(t, err, "embedded public key should be parseable")
+ require.NotNil(t, pubKey)
+}
+
+func TestVerifyOfflineLicenseSignature_RejectsForgedLicense(t *testing.T) {
+ t.Parallel()
+
+ // This tests that forged licenses signed with a different key are rejected
+ // by the production verifyOfflineLicenseSignature function.
+ forgedJSON := `{"issuedTo":"hacker","features":{"allFeatures":true}}`
+ forgedSignature := "bm90YXZhbGlkc2lnbmF0dXJl"
+
+ err := verifyOfflineLicenseSignature([]byte(forgedJSON), forgedSignature)
+ require.Error(t, err, "forged license should be rejected by production verification")
+}