[PD1] cryptoUtils start and protocol tweaks

This commit is contained in:
Tiago Sousa 2024-04-17 19:54:14 +01:00
parent cdaae8fb7e
commit c57c093867
Signed by: tiago
SSH key fingerprint: SHA256:odOD9vln9U7qNe1R8o3UCbE3jkQCkr5/q5mgd5hwua0
4 changed files with 128 additions and 80 deletions

View file

@ -1,7 +1,49 @@
package cryptoUtils
import "fmt"
import (
"PD1/internal/client"
"PD1/internal/protocol"
"crypto/rsa"
"crypto/x509"
"fmt"
"log"
"os"
"software.sslmate.com/src/go-pkcs12"
)
func Print() {
fmt.Println("crypto package")
}
func getUserInfo(certFilename string) (
rsa.PrivateKey,
*x509.Certificate,
[]*x509.Certificate,
error) {
var privKey rsa.PrivateKey
certFile, err := os.ReadFile(certFilename)
if err != nil {
log.Panicln("Provided certificate %v couldn't be opened", certFilename)
return rsa.PrivateKey{}, nil, nil, err
}
password := client.AskUserPassword()
privKeyInterface, cert, caCerts, err := pkcs12.DecodeChain(certFile, password)
privKey = privKeyInterface.(rsa.PrivateKey)
if err != nil {
log.Panicln("PKCS12 key store couldn't be decoded")
return rsa.PrivateKey{}, nil, nil, err
}
if err := privKey.Validate(); err != nil {
log.Panicln("Private key is not valid")
return rsa.PrivateKey{}, nil, nil, err
}
return privKey, cert, caCerts, nil
}
func encryptMessageContent(privKey rsa.PrivateKey, peerPubKey rsa.PublicKey, content []byte) []byte {
// Digital envolope
func Print(){
fmt.Println("crypto package")
}