-
I am creating a keypair with the p256 crate and then I want to send the publickey to another application implemented in golang where I basically just take the data directly and put it into the function x509.ParsePKIXPublicKey(data). However when I am trying to parse the publickey bytes in the golang application I am getting an error. I think it has something to do with the format. use std::io::Write;
use std::os::unix::net::UnixStream;
use std::path::Path;
use p256::SecretKey;
use p256::elliptic_curve::sec1::ToEncodedPoint;
fn main() {
println!("connecting!");
let sock_path = Path::new("/home/martin/ecsock");
let mut stream = UnixStream::connect(sock_path).unwrap();
println!("Generating key pair!");
let a = SecretKey::random(& mut rand_core::OsRng);
let x = a.public_key().to_encoded_point(false);
println!("Sending public key");
stream.write_all(x.as_bytes()).unwrap();
} When I am receiving on the golang application I am getting the following error: Sometimes it is "length too large" and other times it is "tags don't match"
The golang implementation looks like this: package main
import (
"crypto/x509"
"fmt"
"log"
"net"
"os"
"os/signal"
"syscall"
)
func main() {
go unixHandler()
handleCrash(func() { os.Remove("/home/martin/ecsock") })
}
func handleCrash(onCrash func()) {
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)
<-signalCh
onCrash()
}
func unixHandler() {
l, err := net.Listen("unix", "/home/martin/ecsock")
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
log.Fatal(err)
}
go handleConn(conn)
}
}
func handleConn(conn net.Conn) {
data, err := read(conn)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Length: %d\n", len(data))
fmt.Printf("Data: %v\n", data)
_, err = x509.ParsePKIXPublicKey(data)
if err != nil {
fmt.Printf("Error: %s\n\n", err)
return
}
}
func read(conn net.Conn) ([]byte, error) {
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
return nil, fmt.Errorf("Failed reading bytes: %s\n", err)
}
return buf[:n], nil
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The format you're working with appears to be X.509 SubjectPublicKeyInfo (a.k.a. SPKI). Here's the method for encoding that: You'll need to |
Beta Was this translation helpful? Give feedback.
The format you're working with appears to be X.509 SubjectPublicKeyInfo (a.k.a. SPKI).
Here's the method for encoding that:
EncodePublicKey::to_public_key_der
You'll need to
use p256::elliptic_curve::pkcs8::EncodePublicKey
to have the required trait in scope.