userid.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. "io"
  7. "io/ioutil"
  8. "strings"
  9. )
  10. // UserId contains text that is intended to represent the name and email
  11. // address of the key holder. See RFC 4880, section 5.11. By convention, this
  12. // takes the form "Full Name (Comment) <email@example.com>"
  13. type UserId struct {
  14. Id string // By convention, this takes the form "Full Name (Comment) <email@example.com>" which is split out in the fields below.
  15. Name, Comment, Email string
  16. }
  17. func hasInvalidCharacters(s string) bool {
  18. for _, c := range s {
  19. switch c {
  20. case '(', ')', '<', '>', 0:
  21. return true
  22. }
  23. }
  24. return false
  25. }
  26. // NewUserId returns a UserId or nil if any of the arguments contain invalid
  27. // characters. The invalid characters are '\x00', '(', ')', '<' and '>'
  28. func NewUserId(name, comment, email string) *UserId {
  29. // RFC 4880 doesn't deal with the structure of userid strings; the
  30. // name, comment and email form is just a convention. However, there's
  31. // no convention about escaping the metacharacters and GPG just refuses
  32. // to create user ids where, say, the name contains a '('. We mirror
  33. // this behaviour.
  34. if hasInvalidCharacters(name) || hasInvalidCharacters(comment) || hasInvalidCharacters(email) {
  35. return nil
  36. }
  37. uid := new(UserId)
  38. uid.Name, uid.Comment, uid.Email = name, comment, email
  39. uid.Id = name
  40. if len(comment) > 0 {
  41. if len(uid.Id) > 0 {
  42. uid.Id += " "
  43. }
  44. uid.Id += "("
  45. uid.Id += comment
  46. uid.Id += ")"
  47. }
  48. if len(email) > 0 {
  49. if len(uid.Id) > 0 {
  50. uid.Id += " "
  51. }
  52. uid.Id += "<"
  53. uid.Id += email
  54. uid.Id += ">"
  55. }
  56. return uid
  57. }
  58. func (uid *UserId) parse(r io.Reader) (err error) {
  59. // RFC 4880, section 5.11
  60. b, err := ioutil.ReadAll(r)
  61. if err != nil {
  62. return
  63. }
  64. uid.Id = string(b)
  65. uid.Name, uid.Comment, uid.Email = parseUserId(uid.Id)
  66. return
  67. }
  68. // Serialize marshals uid to w in the form of an OpenPGP packet, including
  69. // header.
  70. func (uid *UserId) Serialize(w io.Writer) error {
  71. err := serializeHeader(w, packetTypeUserId, len(uid.Id))
  72. if err != nil {
  73. return err
  74. }
  75. _, err = w.Write([]byte(uid.Id))
  76. return err
  77. }
  78. // parseUserId extracts the name, comment and email from a user id string that
  79. // is formatted as "Full Name (Comment) <email@example.com>".
  80. func parseUserId(id string) (name, comment, email string) {
  81. var n, c, e struct {
  82. start, end int
  83. }
  84. var state int
  85. for offset, rune := range id {
  86. switch state {
  87. case 0:
  88. // Entering name
  89. n.start = offset
  90. state = 1
  91. fallthrough
  92. case 1:
  93. // In name
  94. if rune == '(' {
  95. state = 2
  96. n.end = offset
  97. } else if rune == '<' {
  98. state = 5
  99. n.end = offset
  100. }
  101. case 2:
  102. // Entering comment
  103. c.start = offset
  104. state = 3
  105. fallthrough
  106. case 3:
  107. // In comment
  108. if rune == ')' {
  109. state = 4
  110. c.end = offset
  111. }
  112. case 4:
  113. // Between comment and email
  114. if rune == '<' {
  115. state = 5
  116. }
  117. case 5:
  118. // Entering email
  119. e.start = offset
  120. state = 6
  121. fallthrough
  122. case 6:
  123. // In email
  124. if rune == '>' {
  125. state = 7
  126. e.end = offset
  127. }
  128. default:
  129. // After email
  130. }
  131. }
  132. switch state {
  133. case 1:
  134. // ended in the name
  135. n.end = len(id)
  136. case 3:
  137. // ended in comment
  138. c.end = len(id)
  139. case 6:
  140. // ended in email
  141. e.end = len(id)
  142. }
  143. name = strings.TrimSpace(id[n.start:n.end])
  144. comment = strings.TrimSpace(id[c.start:c.end])
  145. email = strings.TrimSpace(id[e.start:e.end])
  146. return
  147. }