2015-06-10 22:12:46 +00:00
|
|
|
package acme
|
|
|
|
|
2015-06-13 01:55:53 +00:00
|
|
|
import (
|
2015-10-23 14:24:02 +00:00
|
|
|
"crypto/rsa"
|
2015-06-13 01:55:53 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/pem"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2015-06-13 17:13:04 +00:00
|
|
|
"strings"
|
2015-06-13 01:55:53 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-11-02 23:02:47 +00:00
|
|
|
// OnSimpleHTTPStart hook will get called BEFORE SimpleHTTP starts to listen on a port.
|
|
|
|
var OnSimpleHTTPStart func(string)
|
|
|
|
|
|
|
|
// OnSimpleHTTPEnd hook will get called AFTER SimpleHTTP determined the status of the domain.
|
|
|
|
var OnSimpleHTTPEnd func(bool)
|
|
|
|
|
2015-06-13 01:55:53 +00:00
|
|
|
type simpleHTTPChallenge struct {
|
|
|
|
jws *jws
|
|
|
|
optPort string
|
|
|
|
}
|
2015-06-10 22:12:46 +00:00
|
|
|
|
2015-06-13 01:55:53 +00:00
|
|
|
func (s *simpleHTTPChallenge) Solve(chlng challenge, domain string) error {
|
|
|
|
|
2015-11-06 06:43:42 +00:00
|
|
|
logf("Trying to solve SimpleHTTP")
|
2015-06-13 01:55:53 +00:00
|
|
|
|
2015-06-13 02:26:33 +00:00
|
|
|
// Generate random string for the path. The acme server will
|
|
|
|
// access this path on the server in order to validate the request
|
2015-09-26 17:45:52 +00:00
|
|
|
listener, err := s.startHTTPSServer(domain, chlng.Token)
|
2015-06-13 01:55:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not start HTTPS server for challenge -> %v", err)
|
|
|
|
}
|
2015-10-22 04:16:29 +00:00
|
|
|
defer listener.Close()
|
2015-06-13 01:55:53 +00:00
|
|
|
|
2015-06-13 02:26:33 +00:00
|
|
|
// Tell the server about the generated random path
|
2015-09-26 17:45:52 +00:00
|
|
|
jsonBytes, err := json.Marshal(challenge{Resource: "challenge", Type: chlng.Type, Token: chlng.Token})
|
2015-06-13 01:55:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.New("Failed to marshal network message...")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.jws.post(chlng.URI, jsonBytes)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to post JWS message. -> %v", err)
|
|
|
|
}
|
|
|
|
|
2015-06-13 02:26:33 +00:00
|
|
|
// After the path is sent, the ACME server will access our server.
|
|
|
|
// Repeatedly check the server for an updated status on our request.
|
2015-06-13 01:55:53 +00:00
|
|
|
var challengeResponse challenge
|
2015-10-22 04:16:29 +00:00
|
|
|
Loop:
|
2015-06-13 01:55:53 +00:00
|
|
|
for {
|
2015-10-29 00:42:05 +00:00
|
|
|
if resp.StatusCode >= http.StatusBadRequest {
|
|
|
|
return handleHTTPError(resp)
|
|
|
|
}
|
|
|
|
|
2015-10-22 04:16:29 +00:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(&challengeResponse)
|
|
|
|
resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-13 01:55:53 +00:00
|
|
|
|
|
|
|
switch challengeResponse.Status {
|
|
|
|
case "valid":
|
2015-11-02 23:02:47 +00:00
|
|
|
if OnSimpleHTTPEnd != nil {
|
|
|
|
OnSimpleHTTPEnd(true)
|
|
|
|
}
|
2015-11-06 06:43:42 +00:00
|
|
|
logf("The server validated our request")
|
2015-10-22 04:16:29 +00:00
|
|
|
break Loop
|
2015-06-13 01:55:53 +00:00
|
|
|
case "pending":
|
|
|
|
break
|
|
|
|
case "invalid":
|
2015-11-02 23:02:47 +00:00
|
|
|
if OnSimpleHTTPEnd != nil {
|
2015-11-03 19:13:20 +00:00
|
|
|
OnSimpleHTTPEnd(false)
|
2015-11-02 23:02:47 +00:00
|
|
|
}
|
2015-06-13 01:55:53 +00:00
|
|
|
return errors.New("The server could not validate our request.")
|
|
|
|
default:
|
|
|
|
return errors.New("The server returned an unexpected state.")
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
resp, err = http.Get(chlng.URI)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Starts a temporary HTTPS server on port 443. As soon as the challenge passed validation,
|
|
|
|
// this server will get shut down. The certificate generated here is only held in memory.
|
2015-09-26 17:45:52 +00:00
|
|
|
func (s *simpleHTTPChallenge) startHTTPSServer(domain string, token string) (net.Listener, error) {
|
2015-06-13 02:26:33 +00:00
|
|
|
|
|
|
|
// Generate a new RSA key and a self-signed certificate.
|
2015-10-23 14:24:02 +00:00
|
|
|
tempPrivKey, err := generatePrivateKey(rsakey, 2048)
|
|
|
|
rsaPrivKey := tempPrivKey.(*rsa.PrivateKey)
|
2015-06-13 01:55:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-23 14:24:02 +00:00
|
|
|
tempCertPEM, err := generatePemCert(rsaPrivKey, domain)
|
2015-06-13 01:55:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-10-23 14:24:02 +00:00
|
|
|
pemBytes := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(rsaPrivKey)})
|
2015-06-13 01:55:53 +00:00
|
|
|
tempKeyPair, err := tls.X509KeyPair(
|
|
|
|
tempCertPEM,
|
|
|
|
pemBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConf := new(tls.Config)
|
|
|
|
tlsConf.Certificates = []tls.Certificate{tempKeyPair}
|
|
|
|
|
2015-09-26 17:45:52 +00:00
|
|
|
path := "/.well-known/acme-challenge/" + token
|
2015-11-02 23:02:47 +00:00
|
|
|
if OnSimpleHTTPStart != nil {
|
|
|
|
OnSimpleHTTPStart(path)
|
|
|
|
}
|
2015-06-13 01:55:53 +00:00
|
|
|
|
2015-06-13 02:26:33 +00:00
|
|
|
// Allow for CLI override
|
2015-06-13 01:55:53 +00:00
|
|
|
port := ":443"
|
|
|
|
if s.optPort != "" {
|
|
|
|
port = ":" + s.optPort
|
|
|
|
}
|
2015-06-13 02:26:33 +00:00
|
|
|
|
2015-11-03 19:13:20 +00:00
|
|
|
tlsListener, err := tls.Listen("tcp", domain+port, tlsConf)
|
2015-06-13 01:55:53 +00:00
|
|
|
if err != nil {
|
2015-11-09 17:41:27 +00:00
|
|
|
// if the domain:port bind failed, fall back to :port bind and try that instead.
|
|
|
|
tlsListener, err = tls.Listen("tcp", port, tlsConf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-06-13 01:55:53 +00:00
|
|
|
}
|
|
|
|
|
2015-10-18 00:16:15 +00:00
|
|
|
jsonBytes, err := json.Marshal(challenge{Type: "simpleHttp", Token: token, TLS: true})
|
2015-09-26 17:45:52 +00:00
|
|
|
if err != nil {
|
2015-10-18 00:16:15 +00:00
|
|
|
return nil, errors.New("startHTTPSServer: Failed to marshal network message")
|
2015-09-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
signed, err := s.jws.signContent(jsonBytes)
|
|
|
|
if err != nil {
|
2015-10-30 12:13:05 +00:00
|
|
|
return nil, fmt.Errorf("startHTTPSServer: Failed to sign message. %s", err)
|
2015-09-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
signedCompact := signed.FullSerialize()
|
|
|
|
if err != nil {
|
2015-10-18 00:16:15 +00:00
|
|
|
return nil, errors.New("startHTTPSServer: Failed to serialize message")
|
2015-09-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
|
2015-06-13 02:26:33 +00:00
|
|
|
// The handler validates the HOST header and request type.
|
|
|
|
// For validation it then writes the token the server returned with the challenge
|
2015-06-13 01:55:53 +00:00
|
|
|
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
2015-09-26 17:45:52 +00:00
|
|
|
if strings.HasPrefix(r.Host, domain) && r.Method == "GET" {
|
|
|
|
w.Header().Add("Content-Type", "application/jose+json")
|
|
|
|
w.Write([]byte(signedCompact))
|
2015-11-06 06:43:42 +00:00
|
|
|
logf("Served JWS payload...")
|
2015-09-26 17:45:52 +00:00
|
|
|
} else {
|
2015-11-06 06:43:42 +00:00
|
|
|
logf("Received request for domain %s with method %s", r.Host, r.Method)
|
2015-09-26 17:45:52 +00:00
|
|
|
w.Write([]byte("TEST"))
|
2015-06-13 01:55:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
go http.Serve(tlsListener, nil)
|
|
|
|
|
|
|
|
return tlsListener, nil
|
|
|
|
}
|