certs.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. // Copyright 2012 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 ssh
  5. import (
  6. "bytes"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "net"
  11. "sort"
  12. "time"
  13. )
  14. // Certificate algorithm names from [PROTOCOL.certkeys]. These values can appear
  15. // in Certificate.Type, PublicKey.Type, and ClientConfig.HostKeyAlgorithms.
  16. // Unlike key algorithm names, these are not passed to AlgorithmSigner nor
  17. // returned by MultiAlgorithmSigner and don't appear in the Signature.Format
  18. // field.
  19. const (
  20. CertAlgoRSAv01 = "ssh-rsa-cert-v01@openssh.com"
  21. // Deprecated: DSA is only supported at insecure key sizes, and was removed
  22. // from major implementations.
  23. CertAlgoDSAv01 = InsecureCertAlgoDSAv01
  24. // Deprecated: DSA is only supported at insecure key sizes, and was removed
  25. // from major implementations.
  26. InsecureCertAlgoDSAv01 = "ssh-dss-cert-v01@openssh.com"
  27. CertAlgoECDSA256v01 = "ecdsa-sha2-nistp256-cert-v01@openssh.com"
  28. CertAlgoECDSA384v01 = "ecdsa-sha2-nistp384-cert-v01@openssh.com"
  29. CertAlgoECDSA521v01 = "ecdsa-sha2-nistp521-cert-v01@openssh.com"
  30. CertAlgoSKECDSA256v01 = "sk-ecdsa-sha2-nistp256-cert-v01@openssh.com"
  31. CertAlgoED25519v01 = "ssh-ed25519-cert-v01@openssh.com"
  32. CertAlgoSKED25519v01 = "sk-ssh-ed25519-cert-v01@openssh.com"
  33. // CertAlgoRSASHA256v01 and CertAlgoRSASHA512v01 can't appear as a
  34. // Certificate.Type (or PublicKey.Type), but only in
  35. // ClientConfig.HostKeyAlgorithms.
  36. CertAlgoRSASHA256v01 = "rsa-sha2-256-cert-v01@openssh.com"
  37. CertAlgoRSASHA512v01 = "rsa-sha2-512-cert-v01@openssh.com"
  38. )
  39. const (
  40. // Deprecated: use CertAlgoRSAv01.
  41. CertSigAlgoRSAv01 = CertAlgoRSAv01
  42. // Deprecated: use CertAlgoRSASHA256v01.
  43. CertSigAlgoRSASHA2256v01 = CertAlgoRSASHA256v01
  44. // Deprecated: use CertAlgoRSASHA512v01.
  45. CertSigAlgoRSASHA2512v01 = CertAlgoRSASHA512v01
  46. )
  47. // Certificate types distinguish between host and user
  48. // certificates. The values can be set in the CertType field of
  49. // Certificate.
  50. const (
  51. UserCert = 1
  52. HostCert = 2
  53. )
  54. // Signature represents a cryptographic signature.
  55. type Signature struct {
  56. Format string
  57. Blob []byte
  58. Rest []byte `ssh:"rest"`
  59. }
  60. // CertTimeInfinity can be used for OpenSSHCertV01.ValidBefore to indicate that
  61. // a certificate does not expire.
  62. const CertTimeInfinity = 1<<64 - 1
  63. // An Certificate represents an OpenSSH certificate as defined in
  64. // [PROTOCOL.certkeys]?rev=1.8. The Certificate type implements the
  65. // PublicKey interface, so it can be unmarshaled using
  66. // ParsePublicKey.
  67. type Certificate struct {
  68. Nonce []byte
  69. Key PublicKey
  70. Serial uint64
  71. CertType uint32
  72. KeyId string
  73. ValidPrincipals []string
  74. ValidAfter uint64
  75. ValidBefore uint64
  76. Permissions
  77. Reserved []byte
  78. SignatureKey PublicKey
  79. Signature *Signature
  80. }
  81. // genericCertData holds the key-independent part of the certificate data.
  82. // Overall, certificates contain an nonce, public key fields and
  83. // key-independent fields.
  84. type genericCertData struct {
  85. Serial uint64
  86. CertType uint32
  87. KeyId string
  88. ValidPrincipals []byte
  89. ValidAfter uint64
  90. ValidBefore uint64
  91. CriticalOptions []byte
  92. Extensions []byte
  93. Reserved []byte
  94. SignatureKey []byte
  95. Signature []byte
  96. }
  97. func marshalStringList(namelist []string) []byte {
  98. var to []byte
  99. for _, name := range namelist {
  100. s := struct{ N string }{name}
  101. to = append(to, Marshal(&s)...)
  102. }
  103. return to
  104. }
  105. type optionsTuple struct {
  106. Key string
  107. Value []byte
  108. }
  109. type optionsTupleValue struct {
  110. Value string
  111. }
  112. // serialize a map of critical options or extensions
  113. // issue #10569 - per [PROTOCOL.certkeys] and SSH implementation,
  114. // we need two length prefixes for a non-empty string value
  115. func marshalTuples(tups map[string]string) []byte {
  116. keys := make([]string, 0, len(tups))
  117. for key := range tups {
  118. keys = append(keys, key)
  119. }
  120. sort.Strings(keys)
  121. var ret []byte
  122. for _, key := range keys {
  123. s := optionsTuple{Key: key}
  124. if value := tups[key]; len(value) > 0 {
  125. s.Value = Marshal(&optionsTupleValue{value})
  126. }
  127. ret = append(ret, Marshal(&s)...)
  128. }
  129. return ret
  130. }
  131. // issue #10569 - per [PROTOCOL.certkeys] and SSH implementation,
  132. // we need two length prefixes for a non-empty option value
  133. func parseTuples(in []byte) (map[string]string, error) {
  134. tups := map[string]string{}
  135. var lastKey string
  136. var haveLastKey bool
  137. for len(in) > 0 {
  138. var key, val, extra []byte
  139. var ok bool
  140. if key, in, ok = parseString(in); !ok {
  141. return nil, errShortRead
  142. }
  143. keyStr := string(key)
  144. // according to [PROTOCOL.certkeys], the names must be in
  145. // lexical order.
  146. if haveLastKey && keyStr <= lastKey {
  147. return nil, fmt.Errorf("ssh: certificate options are not in lexical order")
  148. }
  149. lastKey, haveLastKey = keyStr, true
  150. // the next field is a data field, which if non-empty has a string embedded
  151. if val, in, ok = parseString(in); !ok {
  152. return nil, errShortRead
  153. }
  154. if len(val) > 0 {
  155. val, extra, ok = parseString(val)
  156. if !ok {
  157. return nil, errShortRead
  158. }
  159. if len(extra) > 0 {
  160. return nil, fmt.Errorf("ssh: unexpected trailing data after certificate option value")
  161. }
  162. tups[keyStr] = string(val)
  163. } else {
  164. tups[keyStr] = ""
  165. }
  166. }
  167. return tups, nil
  168. }
  169. func parseCert(in []byte, privAlgo string) (*Certificate, error) {
  170. nonce, rest, ok := parseString(in)
  171. if !ok {
  172. return nil, errShortRead
  173. }
  174. key, rest, err := parsePubKey(rest, privAlgo)
  175. if err != nil {
  176. return nil, err
  177. }
  178. var g genericCertData
  179. if err := Unmarshal(rest, &g); err != nil {
  180. return nil, err
  181. }
  182. c := &Certificate{
  183. Nonce: nonce,
  184. Key: key,
  185. Serial: g.Serial,
  186. CertType: g.CertType,
  187. KeyId: g.KeyId,
  188. ValidAfter: g.ValidAfter,
  189. ValidBefore: g.ValidBefore,
  190. }
  191. for principals := g.ValidPrincipals; len(principals) > 0; {
  192. principal, rest, ok := parseString(principals)
  193. if !ok {
  194. return nil, errShortRead
  195. }
  196. c.ValidPrincipals = append(c.ValidPrincipals, string(principal))
  197. principals = rest
  198. }
  199. c.CriticalOptions, err = parseTuples(g.CriticalOptions)
  200. if err != nil {
  201. return nil, err
  202. }
  203. c.Extensions, err = parseTuples(g.Extensions)
  204. if err != nil {
  205. return nil, err
  206. }
  207. c.Reserved = g.Reserved
  208. k, err := ParsePublicKey(g.SignatureKey)
  209. if err != nil {
  210. return nil, err
  211. }
  212. // The Type() function is intended to return only certificate key types, but
  213. // we use certKeyAlgoNames anyway for safety, to match [Certificate.Type].
  214. if _, ok := certKeyAlgoNames[k.Type()]; ok {
  215. return nil, fmt.Errorf("ssh: the signature key type %q is invalid for certificates", k.Type())
  216. }
  217. c.SignatureKey = k
  218. c.Signature, rest, ok = parseSignatureBody(g.Signature)
  219. if !ok || len(rest) > 0 {
  220. return nil, errors.New("ssh: signature parse error")
  221. }
  222. return c, nil
  223. }
  224. type openSSHCertSigner struct {
  225. pub *Certificate
  226. signer Signer
  227. }
  228. type algorithmOpenSSHCertSigner struct {
  229. *openSSHCertSigner
  230. algorithmSigner AlgorithmSigner
  231. }
  232. // NewCertSigner returns a Signer that signs with the given Certificate, whose
  233. // private key is held by signer. It returns an error if the public key in cert
  234. // doesn't match the key used by signer.
  235. func NewCertSigner(cert *Certificate, signer Signer) (Signer, error) {
  236. if !bytes.Equal(cert.Key.Marshal(), signer.PublicKey().Marshal()) {
  237. return nil, errors.New("ssh: signer and cert have different public key")
  238. }
  239. switch s := signer.(type) {
  240. case MultiAlgorithmSigner:
  241. return &multiAlgorithmSigner{
  242. AlgorithmSigner: &algorithmOpenSSHCertSigner{
  243. &openSSHCertSigner{cert, signer}, s},
  244. supportedAlgorithms: s.Algorithms(),
  245. }, nil
  246. case AlgorithmSigner:
  247. return &algorithmOpenSSHCertSigner{
  248. &openSSHCertSigner{cert, signer}, s}, nil
  249. default:
  250. return &openSSHCertSigner{cert, signer}, nil
  251. }
  252. }
  253. func (s *openSSHCertSigner) Sign(rand io.Reader, data []byte) (*Signature, error) {
  254. return s.signer.Sign(rand, data)
  255. }
  256. func (s *openSSHCertSigner) PublicKey() PublicKey {
  257. return s.pub
  258. }
  259. func (s *algorithmOpenSSHCertSigner) SignWithAlgorithm(rand io.Reader, data []byte, algorithm string) (*Signature, error) {
  260. return s.algorithmSigner.SignWithAlgorithm(rand, data, algorithm)
  261. }
  262. const sourceAddressCriticalOption = "source-address"
  263. // CertChecker does the work of verifying a certificate. Its methods
  264. // can be plugged into ClientConfig.HostKeyCallback and
  265. // ServerConfig.PublicKeyCallback. For the CertChecker to work,
  266. // minimally, the IsAuthority callback should be set.
  267. type CertChecker struct {
  268. // SupportedCriticalOptions lists the CriticalOptions that the
  269. // server application layer understands. These are only used
  270. // for user certificates.
  271. SupportedCriticalOptions []string
  272. // IsUserAuthority should return true if the key is recognized as an
  273. // authority for user certificate. This must be set if this CertChecker
  274. // will be checking user certificates.
  275. IsUserAuthority func(auth PublicKey) bool
  276. // IsHostAuthority should report whether the key is recognized as
  277. // an authority for this host. This must be set if this CertChecker
  278. // will be checking host certificates.
  279. IsHostAuthority func(auth PublicKey, address string) bool
  280. // Clock is used for verifying time stamps. If nil, time.Now
  281. // is used.
  282. Clock func() time.Time
  283. // UserKeyFallback is called when CertChecker.Authenticate encounters a
  284. // public key that is not a certificate. It must implement validation
  285. // of user keys or else, if nil, all such keys are rejected.
  286. UserKeyFallback func(conn ConnMetadata, key PublicKey) (*Permissions, error)
  287. // HostKeyFallback is called when CertChecker.CheckHostKey encounters a
  288. // public key that is not a certificate. It must implement host key
  289. // validation or else, if nil, all such keys are rejected.
  290. HostKeyFallback HostKeyCallback
  291. // IsRevoked is called for each certificate so that revocation checking
  292. // can be implemented. It should return true if the given certificate
  293. // is revoked and false otherwise. If nil, no certificates are
  294. // considered to have been revoked.
  295. IsRevoked func(cert *Certificate) bool
  296. }
  297. // CheckHostKey checks a host key certificate. This method can be
  298. // plugged into ClientConfig.HostKeyCallback.
  299. func (c *CertChecker) CheckHostKey(addr string, remote net.Addr, key PublicKey) error {
  300. cert, ok := key.(*Certificate)
  301. if !ok {
  302. if c.HostKeyFallback != nil {
  303. return c.HostKeyFallback(addr, remote, key)
  304. }
  305. return errors.New("ssh: non-certificate host key")
  306. }
  307. if cert.CertType != HostCert {
  308. return fmt.Errorf("ssh: certificate presented as a host key has type %d", cert.CertType)
  309. }
  310. if !c.IsHostAuthority(cert.SignatureKey, addr) {
  311. return fmt.Errorf("ssh: no authorities for hostname: %v", addr)
  312. }
  313. hostname, _, err := net.SplitHostPort(addr)
  314. if err != nil {
  315. return err
  316. }
  317. // Pass hostname only as principal for host certificates (consistent with OpenSSH)
  318. return c.CheckCert(hostname, cert)
  319. }
  320. // Authenticate checks a user certificate. Authenticate can be used as
  321. // a value for ServerConfig.PublicKeyCallback.
  322. func (c *CertChecker) Authenticate(conn ConnMetadata, pubKey PublicKey) (*Permissions, error) {
  323. cert, ok := pubKey.(*Certificate)
  324. if !ok {
  325. if c.UserKeyFallback != nil {
  326. return c.UserKeyFallback(conn, pubKey)
  327. }
  328. return nil, errors.New("ssh: normal key pairs not accepted")
  329. }
  330. if cert.CertType != UserCert {
  331. return nil, fmt.Errorf("ssh: cert has type %d", cert.CertType)
  332. }
  333. if !c.IsUserAuthority(cert.SignatureKey) {
  334. return nil, fmt.Errorf("ssh: certificate signed by unrecognized authority")
  335. }
  336. if err := c.CheckCert(conn.User(), cert); err != nil {
  337. return nil, err
  338. }
  339. return &cert.Permissions, nil
  340. }
  341. // CheckCert checks CriticalOptions, ValidPrincipals, revocation, timestamp and
  342. // the signature of the certificate.
  343. func (c *CertChecker) CheckCert(principal string, cert *Certificate) error {
  344. if c.IsRevoked != nil && c.IsRevoked(cert) {
  345. return fmt.Errorf("ssh: certificate serial %d revoked", cert.Serial)
  346. }
  347. for opt := range cert.CriticalOptions {
  348. // sourceAddressCriticalOption will be enforced by
  349. // serverAuthenticate
  350. if opt == sourceAddressCriticalOption {
  351. continue
  352. }
  353. found := false
  354. for _, supp := range c.SupportedCriticalOptions {
  355. if supp == opt {
  356. found = true
  357. break
  358. }
  359. }
  360. if !found {
  361. return fmt.Errorf("ssh: unsupported critical option %q in certificate", opt)
  362. }
  363. }
  364. if len(cert.ValidPrincipals) > 0 {
  365. // By default, certs are valid for all users/hosts.
  366. found := false
  367. for _, p := range cert.ValidPrincipals {
  368. if p == principal {
  369. found = true
  370. break
  371. }
  372. }
  373. if !found {
  374. return fmt.Errorf("ssh: principal %q not in the set of valid principals for given certificate: %q", principal, cert.ValidPrincipals)
  375. }
  376. }
  377. clock := c.Clock
  378. if clock == nil {
  379. clock = time.Now
  380. }
  381. unixNow := clock().Unix()
  382. if after := int64(cert.ValidAfter); after < 0 || unixNow < int64(cert.ValidAfter) {
  383. return fmt.Errorf("ssh: cert is not yet valid")
  384. }
  385. if before := int64(cert.ValidBefore); cert.ValidBefore != uint64(CertTimeInfinity) && (unixNow >= before || before < 0) {
  386. return fmt.Errorf("ssh: cert has expired")
  387. }
  388. if err := cert.SignatureKey.Verify(cert.bytesForSigning(), cert.Signature); err != nil {
  389. return fmt.Errorf("ssh: certificate signature does not verify")
  390. }
  391. return nil
  392. }
  393. // SignCert signs the certificate with an authority, setting the Nonce,
  394. // SignatureKey, and Signature fields. If the authority implements the
  395. // MultiAlgorithmSigner interface the first algorithm in the list is used. This
  396. // is useful if you want to sign with a specific algorithm. As specified in
  397. // [SSH-CERTS], Section 2.1.1, authority can't be a [Certificate].
  398. func (c *Certificate) SignCert(rand io.Reader, authority Signer) error {
  399. c.Nonce = make([]byte, 32)
  400. if _, err := io.ReadFull(rand, c.Nonce); err != nil {
  401. return err
  402. }
  403. // The Type() function is intended to return only certificate key types, but
  404. // we use certKeyAlgoNames anyway for safety, to match [Certificate.Type].
  405. if _, ok := certKeyAlgoNames[authority.PublicKey().Type()]; ok {
  406. return fmt.Errorf("ssh: certificates cannot be used as authority (public key type %q)",
  407. authority.PublicKey().Type())
  408. }
  409. c.SignatureKey = authority.PublicKey()
  410. if v, ok := authority.(MultiAlgorithmSigner); ok {
  411. if len(v.Algorithms()) == 0 {
  412. return errors.New("the provided authority has no signature algorithm")
  413. }
  414. // Use the first algorithm in the list.
  415. sig, err := v.SignWithAlgorithm(rand, c.bytesForSigning(), v.Algorithms()[0])
  416. if err != nil {
  417. return err
  418. }
  419. c.Signature = sig
  420. return nil
  421. } else if v, ok := authority.(AlgorithmSigner); ok && v.PublicKey().Type() == KeyAlgoRSA {
  422. // Default to KeyAlgoRSASHA512 for ssh-rsa signers.
  423. // TODO: consider using KeyAlgoRSASHA256 as default.
  424. sig, err := v.SignWithAlgorithm(rand, c.bytesForSigning(), KeyAlgoRSASHA512)
  425. if err != nil {
  426. return err
  427. }
  428. c.Signature = sig
  429. return nil
  430. }
  431. sig, err := authority.Sign(rand, c.bytesForSigning())
  432. if err != nil {
  433. return err
  434. }
  435. c.Signature = sig
  436. return nil
  437. }
  438. // certKeyAlgoNames is a mapping from known certificate algorithm names to the
  439. // corresponding public key signature algorithm.
  440. //
  441. // This map must be kept in sync with the one in agent/client.go.
  442. var certKeyAlgoNames = map[string]string{
  443. CertAlgoRSAv01: KeyAlgoRSA,
  444. CertAlgoRSASHA256v01: KeyAlgoRSASHA256,
  445. CertAlgoRSASHA512v01: KeyAlgoRSASHA512,
  446. InsecureCertAlgoDSAv01: InsecureKeyAlgoDSA,
  447. CertAlgoECDSA256v01: KeyAlgoECDSA256,
  448. CertAlgoECDSA384v01: KeyAlgoECDSA384,
  449. CertAlgoECDSA521v01: KeyAlgoECDSA521,
  450. CertAlgoSKECDSA256v01: KeyAlgoSKECDSA256,
  451. CertAlgoED25519v01: KeyAlgoED25519,
  452. CertAlgoSKED25519v01: KeyAlgoSKED25519,
  453. }
  454. // underlyingAlgo returns the signature algorithm associated with algo (which is
  455. // an advertised or negotiated public key or host key algorithm). These are
  456. // usually the same, except for certificate algorithms.
  457. func underlyingAlgo(algo string) string {
  458. if a, ok := certKeyAlgoNames[algo]; ok {
  459. return a
  460. }
  461. return algo
  462. }
  463. // certificateAlgo returns the certificate algorithms that uses the provided
  464. // underlying signature algorithm.
  465. func certificateAlgo(algo string) (certAlgo string, ok bool) {
  466. for certName, algoName := range certKeyAlgoNames {
  467. if algoName == algo {
  468. return certName, true
  469. }
  470. }
  471. return "", false
  472. }
  473. func (cert *Certificate) bytesForSigning() []byte {
  474. c2 := *cert
  475. c2.Signature = nil
  476. out := c2.Marshal()
  477. // Drop trailing signature length.
  478. return out[:len(out)-4]
  479. }
  480. // Marshal serializes c into OpenSSH's wire format. It is part of the
  481. // PublicKey interface.
  482. func (c *Certificate) Marshal() []byte {
  483. generic := genericCertData{
  484. Serial: c.Serial,
  485. CertType: c.CertType,
  486. KeyId: c.KeyId,
  487. ValidPrincipals: marshalStringList(c.ValidPrincipals),
  488. ValidAfter: uint64(c.ValidAfter),
  489. ValidBefore: uint64(c.ValidBefore),
  490. CriticalOptions: marshalTuples(c.CriticalOptions),
  491. Extensions: marshalTuples(c.Extensions),
  492. Reserved: c.Reserved,
  493. SignatureKey: c.SignatureKey.Marshal(),
  494. }
  495. if c.Signature != nil {
  496. generic.Signature = Marshal(c.Signature)
  497. }
  498. genericBytes := Marshal(&generic)
  499. keyBytes := c.Key.Marshal()
  500. _, keyBytes, _ = parseString(keyBytes)
  501. prefix := Marshal(&struct {
  502. Name string
  503. Nonce []byte
  504. Key []byte `ssh:"rest"`
  505. }{c.Type(), c.Nonce, keyBytes})
  506. result := make([]byte, 0, len(prefix)+len(genericBytes))
  507. result = append(result, prefix...)
  508. result = append(result, genericBytes...)
  509. return result
  510. }
  511. // Type returns the certificate algorithm name. It is part of the PublicKey interface.
  512. func (c *Certificate) Type() string {
  513. certName, ok := certificateAlgo(c.Key.Type())
  514. if !ok {
  515. panic("unknown certificate type for key type " + c.Key.Type())
  516. }
  517. return certName
  518. }
  519. // Verify verifies a signature against the certificate's public
  520. // key. It is part of the PublicKey interface.
  521. func (c *Certificate) Verify(data []byte, sig *Signature) error {
  522. return c.Key.Verify(data, sig)
  523. }
  524. func parseSignatureBody(in []byte) (out *Signature, rest []byte, ok bool) {
  525. format, in, ok := parseString(in)
  526. if !ok {
  527. return
  528. }
  529. out = &Signature{
  530. Format: string(format),
  531. }
  532. if out.Blob, in, ok = parseString(in); !ok {
  533. return
  534. }
  535. switch out.Format {
  536. case KeyAlgoSKECDSA256, CertAlgoSKECDSA256v01, KeyAlgoSKED25519, CertAlgoSKED25519v01:
  537. out.Rest = in
  538. return out, nil, ok
  539. }
  540. return out, in, ok
  541. }
  542. func parseSignature(in []byte) (out *Signature, rest []byte, ok bool) {
  543. sigBytes, rest, ok := parseString(in)
  544. if !ok {
  545. return
  546. }
  547. out, trailing, ok := parseSignatureBody(sigBytes)
  548. if !ok || len(trailing) > 0 {
  549. return nil, nil, false
  550. }
  551. return
  552. }