1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
5 // Generate and verify permission signatures for Keep locators.
7 // See https://dev.arvados.org/projects/arvados/wiki/Keep_locator_format
23 // ErrSignatureExpired - a signature was rejected because the
24 // expiry time has passed.
25 ErrSignatureExpired = errors.New("Signature expired")
26 // ErrSignatureInvalid - a signature was rejected because it
27 // was badly formatted or did not match the given secret key.
28 ErrSignatureInvalid = errors.New("Invalid signature")
29 // ErrSignatureMissing - the given locator does not have a
31 ErrSignatureMissing = errors.New("Missing signature")
34 // makePermSignature generates a SHA-1 HMAC digest for the given blob,
35 // token, expiry, and site secret.
36 func makePermSignature(blobHash []byte, apiToken, expiry, blobSignatureTTL string, permissionSecret []byte) string {
37 hmac := hmac.New(sha1.New, permissionSecret)
39 hmac.Write([]byte("@"))
40 hmac.Write([]byte(apiToken))
41 hmac.Write([]byte("@"))
42 hmac.Write([]byte(expiry))
43 hmac.Write([]byte("@"))
44 hmac.Write([]byte(blobSignatureTTL))
45 digest := hmac.Sum(nil)
46 return fmt.Sprintf("%x", digest)
50 mBlkRe = regexp.MustCompile(`^[0-9a-f]{32}.*`)
51 mPermHintRe = regexp.MustCompile(`\+A[^+]*`)
54 // SignManifest signs all locators in the given manifest, discarding
55 // any existing signatures.
56 func SignManifest(manifest string, apiToken string, expiry time.Time, ttl time.Duration, permissionSecret []byte) string {
57 return regexp.MustCompile(`\S+`).ReplaceAllStringFunc(manifest, func(tok string) string {
58 if mBlkRe.MatchString(tok) {
59 return SignLocator(mPermHintRe.ReplaceAllString(tok, ""), apiToken, expiry, ttl, permissionSecret)
65 // SignLocator returns blobLocator with a permission signature
66 // added. If either permissionSecret or apiToken is empty, blobLocator
67 // is returned untouched.
69 // This function is intended to be used by system components and admin
70 // utilities: userland programs do not know the permissionSecret.
71 func SignLocator(blobLocator, apiToken string, expiry time.Time, blobSignatureTTL time.Duration, permissionSecret []byte) string {
72 if len(permissionSecret) == 0 || apiToken == "" {
75 // Strip off all hints: only the hash is used to sign.
76 blobHash := []byte(blobLocator)
77 if hints := bytes.IndexRune(blobHash, '+'); hints > 0 {
78 blobHash = blobHash[:hints]
80 timestampHex := fmt.Sprintf("%08x", expiry.Unix())
81 blobSignatureTTLHex := strconv.FormatInt(int64(blobSignatureTTL.Seconds()), 16)
83 "+A" + makePermSignature(blobHash, apiToken, timestampHex, blobSignatureTTLHex, permissionSecret) +
87 var SignedLocatorRe = regexp.MustCompile(
89 `^([[:xdigit:]]{32})(\+[0-9]+)?((\+[B-Z][A-Za-z0-9@_-]*)*)(\+A([[:xdigit:]]{40})@([[:xdigit:]]{8}))((\+[B-Z][A-Za-z0-9@_-]*)*)$`)
91 // VerifySignature returns nil if the signature on the signedLocator
92 // can be verified using the given apiToken. Otherwise it returns
93 // ErrSignatureExpired (if the signature's expiry time has passed,
94 // which is something the client could have figured out
95 // independently), ErrSignatureMissing (if there is no signature hint
96 // at all), or ErrSignatureInvalid (if the signature is present but
97 // badly formatted or incorrect).
99 // This function is intended to be used by system components and admin
100 // utilities: userland programs do not know the permissionSecret.
101 func VerifySignature(signedLocator, apiToken string, blobSignatureTTL time.Duration, permissionSecret []byte) error {
102 matches := SignedLocatorRe.FindStringSubmatch(signedLocator)
104 return ErrSignatureMissing
106 blobHash := []byte(matches[1])
107 signatureHex := matches[6]
108 expiryHex := matches[7]
109 if expiryTime, err := parseHexTimestamp(expiryHex); err != nil {
110 return ErrSignatureInvalid
111 } else if expiryTime.Before(time.Now()) {
112 return ErrSignatureExpired
114 blobSignatureTTLHex := strconv.FormatInt(int64(blobSignatureTTL.Seconds()), 16)
115 if signatureHex != makePermSignature(blobHash, apiToken, expiryHex, blobSignatureTTLHex, permissionSecret) {
116 return ErrSignatureInvalid
121 func parseHexTimestamp(timestampHex string) (ts time.Time, err error) {
122 if tsInt, e := strconv.ParseInt(timestampHex, 16, 0); e == nil {
123 ts = time.Unix(tsInt, 0)