Skip to content

Instantly share code, notes, and snippets.

@salrashid123
Created October 28, 2023 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salrashid123/f59b831dc75f8b1fd4b1aa98d7aa0f87 to your computer and use it in GitHub Desktop.
Save salrashid123/f59b831dc75f8b1fd4b1aa98d7aa0f87 to your computer and use it in GitHub Desktop.
Extract EKM using golang [RFC5705](https://datatracker.ietf.org/doc/html/rfc5705)
package main
/*
Sample that prints the EKM value for a TLS connection:
https://www.openssl.org/docs/man1.1.1/man3/SSL_export_keying_material.html
https://github.com/salrashid123/go_mtls_scratchpad/tree/main#exported-key-material
*/
import (
"context"
"crypto/tls"
"encoding/hex"
"io/ioutil"
"log"
"net"
"net/http"
)
var (
ekm []byte
)
func main() {
conn, err := tls.Dial("tcp", "httpbin.org:443", &tls.Config{})
if err != nil {
log.Fatal(err)
}
cs := conn.ConnectionState()
ekm, err = cs.ExportKeyingMaterial("my_nonce", nil, 32)
if err != nil {
log.Fatal(err)
}
log.Printf("EKM my_nonce: %s\n", hex.EncodeToString(ekm))
tr := &http.Transport{
DialTLSContext: func(ctx context.Context, network string, addr string) (net.Conn, error) {
return conn, nil
},
}
client := http.Client{
Transport: tr,
}
req, err := http.NewRequest(http.MethodGet, "https://httpbin.org/get", nil)
if err != nil {
log.Fatal(err)
}
// do something here with the ekm value...
req.Header.Add("ekm", hex.EncodeToString(ekm))
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
htmlData, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
log.Printf("%v\n", resp.Status)
log.Printf(string(htmlData))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment