1 // Generate and verify permission signatures for Keep locators.
3 // See https://dev.arvados.org/projects/arvados/wiki/Keep_locator_format
19 // ErrSignatureExpired - a signature was rejected because the
20 // expiry time has passed.
21 ErrSignatureExpired = errors.New("Signature expired")
22 // ErrSignatureInvalid - a signature was rejected because it
23 // was badly formatted or did not match the given secret key.
24 ErrSignatureInvalid = errors.New("Invalid signature")
25 // ErrSignatureMissing - the given locator does not have a
27 ErrSignatureMissing = errors.New("Missing signature")
30 // makePermSignature generates a SHA-1 HMAC digest for the given blob,
31 // token, expiry, and site secret.
32 func makePermSignature(blobHash, apiToken, expiry string, permissionSecret []byte) string {
33 hmac := hmac.New(sha1.New, permissionSecret)
34 hmac.Write([]byte(blobHash))
35 hmac.Write([]byte("@"))
36 hmac.Write([]byte(apiToken))
37 hmac.Write([]byte("@"))
38 hmac.Write([]byte(expiry))
39 digest := hmac.Sum(nil)
40 return fmt.Sprintf("%x", digest)
43 // SignLocator returns blobLocator with a permission signature
44 // added. If either permissionSecret or apiToken is empty, blobLocator
45 // is returned untouched.
47 // This function is intended to be used by system components and admin
48 // utilities: userland programs do not know the permissionSecret.
49 func SignLocator(blobLocator, apiToken string, expiry time.Time, permissionSecret []byte) string {
50 if len(permissionSecret) == 0 || apiToken == "" {
53 // Strip off all hints: only the hash is used to sign.
54 blobHash := strings.Split(blobLocator, "+")[0]
55 timestampHex := fmt.Sprintf("%08x", expiry.Unix())
57 "+A" + makePermSignature(blobHash, apiToken, timestampHex, permissionSecret) +
61 var signedLocatorRe = regexp.MustCompile(`^([[:xdigit:]]{32}).*\+A([[:xdigit:]]{40})@([[:xdigit:]]{8})`)
63 // VerifySignature returns nil if the signature on the signedLocator
64 // can be verified using the given apiToken. Otherwise it returns
65 // ErrSignatureExpired (if the signature's expiry time has passed,
66 // which is something the client could have figured out
67 // independently), ErrSignatureMissing (if there is no signature hint
68 // at all), or ErrSignatureInvalid (if the signature is present but
69 // badly formatted or incorrect).
71 // This function is intended to be used by system components and admin
72 // utilities: userland programs do not know the permissionSecret.
73 func VerifySignature(signedLocator, apiToken string, permissionSecret []byte) error {
74 matches := signedLocatorRe.FindStringSubmatch(signedLocator)
76 return ErrSignatureMissing
78 blobHash := matches[1]
79 signatureHex := matches[2]
80 expiryHex := matches[3]
81 if expiryTime, err := parseHexTimestamp(expiryHex); err != nil {
82 return ErrSignatureInvalid
83 } else if expiryTime.Before(time.Now()) {
84 return ErrSignatureExpired
86 if signatureHex != makePermSignature(blobHash, apiToken, expiryHex, permissionSecret) {
87 return ErrSignatureInvalid
92 func parseHexTimestamp(timestampHex string) (ts time.Time, err error) {
93 if tsInt, e := strconv.ParseInt(timestampHex, 16, 0); e == nil {
94 ts = time.Unix(tsInt, 0)