2015-06-11 22:13:43 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-01-27 01:01:39 +00:00
|
|
|
"crypto"
|
2015-10-23 14:24:02 +00:00
|
|
|
"crypto/ecdsa"
|
2016-01-27 01:01:39 +00:00
|
|
|
"crypto/elliptic"
|
2015-06-11 22:13:43 +00:00
|
|
|
"crypto/rsa"
|
2015-09-26 17:45:52 +00:00
|
|
|
"fmt"
|
2015-06-11 22:13:43 +00:00
|
|
|
"net/http"
|
2016-08-18 20:35:03 +00:00
|
|
|
"sync"
|
2015-06-11 22:13:43 +00:00
|
|
|
|
2018-05-27 21:55:19 +00:00
|
|
|
"gopkg.in/square/go-jose.v2"
|
2015-06-11 22:13:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type jws struct {
|
2018-05-27 21:55:19 +00:00
|
|
|
getNonceURL string
|
|
|
|
privKey crypto.PrivateKey
|
|
|
|
kid string
|
|
|
|
nonces nonceManager
|
2015-10-23 14:24:02 +00:00
|
|
|
}
|
|
|
|
|
2017-01-15 15:54:49 +00:00
|
|
|
// Posts a JWS signed message to the specified URL.
|
|
|
|
// It does NOT close the response body, so the caller must
|
|
|
|
// do that if no error was returned.
|
2015-06-11 22:13:43 +00:00
|
|
|
func (j *jws) post(url string, content []byte) (*http.Response, error) {
|
2018-05-27 21:55:19 +00:00
|
|
|
signedContent, err := j.signContent(url, content)
|
2015-09-26 17:45:52 +00:00
|
|
|
if err != nil {
|
2018-05-28 16:31:28 +00:00
|
|
|
return nil, fmt.Errorf("failed to sign content -> %s", err.Error())
|
2015-09-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 21:55:19 +00:00
|
|
|
data := bytes.NewBuffer([]byte(signedContent.FullSerialize()))
|
|
|
|
resp, err := httpPost(url, "application/jose+json", data)
|
2017-03-17 18:58:44 +00:00
|
|
|
if err != nil {
|
2018-05-28 16:31:28 +00:00
|
|
|
return nil, fmt.Errorf("failed to HTTP POST to %s -> %s", url, err.Error())
|
2017-03-17 18:58:44 +00:00
|
|
|
}
|
2017-03-30 00:25:34 +00:00
|
|
|
|
2017-02-19 03:48:45 +00:00
|
|
|
nonce, nonceErr := getNonceFromResponse(resp)
|
|
|
|
if nonceErr == nil {
|
|
|
|
j.nonces.Push(nonce)
|
|
|
|
}
|
2015-09-26 17:45:52 +00:00
|
|
|
|
2017-02-19 04:50:21 +00:00
|
|
|
return resp, nil
|
2015-09-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 21:55:19 +00:00
|
|
|
func (j *jws) signContent(url string, content []byte) (*jose.JSONWebSignature, error) {
|
2016-01-27 01:01:39 +00:00
|
|
|
|
|
|
|
var alg jose.SignatureAlgorithm
|
|
|
|
switch k := j.privKey.(type) {
|
|
|
|
case *rsa.PrivateKey:
|
|
|
|
alg = jose.RS256
|
|
|
|
case *ecdsa.PrivateKey:
|
|
|
|
if k.Curve == elliptic.P256() {
|
|
|
|
alg = jose.ES256
|
|
|
|
} else if k.Curve == elliptic.P384() {
|
|
|
|
alg = jose.ES384
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 21:55:19 +00:00
|
|
|
jsonKey := jose.JSONWebKey{
|
|
|
|
Key: j.privKey,
|
|
|
|
KeyID: j.kid,
|
|
|
|
}
|
|
|
|
|
|
|
|
signKey := jose.SigningKey{
|
|
|
|
Algorithm: alg,
|
|
|
|
Key: jsonKey,
|
|
|
|
}
|
|
|
|
options := jose.SignerOptions{
|
|
|
|
NonceSource: j,
|
|
|
|
ExtraHeaders: make(map[jose.HeaderKey]interface{}),
|
|
|
|
}
|
|
|
|
options.ExtraHeaders["url"] = url
|
|
|
|
if j.kid == "" {
|
|
|
|
options.EmbedJWK = true
|
|
|
|
}
|
|
|
|
|
|
|
|
signer, err := jose.NewSigner(signKey, &options)
|
2015-06-11 22:13:43 +00:00
|
|
|
if err != nil {
|
2018-05-28 16:31:28 +00:00
|
|
|
return nil, fmt.Errorf("failed to create jose signer -> %s", err.Error())
|
2015-06-11 22:13:43 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 01:06:22 +00:00
|
|
|
signed, err := signer.Sign(content)
|
2015-06-11 22:13:43 +00:00
|
|
|
if err != nil {
|
2018-05-28 16:31:28 +00:00
|
|
|
return nil, fmt.Errorf("failed to sign content -> %s", err.Error())
|
2015-06-11 22:13:43 +00:00
|
|
|
}
|
2015-09-26 17:45:52 +00:00
|
|
|
return signed, nil
|
|
|
|
}
|
|
|
|
|
2018-05-27 21:55:19 +00:00
|
|
|
func (j *jws) signEABContent(url, kid string, hmac []byte) (*jose.JSONWebSignature, error) {
|
|
|
|
jwk := jose.JSONWebKey{Key: j.privKey}
|
|
|
|
jwkJSON, err := jwk.Public().MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("acme: error encoding eab jwk key: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
signer, err := jose.NewSigner(
|
|
|
|
jose.SigningKey{Algorithm: jose.HS256, Key: hmac},
|
|
|
|
&jose.SignerOptions{
|
|
|
|
EmbedJWK: false,
|
|
|
|
ExtraHeaders: map[jose.HeaderKey]interface{}{
|
|
|
|
"kid": kid,
|
|
|
|
"url": url,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2018-05-28 16:31:28 +00:00
|
|
|
return nil, fmt.Errorf("failed to create External Account Binding jose signer -> %s", err.Error())
|
2018-05-27 21:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
signed, err := signer.Sign(jwkJSON)
|
|
|
|
if err != nil {
|
2018-05-28 16:31:28 +00:00
|
|
|
return nil, fmt.Errorf("failed to External Account Binding sign content -> %s", err.Error())
|
2018-05-27 21:55:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return signed, nil
|
|
|
|
}
|
|
|
|
|
2017-02-19 03:48:45 +00:00
|
|
|
func (j *jws) Nonce() (string, error) {
|
|
|
|
if nonce, ok := j.nonces.Pop(); ok {
|
|
|
|
return nonce, nil
|
2015-09-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-27 21:55:19 +00:00
|
|
|
return getNonce(j.getNonceURL)
|
2015-09-26 17:45:52 +00:00
|
|
|
}
|
2015-06-11 22:13:43 +00:00
|
|
|
|
2017-02-19 03:48:45 +00:00
|
|
|
type nonceManager struct {
|
|
|
|
nonces []string
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *nonceManager) Pop() (string, bool) {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
|
|
|
|
if len(n.nonces) == 0 {
|
|
|
|
return "", false
|
2015-06-11 22:13:43 +00:00
|
|
|
}
|
|
|
|
|
2017-02-19 03:48:45 +00:00
|
|
|
nonce := n.nonces[len(n.nonces)-1]
|
|
|
|
n.nonces = n.nonces[:len(n.nonces)-1]
|
|
|
|
return nonce, true
|
2015-09-26 17:45:52 +00:00
|
|
|
}
|
|
|
|
|
2017-02-19 03:48:45 +00:00
|
|
|
func (n *nonceManager) Push(nonce string) {
|
|
|
|
n.Lock()
|
|
|
|
defer n.Unlock()
|
|
|
|
n.nonces = append(n.nonces, nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNonce(url string) (string, error) {
|
|
|
|
resp, err := httpHead(url)
|
|
|
|
if err != nil {
|
2018-05-28 16:31:28 +00:00
|
|
|
return "", fmt.Errorf("failed to get nonce from HTTP HEAD -> %s", err.Error())
|
2015-09-26 17:45:52 +00:00
|
|
|
}
|
2017-02-19 03:48:45 +00:00
|
|
|
|
|
|
|
return getNonceFromResponse(resp)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNonceFromResponse(resp *http.Response) (string, error) {
|
|
|
|
nonce := resp.Header.Get("Replay-Nonce")
|
|
|
|
if nonce == "" {
|
2018-05-28 16:31:28 +00:00
|
|
|
return "", fmt.Errorf("server did not respond with a proper nonce header")
|
2016-04-11 04:22:00 +00:00
|
|
|
}
|
2017-02-19 03:48:45 +00:00
|
|
|
|
2015-11-12 01:06:22 +00:00
|
|
|
return nonce, nil
|
2015-06-11 22:13:43 +00:00
|
|
|
}
|