2024-04-16 12:23:00 +01:00
|
|
|
package cryptoUtils
|
2024-04-16 09:02:23 +01:00
|
|
|
|
2024-04-17 19:54:14 +01:00
|
|
|
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
|
2024-04-16 09:02:23 +01:00
|
|
|
|
|
|
|
}
|