diff --git a/application/ricochetonion.go b/application/ricochetonion.go index b3dc6ec..82f965d 100644 --- a/application/ricochetonion.go +++ b/application/ricochetonion.go @@ -6,8 +6,10 @@ import ( "net" ) -func SetupOnion(proxyServer string, authentication string, pk *rsa.PrivateKey, onionport uint16) (net.Listener, error) { - c, err := bulb.Dial("tcp4", proxyServer) +// "127.0.0.1:9051" "tcp4" +// "/var/run/tor/control" "unix" +func SetupOnion(torControlAddress string, torControlSocketType string, authentication string, pk *rsa.PrivateKey, onionport uint16) (net.Listener, error) { + c, err := bulb.Dial(torControlSocketType, torControlAddress) if err != nil { return nil, err } diff --git a/utils/crypto.go b/utils/crypto.go index 4852fb0..7964d6a 100644 --- a/utils/crypto.go +++ b/utils/crypto.go @@ -5,20 +5,37 @@ import ( "crypto/x509" "encoding/pem" "io/ioutil" + "errors" + "crypto/rand" ) const ( InvalidPrivateKeyFileError = Error("InvalidPrivateKeyFileError") + RICOCHET_KEY_SIZE = 1024 ) +// Generate a private key for use +func GeneratePrivateKey() (*rsa.PrivateKey, error) { + privateKey, err := rsa.GenerateKey(rand.Reader, RICOCHET_KEY_SIZE) + if err != nil { + return nil, errors.New("Could not generate key: " + err.Error()) + } + privateKeyDer := x509.MarshalPKCS1PrivateKey(privateKey) + return x509.ParsePKCS1PrivateKey(privateKeyDer) +} + // LoadPrivateKeyFromFile loads a private key from a file... func LoadPrivateKeyFromFile(filename string) (*rsa.PrivateKey, error) { pemData, err := ioutil.ReadFile(filename) - if err != nil { return nil, err } + return ParsePrivateKey(pemData) +} + +// Convert a private key string to a usable private key +func ParsePrivateKey(pemData []byte) (*rsa.PrivateKey, error) { block, _ := pem.Decode(pemData) if block == nil || block.Type != "RSA PRIVATE KEY" { return nil, InvalidPrivateKeyFileError @@ -26,3 +43,14 @@ func LoadPrivateKeyFromFile(filename string) (*rsa.PrivateKey, error) { return x509.ParsePKCS1PrivateKey(block.Bytes) } + +// turn a private key into storable string +func PrivateKeyToString(privateKey *rsa.PrivateKey) string { + privateKeyBlock := pem.Block{ + Type: "RSA PRIVATE KEY", + Headers: nil, + Bytes: x509.MarshalPKCS1PrivateKey(privateKey), + } + + return string(pem.EncodeToMemory(&privateKeyBlock)) +}