-
Notifications
You must be signed in to change notification settings - Fork 50
/
server.go
68 lines (58 loc) · 1.82 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"fmt"
"math/big"
"net/http"
"time"
"github.com/edgelesssys/ego/enclave"
)
// serverAddr is the address of the server
const serverAddr = "0.0.0.0:8080"
// attestationProviderURL is the URL of the attestation provider
const attestationProviderURL = "https://shareduks.uks.attest.azure.net"
func main() {
// Create a self signed certificate.
cert, priv := createCertificate()
fmt.Println("🆗 Generated Certificate.")
// Cerate an Azure Attestation Token.
token, err := enclave.CreateAzureAttestationToken(cert, attestationProviderURL)
if err != nil {
panic(err)
}
fmt.Println("🆗 Created an Microsoft Azure Attestation Token.")
// Create HTTPS server.
http.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(token)) })
http.HandleFunc("/secret", func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("📫 %v sent secret %v\n", r.RemoteAddr, r.URL.Query()["s"])
})
tlsCfg := tls.Config{
Certificates: []tls.Certificate{
{
Certificate: [][]byte{cert},
PrivateKey: priv,
},
},
}
server := http.Server{Addr: serverAddr, TLSConfig: &tlsCfg}
fmt.Printf("📎 Token now available under https://%s/token\n", serverAddr)
fmt.Printf("👂 Listening on https://%s/secret for secrets...\n", serverAddr)
err = server.ListenAndServeTLS("", "")
fmt.Println(err)
}
func createCertificate() ([]byte, crypto.PrivateKey) {
template := &x509.Certificate{
SerialNumber: &big.Int{},
Subject: pkix.Name{CommonName: "localhost"},
NotAfter: time.Now().Add(time.Hour),
DNSNames: []string{"localhost"},
}
priv, _ := rsa.GenerateKey(rand.Reader, 2048)
cert, _ := x509.CreateCertificate(rand.Reader, template, template, &priv.PublicKey, priv)
return cert, priv
}