one_pass_signature.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package packet
  5. import (
  6. "crypto"
  7. "encoding/binary"
  8. "golang.org/x/crypto/openpgp/errors"
  9. "golang.org/x/crypto/openpgp/s2k"
  10. "io"
  11. "strconv"
  12. )
  13. // OnePassSignature represents a one-pass signature packet. See RFC 4880,
  14. // section 5.4.
  15. type OnePassSignature struct {
  16. SigType SignatureType
  17. Hash crypto.Hash
  18. PubKeyAlgo PublicKeyAlgorithm
  19. KeyId uint64
  20. IsLast bool
  21. }
  22. const onePassSignatureVersion = 3
  23. func (ops *OnePassSignature) parse(r io.Reader) (err error) {
  24. var buf [13]byte
  25. _, err = readFull(r, buf[:])
  26. if err != nil {
  27. return
  28. }
  29. if buf[0] != onePassSignatureVersion {
  30. err = errors.UnsupportedError("one-pass-signature packet version " + strconv.Itoa(int(buf[0])))
  31. }
  32. var ok bool
  33. ops.Hash, ok = s2k.HashIdToHash(buf[2])
  34. if !ok {
  35. return errors.UnsupportedError("hash function: " + strconv.Itoa(int(buf[2])))
  36. }
  37. ops.SigType = SignatureType(buf[1])
  38. ops.PubKeyAlgo = PublicKeyAlgorithm(buf[3])
  39. ops.KeyId = binary.BigEndian.Uint64(buf[4:12])
  40. ops.IsLast = buf[12] != 0
  41. return
  42. }
  43. // Serialize marshals the given OnePassSignature to w.
  44. func (ops *OnePassSignature) Serialize(w io.Writer) error {
  45. var buf [13]byte
  46. buf[0] = onePassSignatureVersion
  47. buf[1] = uint8(ops.SigType)
  48. var ok bool
  49. buf[2], ok = s2k.HashToHashId(ops.Hash)
  50. if !ok {
  51. return errors.UnsupportedError("hash type: " + strconv.Itoa(int(ops.Hash)))
  52. }
  53. buf[3] = uint8(ops.PubKeyAlgo)
  54. binary.BigEndian.PutUint64(buf[4:12], ops.KeyId)
  55. if ops.IsLast {
  56. buf[12] = 1
  57. }
  58. if err := serializeHeader(w, packetTypeOnePassSignature, len(buf)); err != nil {
  59. return err
  60. }
  61. _, err := w.Write(buf[:])
  62. return err
  63. }