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.

300 lines
9.6 KiB

  1. // Copyright 2017 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 cryptobyte
  5. import (
  6. "bytes"
  7. encoding_asn1 "encoding/asn1"
  8. "math/big"
  9. "reflect"
  10. "testing"
  11. "time"
  12. "golang.org/x/crypto/cryptobyte/asn1"
  13. )
  14. type readASN1Test struct {
  15. name string
  16. in []byte
  17. tag asn1.Tag
  18. ok bool
  19. out interface{}
  20. }
  21. var readASN1TestData = []readASN1Test{
  22. {"valid", []byte{0x30, 2, 1, 2}, 0x30, true, []byte{1, 2}},
  23. {"truncated", []byte{0x30, 3, 1, 2}, 0x30, false, nil},
  24. {"zero length of length", []byte{0x30, 0x80}, 0x30, false, nil},
  25. {"invalid long form length", []byte{0x30, 0x81, 1, 1}, 0x30, false, nil},
  26. {"non-minimal length", append([]byte{0x30, 0x82, 0, 0x80}, make([]byte, 0x80)...), 0x30, false, nil},
  27. {"invalid tag", []byte{0xa1, 3, 0x4, 1, 1}, 31, false, nil},
  28. {"high tag", []byte{0x1f, 0x81, 0x80, 0x01, 2, 1, 2}, 0xff /* actually 0x4001, but tag is uint8 */, false, nil},
  29. }
  30. func TestReadASN1(t *testing.T) {
  31. for _, test := range readASN1TestData {
  32. t.Run(test.name, func(t *testing.T) {
  33. var in, out String = test.in, nil
  34. ok := in.ReadASN1(&out, test.tag)
  35. if ok != test.ok || ok && !bytes.Equal(out, test.out.([]byte)) {
  36. t.Errorf("in.ReadASN1() = %v, want %v; out = %v, want %v", ok, test.ok, out, test.out)
  37. }
  38. })
  39. }
  40. }
  41. func TestReadASN1Optional(t *testing.T) {
  42. var empty String
  43. var present bool
  44. ok := empty.ReadOptionalASN1(nil, &present, 0xa0)
  45. if !ok || present {
  46. t.Errorf("empty.ReadOptionalASN1() = %v, want true; present = %v want false", ok, present)
  47. }
  48. var in, out String = []byte{0xa1, 3, 0x4, 1, 1}, nil
  49. ok = in.ReadOptionalASN1(&out, &present, 0xa0)
  50. if !ok || present {
  51. t.Errorf("in.ReadOptionalASN1() = %v, want true, present = %v, want false", ok, present)
  52. }
  53. ok = in.ReadOptionalASN1(&out, &present, 0xa1)
  54. wantBytes := []byte{4, 1, 1}
  55. if !ok || !present || !bytes.Equal(out, wantBytes) {
  56. t.Errorf("in.ReadOptionalASN1() = %v, want true; present = %v, want true; out = %v, want = %v", ok, present, out, wantBytes)
  57. }
  58. }
  59. var optionalOctetStringTestData = []struct {
  60. readASN1Test
  61. present bool
  62. }{
  63. {readASN1Test{"empty", []byte{}, 0xa0, true, []byte{}}, false},
  64. {readASN1Test{"invalid", []byte{0xa1, 3, 0x4, 2, 1}, 0xa1, false, []byte{}}, true},
  65. {readASN1Test{"missing", []byte{0xa1, 3, 0x4, 1, 1}, 0xa0, true, []byte{}}, false},
  66. {readASN1Test{"present", []byte{0xa1, 3, 0x4, 1, 1}, 0xa1, true, []byte{1}}, true},
  67. }
  68. func TestReadASN1OptionalOctetString(t *testing.T) {
  69. for _, test := range optionalOctetStringTestData {
  70. t.Run(test.name, func(t *testing.T) {
  71. in := String(test.in)
  72. var out []byte
  73. var present bool
  74. ok := in.ReadOptionalASN1OctetString(&out, &present, test.tag)
  75. if ok != test.ok || present != test.present || !bytes.Equal(out, test.out.([]byte)) {
  76. t.Errorf("in.ReadOptionalASN1OctetString() = %v, want %v; present = %v want %v; out = %v, want %v", ok, test.ok, present, test.present, out, test.out)
  77. }
  78. })
  79. }
  80. }
  81. const defaultInt = -1
  82. var optionalIntTestData = []readASN1Test{
  83. {"empty", []byte{}, 0xa0, true, defaultInt},
  84. {"invalid", []byte{0xa1, 3, 0x2, 2, 127}, 0xa1, false, 0},
  85. {"missing", []byte{0xa1, 3, 0x2, 1, 127}, 0xa0, true, defaultInt},
  86. {"present", []byte{0xa1, 3, 0x2, 1, 42}, 0xa1, true, 42},
  87. }
  88. func TestReadASN1OptionalInteger(t *testing.T) {
  89. for _, test := range optionalIntTestData {
  90. t.Run(test.name, func(t *testing.T) {
  91. in := String(test.in)
  92. var out int
  93. ok := in.ReadOptionalASN1Integer(&out, test.tag, defaultInt)
  94. if ok != test.ok || ok && out != test.out.(int) {
  95. t.Errorf("in.ReadOptionalASN1Integer() = %v, want %v; out = %v, want %v", ok, test.ok, out, test.out)
  96. }
  97. })
  98. }
  99. }
  100. func TestReadASN1IntegerSigned(t *testing.T) {
  101. testData64 := []struct {
  102. in []byte
  103. out int64
  104. }{
  105. {[]byte{2, 3, 128, 0, 0}, -0x800000},
  106. {[]byte{2, 2, 255, 0}, -256},
  107. {[]byte{2, 2, 255, 127}, -129},
  108. {[]byte{2, 1, 128}, -128},
  109. {[]byte{2, 1, 255}, -1},
  110. {[]byte{2, 1, 0}, 0},
  111. {[]byte{2, 1, 1}, 1},
  112. {[]byte{2, 1, 2}, 2},
  113. {[]byte{2, 1, 127}, 127},
  114. {[]byte{2, 2, 0, 128}, 128},
  115. {[]byte{2, 2, 1, 0}, 256},
  116. {[]byte{2, 4, 0, 128, 0, 0}, 0x800000},
  117. }
  118. for i, test := range testData64 {
  119. in := String(test.in)
  120. var out int64
  121. ok := in.ReadASN1Integer(&out)
  122. if !ok || out != test.out {
  123. t.Errorf("#%d: in.ReadASN1Integer() = %v, want true; out = %d, want %d", i, ok, out, test.out)
  124. }
  125. }
  126. // Repeat the same cases, reading into a big.Int.
  127. t.Run("big.Int", func(t *testing.T) {
  128. for i, test := range testData64 {
  129. in := String(test.in)
  130. var out big.Int
  131. ok := in.ReadASN1Integer(&out)
  132. if !ok || out.Int64() != test.out {
  133. t.Errorf("#%d: in.ReadASN1Integer() = %v, want true; out = %d, want %d", i, ok, out.Int64(), test.out)
  134. }
  135. }
  136. })
  137. }
  138. func TestReadASN1IntegerUnsigned(t *testing.T) {
  139. testData := []struct {
  140. in []byte
  141. out uint64
  142. }{
  143. {[]byte{2, 1, 0}, 0},
  144. {[]byte{2, 1, 1}, 1},
  145. {[]byte{2, 1, 2}, 2},
  146. {[]byte{2, 1, 127}, 127},
  147. {[]byte{2, 2, 0, 128}, 128},
  148. {[]byte{2, 2, 1, 0}, 256},
  149. {[]byte{2, 4, 0, 128, 0, 0}, 0x800000},
  150. {[]byte{2, 8, 127, 255, 255, 255, 255, 255, 255, 255}, 0x7fffffffffffffff},
  151. {[]byte{2, 9, 0, 128, 0, 0, 0, 0, 0, 0, 0}, 0x8000000000000000},
  152. {[]byte{2, 9, 0, 255, 255, 255, 255, 255, 255, 255, 255}, 0xffffffffffffffff},
  153. }
  154. for i, test := range testData {
  155. in := String(test.in)
  156. var out uint64
  157. ok := in.ReadASN1Integer(&out)
  158. if !ok || out != test.out {
  159. t.Errorf("#%d: in.ReadASN1Integer() = %v, want true; out = %d, want %d", i, ok, out, test.out)
  160. }
  161. }
  162. }
  163. func TestReadASN1IntegerInvalid(t *testing.T) {
  164. testData := []String{
  165. []byte{3, 1, 0}, // invalid tag
  166. // truncated
  167. []byte{2, 1},
  168. []byte{2, 2, 0},
  169. // not minimally encoded
  170. []byte{2, 2, 0, 1},
  171. []byte{2, 2, 0xff, 0xff},
  172. }
  173. for i, test := range testData {
  174. var out int64
  175. if test.ReadASN1Integer(&out) {
  176. t.Errorf("#%d: in.ReadASN1Integer() = true, want false (out = %d)", i, out)
  177. }
  178. }
  179. }
  180. func TestASN1ObjectIdentifier(t *testing.T) {
  181. testData := []struct {
  182. in []byte
  183. ok bool
  184. out []int
  185. }{
  186. {[]byte{}, false, []int{}},
  187. {[]byte{6, 0}, false, []int{}},
  188. {[]byte{5, 1, 85}, false, []int{2, 5}},
  189. {[]byte{6, 1, 85}, true, []int{2, 5}},
  190. {[]byte{6, 2, 85, 0x02}, true, []int{2, 5, 2}},
  191. {[]byte{6, 4, 85, 0x02, 0xc0, 0x00}, true, []int{2, 5, 2, 0x2000}},
  192. {[]byte{6, 3, 0x81, 0x34, 0x03}, true, []int{2, 100, 3}},
  193. {[]byte{6, 7, 85, 0x02, 0xc0, 0x80, 0x80, 0x80, 0x80}, false, []int{}},
  194. }
  195. for i, test := range testData {
  196. in := String(test.in)
  197. var out encoding_asn1.ObjectIdentifier
  198. ok := in.ReadASN1ObjectIdentifier(&out)
  199. if ok != test.ok || ok && !out.Equal(test.out) {
  200. t.Errorf("#%d: in.ReadASN1ObjectIdentifier() = %v, want %v; out = %v, want %v", i, ok, test.ok, out, test.out)
  201. continue
  202. }
  203. var b Builder
  204. b.AddASN1ObjectIdentifier(out)
  205. result, err := b.Bytes()
  206. if builderOk := err == nil; test.ok != builderOk {
  207. t.Errorf("#%d: error from Builder.Bytes: %s", i, err)
  208. continue
  209. }
  210. if test.ok && !bytes.Equal(result, test.in) {
  211. t.Errorf("#%d: reserialisation didn't match, got %x, want %x", i, result, test.in)
  212. continue
  213. }
  214. }
  215. }
  216. func TestReadASN1GeneralizedTime(t *testing.T) {
  217. testData := []struct {
  218. in string
  219. ok bool
  220. out time.Time
  221. }{
  222. {"20100102030405Z", true, time.Date(2010, 01, 02, 03, 04, 05, 0, time.UTC)},
  223. {"20100102030405", false, time.Time{}},
  224. {"20100102030405+0607", true, time.Date(2010, 01, 02, 03, 04, 05, 0, time.FixedZone("", 6*60*60+7*60))},
  225. {"20100102030405-0607", true, time.Date(2010, 01, 02, 03, 04, 05, 0, time.FixedZone("", -6*60*60-7*60))},
  226. /* These are invalid times. However, the time package normalises times
  227. * and they were accepted in some versions. See #11134. */
  228. {"00000100000000Z", false, time.Time{}},
  229. {"20101302030405Z", false, time.Time{}},
  230. {"20100002030405Z", false, time.Time{}},
  231. {"20100100030405Z", false, time.Time{}},
  232. {"20100132030405Z", false, time.Time{}},
  233. {"20100231030405Z", false, time.Time{}},
  234. {"20100102240405Z", false, time.Time{}},
  235. {"20100102036005Z", false, time.Time{}},
  236. {"20100102030460Z", false, time.Time{}},
  237. {"-20100102030410Z", false, time.Time{}},
  238. {"2010-0102030410Z", false, time.Time{}},
  239. {"2010-0002030410Z", false, time.Time{}},
  240. {"201001-02030410Z", false, time.Time{}},
  241. {"20100102-030410Z", false, time.Time{}},
  242. {"2010010203-0410Z", false, time.Time{}},
  243. {"201001020304-10Z", false, time.Time{}},
  244. }
  245. for i, test := range testData {
  246. in := String(append([]byte{byte(asn1.GeneralizedTime), byte(len(test.in))}, test.in...))
  247. var out time.Time
  248. ok := in.ReadASN1GeneralizedTime(&out)
  249. if ok != test.ok || ok && !reflect.DeepEqual(out, test.out) {
  250. t.Errorf("#%d: in.ReadASN1GeneralizedTime() = %v, want %v; out = %q, want %q", i, ok, test.ok, out, test.out)
  251. }
  252. }
  253. }
  254. func TestReadASN1BitString(t *testing.T) {
  255. testData := []struct {
  256. in []byte
  257. ok bool
  258. out encoding_asn1.BitString
  259. }{
  260. {[]byte{}, false, encoding_asn1.BitString{}},
  261. {[]byte{0x00}, true, encoding_asn1.BitString{}},
  262. {[]byte{0x07, 0x00}, true, encoding_asn1.BitString{Bytes: []byte{0}, BitLength: 1}},
  263. {[]byte{0x07, 0x01}, false, encoding_asn1.BitString{}},
  264. {[]byte{0x07, 0x40}, false, encoding_asn1.BitString{}},
  265. {[]byte{0x08, 0x00}, false, encoding_asn1.BitString{}},
  266. {[]byte{0xff}, false, encoding_asn1.BitString{}},
  267. {[]byte{0xfe, 0x00}, false, encoding_asn1.BitString{}},
  268. }
  269. for i, test := range testData {
  270. in := String(append([]byte{3, byte(len(test.in))}, test.in...))
  271. var out encoding_asn1.BitString
  272. ok := in.ReadASN1BitString(&out)
  273. if ok != test.ok || ok && (!bytes.Equal(out.Bytes, test.out.Bytes) || out.BitLength != test.out.BitLength) {
  274. t.Errorf("#%d: in.ReadASN1BitString() = %v, want %v; out = %v, want %v", i, ok, test.ok, out, test.out)
  275. }
  276. }
  277. }