ring-native 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGES.md +7 -0
- data/Makefile +5 -0
- data/README.md +12 -5
- data/Rakefile +4 -0
- data/ext/ring/extconf.rb +4 -5
- data/lib/ring/native.rb +3 -1
- data/lib/ring/native/version.rb +5 -1
- data/ring-native.gemspec +6 -6
- data/vendor/ring-ffi/Cargo.lock +26 -0
- data/vendor/ring-ffi/Cargo.toml +45 -0
- data/vendor/ring-ffi/LICENSE +16 -0
- data/vendor/ring-ffi/README.md +59 -0
- data/vendor/ring-ffi/src/lib.rs +79 -0
- metadata +10 -255
- data/vendor/ring/BUILDING.md +0 -40
- data/vendor/ring/Cargo.toml +0 -43
- data/vendor/ring/LICENSE +0 -185
- data/vendor/ring/Makefile +0 -35
- data/vendor/ring/PORTING.md +0 -163
- data/vendor/ring/README.md +0 -113
- data/vendor/ring/STYLE.md +0 -197
- data/vendor/ring/appveyor.yml +0 -27
- data/vendor/ring/build.rs +0 -108
- data/vendor/ring/crypto/aes/aes.c +0 -1142
- data/vendor/ring/crypto/aes/aes_test.Windows.vcxproj +0 -25
- data/vendor/ring/crypto/aes/aes_test.cc +0 -93
- data/vendor/ring/crypto/aes/asm/aes-586.pl +0 -2368
- data/vendor/ring/crypto/aes/asm/aes-armv4.pl +0 -1249
- data/vendor/ring/crypto/aes/asm/aes-x86_64.pl +0 -2246
- data/vendor/ring/crypto/aes/asm/aesni-x86.pl +0 -1318
- data/vendor/ring/crypto/aes/asm/aesni-x86_64.pl +0 -2084
- data/vendor/ring/crypto/aes/asm/aesv8-armx.pl +0 -675
- data/vendor/ring/crypto/aes/asm/bsaes-armv7.pl +0 -1364
- data/vendor/ring/crypto/aes/asm/bsaes-x86_64.pl +0 -1565
- data/vendor/ring/crypto/aes/asm/vpaes-x86.pl +0 -841
- data/vendor/ring/crypto/aes/asm/vpaes-x86_64.pl +0 -1116
- data/vendor/ring/crypto/aes/internal.h +0 -87
- data/vendor/ring/crypto/aes/mode_wrappers.c +0 -61
- data/vendor/ring/crypto/bn/add.c +0 -394
- data/vendor/ring/crypto/bn/asm/armv4-mont.pl +0 -694
- data/vendor/ring/crypto/bn/asm/armv8-mont.pl +0 -1503
- data/vendor/ring/crypto/bn/asm/bn-586.pl +0 -774
- data/vendor/ring/crypto/bn/asm/co-586.pl +0 -287
- data/vendor/ring/crypto/bn/asm/rsaz-avx2.pl +0 -1882
- data/vendor/ring/crypto/bn/asm/x86-mont.pl +0 -592
- data/vendor/ring/crypto/bn/asm/x86_64-gcc.c +0 -599
- data/vendor/ring/crypto/bn/asm/x86_64-mont.pl +0 -1393
- data/vendor/ring/crypto/bn/asm/x86_64-mont5.pl +0 -3507
- data/vendor/ring/crypto/bn/bn.c +0 -352
- data/vendor/ring/crypto/bn/bn_asn1.c +0 -74
- data/vendor/ring/crypto/bn/bn_test.Windows.vcxproj +0 -25
- data/vendor/ring/crypto/bn/bn_test.cc +0 -1696
- data/vendor/ring/crypto/bn/cmp.c +0 -200
- data/vendor/ring/crypto/bn/convert.c +0 -433
- data/vendor/ring/crypto/bn/ctx.c +0 -311
- data/vendor/ring/crypto/bn/div.c +0 -594
- data/vendor/ring/crypto/bn/exponentiation.c +0 -1335
- data/vendor/ring/crypto/bn/gcd.c +0 -711
- data/vendor/ring/crypto/bn/generic.c +0 -1019
- data/vendor/ring/crypto/bn/internal.h +0 -316
- data/vendor/ring/crypto/bn/montgomery.c +0 -516
- data/vendor/ring/crypto/bn/mul.c +0 -888
- data/vendor/ring/crypto/bn/prime.c +0 -829
- data/vendor/ring/crypto/bn/random.c +0 -334
- data/vendor/ring/crypto/bn/rsaz_exp.c +0 -262
- data/vendor/ring/crypto/bn/rsaz_exp.h +0 -53
- data/vendor/ring/crypto/bn/shift.c +0 -276
- data/vendor/ring/crypto/bytestring/bytestring_test.Windows.vcxproj +0 -25
- data/vendor/ring/crypto/bytestring/bytestring_test.cc +0 -421
- data/vendor/ring/crypto/bytestring/cbb.c +0 -399
- data/vendor/ring/crypto/bytestring/cbs.c +0 -227
- data/vendor/ring/crypto/bytestring/internal.h +0 -46
- data/vendor/ring/crypto/chacha/chacha_generic.c +0 -140
- data/vendor/ring/crypto/chacha/chacha_vec.c +0 -323
- data/vendor/ring/crypto/chacha/chacha_vec_arm.S +0 -1447
- data/vendor/ring/crypto/chacha/chacha_vec_arm_generate.go +0 -153
- data/vendor/ring/crypto/cipher/cipher_test.Windows.vcxproj +0 -25
- data/vendor/ring/crypto/cipher/e_aes.c +0 -390
- data/vendor/ring/crypto/cipher/e_chacha20poly1305.c +0 -208
- data/vendor/ring/crypto/cipher/internal.h +0 -173
- data/vendor/ring/crypto/cipher/test/aes_128_gcm_tests.txt +0 -543
- data/vendor/ring/crypto/cipher/test/aes_128_key_wrap_tests.txt +0 -9
- data/vendor/ring/crypto/cipher/test/aes_256_gcm_tests.txt +0 -475
- data/vendor/ring/crypto/cipher/test/aes_256_key_wrap_tests.txt +0 -23
- data/vendor/ring/crypto/cipher/test/chacha20_poly1305_old_tests.txt +0 -422
- data/vendor/ring/crypto/cipher/test/chacha20_poly1305_tests.txt +0 -484
- data/vendor/ring/crypto/cipher/test/cipher_test.txt +0 -100
- data/vendor/ring/crypto/constant_time_test.Windows.vcxproj +0 -25
- data/vendor/ring/crypto/constant_time_test.c +0 -304
- data/vendor/ring/crypto/cpu-arm-asm.S +0 -32
- data/vendor/ring/crypto/cpu-arm.c +0 -199
- data/vendor/ring/crypto/cpu-intel.c +0 -261
- data/vendor/ring/crypto/crypto.c +0 -151
- data/vendor/ring/crypto/curve25519/asm/x25519-arm.S +0 -2118
- data/vendor/ring/crypto/curve25519/curve25519.c +0 -4888
- data/vendor/ring/crypto/curve25519/x25519_test.cc +0 -128
- data/vendor/ring/crypto/digest/md32_common.h +0 -181
- data/vendor/ring/crypto/ec/asm/p256-x86_64-asm.pl +0 -2725
- data/vendor/ring/crypto/ec/ec.c +0 -193
- data/vendor/ring/crypto/ec/ec_curves.c +0 -61
- data/vendor/ring/crypto/ec/ec_key.c +0 -228
- data/vendor/ring/crypto/ec/ec_montgomery.c +0 -114
- data/vendor/ring/crypto/ec/example_mul.Windows.vcxproj +0 -25
- data/vendor/ring/crypto/ec/internal.h +0 -243
- data/vendor/ring/crypto/ec/oct.c +0 -253
- data/vendor/ring/crypto/ec/p256-64.c +0 -1794
- data/vendor/ring/crypto/ec/p256-x86_64-table.h +0 -9548
- data/vendor/ring/crypto/ec/p256-x86_64.c +0 -509
- data/vendor/ring/crypto/ec/simple.c +0 -1007
- data/vendor/ring/crypto/ec/util-64.c +0 -183
- data/vendor/ring/crypto/ec/wnaf.c +0 -508
- data/vendor/ring/crypto/ecdh/ecdh.c +0 -155
- data/vendor/ring/crypto/ecdsa/ecdsa.c +0 -304
- data/vendor/ring/crypto/ecdsa/ecdsa_asn1.c +0 -193
- data/vendor/ring/crypto/ecdsa/ecdsa_test.Windows.vcxproj +0 -25
- data/vendor/ring/crypto/ecdsa/ecdsa_test.cc +0 -327
- data/vendor/ring/crypto/header_removed.h +0 -17
- data/vendor/ring/crypto/internal.h +0 -495
- data/vendor/ring/crypto/libring.Windows.vcxproj +0 -101
- data/vendor/ring/crypto/mem.c +0 -98
- data/vendor/ring/crypto/modes/asm/aesni-gcm-x86_64.pl +0 -1045
- data/vendor/ring/crypto/modes/asm/ghash-armv4.pl +0 -517
- data/vendor/ring/crypto/modes/asm/ghash-x86.pl +0 -1393
- data/vendor/ring/crypto/modes/asm/ghash-x86_64.pl +0 -1741
- data/vendor/ring/crypto/modes/asm/ghashv8-armx.pl +0 -422
- data/vendor/ring/crypto/modes/ctr.c +0 -226
- data/vendor/ring/crypto/modes/gcm.c +0 -1206
- data/vendor/ring/crypto/modes/gcm_test.Windows.vcxproj +0 -25
- data/vendor/ring/crypto/modes/gcm_test.c +0 -348
- data/vendor/ring/crypto/modes/internal.h +0 -299
- data/vendor/ring/crypto/perlasm/arm-xlate.pl +0 -170
- data/vendor/ring/crypto/perlasm/readme +0 -100
- data/vendor/ring/crypto/perlasm/x86_64-xlate.pl +0 -1164
- data/vendor/ring/crypto/perlasm/x86asm.pl +0 -292
- data/vendor/ring/crypto/perlasm/x86gas.pl +0 -263
- data/vendor/ring/crypto/perlasm/x86masm.pl +0 -200
- data/vendor/ring/crypto/perlasm/x86nasm.pl +0 -187
- data/vendor/ring/crypto/poly1305/poly1305.c +0 -331
- data/vendor/ring/crypto/poly1305/poly1305_arm.c +0 -301
- data/vendor/ring/crypto/poly1305/poly1305_arm_asm.S +0 -2015
- data/vendor/ring/crypto/poly1305/poly1305_test.Windows.vcxproj +0 -25
- data/vendor/ring/crypto/poly1305/poly1305_test.cc +0 -80
- data/vendor/ring/crypto/poly1305/poly1305_test.txt +0 -52
- data/vendor/ring/crypto/poly1305/poly1305_vec.c +0 -892
- data/vendor/ring/crypto/rand/asm/rdrand-x86_64.pl +0 -75
- data/vendor/ring/crypto/rand/internal.h +0 -32
- data/vendor/ring/crypto/rand/rand.c +0 -189
- data/vendor/ring/crypto/rand/urandom.c +0 -219
- data/vendor/ring/crypto/rand/windows.c +0 -56
- data/vendor/ring/crypto/refcount_c11.c +0 -66
- data/vendor/ring/crypto/refcount_lock.c +0 -53
- data/vendor/ring/crypto/refcount_test.Windows.vcxproj +0 -25
- data/vendor/ring/crypto/refcount_test.c +0 -58
- data/vendor/ring/crypto/rsa/blinding.c +0 -462
- data/vendor/ring/crypto/rsa/internal.h +0 -108
- data/vendor/ring/crypto/rsa/padding.c +0 -300
- data/vendor/ring/crypto/rsa/rsa.c +0 -450
- data/vendor/ring/crypto/rsa/rsa_asn1.c +0 -261
- data/vendor/ring/crypto/rsa/rsa_impl.c +0 -944
- data/vendor/ring/crypto/rsa/rsa_test.Windows.vcxproj +0 -25
- data/vendor/ring/crypto/rsa/rsa_test.cc +0 -437
- data/vendor/ring/crypto/sha/asm/sha-armv8.pl +0 -436
- data/vendor/ring/crypto/sha/asm/sha-x86_64.pl +0 -2390
- data/vendor/ring/crypto/sha/asm/sha256-586.pl +0 -1275
- data/vendor/ring/crypto/sha/asm/sha256-armv4.pl +0 -735
- data/vendor/ring/crypto/sha/asm/sha256-armv8.pl +0 -14
- data/vendor/ring/crypto/sha/asm/sha256-x86_64.pl +0 -14
- data/vendor/ring/crypto/sha/asm/sha512-586.pl +0 -911
- data/vendor/ring/crypto/sha/asm/sha512-armv4.pl +0 -666
- data/vendor/ring/crypto/sha/asm/sha512-armv8.pl +0 -14
- data/vendor/ring/crypto/sha/asm/sha512-x86_64.pl +0 -14
- data/vendor/ring/crypto/sha/sha1.c +0 -271
- data/vendor/ring/crypto/sha/sha256.c +0 -204
- data/vendor/ring/crypto/sha/sha512.c +0 -355
- data/vendor/ring/crypto/test/file_test.cc +0 -326
- data/vendor/ring/crypto/test/file_test.h +0 -181
- data/vendor/ring/crypto/test/malloc.cc +0 -150
- data/vendor/ring/crypto/test/scoped_types.h +0 -95
- data/vendor/ring/crypto/test/test.Windows.vcxproj +0 -35
- data/vendor/ring/crypto/test/test_util.cc +0 -46
- data/vendor/ring/crypto/test/test_util.h +0 -41
- data/vendor/ring/crypto/thread_none.c +0 -55
- data/vendor/ring/crypto/thread_pthread.c +0 -165
- data/vendor/ring/crypto/thread_test.Windows.vcxproj +0 -25
- data/vendor/ring/crypto/thread_test.c +0 -200
- data/vendor/ring/crypto/thread_win.c +0 -282
- data/vendor/ring/examples/checkdigest.rs +0 -103
- data/vendor/ring/include/openssl/aes.h +0 -121
- data/vendor/ring/include/openssl/arm_arch.h +0 -129
- data/vendor/ring/include/openssl/base.h +0 -156
- data/vendor/ring/include/openssl/bn.h +0 -794
- data/vendor/ring/include/openssl/buffer.h +0 -18
- data/vendor/ring/include/openssl/bytestring.h +0 -235
- data/vendor/ring/include/openssl/chacha.h +0 -37
- data/vendor/ring/include/openssl/cmac.h +0 -76
- data/vendor/ring/include/openssl/cpu.h +0 -184
- data/vendor/ring/include/openssl/crypto.h +0 -43
- data/vendor/ring/include/openssl/curve25519.h +0 -88
- data/vendor/ring/include/openssl/ec.h +0 -225
- data/vendor/ring/include/openssl/ec_key.h +0 -129
- data/vendor/ring/include/openssl/ecdh.h +0 -110
- data/vendor/ring/include/openssl/ecdsa.h +0 -156
- data/vendor/ring/include/openssl/err.h +0 -201
- data/vendor/ring/include/openssl/mem.h +0 -101
- data/vendor/ring/include/openssl/obj_mac.h +0 -71
- data/vendor/ring/include/openssl/opensslfeatures.h +0 -68
- data/vendor/ring/include/openssl/opensslv.h +0 -18
- data/vendor/ring/include/openssl/ossl_typ.h +0 -18
- data/vendor/ring/include/openssl/poly1305.h +0 -51
- data/vendor/ring/include/openssl/rand.h +0 -70
- data/vendor/ring/include/openssl/rsa.h +0 -399
- data/vendor/ring/include/openssl/thread.h +0 -133
- data/vendor/ring/include/openssl/type_check.h +0 -71
- data/vendor/ring/mk/Common.props +0 -63
- data/vendor/ring/mk/Windows.props +0 -42
- data/vendor/ring/mk/WindowsTest.props +0 -18
- data/vendor/ring/mk/appveyor.bat +0 -62
- data/vendor/ring/mk/bottom_of_makefile.mk +0 -54
- data/vendor/ring/mk/ring.mk +0 -266
- data/vendor/ring/mk/top_of_makefile.mk +0 -214
- data/vendor/ring/mk/travis.sh +0 -40
- data/vendor/ring/mk/update-travis-yml.py +0 -229
- data/vendor/ring/ring.sln +0 -153
- data/vendor/ring/src/aead.rs +0 -682
- data/vendor/ring/src/agreement.rs +0 -248
- data/vendor/ring/src/c.rs +0 -129
- data/vendor/ring/src/constant_time.rs +0 -37
- data/vendor/ring/src/der.rs +0 -96
- data/vendor/ring/src/digest.rs +0 -690
- data/vendor/ring/src/digest_tests.txt +0 -57
- data/vendor/ring/src/ecc.rs +0 -28
- data/vendor/ring/src/ecc_build.rs +0 -279
- data/vendor/ring/src/ecc_curves.rs +0 -117
- data/vendor/ring/src/ed25519_tests.txt +0 -2579
- data/vendor/ring/src/exe_tests.rs +0 -46
- data/vendor/ring/src/ffi.rs +0 -29
- data/vendor/ring/src/file_test.rs +0 -187
- data/vendor/ring/src/hkdf.rs +0 -153
- data/vendor/ring/src/hkdf_tests.txt +0 -59
- data/vendor/ring/src/hmac.rs +0 -414
- data/vendor/ring/src/hmac_tests.txt +0 -97
- data/vendor/ring/src/input.rs +0 -312
- data/vendor/ring/src/lib.rs +0 -41
- data/vendor/ring/src/pbkdf2.rs +0 -265
- data/vendor/ring/src/pbkdf2_tests.txt +0 -113
- data/vendor/ring/src/polyfill.rs +0 -57
- data/vendor/ring/src/rand.rs +0 -28
- data/vendor/ring/src/signature.rs +0 -314
- data/vendor/ring/third-party/NIST/README.md +0 -9
- data/vendor/ring/third-party/NIST/SHAVS/SHA1LongMsg.rsp +0 -263
- data/vendor/ring/third-party/NIST/SHAVS/SHA1Monte.rsp +0 -309
- data/vendor/ring/third-party/NIST/SHAVS/SHA1ShortMsg.rsp +0 -267
- data/vendor/ring/third-party/NIST/SHAVS/SHA224LongMsg.rsp +0 -263
- data/vendor/ring/third-party/NIST/SHAVS/SHA224Monte.rsp +0 -309
- data/vendor/ring/third-party/NIST/SHAVS/SHA224ShortMsg.rsp +0 -267
- data/vendor/ring/third-party/NIST/SHAVS/SHA256LongMsg.rsp +0 -263
- data/vendor/ring/third-party/NIST/SHAVS/SHA256Monte.rsp +0 -309
- data/vendor/ring/third-party/NIST/SHAVS/SHA256ShortMsg.rsp +0 -267
- data/vendor/ring/third-party/NIST/SHAVS/SHA384LongMsg.rsp +0 -519
- data/vendor/ring/third-party/NIST/SHAVS/SHA384Monte.rsp +0 -309
- data/vendor/ring/third-party/NIST/SHAVS/SHA384ShortMsg.rsp +0 -523
- data/vendor/ring/third-party/NIST/SHAVS/SHA512LongMsg.rsp +0 -519
- data/vendor/ring/third-party/NIST/SHAVS/SHA512Monte.rsp +0 -309
- data/vendor/ring/third-party/NIST/SHAVS/SHA512ShortMsg.rsp +0 -523
- data/vendor/ring/third-party/NIST/sha256sums.txt +0 -1
@@ -1,399 +0,0 @@
|
|
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
|
-
}
|
@@ -1,227 +0,0 @@
|
|
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
|
-
}
|