Skip to content

Instantly share code, notes, and snippets.

@itsfolf
Created October 21, 2024 02:27
Show Gist options
  • Select an option

  • Save itsfolf/25fcaa047e4f6a8c1ae8b3a06e36634e to your computer and use it in GitHub Desktop.

Select an option

Save itsfolf/25fcaa047e4f6a8c1ae8b3a06e36634e to your computer and use it in GitHub Desktop.
VictoriaMetrics Enterprise license stuffs
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/x509"
"encoding/base32"
"encoding/base64"
"encoding/hex"
"encoding/pem"
"fmt"
"strings"
)
var BINARY_AES_KEY = "d6be402ee1c2062028efe816f2043ee3f8f1a62a059dbbeb44e3ef1f6a63ca40"
var BINARY_IV = "935bc6676bbbad7bd8b3132b5c7987bf"
var BINARY_ENCRYPTED_PUBKEY = "F3GpP+5ctz11cKfka2y4Sd4MeL95kuzeklV9ckXkM4WvpMh05/aa2q/j0mQqy4H/z0MJizpRxqO0nLwWLrqakGHrPZS3ESnRAU3Q4JPemP4eM3DbHibllKI9oAe8iA4LrBhHLPM00wVVuZKfv7J5Naw++OmSwVZDSQ9AfP6Am2MveFoyz5cbZXKgBuxP5OsHBGv3bUQ9ZyazCfdsPnNM15VdUd/m7Fr4+3pZz6/3aWIevOJ860bw8Ld8rg/RW/td"
func verifykey(pubkey string, key string) {
aesKey, err := hex.DecodeString(BINARY_AES_KEY)
if err != nil {
panic(err)
}
aesCipher, err := aes.NewCipher(aesKey)
if err != nil {
panic(err)
}
iv, err := hex.DecodeString(BINARY_IV)
if err != nil {
panic(err)
}
cipher := cipher.NewCBCDecrypter(aesCipher, iv)
pubkeyBytes, err := base64.StdEncoding.DecodeString(pubkey)
if err != nil {
panic(err)
}
cipher.CryptBlocks(pubkeyBytes, pubkeyBytes)
fmt.Printf("Using public key:\n %s\n", string(pubkeyBytes))
decodedKey, _ := pem.Decode(pubkeyBytes)
pub, err := x509.ParsePKIXPublicKey(decodedKey.Bytes)
if err != nil {
panic(err)
}
pubt := pub.(*ecdsa.PublicKey)
splitkey := strings.Split(key, "_")
licenseKey := splitkey[0]
signature := splitkey[1]
payloadbytes, err := base32.StdEncoding.WithPadding(base32.NoPadding).DecodeString(licenseKey)
if err != nil {
panic(err)
}
licensesig, err := base32.StdEncoding.WithPadding(base32.NoPadding).DecodeString(signature)
if err != nil {
panic(err)
}
h := sha256.New()
h.Reset()
h.Write(payloadbytes)
res := h.Sum(nil)
result := ecdsa.VerifyASN1(pubt, res, licensesig)
fmt.Printf("Result: %s", result)
}
func newKey(license string) {
aesKey, err := hex.DecodeString(BINARY_AES_KEY)
if err != nil {
panic(err)
}
aesCipher, err := aes.NewCipher(aesKey)
if err != nil {
panic(err)
}
iv, err := hex.DecodeString(BINARY_IV)
if err != nil {
panic(err)
}
cipher := cipher.NewCBCEncrypter(aesCipher, iv)
newPrivateKey, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
newPublicKey := &newPrivateKey.PublicKey
pubMar, _ := x509.MarshalPKIXPublicKey(newPublicKey)
var block = &pem.Block{
Type: "PUBLIC KEY",
Bytes: pubMar,
}
pem := pem.EncodeToMemory(block)
i := aes.BlockSize - (len(pem) % aes.BlockSize)
pem = append(pem, bytes.Repeat([]byte{byte(i)}, i)...)
cipher.CryptBlocks(pem, pem)
final := base64.StdEncoding.EncodeToString(pem)
fmt.Printf("\nPublic key to include in binary: %s", final)
var newLicenseBytes = []byte(license)
newH := sha256.New()
newH.Reset()
newH.Write(newLicenseBytes)
newHash := newH.Sum(nil)
newSig, err := ecdsa.SignASN1(rand.Reader, newPrivateKey, newHash)
newLicense32 := base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(newLicenseBytes)
newSig32 := base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(newSig)
fmt.Printf("\nNew license: %s_%s", newLicense32, newSig32)
}
func main() {
var trialKey = "PMRGU5DJEI5CENLEGVRWMNDCHEWTQZRSGYWTCMLFMYWTSNDFGIWTKZJUGE4DGMTBGM4TGMRCFQRHG5LCEI5CE3TFOJ2GK43BOBYGQ2LSMVAG4YLWMFWGGYLEMV2HGLTDN5WSELBCMF2WIIR2LMRHIZLTOQRCYITWNVRGCY3LOVYG2YLOMFTWK4RCFQRHM3LCMFRWW5LQEIWCE5TNOJSXG5DPOJSSELBCOZWWC3TPNVQWY6JCFQRHM3LHMF2GK53BPERCYITWNVRWY5LTORSXEIRMEJ3G243JNZTWYZJCFQRHM3LBNRSXE5BCFQRHM3LBM5SW45BCFQRHM3LBOV2GQIS5FQRGK6DQEI5DCNZTGQ3DIMRQGQYCYITBNRWG652PMZTGY2LOMURDUZTBNRZWK7I_GBCQEIIA2JSU4O7VBGL3DYQLYZVHSXXETYIW3QTHD2GDP2YYFGLEYUFDEPQAEIB6VLOPNJ5FRD7ZK6NT7XRDSKMGLQAT7U6FS3I7YRXKPFTT4MHJ4U"
verifykey(BINARY_ENCRYPTED_PUBKEY, trialKey)
newKey(`{"jti":"3073941a-f839-4db4-9c80-66a25c363cbe","sub":"meow@colon.three","aud":["test","vmbackupmanager","vmbackup","vmrestore","vmanomaly","vmgateway","vmcluster","vmsingle","vmalert","vmagent","vmauth"],"exp":2045004414,"allowOffline":true}`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment