Skip to content

Commit a340c5c

Browse files
authored
update CF URLSigner to use crypto.Signer interface (#2087)
CloudFront URLSigner uses *rsa.PrivateKey to sign the URL. If we update the function to receive the crypto.Signer interface (already implemented by rsa.PrivateKey) then we can use other keys to sign. For example hardware keys.
1 parent c068d16 commit a340c5c

2 files changed

Lines changed: 14 additions & 15 deletions

File tree

feature/cloudfront/sign/policy.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"crypto"
66
"crypto/rand"
7-
"crypto/rsa"
87
"crypto/sha1"
98
"encoding/base64"
109
"encoding/json"
@@ -92,7 +91,7 @@ var randReader = rand.Reader
9291
// The signature and policy should be added to the signed URL following the
9392
// guidelines in:
9493
// http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-signed-urls.html
95-
func (p *Policy) Sign(privKey *rsa.PrivateKey) (b64Signature, b64Policy []byte, err error) {
94+
func (p *Policy) Sign(signer crypto.Signer) (b64Signature, b64Policy []byte, err error) {
9695
if err = p.Validate(); err != nil {
9796
return nil, nil, err
9897
}
@@ -105,7 +104,7 @@ func (p *Policy) Sign(privKey *rsa.PrivateKey) (b64Signature, b64Policy []byte,
105104
awsEscapeEncoded(b64Policy)
106105

107106
// Build and escape the signature
108-
b64Signature, err = signEncodedPolicy(randReader, jsonPolicy, privKey)
107+
b64Signature, err = signEncodedPolicy(randReader, jsonPolicy, signer)
109108
if err != nil {
110109
return nil, nil, err
111110
}
@@ -199,13 +198,13 @@ func encodePolicy(p *Policy) (b64Policy, jsonPolicy []byte, err error) {
199198
}
200199

201200
// signEncodedPolicy will sign and base 64 encode the JSON encoded policy.
202-
func signEncodedPolicy(randReader io.Reader, jsonPolicy []byte, privKey *rsa.PrivateKey) ([]byte, error) {
201+
func signEncodedPolicy(randReader io.Reader, jsonPolicy []byte, signer crypto.Signer) ([]byte, error) {
203202
hash := sha1.New()
204203
if _, err := bytes.NewReader(jsonPolicy).WriteTo(hash); err != nil {
205204
return nil, fmt.Errorf("failed to calculate signing hash, %s", err.Error())
206205
}
207206

208-
sig, err := rsa.SignPKCS1v15(randReader, privKey, crypto.SHA1, hash.Sum(nil))
207+
sig, err := signer.Sign(randReader, hash.Sum(nil), crypto.SHA1)
209208
if err != nil {
210209
return nil, fmt.Errorf("failed to sign policy, %s", err.Error())
211210
}

feature/cloudfront/sign/sign_url.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
package sign
1919

2020
import (
21-
"crypto/rsa"
21+
"crypto"
2222
"fmt"
2323
"net/url"
2424
"strings"
@@ -31,16 +31,16 @@ import (
3131
//
3232
// The signer is safe to use concurrently.
3333
type URLSigner struct {
34-
keyID string
35-
privKey *rsa.PrivateKey
34+
keyID string
35+
signer crypto.Signer
3636
}
3737

3838
// NewURLSigner constructs and returns a new URLSigner to be used to for signing
3939
// Amazon CloudFront URL resources with.
40-
func NewURLSigner(keyID string, privKey *rsa.PrivateKey) *URLSigner {
40+
func NewURLSigner(keyID string, signer crypto.Signer) *URLSigner {
4141
return &URLSigner{
42-
keyID: keyID,
43-
privKey: privKey,
42+
keyID: keyID,
43+
signer: signer,
4444
}
4545
}
4646

@@ -70,7 +70,7 @@ func (s URLSigner) Sign(url string, expires time.Time) (string, error) {
7070
return "", err
7171
}
7272

73-
return signURL(scheme, cleanedURL, s.keyID, NewCannedPolicy(resource, expires), false, s.privKey)
73+
return signURL(scheme, cleanedURL, s.keyID, NewCannedPolicy(resource, expires), false, s.signer)
7474
}
7575

7676
// SignWithPolicy will sign a URL with the Policy provided. The URL will be
@@ -114,16 +114,16 @@ func (s URLSigner) SignWithPolicy(url string, p *Policy) (string, error) {
114114
return "", err
115115
}
116116

117-
return signURL(scheme, cleanedURL, s.keyID, p, true, s.privKey)
117+
return signURL(scheme, cleanedURL, s.keyID, p, true, s.signer)
118118
}
119119

120-
func signURL(scheme, url, keyID string, p *Policy, customPolicy bool, privKey *rsa.PrivateKey) (string, error) {
120+
func signURL(scheme, url, keyID string, p *Policy, customPolicy bool, signer crypto.Signer) (string, error) {
121121
// Validation URL elements
122122
if err := validateURL(url); err != nil {
123123
return "", err
124124
}
125125

126-
b64Signature, b64Policy, err := p.Sign(privKey)
126+
b64Signature, b64Policy, err := p.Sign(signer)
127127
if err != nil {
128128
return "", err
129129
}

0 commit comments

Comments
 (0)