static int test_PACKET_end() { PACKET pkt; if (!TEST_true(PACKET_buf_init(&pkt, smbuf, sizeof(smbuf)) || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN) || !TEST_ptr_ne(PACKET_end(&pkt), smbuf + BUF_LEN) || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 1)) || !TEST_ptr_eq(PACKET_end(&pkt), smbuf + BUF_LEN) || !TEST_true(PACKET_forward(&pkt, 1)) || !TEST_ptr_eq(PACKET_end(&pkt), smbuf + BUF_LEN))) return 0; return 1; }
/* Tests loading a bad key in PKCS8 format */ static int test_EVP_PKCS82PKEY(void) { int ret = 0; const unsigned char *derp = kExampleBadECKeyDER; PKCS8_PRIV_KEY_INFO *p8inf = NULL; EVP_PKEY *pkey = NULL; if (!TEST_ptr(p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp, sizeof(kExampleBadECKeyDER)))) goto done; if (!TEST_ptr_eq(derp, kExampleBadECKeyDER + sizeof(kExampleBadECKeyDER))) goto done; if (!TEST_ptr_null(pkey = EVP_PKCS82PKEY(p8inf))) goto done; ret = 1; done: PKCS8_PRIV_KEY_INFO_free(p8inf); EVP_PKEY_free(pkey); return ret; }
static int test_SU_stack(void) { STACK_OF(SU) *s = sk_SU_new_null(); SU v[10]; const int n = OSSL_NELEM(v); int i; int testresult = 0; /* allocate and push */ for (i = 0; i < n; i++) { if ((i & 1) == 0) v[i].n = i; else v[i].c = 'A' + i; if (!TEST_int_eq(sk_SU_num(s), i)) { TEST_info("SU stack size %d", i); goto end; } sk_SU_push(s, v + i); } if (!TEST_int_eq(sk_SU_num(s), n)) goto end; /* check the pointers are correct */ for (i = 0; i < n; i++) if (!TEST_ptr_eq(sk_SU_value(s, i), v + i)) { TEST_info("SU pointer check %d", i); goto end; } testresult = 1; end: sk_SU_free(s); return testresult; }
static int test_property_defn_cache(void) { OSSL_METHOD_STORE *store; OSSL_PROPERTY_LIST *red, *blue; int r = 0; if (TEST_ptr(store = ossl_method_store_new(NULL)) && add_property_names("red", "blue", NULL) && TEST_ptr(red = ossl_parse_property(NULL, "red")) && TEST_ptr(blue = ossl_parse_property(NULL, "blue")) && TEST_ptr_ne(red, blue) && TEST_true(ossl_prop_defn_set(NULL, "red", red)) && TEST_true(ossl_prop_defn_set(NULL, "blue", blue)) && TEST_ptr_eq(ossl_prop_defn_get(NULL, "red"), red) && TEST_ptr_eq(ossl_prop_defn_get(NULL, "blue"), blue)) r = 1; ossl_method_store_free(store); return r; }
static int test_d2i_AutoPrivateKey(int i) { int ret = 0; const unsigned char *p; EVP_PKEY *pkey = NULL; const APK_DATA *ak = &keydata[i]; const unsigned char *input = ak->kder; size_t input_len = ak->size; int expected_id = ak->evptype; p = input; if (!TEST_ptr(pkey = d2i_AutoPrivateKey(NULL, &p, input_len)) || !TEST_ptr_eq(p, input + input_len) || !TEST_int_eq(EVP_PKEY_id(pkey), expected_id)) goto done; ret = 1; done: EVP_PKEY_free(pkey); return ret; }
static int test_EVP_PKEY_check(int i) { int ret = 0; const unsigned char *p; EVP_PKEY *pkey = NULL; #ifndef OPENSSL_NO_EC EC_KEY *eckey = NULL; #endif EVP_PKEY_CTX *ctx = NULL; EVP_PKEY_CTX *ctx2 = NULL; const APK_DATA *ak = &keycheckdata[i]; const unsigned char *input = ak->kder; size_t input_len = ak->size; int expected_id = ak->evptype; int expected_check = ak->check; int expected_pub_check = ak->pub_check; int expected_param_check = ak->param_check; int type = ak->type; BIO *pubkey = NULL; p = input; switch (type) { case 0: if (!TEST_ptr(pkey = d2i_AutoPrivateKey(NULL, &p, input_len)) || !TEST_ptr_eq(p, input + input_len) || !TEST_int_eq(EVP_PKEY_id(pkey), expected_id)) goto done; break; #ifndef OPENSSL_NO_EC case 1: if (!TEST_ptr(pubkey = BIO_new_mem_buf(input, input_len)) || !TEST_ptr(eckey = d2i_EC_PUBKEY_bio(pubkey, NULL)) || !TEST_ptr(pkey = EVP_PKEY_new()) || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey))) goto done; break; case 2: if (!TEST_ptr(eckey = d2i_ECParameters(NULL, &p, input_len)) || !TEST_ptr_eq(p, input + input_len) || !TEST_ptr(pkey = EVP_PKEY_new()) || !TEST_true(EVP_PKEY_assign_EC_KEY(pkey, eckey))) goto done; break; #endif default: return 0; } if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(pkey, NULL))) goto done; if (!TEST_int_eq(EVP_PKEY_check(ctx), expected_check)) goto done; if (!TEST_int_eq(EVP_PKEY_public_check(ctx), expected_pub_check)) goto done; if (!TEST_int_eq(EVP_PKEY_param_check(ctx), expected_param_check)) goto done; ctx2 = EVP_PKEY_CTX_new_id(0xdefaced, NULL); /* assign the pkey directly, as an internal test */ EVP_PKEY_up_ref(pkey); ctx2->pkey = pkey; if (!TEST_int_eq(EVP_PKEY_check(ctx2), 0xbeef)) goto done; if (!TEST_int_eq(EVP_PKEY_public_check(ctx2), 0xbeef)) goto done; if (!TEST_int_eq(EVP_PKEY_param_check(ctx2), 0xbeef)) goto done; ret = 1; done: EVP_PKEY_CTX_free(ctx); EVP_PKEY_CTX_free(ctx2); EVP_PKEY_free(pkey); BIO_free(pubkey); return ret; }
static int test_param_construct(void) { static const char *int_names[] = { "int", "long", "int32", "int64" }; static const char *uint_names[] = { "uint", "ulong", "uint32", "uint64", "size_t" }; static const unsigned char bn_val[16] = { 0xac, 0x75, 0x22, 0x7d, 0x81, 0x06, 0x7a, 0x23, 0xa6, 0xed, 0x87, 0xc7, 0xab, 0xf4, 0x73, 0x22 }; OSSL_PARAM params[20]; char buf[100], buf2[100], *bufp, *bufp2; unsigned char ubuf[100]; void *vp, *vpn = NULL, *vp2; OSSL_PARAM *p; const OSSL_PARAM *cp; static const OSSL_PARAM pend = OSSL_PARAM_END; int i, n = 0, ret = 0; unsigned int u; long int l; unsigned long int ul; int32_t i32; uint32_t u32; int64_t i64; uint64_t u64; size_t j, k, s, sz; double d, d2; BIGNUM *bn = NULL, *bn2 = NULL; params[n++] = OSSL_PARAM_construct_int("int", &i, &sz); params[n++] = OSSL_PARAM_construct_uint("uint", &u, &sz); params[n++] = OSSL_PARAM_construct_long("long", &l, &sz); params[n++] = OSSL_PARAM_construct_ulong("ulong", &ul, &sz); params[n++] = OSSL_PARAM_construct_int32("int32", &i32, &sz); params[n++] = OSSL_PARAM_construct_int64("int64", &i64, &sz); params[n++] = OSSL_PARAM_construct_uint32("uint32", &u32, &sz); params[n++] = OSSL_PARAM_construct_uint64("uint64", &u64, &sz); params[n++] = OSSL_PARAM_construct_size_t("size_t", &s, &sz); params[n++] = OSSL_PARAM_construct_double("double", &d, &sz); params[n++] = OSSL_PARAM_construct_BN("bignum", ubuf, sizeof(ubuf), &sz); params[n++] = OSSL_PARAM_construct_utf8_string("utf8str", buf, sizeof(buf), &sz); params[n++] = OSSL_PARAM_construct_octet_string("octstr", buf, sizeof(buf), &sz); params[n++] = OSSL_PARAM_construct_utf8_ptr("utf8ptr", &bufp, &sz); params[n++] = OSSL_PARAM_construct_octet_ptr("octptr", &vp, &sz); params[n] = pend; /* Search failure */ if (!TEST_ptr_null(OSSL_PARAM_locate(params, "fnord"))) goto err; /* All signed integral types */ for (j = 0; j < OSSL_NELEM(int_names); j++) { if (!TEST_ptr(cp = OSSL_PARAM_locate(params, int_names[j])) || !TEST_true(OSSL_PARAM_set_int32(cp, (int32_t)(3 + j))) || !TEST_true(OSSL_PARAM_get_int64(cp, &i64)) || !TEST_size_t_eq(cp->data_size, sz) || !TEST_size_t_eq((size_t)i64, 3 + j)) { TEST_note("iteration %zu var %s", j + 1, int_names[j]); goto err; } } /* All unsigned integral types */ for (j = 0; j < OSSL_NELEM(uint_names); j++) { if (!TEST_ptr(cp = OSSL_PARAM_locate(params, uint_names[j])) || !TEST_true(OSSL_PARAM_set_uint32(cp, (uint32_t)(3 + j))) || !TEST_true(OSSL_PARAM_get_uint64(cp, &u64)) || !TEST_size_t_eq(cp->data_size, sz) || !TEST_size_t_eq((size_t)u64, 3 + j)) { TEST_note("iteration %zu var %s", j + 1, uint_names[j]); goto err; } } /* Real */ if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "double")) || !TEST_true(OSSL_PARAM_set_double(cp, 3.14)) || !TEST_true(OSSL_PARAM_get_double(cp, &d2)) || !TEST_size_t_eq(sz, sizeof(double)) || !TEST_double_eq(d, d2)) goto err; /* UTF8 string */ bufp = NULL; if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "utf8str")) || !TEST_true(OSSL_PARAM_set_utf8_string(cp, "abcdef")) || !TEST_size_t_eq(sz, sizeof("abcdef")) || !TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, 0)) || !TEST_str_eq(bufp, "abcdef")) goto err; OPENSSL_free(bufp); bufp = buf2; if (!TEST_true(OSSL_PARAM_get_utf8_string(cp, &bufp, sizeof(buf2))) || !TEST_str_eq(buf2, "abcdef")) goto err; /* UTF8 pointer */ bufp = buf; sz = 0; if (!TEST_ptr(cp = OSSL_PARAM_locate(params, "utf8ptr")) || !TEST_true(OSSL_PARAM_set_utf8_ptr(cp, "tuvwxyz")) || !TEST_size_t_eq(sz, sizeof("tuvwxyz")) || !TEST_str_eq(bufp, "tuvwxyz") || !TEST_true(OSSL_PARAM_get_utf8_ptr(cp, (const char **)&bufp2)) || !TEST_ptr_eq(bufp2, bufp)) goto err; /* OCTET string */ if (!TEST_ptr(p = locate(params, "octstr")) || !TEST_true(OSSL_PARAM_set_octet_string(p, "abcdefghi", sizeof("abcdefghi"))) || !TEST_size_t_eq(sz, sizeof("abcdefghi"))) goto err; /* Match the return size to avoid trailing garbage bytes */ p->data_size = *p->return_size; if (!TEST_true(OSSL_PARAM_get_octet_string(p, &vpn, 0, &s)) || !TEST_size_t_eq(s, sizeof("abcdefghi")) || !TEST_mem_eq(vpn, sizeof("abcdefghi"), "abcdefghi", sizeof("abcdefghi"))) goto err; vp = buf2; if (!TEST_true(OSSL_PARAM_get_octet_string(p, &vp, sizeof(buf2), &s)) || !TEST_size_t_eq(s, sizeof("abcdefghi")) || !TEST_mem_eq(vp, sizeof("abcdefghi"), "abcdefghi", sizeof("abcdefghi"))) goto err; /* OCTET pointer */ vp = &l; sz = 0; if (!TEST_ptr(p = locate(params, "octptr")) || !TEST_true(OSSL_PARAM_set_octet_ptr(p, &ul, sizeof(ul))) || !TEST_size_t_eq(sz, sizeof(ul)) || !TEST_ptr_eq(vp, &ul)) goto err; /* Match the return size to avoid trailing garbage bytes */ p->data_size = *p->return_size; if (!TEST_true(OSSL_PARAM_get_octet_ptr(p, (const void **)&vp2, &k)) || !TEST_size_t_eq(k, sizeof(ul)) || !TEST_ptr_eq(vp2, vp)) goto err; /* BIGNUM */ if (!TEST_ptr(p = locate(params, "bignum")) || !TEST_ptr(bn = BN_lebin2bn(bn_val, (int)sizeof(bn_val), NULL)) || !TEST_true(OSSL_PARAM_set_BN(p, bn)) || !TEST_size_t_eq(sz, sizeof(bn_val))) goto err; /* Match the return size to avoid trailing garbage bytes */ p->data_size = *p->return_size; if(!TEST_true(OSSL_PARAM_get_BN(p, &bn2)) || !TEST_BN_eq(bn, bn2)) goto err; ret = 1; err: OPENSSL_free(vpn); BN_free(bn); BN_free(bn2); return ret; }
static int test_int_stack(void) { static int v[] = { 1, 2, -4, 16, 999, 1, -173, 1, 9 }; static int notpresent = -1; const int n = OSSL_NELEM(v); static struct { int value; int unsorted; int sorted; int ex; } finds[] = { { 2, 1, 5, 5 }, { 9, 7, 6, 6 }, { -173, 5, 0, 0 }, { 999, 3, 8, 8 }, { 0, -1, -1, 1 } }; const int n_finds = OSSL_NELEM(finds); static struct { int value; int ex; } exfinds[] = { { 3, 5 }, { 1000, 8 }, { 20, 8 }, { -999, 0 }, { -5, 0 }, { 8, 5 } }; const int n_exfinds = OSSL_NELEM(exfinds); STACK_OF(sint) *s = sk_sint_new_null(); int i; int testresult = 0; /* Check push and num */ for (i = 0; i < n; i++) { if (!TEST_int_eq(sk_sint_num(s), i)) { TEST_info("int stack size %d", i); goto end; } sk_sint_push(s, v + i); } if (!TEST_int_eq(sk_sint_num(s), n)) goto end; /* check the values */ for (i = 0; i < n; i++) if (!TEST_ptr_eq(sk_sint_value(s, i), v + i)) { TEST_info("int value %d", i); goto end; } /* find unsorted -- the pointers are compared */ for (i = 0; i < n_finds; i++) { int *val = (finds[i].unsorted == -1) ? ¬present : v + finds[i].unsorted; if (!TEST_int_eq(sk_sint_find(s, val), finds[i].unsorted)) { TEST_info("int unsorted find %d", i); goto end; } } /* find_ex unsorted */ for (i = 0; i < n_finds; i++) { int *val = (finds[i].unsorted == -1) ? ¬present : v + finds[i].unsorted; if (!TEST_int_eq(sk_sint_find_ex(s, val), finds[i].unsorted)) { TEST_info("int unsorted find_ex %d", i); goto end; } } /* sorting */ if (!TEST_false(sk_sint_is_sorted(s))) goto end; sk_sint_set_cmp_func(s, &int_compare); sk_sint_sort(s); if (!TEST_true(sk_sint_is_sorted(s))) goto end; /* find sorted -- the value is matched so we don't need to locate it */ for (i = 0; i < n_finds; i++) if (!TEST_int_eq(sk_sint_find(s, &finds[i].value), finds[i].sorted)) { TEST_info("int sorted find %d", i); goto end; } /* find_ex sorted */ for (i = 0; i < n_finds; i++) if (!TEST_int_eq(sk_sint_find_ex(s, &finds[i].value), finds[i].ex)) { TEST_info("int sorted find_ex present %d", i); goto end; } for (i = 0; i < n_exfinds; i++) if (!TEST_int_eq(sk_sint_find_ex(s, &exfinds[i].value), exfinds[i].ex)){ TEST_info("int sorted find_ex absent %d", i); goto end; } /* shift */ if (!TEST_ptr_eq(sk_sint_shift(s), v + 6)) goto end; testresult = 1; end: sk_sint_free(s); return testresult; }
static int test_SS_stack(void) { STACK_OF(SS) *s = sk_SS_new_null(); STACK_OF(SS) *r = NULL; SS *v[10], *p; const int n = OSSL_NELEM(v); int i; int testresult = 0; /* allocate and push */ for (i = 0; i < n; i++) { v[i] = OPENSSL_malloc(sizeof(*v[i])); if (!TEST_ptr(v[i])) goto end; v[i]->n = i; v[i]->c = 'A' + i; if (!TEST_int_eq(sk_SS_num(s), i)) { TEST_info("SS stack size %d", i); goto end; } sk_SS_push(s, v[i]); } if (!TEST_int_eq(sk_SS_num(s), n)) goto end; /* deepcopy */ r = sk_SS_deep_copy(s, &SS_copy, &SS_free); if (!TEST_ptr(r)) goto end; for (i = 0; i < n; i++) { p = sk_SS_value(r, i); if (!TEST_ptr_ne(p, v[i])) { TEST_info("SS deepcopy non-copy %d", i); goto end; } if (!TEST_int_eq(p->n, v[i]->n)) { TEST_info("test SS deepcopy int %d", i); goto end; } if (!TEST_char_eq(p->c, v[i]->c)) { TEST_info("SS deepcopy char %d", i); goto end; } } /* pop_free - we rely on the malloc debug to catch the leak */ sk_SS_pop_free(r, &SS_free); r = NULL; /* delete_ptr */ p = sk_SS_delete_ptr(s, v[3]); if (!TEST_ptr(p)) goto end; SS_free(p); if (!TEST_int_eq(sk_SS_num(s), n - 1)) goto end; for (i = 0; i < n-1; i++) if (!TEST_ptr_eq(sk_SS_value(s, i), v[i<3 ? i : 1+i])) { TEST_info("SS delete ptr item %d", i); goto end; } testresult = 1; end: sk_SS_pop_free(r, &SS_free); sk_SS_pop_free(s, &SS_free); return testresult; }
static int test_uchar_stack(void) { static const unsigned char v[] = { 1, 3, 7, 5, 255, 0 }; const int n = OSSL_NELEM(v); STACK_OF(uchar) *s = sk_uchar_new(&uchar_compare), *r = NULL; int i; int testresult = 0; /* unshift and num */ for (i = 0; i < n; i++) { if (!TEST_int_eq(sk_uchar_num(s), i)) { TEST_info("uchar stack size %d", i); goto end; } sk_uchar_unshift(s, v + i); } if (!TEST_int_eq(sk_uchar_num(s), n)) goto end; /* dup */ r = sk_uchar_dup(s); if (!TEST_int_eq(sk_uchar_num(r), n)) goto end; sk_uchar_sort(r); /* pop */ for (i = 0; i < n; i++) if (!TEST_ptr_eq(sk_uchar_pop(s), v + i)) { TEST_info("uchar pop %d", i); goto end; } /* free -- we rely on the debug malloc to detect leakage here */ sk_uchar_free(s); s = NULL; /* dup again */ if (!TEST_int_eq(sk_uchar_num(r), n)) goto end; /* zero */ sk_uchar_zero(r); if (!TEST_int_eq(sk_uchar_num(r), 0)) goto end; /* insert */ sk_uchar_insert(r, v, 0); sk_uchar_insert(r, v + 2, -1); sk_uchar_insert(r, v + 1, 1); for (i = 0; i < 3; i++) if (!TEST_ptr_eq(sk_uchar_value(r, i), v + i)) { TEST_info("uchar insert %d", i); goto end; } /* delete */ if (!TEST_ptr_null(sk_uchar_delete(r, 12))) goto end; if (!TEST_ptr_eq(sk_uchar_delete(r, 1), v + 1)) goto end; /* set */ sk_uchar_set(r, 1, v + 1); for (i = 0; i < 2; i++) if (!TEST_ptr_eq(sk_uchar_value(r, i), v + i)) { TEST_info("uchar set %d", i); goto end; } testresult = 1; end: sk_uchar_free(r); sk_uchar_free(s); return testresult; }