Merge branch 'main' of gon:Uni/CSI-ES-2324

This commit is contained in:
Afonso Franco 2024-05-30 15:48:42 +01:00
commit 820580e168
Signed by: afonso
SSH key fingerprint: SHA256:PQTRDHPH3yALEGtHXnXBp3Orfcn21pK20t0tS1kHg54

View file

@ -252,9 +252,8 @@ func (k *KeyStore) GetServerTLSConfig() *tls.Config {
return tlsConfig
}
func (k KeyStore) EncryptMessageContent(receiverCert *x509.Certificate, content []byte) ([]byte, error) {
func (k KeyStore) EncryptMessageContent(receiverCert *x509.Certificate, recieverId string, content []byte) ([]byte, error) {
// Digital envolope
// Create a random symmetric key
dataKey := make([]byte, 32)
if _, err := rand.Read(dataKey); err != nil {
@ -272,7 +271,7 @@ func (k KeyStore) EncryptMessageContent(receiverCert *x509.Certificate, content
}
// sign the message and append the signature
hashedContent := sha256.Sum256(content)
hashedContent := sha256.Sum256(append(content, []byte(recieverId)...))
signature, err := rsa.SignPKCS1v15(nil, k.privKey, crypto.SHA256, hashedContent[:])
if err != nil {
return nil, err
@ -288,7 +287,7 @@ func (k KeyStore) EncryptMessageContent(receiverCert *x509.Certificate, content
return pair(encryptedDataKey, ciphertext), nil
}
func (k KeyStore) DecryptMessageContent(senderCert *x509.Certificate, cipherContent []byte) ([]byte, error) {
func (k KeyStore) DecryptMessageContent(senderCert *x509.Certificate, recieverId string, cipherContent []byte) ([]byte, error) {
encryptedDataKey, encryptedMsg := unPair(cipherContent)
dataKey, err := rsa.DecryptOAEP(sha256.New(), rand.Reader, k.GetPrivKey(), encryptedDataKey, nil)
if err != nil {
@ -307,7 +306,7 @@ func (k KeyStore) DecryptMessageContent(senderCert *x509.Certificate, cipherCont
}
// check signature with sender public key
signature, content := unPair(contentAndSig)
hashedContent := sha256.Sum256(content)
hashedContent := sha256.Sum256(append(content, []byte(recieverId)...))
senderKey := senderCert.PublicKey.(*rsa.PublicKey)
if err := rsa.VerifyPKCS1v15(senderKey, crypto.SHA256, hashedContent[:], signature); err != nil {
return nil, err