mirror of
https://github.com/fhmq/hmq.git
synced 2026-09-01 23:34:52 +00:00
Plugins support (#46)
* modify * update * add acl * add feature * update dockerfile * add deploy * update * update * plugins * plugins * update * update * update * fixed * remove * fixed * add log * update * fixed * update * fix config * add http api * add http api * resp * add config for work chan * update * fixed * update * disable trace * fixed * change acl * fixed * fixed res * dd * dd * ddd * dd * update * fixed * update * add * fixed * update key * add log * update * format * update * update auth * update * update readme * added * update * fixed * fixed * fix * upade * update * update
This commit is contained in:
+89
@@ -0,0 +1,89 @@
|
||||
// Package rfc3962 provides encryption and checksum methods as specified in RFC 3962
|
||||
package rfc3962
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"gopkg.in/jcmturner/aescts.v1"
|
||||
"gopkg.in/jcmturner/gokrb5.v7/crypto/common"
|
||||
"gopkg.in/jcmturner/gokrb5.v7/crypto/etype"
|
||||
)
|
||||
|
||||
// EncryptData encrypts the data provided using methods specific to the etype provided as defined in RFC 3962.
|
||||
func EncryptData(key, data []byte, e etype.EType) ([]byte, []byte, error) {
|
||||
if len(key) != e.GetKeyByteSize() {
|
||||
return []byte{}, []byte{}, fmt.Errorf("incorrect keysize: expected: %v actual: %v", e.GetKeyByteSize(), len(key))
|
||||
}
|
||||
ivz := make([]byte, e.GetCypherBlockBitLength()/8)
|
||||
return aescts.Encrypt(key, ivz, data)
|
||||
}
|
||||
|
||||
// EncryptMessage encrypts the message provided using the methods specific to the etype provided as defined in RFC 3962.
|
||||
// The encrypted data is concatenated with its integrity hash to create an encrypted message.
|
||||
func EncryptMessage(key, message []byte, usage uint32, e etype.EType) ([]byte, []byte, error) {
|
||||
if len(key) != e.GetKeyByteSize() {
|
||||
return []byte{}, []byte{}, fmt.Errorf("incorrect keysize: expected: %v actual: %v", e.GetKeyByteSize(), len(key))
|
||||
}
|
||||
//confounder
|
||||
c := make([]byte, e.GetConfounderByteSize())
|
||||
_, err := rand.Read(c)
|
||||
if err != nil {
|
||||
return []byte{}, []byte{}, fmt.Errorf("could not generate random confounder: %v", err)
|
||||
}
|
||||
plainBytes := append(c, message...)
|
||||
|
||||
// Derive key for encryption from usage
|
||||
var k []byte
|
||||
if usage != 0 {
|
||||
k, err = e.DeriveKey(key, common.GetUsageKe(usage))
|
||||
if err != nil {
|
||||
return []byte{}, []byte{}, fmt.Errorf("error deriving key for encryption: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Encrypt the data
|
||||
iv, b, err := e.EncryptData(k, plainBytes)
|
||||
if err != nil {
|
||||
return iv, b, fmt.Errorf("error encrypting data: %v", err)
|
||||
}
|
||||
|
||||
// Generate and append integrity hash
|
||||
ih, err := common.GetIntegrityHash(plainBytes, key, usage, e)
|
||||
if err != nil {
|
||||
return iv, b, fmt.Errorf("error encrypting data: %v", err)
|
||||
}
|
||||
b = append(b, ih...)
|
||||
return iv, b, nil
|
||||
}
|
||||
|
||||
// DecryptData decrypts the data provided using the methods specific to the etype provided as defined in RFC 3962.
|
||||
func DecryptData(key, data []byte, e etype.EType) ([]byte, error) {
|
||||
if len(key) != e.GetKeyByteSize() {
|
||||
return []byte{}, fmt.Errorf("incorrect keysize: expected: %v actual: %v", e.GetKeyByteSize(), len(key))
|
||||
}
|
||||
ivz := make([]byte, e.GetCypherBlockBitLength()/8)
|
||||
return aescts.Decrypt(key, ivz, data)
|
||||
}
|
||||
|
||||
// DecryptMessage decrypts the message provided using the methods specific to the etype provided as defined in RFC 3962.
|
||||
// The integrity of the message is also verified.
|
||||
func DecryptMessage(key, ciphertext []byte, usage uint32, e etype.EType) ([]byte, error) {
|
||||
//Derive the key
|
||||
k, err := e.DeriveKey(key, common.GetUsageKe(usage))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error deriving key: %v", err)
|
||||
}
|
||||
// Strip off the checksum from the end
|
||||
b, err := e.DecryptData(k, ciphertext[:len(ciphertext)-e.GetHMACBitLength()/8])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//Verify checksum
|
||||
if !e.VerifyIntegrity(key, ciphertext, b, usage) {
|
||||
return nil, errors.New("integrity verification failed")
|
||||
}
|
||||
//Remove the confounder bytes
|
||||
return b[e.GetConfounderByteSize():], nil
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package rfc3962
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
|
||||
"github.com/jcmturner/gofork/x/crypto/pbkdf2"
|
||||
"gopkg.in/jcmturner/gokrb5.v7/crypto/etype"
|
||||
)
|
||||
|
||||
const (
|
||||
s2kParamsZero = 4294967296
|
||||
)
|
||||
|
||||
// StringToKey returns a key derived from the string provided according to the definition in RFC 3961.
|
||||
func StringToKey(secret, salt, s2kparams string, e etype.EType) ([]byte, error) {
|
||||
i, err := S2KparamsToItertions(s2kparams)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return StringToKeyIter(secret, salt, i, e)
|
||||
}
|
||||
|
||||
// StringToPBKDF2 generates an encryption key from a pass phrase and salt string using the PBKDF2 function from PKCS #5 v2.0
|
||||
func StringToPBKDF2(secret, salt string, iterations int64, e etype.EType) []byte {
|
||||
return pbkdf2.Key64([]byte(secret), []byte(salt), iterations, int64(e.GetKeyByteSize()), e.GetHashFunc())
|
||||
}
|
||||
|
||||
// StringToKeyIter returns a key derived from the string provided according to the definition in RFC 3961.
|
||||
func StringToKeyIter(secret, salt string, iterations int64, e etype.EType) ([]byte, error) {
|
||||
tkey := e.RandomToKey(StringToPBKDF2(secret, salt, iterations, e))
|
||||
return e.DeriveKey(tkey, []byte("kerberos"))
|
||||
}
|
||||
|
||||
// S2KparamsToItertions converts the string representation of iterations to an integer
|
||||
func S2KparamsToItertions(s2kparams string) (int64, error) {
|
||||
//process s2kparams string
|
||||
//The parameter string is four octets indicating an unsigned
|
||||
//number in big-endian order. This is the number of iterations to be
|
||||
//performed. If the value is 00 00 00 00, the number of iterations to
|
||||
//be performed is 4,294,967,296 (2**32).
|
||||
var i uint32
|
||||
if len(s2kparams) != 8 {
|
||||
return int64(s2kParamsZero), errors.New("invalid s2kparams length")
|
||||
}
|
||||
b, err := hex.DecodeString(s2kparams)
|
||||
if err != nil {
|
||||
return int64(s2kParamsZero), errors.New("invalid s2kparams, cannot decode string to bytes")
|
||||
}
|
||||
i = binary.BigEndian.Uint32(b)
|
||||
//buf := bytes.NewBuffer(b)
|
||||
//err = binary.Read(buf, binary.BigEndian, &i)
|
||||
if err != nil {
|
||||
return int64(s2kParamsZero), errors.New("invalid s2kparams, cannot convert to big endian int32")
|
||||
}
|
||||
return int64(i), nil
|
||||
}
|
||||
Reference in New Issue
Block a user