You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

verify.go 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // Copyright 2021 The Sigstore Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package sshsig
  16. import (
  17. "io"
  18. "golang.org/x/crypto/ssh"
  19. )
  20. // Verify verifies the signature of the given data and the armored signature using the given public key and the namespace.
  21. // If the namespace is empty, the default namespace (file) is used.
  22. func Verify(message io.Reader, armoredSignature []byte, publicKey []byte, namespace string) error {
  23. if namespace == "" {
  24. namespace = defaultNamespace
  25. }
  26. decodedSignature, err := Decode(armoredSignature)
  27. if err != nil {
  28. return err
  29. }
  30. desiredPk, _, _, _, err := ssh.ParseAuthorizedKey(publicKey)
  31. if err != nil {
  32. return err
  33. }
  34. // Hash the message so we can verify it against the signature.
  35. h := supportedHashAlgorithms[decodedSignature.hashAlg]()
  36. if _, err := io.Copy(h, message); err != nil {
  37. return err
  38. }
  39. hm := h.Sum(nil)
  40. toVerify := MessageWrapper{
  41. Namespace: namespace,
  42. HashAlgorithm: decodedSignature.hashAlg,
  43. Hash: string(hm),
  44. }
  45. signedMessage := ssh.Marshal(toVerify)
  46. signedMessage = append([]byte(magicHeader), signedMessage...)
  47. return desiredPk.Verify(signedMessage, decodedSignature.signature)
  48. }