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.
 
 
 

334 lines
10 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. // Repeat with the implicit-tagging functions
  138. t.Run("WithTag", func(t *testing.T) {
  139. for i, test := range testData64 {
  140. tag := asn1.Tag((i * 3) % 32).ContextSpecific()
  141. testData := make([]byte, len(test.in))
  142. copy(testData, test.in)
  143. // Alter the tag of the test case.
  144. testData[0] = uint8(tag)
  145. in := String(testData)
  146. var out int64
  147. ok := in.ReadASN1Int64WithTag(&out, tag)
  148. if !ok || out != test.out {
  149. t.Errorf("#%d: in.ReadASN1Int64WithTag() = %v, want true; out = %d, want %d", i, ok, out, test.out)
  150. }
  151. var b Builder
  152. b.AddASN1Int64WithTag(test.out, tag)
  153. result, err := b.Bytes()
  154. if err != nil {
  155. t.Errorf("#%d: AddASN1Int64WithTag failed: %s", i, err)
  156. continue
  157. }
  158. if !bytes.Equal(result, testData) {
  159. t.Errorf("#%d: AddASN1Int64WithTag: got %x, want %x", i, result, testData)
  160. }
  161. }
  162. })
  163. }
  164. func TestReadASN1IntegerUnsigned(t *testing.T) {
  165. testData := []struct {
  166. in []byte
  167. out uint64
  168. }{
  169. {[]byte{2, 1, 0}, 0},
  170. {[]byte{2, 1, 1}, 1},
  171. {[]byte{2, 1, 2}, 2},
  172. {[]byte{2, 1, 127}, 127},
  173. {[]byte{2, 2, 0, 128}, 128},
  174. {[]byte{2, 2, 1, 0}, 256},
  175. {[]byte{2, 4, 0, 128, 0, 0}, 0x800000},
  176. {[]byte{2, 8, 127, 255, 255, 255, 255, 255, 255, 255}, 0x7fffffffffffffff},
  177. {[]byte{2, 9, 0, 128, 0, 0, 0, 0, 0, 0, 0}, 0x8000000000000000},
  178. {[]byte{2, 9, 0, 255, 255, 255, 255, 255, 255, 255, 255}, 0xffffffffffffffff},
  179. }
  180. for i, test := range testData {
  181. in := String(test.in)
  182. var out uint64
  183. ok := in.ReadASN1Integer(&out)
  184. if !ok || out != test.out {
  185. t.Errorf("#%d: in.ReadASN1Integer() = %v, want true; out = %d, want %d", i, ok, out, test.out)
  186. }
  187. }
  188. }
  189. func TestReadASN1IntegerInvalid(t *testing.T) {
  190. testData := []String{
  191. []byte{3, 1, 0}, // invalid tag
  192. // truncated
  193. []byte{2, 1},
  194. []byte{2, 2, 0},
  195. // not minimally encoded
  196. []byte{2, 2, 0, 1},
  197. []byte{2, 2, 0xff, 0xff},
  198. }
  199. for i, test := range testData {
  200. var out int64
  201. if test.ReadASN1Integer(&out) {
  202. t.Errorf("#%d: in.ReadASN1Integer() = true, want false (out = %d)", i, out)
  203. }
  204. }
  205. }
  206. func TestASN1ObjectIdentifier(t *testing.T) {
  207. testData := []struct {
  208. in []byte
  209. ok bool
  210. out []int
  211. }{
  212. {[]byte{}, false, []int{}},
  213. {[]byte{6, 0}, false, []int{}},
  214. {[]byte{5, 1, 85}, false, []int{2, 5}},
  215. {[]byte{6, 1, 85}, true, []int{2, 5}},
  216. {[]byte{6, 2, 85, 0x02}, true, []int{2, 5, 2}},
  217. {[]byte{6, 4, 85, 0x02, 0xc0, 0x00}, true, []int{2, 5, 2, 0x2000}},
  218. {[]byte{6, 3, 0x81, 0x34, 0x03}, true, []int{2, 100, 3}},
  219. {[]byte{6, 7, 85, 0x02, 0xc0, 0x80, 0x80, 0x80, 0x80}, false, []int{}},
  220. }
  221. for i, test := range testData {
  222. in := String(test.in)
  223. var out encoding_asn1.ObjectIdentifier
  224. ok := in.ReadASN1ObjectIdentifier(&out)
  225. if ok != test.ok || ok && !out.Equal(test.out) {
  226. t.Errorf("#%d: in.ReadASN1ObjectIdentifier() = %v, want %v; out = %v, want %v", i, ok, test.ok, out, test.out)
  227. continue
  228. }
  229. var b Builder
  230. b.AddASN1ObjectIdentifier(out)
  231. result, err := b.Bytes()
  232. if builderOk := err == nil; test.ok != builderOk {
  233. t.Errorf("#%d: error from Builder.Bytes: %s", i, err)
  234. continue
  235. }
  236. if test.ok && !bytes.Equal(result, test.in) {
  237. t.Errorf("#%d: reserialisation didn't match, got %x, want %x", i, result, test.in)
  238. continue
  239. }
  240. }
  241. }
  242. func TestReadASN1GeneralizedTime(t *testing.T) {
  243. testData := []struct {
  244. in string
  245. ok bool
  246. out time.Time
  247. }{
  248. {"20100102030405Z", true, time.Date(2010, 01, 02, 03, 04, 05, 0, time.UTC)},
  249. {"20100102030405", false, time.Time{}},
  250. {"20100102030405+0607", true, time.Date(2010, 01, 02, 03, 04, 05, 0, time.FixedZone("", 6*60*60+7*60))},
  251. {"20100102030405-0607", true, time.Date(2010, 01, 02, 03, 04, 05, 0, time.FixedZone("", -6*60*60-7*60))},
  252. /* These are invalid times. However, the time package normalises times
  253. * and they were accepted in some versions. See #11134. */
  254. {"00000100000000Z", false, time.Time{}},
  255. {"20101302030405Z", false, time.Time{}},
  256. {"20100002030405Z", false, time.Time{}},
  257. {"20100100030405Z", false, time.Time{}},
  258. {"20100132030405Z", false, time.Time{}},
  259. {"20100231030405Z", false, time.Time{}},
  260. {"20100102240405Z", false, time.Time{}},
  261. {"20100102036005Z", false, time.Time{}},
  262. {"20100102030460Z", false, time.Time{}},
  263. {"-20100102030410Z", false, time.Time{}},
  264. {"2010-0102030410Z", false, time.Time{}},
  265. {"2010-0002030410Z", false, time.Time{}},
  266. {"201001-02030410Z", false, time.Time{}},
  267. {"20100102-030410Z", false, time.Time{}},
  268. {"2010010203-0410Z", false, time.Time{}},
  269. {"201001020304-10Z", false, time.Time{}},
  270. }
  271. for i, test := range testData {
  272. in := String(append([]byte{byte(asn1.GeneralizedTime), byte(len(test.in))}, test.in...))
  273. var out time.Time
  274. ok := in.ReadASN1GeneralizedTime(&out)
  275. if ok != test.ok || ok && !reflect.DeepEqual(out, test.out) {
  276. t.Errorf("#%d: in.ReadASN1GeneralizedTime() = %v, want %v; out = %q, want %q", i, ok, test.ok, out, test.out)
  277. }
  278. }
  279. }
  280. func TestReadASN1BitString(t *testing.T) {
  281. testData := []struct {
  282. in []byte
  283. ok bool
  284. out encoding_asn1.BitString
  285. }{
  286. {[]byte{}, false, encoding_asn1.BitString{}},
  287. {[]byte{0x00}, true, encoding_asn1.BitString{}},
  288. {[]byte{0x07, 0x00}, true, encoding_asn1.BitString{Bytes: []byte{0}, BitLength: 1}},
  289. {[]byte{0x07, 0x01}, false, encoding_asn1.BitString{}},
  290. {[]byte{0x07, 0x40}, false, encoding_asn1.BitString{}},
  291. {[]byte{0x08, 0x00}, false, encoding_asn1.BitString{}},
  292. {[]byte{0xff}, false, encoding_asn1.BitString{}},
  293. {[]byte{0xfe, 0x00}, false, encoding_asn1.BitString{}},
  294. }
  295. for i, test := range testData {
  296. in := String(append([]byte{3, byte(len(test.in))}, test.in...))
  297. var out encoding_asn1.BitString
  298. ok := in.ReadASN1BitString(&out)
  299. if ok != test.ok || ok && (!bytes.Equal(out.Bytes, test.out.Bytes) || out.BitLength != test.out.BitLength) {
  300. t.Errorf("#%d: in.ReadASN1BitString() = %v, want %v; out = %v, want %v", i, ok, test.ok, out, test.out)
  301. }
  302. }
  303. }