-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
40 lines (36 loc) · 976 Bytes
/
authentication.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
package main
import (
"crypto"
"crypto/rsa"
"crypto/sha512"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"errors"
)
// ParsePublicKey parses a public PEM file into a proper
// PublicKey struct
func ParsePublicKey(pubPEM string) (*rsa.PublicKey, error) {
block, _ := pem.Decode([]byte(pubPEM))
if block == nil {
err := errors.New("Failed to parse PEM block containing the public key")
return nil, err
}
pubKey, err := x509.ParsePKIXPublicKey(block.Bytes)
return pubKey.(*rsa.PublicKey), err
}
// VerifySignature verifies the signature sent with the request with
// the pem we retried from the Chef server
func VerifySignature(encodedSig, pubPEM string) error {
pubKey, err := ParsePublicKey(pubPEM)
if err != nil {
return err
}
hashed := sha512.Sum512([]byte(pubPEM))
signature, err := base64.StdEncoding.DecodeString(encodedSig)
if err != nil {
return err
}
err = rsa.VerifyPKCS1v15(pubKey, crypto.SHA512, hashed[:], signature)
return err
}