ring-native 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (261) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/Gemfile +3 -0
  4. data/README.md +22 -0
  5. data/Rakefile +1 -0
  6. data/ext/ring/extconf.rb +29 -0
  7. data/lib/ring/native.rb +8 -0
  8. data/lib/ring/native/version.rb +5 -0
  9. data/ring-native.gemspec +25 -0
  10. data/vendor/ring/BUILDING.md +40 -0
  11. data/vendor/ring/Cargo.toml +43 -0
  12. data/vendor/ring/LICENSE +185 -0
  13. data/vendor/ring/Makefile +35 -0
  14. data/vendor/ring/PORTING.md +163 -0
  15. data/vendor/ring/README.md +113 -0
  16. data/vendor/ring/STYLE.md +197 -0
  17. data/vendor/ring/appveyor.yml +27 -0
  18. data/vendor/ring/build.rs +108 -0
  19. data/vendor/ring/crypto/aes/aes.c +1142 -0
  20. data/vendor/ring/crypto/aes/aes_test.Windows.vcxproj +25 -0
  21. data/vendor/ring/crypto/aes/aes_test.cc +93 -0
  22. data/vendor/ring/crypto/aes/asm/aes-586.pl +2368 -0
  23. data/vendor/ring/crypto/aes/asm/aes-armv4.pl +1249 -0
  24. data/vendor/ring/crypto/aes/asm/aes-x86_64.pl +2246 -0
  25. data/vendor/ring/crypto/aes/asm/aesni-x86.pl +1318 -0
  26. data/vendor/ring/crypto/aes/asm/aesni-x86_64.pl +2084 -0
  27. data/vendor/ring/crypto/aes/asm/aesv8-armx.pl +675 -0
  28. data/vendor/ring/crypto/aes/asm/bsaes-armv7.pl +1364 -0
  29. data/vendor/ring/crypto/aes/asm/bsaes-x86_64.pl +1565 -0
  30. data/vendor/ring/crypto/aes/asm/vpaes-x86.pl +841 -0
  31. data/vendor/ring/crypto/aes/asm/vpaes-x86_64.pl +1116 -0
  32. data/vendor/ring/crypto/aes/internal.h +87 -0
  33. data/vendor/ring/crypto/aes/mode_wrappers.c +61 -0
  34. data/vendor/ring/crypto/bn/add.c +394 -0
  35. data/vendor/ring/crypto/bn/asm/armv4-mont.pl +694 -0
  36. data/vendor/ring/crypto/bn/asm/armv8-mont.pl +1503 -0
  37. data/vendor/ring/crypto/bn/asm/bn-586.pl +774 -0
  38. data/vendor/ring/crypto/bn/asm/co-586.pl +287 -0
  39. data/vendor/ring/crypto/bn/asm/rsaz-avx2.pl +1882 -0
  40. data/vendor/ring/crypto/bn/asm/x86-mont.pl +592 -0
  41. data/vendor/ring/crypto/bn/asm/x86_64-gcc.c +599 -0
  42. data/vendor/ring/crypto/bn/asm/x86_64-mont.pl +1393 -0
  43. data/vendor/ring/crypto/bn/asm/x86_64-mont5.pl +3507 -0
  44. data/vendor/ring/crypto/bn/bn.c +352 -0
  45. data/vendor/ring/crypto/bn/bn_asn1.c +74 -0
  46. data/vendor/ring/crypto/bn/bn_test.Windows.vcxproj +25 -0
  47. data/vendor/ring/crypto/bn/bn_test.cc +1696 -0
  48. data/vendor/ring/crypto/bn/cmp.c +200 -0
  49. data/vendor/ring/crypto/bn/convert.c +433 -0
  50. data/vendor/ring/crypto/bn/ctx.c +311 -0
  51. data/vendor/ring/crypto/bn/div.c +594 -0
  52. data/vendor/ring/crypto/bn/exponentiation.c +1335 -0
  53. data/vendor/ring/crypto/bn/gcd.c +711 -0
  54. data/vendor/ring/crypto/bn/generic.c +1019 -0
  55. data/vendor/ring/crypto/bn/internal.h +316 -0
  56. data/vendor/ring/crypto/bn/montgomery.c +516 -0
  57. data/vendor/ring/crypto/bn/mul.c +888 -0
  58. data/vendor/ring/crypto/bn/prime.c +829 -0
  59. data/vendor/ring/crypto/bn/random.c +334 -0
  60. data/vendor/ring/crypto/bn/rsaz_exp.c +262 -0
  61. data/vendor/ring/crypto/bn/rsaz_exp.h +53 -0
  62. data/vendor/ring/crypto/bn/shift.c +276 -0
  63. data/vendor/ring/crypto/bytestring/bytestring_test.Windows.vcxproj +25 -0
  64. data/vendor/ring/crypto/bytestring/bytestring_test.cc +421 -0
  65. data/vendor/ring/crypto/bytestring/cbb.c +399 -0
  66. data/vendor/ring/crypto/bytestring/cbs.c +227 -0
  67. data/vendor/ring/crypto/bytestring/internal.h +46 -0
  68. data/vendor/ring/crypto/chacha/chacha_generic.c +140 -0
  69. data/vendor/ring/crypto/chacha/chacha_vec.c +323 -0
  70. data/vendor/ring/crypto/chacha/chacha_vec_arm.S +1447 -0
  71. data/vendor/ring/crypto/chacha/chacha_vec_arm_generate.go +153 -0
  72. data/vendor/ring/crypto/cipher/cipher_test.Windows.vcxproj +25 -0
  73. data/vendor/ring/crypto/cipher/e_aes.c +390 -0
  74. data/vendor/ring/crypto/cipher/e_chacha20poly1305.c +208 -0
  75. data/vendor/ring/crypto/cipher/internal.h +173 -0
  76. data/vendor/ring/crypto/cipher/test/aes_128_gcm_tests.txt +543 -0
  77. data/vendor/ring/crypto/cipher/test/aes_128_key_wrap_tests.txt +9 -0
  78. data/vendor/ring/crypto/cipher/test/aes_256_gcm_tests.txt +475 -0
  79. data/vendor/ring/crypto/cipher/test/aes_256_key_wrap_tests.txt +23 -0
  80. data/vendor/ring/crypto/cipher/test/chacha20_poly1305_old_tests.txt +422 -0
  81. data/vendor/ring/crypto/cipher/test/chacha20_poly1305_tests.txt +484 -0
  82. data/vendor/ring/crypto/cipher/test/cipher_test.txt +100 -0
  83. data/vendor/ring/crypto/constant_time_test.Windows.vcxproj +25 -0
  84. data/vendor/ring/crypto/constant_time_test.c +304 -0
  85. data/vendor/ring/crypto/cpu-arm-asm.S +32 -0
  86. data/vendor/ring/crypto/cpu-arm.c +199 -0
  87. data/vendor/ring/crypto/cpu-intel.c +261 -0
  88. data/vendor/ring/crypto/crypto.c +151 -0
  89. data/vendor/ring/crypto/curve25519/asm/x25519-arm.S +2118 -0
  90. data/vendor/ring/crypto/curve25519/curve25519.c +4888 -0
  91. data/vendor/ring/crypto/curve25519/x25519_test.cc +128 -0
  92. data/vendor/ring/crypto/digest/md32_common.h +181 -0
  93. data/vendor/ring/crypto/ec/asm/p256-x86_64-asm.pl +2725 -0
  94. data/vendor/ring/crypto/ec/ec.c +193 -0
  95. data/vendor/ring/crypto/ec/ec_curves.c +61 -0
  96. data/vendor/ring/crypto/ec/ec_key.c +228 -0
  97. data/vendor/ring/crypto/ec/ec_montgomery.c +114 -0
  98. data/vendor/ring/crypto/ec/example_mul.Windows.vcxproj +25 -0
  99. data/vendor/ring/crypto/ec/internal.h +243 -0
  100. data/vendor/ring/crypto/ec/oct.c +253 -0
  101. data/vendor/ring/crypto/ec/p256-64.c +1794 -0
  102. data/vendor/ring/crypto/ec/p256-x86_64-table.h +9548 -0
  103. data/vendor/ring/crypto/ec/p256-x86_64.c +509 -0
  104. data/vendor/ring/crypto/ec/simple.c +1007 -0
  105. data/vendor/ring/crypto/ec/util-64.c +183 -0
  106. data/vendor/ring/crypto/ec/wnaf.c +508 -0
  107. data/vendor/ring/crypto/ecdh/ecdh.c +155 -0
  108. data/vendor/ring/crypto/ecdsa/ecdsa.c +304 -0
  109. data/vendor/ring/crypto/ecdsa/ecdsa_asn1.c +193 -0
  110. data/vendor/ring/crypto/ecdsa/ecdsa_test.Windows.vcxproj +25 -0
  111. data/vendor/ring/crypto/ecdsa/ecdsa_test.cc +327 -0
  112. data/vendor/ring/crypto/header_removed.h +17 -0
  113. data/vendor/ring/crypto/internal.h +495 -0
  114. data/vendor/ring/crypto/libring.Windows.vcxproj +101 -0
  115. data/vendor/ring/crypto/mem.c +98 -0
  116. data/vendor/ring/crypto/modes/asm/aesni-gcm-x86_64.pl +1045 -0
  117. data/vendor/ring/crypto/modes/asm/ghash-armv4.pl +517 -0
  118. data/vendor/ring/crypto/modes/asm/ghash-x86.pl +1393 -0
  119. data/vendor/ring/crypto/modes/asm/ghash-x86_64.pl +1741 -0
  120. data/vendor/ring/crypto/modes/asm/ghashv8-armx.pl +422 -0
  121. data/vendor/ring/crypto/modes/ctr.c +226 -0
  122. data/vendor/ring/crypto/modes/gcm.c +1206 -0
  123. data/vendor/ring/crypto/modes/gcm_test.Windows.vcxproj +25 -0
  124. data/vendor/ring/crypto/modes/gcm_test.c +348 -0
  125. data/vendor/ring/crypto/modes/internal.h +299 -0
  126. data/vendor/ring/crypto/perlasm/arm-xlate.pl +170 -0
  127. data/vendor/ring/crypto/perlasm/readme +100 -0
  128. data/vendor/ring/crypto/perlasm/x86_64-xlate.pl +1164 -0
  129. data/vendor/ring/crypto/perlasm/x86asm.pl +292 -0
  130. data/vendor/ring/crypto/perlasm/x86gas.pl +263 -0
  131. data/vendor/ring/crypto/perlasm/x86masm.pl +200 -0
  132. data/vendor/ring/crypto/perlasm/x86nasm.pl +187 -0
  133. data/vendor/ring/crypto/poly1305/poly1305.c +331 -0
  134. data/vendor/ring/crypto/poly1305/poly1305_arm.c +301 -0
  135. data/vendor/ring/crypto/poly1305/poly1305_arm_asm.S +2015 -0
  136. data/vendor/ring/crypto/poly1305/poly1305_test.Windows.vcxproj +25 -0
  137. data/vendor/ring/crypto/poly1305/poly1305_test.cc +80 -0
  138. data/vendor/ring/crypto/poly1305/poly1305_test.txt +52 -0
  139. data/vendor/ring/crypto/poly1305/poly1305_vec.c +892 -0
  140. data/vendor/ring/crypto/rand/asm/rdrand-x86_64.pl +75 -0
  141. data/vendor/ring/crypto/rand/internal.h +32 -0
  142. data/vendor/ring/crypto/rand/rand.c +189 -0
  143. data/vendor/ring/crypto/rand/urandom.c +219 -0
  144. data/vendor/ring/crypto/rand/windows.c +56 -0
  145. data/vendor/ring/crypto/refcount_c11.c +66 -0
  146. data/vendor/ring/crypto/refcount_lock.c +53 -0
  147. data/vendor/ring/crypto/refcount_test.Windows.vcxproj +25 -0
  148. data/vendor/ring/crypto/refcount_test.c +58 -0
  149. data/vendor/ring/crypto/rsa/blinding.c +462 -0
  150. data/vendor/ring/crypto/rsa/internal.h +108 -0
  151. data/vendor/ring/crypto/rsa/padding.c +300 -0
  152. data/vendor/ring/crypto/rsa/rsa.c +450 -0
  153. data/vendor/ring/crypto/rsa/rsa_asn1.c +261 -0
  154. data/vendor/ring/crypto/rsa/rsa_impl.c +944 -0
  155. data/vendor/ring/crypto/rsa/rsa_test.Windows.vcxproj +25 -0
  156. data/vendor/ring/crypto/rsa/rsa_test.cc +437 -0
  157. data/vendor/ring/crypto/sha/asm/sha-armv8.pl +436 -0
  158. data/vendor/ring/crypto/sha/asm/sha-x86_64.pl +2390 -0
  159. data/vendor/ring/crypto/sha/asm/sha256-586.pl +1275 -0
  160. data/vendor/ring/crypto/sha/asm/sha256-armv4.pl +735 -0
  161. data/vendor/ring/crypto/sha/asm/sha256-armv8.pl +14 -0
  162. data/vendor/ring/crypto/sha/asm/sha256-x86_64.pl +14 -0
  163. data/vendor/ring/crypto/sha/asm/sha512-586.pl +911 -0
  164. data/vendor/ring/crypto/sha/asm/sha512-armv4.pl +666 -0
  165. data/vendor/ring/crypto/sha/asm/sha512-armv8.pl +14 -0
  166. data/vendor/ring/crypto/sha/asm/sha512-x86_64.pl +14 -0
  167. data/vendor/ring/crypto/sha/sha1.c +271 -0
  168. data/vendor/ring/crypto/sha/sha256.c +204 -0
  169. data/vendor/ring/crypto/sha/sha512.c +355 -0
  170. data/vendor/ring/crypto/test/file_test.cc +326 -0
  171. data/vendor/ring/crypto/test/file_test.h +181 -0
  172. data/vendor/ring/crypto/test/malloc.cc +150 -0
  173. data/vendor/ring/crypto/test/scoped_types.h +95 -0
  174. data/vendor/ring/crypto/test/test.Windows.vcxproj +35 -0
  175. data/vendor/ring/crypto/test/test_util.cc +46 -0
  176. data/vendor/ring/crypto/test/test_util.h +41 -0
  177. data/vendor/ring/crypto/thread_none.c +55 -0
  178. data/vendor/ring/crypto/thread_pthread.c +165 -0
  179. data/vendor/ring/crypto/thread_test.Windows.vcxproj +25 -0
  180. data/vendor/ring/crypto/thread_test.c +200 -0
  181. data/vendor/ring/crypto/thread_win.c +282 -0
  182. data/vendor/ring/examples/checkdigest.rs +103 -0
  183. data/vendor/ring/include/openssl/aes.h +121 -0
  184. data/vendor/ring/include/openssl/arm_arch.h +129 -0
  185. data/vendor/ring/include/openssl/base.h +156 -0
  186. data/vendor/ring/include/openssl/bn.h +794 -0
  187. data/vendor/ring/include/openssl/buffer.h +18 -0
  188. data/vendor/ring/include/openssl/bytestring.h +235 -0
  189. data/vendor/ring/include/openssl/chacha.h +37 -0
  190. data/vendor/ring/include/openssl/cmac.h +76 -0
  191. data/vendor/ring/include/openssl/cpu.h +184 -0
  192. data/vendor/ring/include/openssl/crypto.h +43 -0
  193. data/vendor/ring/include/openssl/curve25519.h +88 -0
  194. data/vendor/ring/include/openssl/ec.h +225 -0
  195. data/vendor/ring/include/openssl/ec_key.h +129 -0
  196. data/vendor/ring/include/openssl/ecdh.h +110 -0
  197. data/vendor/ring/include/openssl/ecdsa.h +156 -0
  198. data/vendor/ring/include/openssl/err.h +201 -0
  199. data/vendor/ring/include/openssl/mem.h +101 -0
  200. data/vendor/ring/include/openssl/obj_mac.h +71 -0
  201. data/vendor/ring/include/openssl/opensslfeatures.h +68 -0
  202. data/vendor/ring/include/openssl/opensslv.h +18 -0
  203. data/vendor/ring/include/openssl/ossl_typ.h +18 -0
  204. data/vendor/ring/include/openssl/poly1305.h +51 -0
  205. data/vendor/ring/include/openssl/rand.h +70 -0
  206. data/vendor/ring/include/openssl/rsa.h +399 -0
  207. data/vendor/ring/include/openssl/thread.h +133 -0
  208. data/vendor/ring/include/openssl/type_check.h +71 -0
  209. data/vendor/ring/mk/Common.props +63 -0
  210. data/vendor/ring/mk/Windows.props +42 -0
  211. data/vendor/ring/mk/WindowsTest.props +18 -0
  212. data/vendor/ring/mk/appveyor.bat +62 -0
  213. data/vendor/ring/mk/bottom_of_makefile.mk +54 -0
  214. data/vendor/ring/mk/ring.mk +266 -0
  215. data/vendor/ring/mk/top_of_makefile.mk +214 -0
  216. data/vendor/ring/mk/travis.sh +40 -0
  217. data/vendor/ring/mk/update-travis-yml.py +229 -0
  218. data/vendor/ring/ring.sln +153 -0
  219. data/vendor/ring/src/aead.rs +682 -0
  220. data/vendor/ring/src/agreement.rs +248 -0
  221. data/vendor/ring/src/c.rs +129 -0
  222. data/vendor/ring/src/constant_time.rs +37 -0
  223. data/vendor/ring/src/der.rs +96 -0
  224. data/vendor/ring/src/digest.rs +690 -0
  225. data/vendor/ring/src/digest_tests.txt +57 -0
  226. data/vendor/ring/src/ecc.rs +28 -0
  227. data/vendor/ring/src/ecc_build.rs +279 -0
  228. data/vendor/ring/src/ecc_curves.rs +117 -0
  229. data/vendor/ring/src/ed25519_tests.txt +2579 -0
  230. data/vendor/ring/src/exe_tests.rs +46 -0
  231. data/vendor/ring/src/ffi.rs +29 -0
  232. data/vendor/ring/src/file_test.rs +187 -0
  233. data/vendor/ring/src/hkdf.rs +153 -0
  234. data/vendor/ring/src/hkdf_tests.txt +59 -0
  235. data/vendor/ring/src/hmac.rs +414 -0
  236. data/vendor/ring/src/hmac_tests.txt +97 -0
  237. data/vendor/ring/src/input.rs +312 -0
  238. data/vendor/ring/src/lib.rs +41 -0
  239. data/vendor/ring/src/pbkdf2.rs +265 -0
  240. data/vendor/ring/src/pbkdf2_tests.txt +113 -0
  241. data/vendor/ring/src/polyfill.rs +57 -0
  242. data/vendor/ring/src/rand.rs +28 -0
  243. data/vendor/ring/src/signature.rs +314 -0
  244. data/vendor/ring/third-party/NIST/README.md +9 -0
  245. data/vendor/ring/third-party/NIST/SHAVS/SHA1LongMsg.rsp +263 -0
  246. data/vendor/ring/third-party/NIST/SHAVS/SHA1Monte.rsp +309 -0
  247. data/vendor/ring/third-party/NIST/SHAVS/SHA1ShortMsg.rsp +267 -0
  248. data/vendor/ring/third-party/NIST/SHAVS/SHA224LongMsg.rsp +263 -0
  249. data/vendor/ring/third-party/NIST/SHAVS/SHA224Monte.rsp +309 -0
  250. data/vendor/ring/third-party/NIST/SHAVS/SHA224ShortMsg.rsp +267 -0
  251. data/vendor/ring/third-party/NIST/SHAVS/SHA256LongMsg.rsp +263 -0
  252. data/vendor/ring/third-party/NIST/SHAVS/SHA256Monte.rsp +309 -0
  253. data/vendor/ring/third-party/NIST/SHAVS/SHA256ShortMsg.rsp +267 -0
  254. data/vendor/ring/third-party/NIST/SHAVS/SHA384LongMsg.rsp +519 -0
  255. data/vendor/ring/third-party/NIST/SHAVS/SHA384Monte.rsp +309 -0
  256. data/vendor/ring/third-party/NIST/SHAVS/SHA384ShortMsg.rsp +523 -0
  257. data/vendor/ring/third-party/NIST/SHAVS/SHA512LongMsg.rsp +519 -0
  258. data/vendor/ring/third-party/NIST/SHAVS/SHA512Monte.rsp +309 -0
  259. data/vendor/ring/third-party/NIST/SHAVS/SHA512ShortMsg.rsp +523 -0
  260. data/vendor/ring/third-party/NIST/sha256sums.txt +1 -0
  261. metadata +333 -0
@@ -0,0 +1,399 @@
1
+ /* Copyright (c) 2014, Google Inc.
2
+ *
3
+ * Permission to use, copy, modify, and/or distribute this software for any
4
+ * purpose with or without fee is hereby granted, provided that the above
5
+ * copyright notice and this permission notice appear in all copies.
6
+ *
7
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
+
15
+ #include <openssl/bytestring.h>
16
+
17
+ #include <assert.h>
18
+ #include <string.h>
19
+
20
+ #include <openssl/mem.h>
21
+
22
+
23
+ void CBB_zero(CBB *cbb) {
24
+ memset(cbb, 0, sizeof(CBB));
25
+ }
26
+
27
+ static int cbb_init(CBB *cbb, uint8_t *buf, size_t cap) {
28
+ /* This assumes that |cbb| has already been zeroed. */
29
+ struct cbb_buffer_st *base;
30
+
31
+ base = OPENSSL_malloc(sizeof(struct cbb_buffer_st));
32
+ if (base == NULL) {
33
+ return 0;
34
+ }
35
+
36
+ base->buf = buf;
37
+ base->len = 0;
38
+ base->cap = cap;
39
+ base->can_resize = 1;
40
+
41
+ cbb->base = base;
42
+ cbb->is_top_level = 1;
43
+ return 1;
44
+ }
45
+
46
+ int CBB_init(CBB *cbb, size_t initial_capacity) {
47
+ CBB_zero(cbb);
48
+
49
+ uint8_t *buf = OPENSSL_malloc(initial_capacity);
50
+ if (initial_capacity > 0 && buf == NULL) {
51
+ return 0;
52
+ }
53
+
54
+ if (!cbb_init(cbb, buf, initial_capacity)) {
55
+ OPENSSL_free(buf);
56
+ return 0;
57
+ }
58
+
59
+ return 1;
60
+ }
61
+
62
+ int CBB_init_fixed(CBB *cbb, uint8_t *buf, size_t len) {
63
+ CBB_zero(cbb);
64
+
65
+ if (!cbb_init(cbb, buf, len)) {
66
+ return 0;
67
+ }
68
+
69
+ cbb->base->can_resize = 0;
70
+ return 1;
71
+ }
72
+
73
+ void CBB_cleanup(CBB *cbb) {
74
+ if (cbb->base) {
75
+ /* Only top-level |CBB|s are cleaned up. Child |CBB|s are non-owning. They
76
+ * are implicitly discarded when the parent is flushed or cleaned up. */
77
+ assert(cbb->is_top_level);
78
+
79
+ if (cbb->base->can_resize) {
80
+ OPENSSL_free(cbb->base->buf);
81
+ }
82
+ OPENSSL_free(cbb->base);
83
+ }
84
+ cbb->base = NULL;
85
+ }
86
+
87
+ static int cbb_buffer_add(struct cbb_buffer_st *base, uint8_t **out,
88
+ size_t len) {
89
+ size_t newlen;
90
+
91
+ if (base == NULL) {
92
+ return 0;
93
+ }
94
+
95
+ newlen = base->len + len;
96
+ if (newlen < base->len) {
97
+ /* Overflow */
98
+ return 0;
99
+ }
100
+
101
+ if (newlen > base->cap) {
102
+ size_t newcap = base->cap * 2;
103
+ uint8_t *newbuf;
104
+
105
+ if (!base->can_resize) {
106
+ return 0;
107
+ }
108
+
109
+ if (newcap < base->cap || newcap < newlen) {
110
+ newcap = newlen;
111
+ }
112
+ newbuf = OPENSSL_realloc(base->buf, newcap);
113
+ if (newbuf == NULL) {
114
+ return 0;
115
+ }
116
+
117
+ base->buf = newbuf;
118
+ base->cap = newcap;
119
+ }
120
+
121
+ if (out) {
122
+ *out = base->buf + base->len;
123
+ }
124
+ base->len = newlen;
125
+ return 1;
126
+ }
127
+
128
+ static int cbb_buffer_add_u(struct cbb_buffer_st *base, uint32_t v,
129
+ size_t len_len) {
130
+ uint8_t *buf;
131
+ size_t i;
132
+
133
+ if (len_len == 0) {
134
+ return 1;
135
+ }
136
+ if (!cbb_buffer_add(base, &buf, len_len)) {
137
+ return 0;
138
+ }
139
+
140
+ for (i = len_len - 1; i < len_len; i--) {
141
+ buf[i] = v;
142
+ v >>= 8;
143
+ }
144
+ return 1;
145
+ }
146
+
147
+ int CBB_finish(CBB *cbb, uint8_t **out_data, size_t *out_len) {
148
+ if (!cbb->is_top_level) {
149
+ return 0;
150
+ }
151
+
152
+ if (!CBB_flush(cbb)) {
153
+ return 0;
154
+ }
155
+
156
+ if (cbb->base->can_resize && (out_data == NULL || out_len == NULL)) {
157
+ /* |out_data| and |out_len| can only be NULL if the CBB is fixed. */
158
+ return 0;
159
+ }
160
+
161
+ if (out_data != NULL) {
162
+ *out_data = cbb->base->buf;
163
+ }
164
+ if (out_len != NULL) {
165
+ *out_len = cbb->base->len;
166
+ }
167
+ cbb->base->buf = NULL;
168
+ CBB_cleanup(cbb);
169
+ return 1;
170
+ }
171
+
172
+ /* CBB_flush recurses and then writes out any pending length prefix. The
173
+ * current length of the underlying base is taken to be the length of the
174
+ * length-prefixed data. */
175
+ int CBB_flush(CBB *cbb) {
176
+ size_t child_start, i, len;
177
+
178
+ if (cbb->base == NULL) {
179
+ return 0;
180
+ }
181
+
182
+ if (cbb->child == NULL || cbb->pending_len_len == 0) {
183
+ return 1;
184
+ }
185
+
186
+ child_start = cbb->offset + cbb->pending_len_len;
187
+
188
+ if (!CBB_flush(cbb->child) ||
189
+ child_start < cbb->offset ||
190
+ cbb->base->len < child_start) {
191
+ return 0;
192
+ }
193
+
194
+ len = cbb->base->len - child_start;
195
+
196
+ if (cbb->pending_is_asn1) {
197
+ /* For ASN.1 we assume that we'll only need a single byte for the length.
198
+ * If that turned out to be incorrect, we have to move the contents along
199
+ * in order to make space. */
200
+ size_t len_len;
201
+ uint8_t initial_length_byte;
202
+
203
+ assert (cbb->pending_len_len == 1);
204
+
205
+ if (len > 0xfffffffe) {
206
+ /* Too large. */
207
+ return 0;
208
+ } else if (len > 0xffffff) {
209
+ len_len = 5;
210
+ initial_length_byte = 0x80 | 4;
211
+ } else if (len > 0xffff) {
212
+ len_len = 4;
213
+ initial_length_byte = 0x80 | 3;
214
+ } else if (len > 0xff) {
215
+ len_len = 3;
216
+ initial_length_byte = 0x80 | 2;
217
+ } else if (len > 0x7f) {
218
+ len_len = 2;
219
+ initial_length_byte = 0x80 | 1;
220
+ } else {
221
+ len_len = 1;
222
+ initial_length_byte = len;
223
+ len = 0;
224
+ }
225
+
226
+ if (len_len != 1) {
227
+ /* We need to move the contents along in order to make space. */
228
+ size_t extra_bytes = len_len - 1;
229
+ if (!cbb_buffer_add(cbb->base, NULL, extra_bytes)) {
230
+ return 0;
231
+ }
232
+ memmove(cbb->base->buf + child_start + extra_bytes,
233
+ cbb->base->buf + child_start, len);
234
+ }
235
+ cbb->base->buf[cbb->offset++] = initial_length_byte;
236
+ cbb->pending_len_len = len_len - 1;
237
+ }
238
+
239
+ for (i = cbb->pending_len_len - 1; i < cbb->pending_len_len; i--) {
240
+ cbb->base->buf[cbb->offset + i] = len;
241
+ len >>= 8;
242
+ }
243
+ if (len != 0) {
244
+ return 0;
245
+ }
246
+
247
+ cbb->child->base = NULL;
248
+ cbb->child = NULL;
249
+ cbb->pending_len_len = 0;
250
+ cbb->pending_is_asn1 = 0;
251
+ cbb->offset = 0;
252
+
253
+ return 1;
254
+ }
255
+
256
+ size_t CBB_len(const CBB *cbb) {
257
+ assert(cbb->child == NULL);
258
+
259
+ return cbb->base->len;
260
+ }
261
+
262
+ static int cbb_add_length_prefixed(CBB *cbb, CBB *out_contents,
263
+ size_t len_len) {
264
+ uint8_t *prefix_bytes;
265
+
266
+ if (!CBB_flush(cbb)) {
267
+ return 0;
268
+ }
269
+
270
+ cbb->offset = cbb->base->len;
271
+ if (!cbb_buffer_add(cbb->base, &prefix_bytes, len_len)) {
272
+ return 0;
273
+ }
274
+
275
+ memset(prefix_bytes, 0, len_len);
276
+ memset(out_contents, 0, sizeof(CBB));
277
+ out_contents->base = cbb->base;
278
+ cbb->child = out_contents;
279
+ cbb->pending_len_len = len_len;
280
+ cbb->pending_is_asn1 = 0;
281
+
282
+ return 1;
283
+ }
284
+
285
+ int CBB_add_u8_length_prefixed(CBB *cbb, CBB *out_contents) {
286
+ return cbb_add_length_prefixed(cbb, out_contents, 1);
287
+ }
288
+
289
+ int CBB_add_u16_length_prefixed(CBB *cbb, CBB *out_contents) {
290
+ return cbb_add_length_prefixed(cbb, out_contents, 2);
291
+ }
292
+
293
+ int CBB_add_u24_length_prefixed(CBB *cbb, CBB *out_contents) {
294
+ return cbb_add_length_prefixed(cbb, out_contents, 3);
295
+ }
296
+
297
+ int CBB_add_asn1(CBB *cbb, CBB *out_contents, uint8_t tag) {
298
+ if ((tag & 0x1f) == 0x1f) {
299
+ /* Long form identifier octets are not supported. */
300
+ return 0;
301
+ }
302
+
303
+ if (!CBB_flush(cbb) ||
304
+ !CBB_add_u8(cbb, tag)) {
305
+ return 0;
306
+ }
307
+
308
+ cbb->offset = cbb->base->len;
309
+ if (!CBB_add_u8(cbb, 0)) {
310
+ return 0;
311
+ }
312
+
313
+ memset(out_contents, 0, sizeof(CBB));
314
+ out_contents->base = cbb->base;
315
+ cbb->child = out_contents;
316
+ cbb->pending_len_len = 1;
317
+ cbb->pending_is_asn1 = 1;
318
+
319
+ return 1;
320
+ }
321
+
322
+ int CBB_add_bytes(CBB *cbb, const uint8_t *data, size_t len) {
323
+ uint8_t *dest;
324
+
325
+ if (!CBB_flush(cbb) ||
326
+ !cbb_buffer_add(cbb->base, &dest, len)) {
327
+ return 0;
328
+ }
329
+ memcpy(dest, data, len);
330
+ return 1;
331
+ }
332
+
333
+ int CBB_add_space(CBB *cbb, uint8_t **out_data, size_t len) {
334
+ if (!CBB_flush(cbb) ||
335
+ !cbb_buffer_add(cbb->base, out_data, len)) {
336
+ return 0;
337
+ }
338
+ return 1;
339
+ }
340
+
341
+ int CBB_add_u8(CBB *cbb, uint8_t value) {
342
+ if (!CBB_flush(cbb)) {
343
+ return 0;
344
+ }
345
+
346
+ return cbb_buffer_add_u(cbb->base, value, 1);
347
+ }
348
+
349
+ int CBB_add_u16(CBB *cbb, uint16_t value) {
350
+ if (!CBB_flush(cbb)) {
351
+ return 0;
352
+ }
353
+
354
+ return cbb_buffer_add_u(cbb->base, value, 2);
355
+ }
356
+
357
+ int CBB_add_u24(CBB *cbb, uint32_t value) {
358
+ if (!CBB_flush(cbb)) {
359
+ return 0;
360
+ }
361
+
362
+ return cbb_buffer_add_u(cbb->base, value, 3);
363
+ }
364
+
365
+ int CBB_add_asn1_uint64(CBB *cbb, uint64_t value) {
366
+ CBB child;
367
+ size_t i;
368
+ int started = 0;
369
+
370
+ if (!CBB_add_asn1(cbb, &child, CBS_ASN1_INTEGER)) {
371
+ return 0;
372
+ }
373
+
374
+ for (i = 0; i < 8; i++) {
375
+ uint8_t byte = (value >> 8*(7-i)) & 0xff;
376
+ if (!started) {
377
+ if (byte == 0) {
378
+ /* Don't encode leading zeros. */
379
+ continue;
380
+ }
381
+ /* If the high bit is set, add a padding byte to make it
382
+ * unsigned. */
383
+ if ((byte & 0x80) && !CBB_add_u8(&child, 0)) {
384
+ return 0;
385
+ }
386
+ started = 1;
387
+ }
388
+ if (!CBB_add_u8(&child, byte)) {
389
+ return 0;
390
+ }
391
+ }
392
+
393
+ /* 0 is encoded as a single 0, not the empty string. */
394
+ if (!started && !CBB_add_u8(&child, 0)) {
395
+ return 0;
396
+ }
397
+
398
+ return CBB_flush(cbb);
399
+ }
@@ -0,0 +1,227 @@
1
+ /* Copyright (c) 2014, Google Inc.
2
+ *
3
+ * Permission to use, copy, modify, and/or distribute this software for any
4
+ * purpose with or without fee is hereby granted, provided that the above
5
+ * copyright notice and this permission notice appear in all copies.
6
+ *
7
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
+
15
+ #include <openssl/mem.h>
16
+ #include <openssl/bytestring.h>
17
+
18
+ #include <assert.h>
19
+ #include <string.h>
20
+
21
+ #include "internal.h"
22
+
23
+
24
+ void CBS_init(CBS *cbs, const uint8_t *data, size_t len) {
25
+ cbs->data = data;
26
+ cbs->len = len;
27
+ }
28
+
29
+ static int cbs_get(CBS *cbs, const uint8_t **p, size_t n) {
30
+ if (cbs->len < n) {
31
+ return 0;
32
+ }
33
+
34
+ *p = cbs->data;
35
+ cbs->data += n;
36
+ cbs->len -= n;
37
+ return 1;
38
+ }
39
+
40
+ static int cbs_skip(CBS *cbs, size_t len) {
41
+ const uint8_t *dummy;
42
+ return cbs_get(cbs, &dummy, len);
43
+ }
44
+
45
+ const uint8_t *CBS_data(const CBS *cbs) {
46
+ return cbs->data;
47
+ }
48
+
49
+ size_t CBS_len(const CBS *cbs) {
50
+ return cbs->len;
51
+ }
52
+
53
+ static int cbs_get_u(CBS *cbs, uint32_t *out, size_t len) {
54
+ uint32_t result = 0;
55
+ size_t i;
56
+ const uint8_t *data;
57
+
58
+ if (!cbs_get(cbs, &data, len)) {
59
+ return 0;
60
+ }
61
+ for (i = 0; i < len; i++) {
62
+ result <<= 8;
63
+ result |= data[i];
64
+ }
65
+ *out = result;
66
+ return 1;
67
+ }
68
+
69
+ static int cbs_get_u8(CBS *cbs, uint8_t *out) {
70
+ const uint8_t *v;
71
+ if (!cbs_get(cbs, &v, 1)) {
72
+ return 0;
73
+ }
74
+ *out = *v;
75
+ return 1;
76
+ }
77
+
78
+ static int cbs_get_bytes(CBS *cbs, CBS *out, size_t len) {
79
+ const uint8_t *v;
80
+ if (!cbs_get(cbs, &v, len)) {
81
+ return 0;
82
+ }
83
+ CBS_init(out, v, len);
84
+ return 1;
85
+ }
86
+
87
+ static int cbs_get_any_asn1_element(CBS *cbs, CBS *out, unsigned *out_tag,
88
+ size_t *out_header_len) {
89
+ uint8_t tag, length_byte;
90
+ CBS header = *cbs;
91
+ CBS throwaway;
92
+
93
+ if (out == NULL) {
94
+ out = &throwaway;
95
+ }
96
+
97
+ if (!cbs_get_u8(&header, &tag) ||
98
+ !cbs_get_u8(&header, &length_byte)) {
99
+ return 0;
100
+ }
101
+
102
+ if ((tag & 0x1f) == 0x1f) {
103
+ /* Long form tags are not supported. */
104
+ return 0;
105
+ }
106
+
107
+ if (out_tag != NULL) {
108
+ *out_tag = tag;
109
+ }
110
+
111
+ size_t len;
112
+ if ((length_byte & 0x80) == 0) {
113
+ /* Short form length. */
114
+ len = ((size_t) length_byte) + 2;
115
+ if (out_header_len != NULL) {
116
+ *out_header_len = 2;
117
+ }
118
+ } else {
119
+ /* Long form length. */
120
+ const size_t num_bytes = length_byte & 0x7f;
121
+ uint32_t len32;
122
+
123
+ if (num_bytes == 0 || num_bytes > 4) {
124
+ return 0;
125
+ }
126
+ if (!cbs_get_u(&header, &len32, num_bytes)) {
127
+ return 0;
128
+ }
129
+ if (len32 < 128) {
130
+ /* Length should have used short-form encoding. */
131
+ return 0;
132
+ }
133
+ if ((len32 >> ((num_bytes-1)*8)) == 0) {
134
+ /* Length should have been at least one byte shorter. */
135
+ return 0;
136
+ }
137
+ len = len32;
138
+ if (len + 2 + num_bytes < len) {
139
+ /* Overflow. */
140
+ return 0;
141
+ }
142
+ len += 2 + num_bytes;
143
+ if (out_header_len != NULL) {
144
+ *out_header_len = 2 + num_bytes;
145
+ }
146
+ }
147
+
148
+ return cbs_get_bytes(cbs, out, len);
149
+ }
150
+
151
+ static int cbs_get_asn1(CBS *cbs, CBS *out, unsigned tag_value,
152
+ int skip_header) {
153
+ size_t header_len;
154
+ unsigned tag;
155
+ CBS throwaway;
156
+
157
+ if (out == NULL) {
158
+ out = &throwaway;
159
+ }
160
+
161
+ if (!cbs_get_any_asn1_element(cbs, out, &tag, &header_len) ||
162
+ tag != tag_value) {
163
+ return 0;
164
+ }
165
+
166
+ if (skip_header && !cbs_skip(out, header_len)) {
167
+ assert(0);
168
+ return 0;
169
+ }
170
+
171
+ return 1;
172
+ }
173
+
174
+ int CBS_get_asn1(CBS *cbs, CBS *out, unsigned tag_value) {
175
+ return cbs_get_asn1(cbs, out, tag_value, 1 /* skip header */);
176
+ }
177
+
178
+ int CBS_get_asn1_element(CBS *cbs, CBS *out, unsigned tag_value) {
179
+ return cbs_get_asn1(cbs, out, tag_value, 0 /* include header */);
180
+ }
181
+
182
+ int CBS_peek_asn1_tag(const CBS *cbs, unsigned tag_value) {
183
+ if (CBS_len(cbs) < 1) {
184
+ return 0;
185
+ }
186
+ return CBS_data(cbs)[0] == tag_value;
187
+ }
188
+
189
+ int CBS_get_asn1_uint64(CBS *cbs, uint64_t *out) {
190
+ CBS bytes;
191
+ const uint8_t *data;
192
+ size_t i, len;
193
+
194
+ if (!CBS_get_asn1(cbs, &bytes, CBS_ASN1_INTEGER)) {
195
+ return 0;
196
+ }
197
+
198
+ *out = 0;
199
+ data = CBS_data(&bytes);
200
+ len = CBS_len(&bytes);
201
+
202
+ if (len == 0) {
203
+ /* An INTEGER is encoded with at least one octet. */
204
+ return 0;
205
+ }
206
+
207
+ if ((data[0] & 0x80) != 0) {
208
+ /* Negative number. */
209
+ return 0;
210
+ }
211
+
212
+ if (data[0] == 0 && len > 1 && (data[1] & 0x80) == 0) {
213
+ /* Extra leading zeros. */
214
+ return 0;
215
+ }
216
+
217
+ for (i = 0; i < len; i++) {
218
+ if ((*out >> 56) != 0) {
219
+ /* Too large to represent as a uint64_t. */
220
+ return 0;
221
+ }
222
+ *out <<= 8;
223
+ *out |= data[i];
224
+ }
225
+
226
+ return 1;
227
+ }