adb: refactor struct adb_header to adb_file_header

In struct adb, do not keep the whole header, just the schema
in host byte order.
cute-signatures
Timo Teräs 2021-07-21 20:09:18 +03:00
parent 2f1186aa40
commit e2ebd761a5
4 changed files with 37 additions and 39 deletions

View File

@ -70,6 +70,11 @@ void adb_reset(struct adb *db)
db->adb.len = 0; db->adb.len = 0;
} }
static int __adb_dummy_cb(struct adb *db, struct adb_block *b, struct apk_istream *is)
{
return 0;
}
static int __adb_m_parse(struct adb *db, apk_blob_t data, struct apk_trust *t, static int __adb_m_parse(struct adb *db, apk_blob_t data, struct apk_trust *t,
int (*cb)(struct adb *, struct adb_block *, struct apk_istream *)) int (*cb)(struct adb *, struct adb_block *, struct apk_istream *))
{ {
@ -121,22 +126,22 @@ err:
int adb_m_blob(struct adb *db, apk_blob_t blob, struct apk_trust *t) int adb_m_blob(struct adb *db, apk_blob_t blob, struct apk_trust *t)
{ {
adb_init(db); adb_init(db);
return __adb_m_parse(db, blob, t, 0); return __adb_m_parse(db, blob, t, __adb_dummy_cb);
} }
static int __adb_m_mmap(struct adb *db, apk_blob_t mmap, uint32_t expected_schema, struct apk_trust *t, static int __adb_m_mmap(struct adb *db, apk_blob_t mmap, uint32_t expected_schema, struct apk_trust *t,
int (*cb)(struct adb *, struct adb_block *, struct apk_istream *)) int (*cb)(struct adb *, struct adb_block *, struct apk_istream *))
{ {
struct adb_header *hdr; struct adb_file_header *hdr;
int r = -APKE_ADB_HEADER; int r = -APKE_ADB_HEADER;
apk_blob_t data = mmap; apk_blob_t data = mmap;
if (!(expected_schema & ADB_SCHEMA_IMPLIED)) { if (!(expected_schema & ADB_SCHEMA_IMPLIED)) {
if (mmap.len < sizeof *hdr) return -APKE_ADB_HEADER; if (mmap.len < sizeof *hdr) return -APKE_ADB_HEADER;
hdr = (struct adb_header *) mmap.ptr; hdr = (struct adb_file_header *) mmap.ptr;
if (hdr->magic != htole32(ADB_FORMAT_MAGIC)) return -APKE_ADB_HEADER; if (hdr->magic != htole32(ADB_FORMAT_MAGIC)) return -APKE_ADB_HEADER;
if (expected_schema && expected_schema != le32toh(hdr->schema)) return -APKE_ADB_SCHEMA; if (expected_schema && expected_schema != le32toh(hdr->schema)) return -APKE_ADB_SCHEMA;
db->hdr = *hdr; db->schema = le32toh(hdr->schema);
data = APK_BLOB_PTR_LEN(mmap.ptr + sizeof *hdr, mmap.len - sizeof *hdr); data = APK_BLOB_PTR_LEN(mmap.ptr + sizeof *hdr, mmap.len - sizeof *hdr);
} }
@ -151,6 +156,7 @@ err:
static int __adb_m_stream(struct adb *db, struct apk_istream *is, uint32_t expected_schema, static int __adb_m_stream(struct adb *db, struct apk_istream *is, uint32_t expected_schema,
struct apk_trust *t, int (*cb)(struct adb *, struct adb_block *, struct apk_istream *)) struct apk_trust *t, int (*cb)(struct adb *, struct adb_block *, struct apk_istream *))
{ {
struct adb_file_header hdr;
struct adb_verify_ctx vfy = {}; struct adb_verify_ctx vfy = {};
struct adb_block blk; struct adb_block blk;
struct apk_segment_istream seg; struct apk_segment_istream seg;
@ -162,15 +168,16 @@ static int __adb_m_stream(struct adb *db, struct apk_istream *is, uint32_t expec
if (IS_ERR(is)) return PTR_ERR(is); if (IS_ERR(is)) return PTR_ERR(is);
if (!(expected_schema & ADB_SCHEMA_IMPLIED)) { if (!(expected_schema & ADB_SCHEMA_IMPLIED)) {
if ((r = apk_istream_read(is, &db->hdr, sizeof db->hdr)) < 0) goto err; if ((r = apk_istream_read(is, &hdr, sizeof hdr)) < 0) goto err;
if (db->hdr.magic != htole32(ADB_FORMAT_MAGIC)) { if (hdr.magic != htole32(ADB_FORMAT_MAGIC)) {
r = -APKE_ADB_HEADER; r = -APKE_ADB_HEADER;
goto err; goto err;
} }
if (expected_schema && expected_schema != le32toh(db->hdr.schema)) { if (expected_schema && expected_schema != le32toh(hdr.schema)) {
r = -APKE_ADB_SCHEMA; r = -APKE_ADB_SCHEMA;
goto err; goto err;
} }
db->schema = le32toh(hdr.schema);
} }
do { do {
@ -230,22 +237,13 @@ err:
return apk_istream_close_error(is, r); return apk_istream_close_error(is, r);
} }
static int __adb_dummy_cb(struct adb *db, struct adb_block *b, struct apk_istream *is)
{
return 0;
}
int adb_m_process(struct adb *db, struct apk_istream *is, uint32_t expected_schema, int adb_m_process(struct adb *db, struct apk_istream *is, uint32_t expected_schema,
struct apk_trust *t, int (*cb)(struct adb *, struct adb_block *, struct apk_istream *)) struct apk_trust *t, int (*cb)(struct adb *, struct adb_block *, struct apk_istream *))
{ {
apk_blob_t mmap = apk_istream_mmap(is); apk_blob_t mmap = apk_istream_mmap(is);
memset(db, 0, sizeof *db); memset(db, 0, sizeof *db);
if (expected_schema & ADB_SCHEMA_IMPLIED) { if (expected_schema & ADB_SCHEMA_IMPLIED)
db->hdr = (struct adb_header) { db->schema = expected_schema & ~ADB_SCHEMA_IMPLIED;
.magic = ADB_FORMAT_MAGIC,
.schema = expected_schema & ~ADB_SCHEMA_IMPLIED,
};
}
if (!cb) cb = __adb_dummy_cb; if (!cb) cb = __adb_dummy_cb;
if (!APK_BLOB_IS_NULL(mmap)) { if (!APK_BLOB_IS_NULL(mmap)) {
db->is = is; db->is = is;
@ -259,8 +257,7 @@ int adb_w_init_dynamic(struct adb *db, uint32_t schema, void *buckets, size_t nu
size_t i; size_t i;
*db = (struct adb) { *db = (struct adb) {
.hdr.magic = htole32(ADB_FORMAT_MAGIC), .schema = schema,
.hdr.schema = htole32(schema),
.num_buckets = num_buckets, .num_buckets = num_buckets,
.bucket = buckets, .bucket = buckets,
}; };
@ -276,7 +273,6 @@ int adb_w_init_dynamic(struct adb *db, uint32_t schema, void *buckets, size_t nu
int adb_w_init_static(struct adb *db, void *buf, size_t bufsz) int adb_w_init_static(struct adb *db, void *buf, size_t bufsz)
{ {
*db = (struct adb) { *db = (struct adb) {
.hdr.magic = htole32(ADB_FORMAT_MAGIC),
.adb.ptr = buf, .adb.ptr = buf,
.alloc_len = bufsz, .alloc_len = bufsz,
}; };
@ -518,7 +514,7 @@ static unsigned iovec_memcmp(struct iovec *vec, size_t nvec, void *base)
static adb_val_t adb_w_error(struct adb *db, int rc) static adb_val_t adb_w_error(struct adb *db, int rc)
{ {
assert(0); assert(0);
db->hdr.magic = 0; db->schema = 0;
return ADB_ERROR(rc); return ADB_ERROR(rc);
} }
@ -927,7 +923,11 @@ int adb_s_field_by_name(const struct adb_object_schema *schema, const char *name
/* Container creation */ /* Container creation */
int adb_c_header(struct apk_ostream *os, struct adb *db) int adb_c_header(struct apk_ostream *os, struct adb *db)
{ {
return apk_ostream_write(os, &db->hdr, sizeof db->hdr); struct adb_file_header hdr = {
.magic = ADB_FORMAT_MAGIC,
.schema = htole32(db->schema),
};
return apk_ostream_write(os, &hdr, sizeof hdr);
} }
int adb_c_block(struct apk_ostream *os, uint32_t type, apk_blob_t val) int adb_c_block(struct apk_ostream *os, uint32_t type, apk_blob_t val)
@ -1010,10 +1010,8 @@ int adb_c_block_copy(struct apk_ostream *os, struct adb_block *b, struct apk_ist
int adb_c_adb(struct apk_ostream *os, struct adb *db, struct apk_trust *t) int adb_c_adb(struct apk_ostream *os, struct adb *db, struct apk_trust *t)
{ {
if (IS_ERR(os)) if (IS_ERR(os)) return PTR_ERR(os);
return apk_ostream_cancel(os, PTR_ERR(os)); if (!db->schema) return apk_ostream_cancel(os, -APKE_ADB_HEADER);
if (db->hdr.magic != htole32(ADB_FORMAT_MAGIC))
return apk_ostream_cancel(os, -APKE_ADB_HEADER);
adb_c_header(os, db); adb_c_header(os, db);
adb_c_block(os, ADB_BLOCK_ADB, db->adb); adb_c_block(os, ADB_BLOCK_ADB, db->adb);
@ -1053,11 +1051,11 @@ static int adb_digest_adb(struct adb_verify_ctx *vfy, unsigned int hash_alg, apk
return 0; return 0;
} }
static int adb_digest_v0_signature(struct apk_digest_ctx *dctx, struct adb_header *hdr, struct adb_sign_v0 *sig0, apk_blob_t md) static int adb_digest_v0_signature(struct apk_digest_ctx *dctx, uint32_t schema, struct adb_sign_v0 *sig0, apk_blob_t md)
{ {
int r; int r;
if ((r = apk_digest_ctx_update(dctx, hdr, sizeof *hdr)) != 0 || if ((r = apk_digest_ctx_update(dctx, &schema, sizeof schema)) != 0 ||
(r = apk_digest_ctx_update(dctx, sig0, sizeof *sig0)) != 0 || (r = apk_digest_ctx_update(dctx, sig0, sizeof *sig0)) != 0 ||
(r = apk_digest_ctx_update(dctx, md.ptr, md.len)) != 0) (r = apk_digest_ctx_update(dctx, md.ptr, md.len)) != 0)
return r; return r;
@ -1096,7 +1094,7 @@ int adb_trust_write_signatures(struct apk_trust *trust, struct adb *db, struct a
siglen = sizeof sig.buf - sizeof sig.v0; siglen = sizeof sig.buf - sizeof sig.v0;
if ((r = apk_sign_start(&trust->dctx, &tkey->key)) != 0 || if ((r = apk_sign_start(&trust->dctx, &tkey->key)) != 0 ||
(r = adb_digest_v0_signature(&trust->dctx, &db->hdr, &sig.v0, md)) != 0 || (r = adb_digest_v0_signature(&trust->dctx, db->schema, &sig.v0, md)) != 0 ||
(r = apk_sign(&trust->dctx, sig.v0.sig, &siglen)) != 0) (r = apk_sign(&trust->dctx, sig.v0.sig, &siglen)) != 0)
goto err; goto err;
@ -1128,7 +1126,7 @@ int adb_trust_verify_signature(struct apk_trust *trust, struct adb *db, struct a
if (adb_digest_adb(vfy, sig->hash_alg, db->adb, &md) != 0) continue; if (adb_digest_adb(vfy, sig->hash_alg, db->adb, &md) != 0) continue;
if (apk_verify_start(&trust->dctx, &tkey->key) != 0 || if (apk_verify_start(&trust->dctx, &tkey->key) != 0 ||
adb_digest_v0_signature(&trust->dctx, &db->hdr, sig0, md) != 0 || adb_digest_v0_signature(&trust->dctx, db->schema, sig0, md) != 0 ||
apk_verify(&trust->dctx, sig0->sig, sigb.len - sizeof *sig0) != 0) apk_verify(&trust->dctx, sig0->sig, sigb.len - sizeof *sig0) != 0)
continue; continue;

View File

@ -46,7 +46,7 @@ typedef uint32_t adb_val_t;
#define ADB_FORMAT_MAGIC 0x2e424441 // ADB. #define ADB_FORMAT_MAGIC 0x2e424441 // ADB.
#define ADB_SCHEMA_IMPLIED 0x80000000 #define ADB_SCHEMA_IMPLIED 0x80000000
struct adb_header { struct adb_file_header {
uint32_t magic; uint32_t magic;
uint32_t schema; uint32_t schema;
}; };
@ -140,8 +140,8 @@ struct adb_w_bucket {
struct adb { struct adb {
struct apk_istream *is; struct apk_istream *is;
apk_blob_t adb; apk_blob_t adb;
struct adb_header hdr; uint32_t schema;
size_t num_buckets; uint32_t num_buckets;
size_t alloc_len; size_t alloc_len;
struct list_head *bucket; struct list_head *bucket;
}; };

View File

@ -112,7 +112,7 @@ static int adb_walk_block(struct adb *db, struct adb_block *b, struct apk_istrea
struct adb_walk *d = ctx->d; struct adb_walk *d = ctx->d;
char tmp[16+ADB_MAX_SIGNATURE_LEN*2]; char tmp[16+ADB_MAX_SIGNATURE_LEN*2];
struct adb_sign_hdr *s; struct adb_sign_hdr *s;
uint32_t schema_magic = ctx->db.hdr.schema; uint32_t schema_magic = ctx->db.schema;
const struct adb_db_schema *ds; const struct adb_db_schema *ds;
int r, len; int r, len;
size_t sz = adb_block_length(b); size_t sz = adb_block_length(b);
@ -120,7 +120,7 @@ static int adb_walk_block(struct adb *db, struct adb_block *b, struct apk_istrea
switch (adb_block_type(b)) { switch (adb_block_type(b)) {
case ADB_BLOCK_ADB: case ADB_BLOCK_ADB:
d->ops->schema(d, db->hdr.schema); d->ops->schema(d, db->schema);
for (ds = d->schemas; ds->magic; ds++) for (ds = d->schemas; ds->magic; ds++)
if (ds->magic == schema_magic) break; if (ds->magic == schema_magic) break;
len = snprintf(tmp, sizeof tmp, "ADB block, size: %zu", sz); len = snprintf(tmp, sizeof tmp, "ADB block, size: %zu", sz);

View File

@ -7,7 +7,7 @@ static int adb_walk_genadb_schema(struct adb_walk *d, uint32_t schema_id)
struct adb_walk_genadb *dt = container_of(d, struct adb_walk_genadb, d); struct adb_walk_genadb *dt = container_of(d, struct adb_walk_genadb, d);
const struct adb_db_schema *s; const struct adb_db_schema *s;
dt->db.hdr.schema = htole32(schema_id); dt->db.schema = schema_id;
for (s = d->schemas; s->magic; s++) for (s = d->schemas; s->magic; s++)
if (s->magic == schema_id) break; if (s->magic == schema_id) break;
if (!s) return -APKE_ADB_SCHEMA; if (!s) return -APKE_ADB_SCHEMA;
@ -29,7 +29,7 @@ static int adb_walk_genadb_start_object(struct adb_walk *d)
{ {
struct adb_walk_genadb *dt = container_of(d, struct adb_walk_genadb, d); struct adb_walk_genadb *dt = container_of(d, struct adb_walk_genadb, d);
if (!dt->db.hdr.schema) return -APKE_ADB_SCHEMA; if (!dt->db.schema) return -APKE_ADB_SCHEMA;
if (dt->nest >= ARRAY_SIZE(dt->objs)) return -APKE_ADB_LIMIT; if (dt->nest >= ARRAY_SIZE(dt->objs)) return -APKE_ADB_LIMIT;
if (dt->curkey[dt->nest] == 0 && if (dt->curkey[dt->nest] == 0 &&
@ -45,7 +45,7 @@ static int adb_walk_genadb_start_object(struct adb_walk *d)
struct adb_adb_schema *schema = container_of(&dt->objs[dt->nest-1].schema->kind, struct adb_adb_schema, kind); struct adb_adb_schema *schema = container_of(&dt->objs[dt->nest-1].schema->kind, struct adb_adb_schema, kind);
if (dt->nestdb >= ARRAY_SIZE(dt->idb)) return -APKE_ADB_LIMIT; if (dt->nestdb >= ARRAY_SIZE(dt->idb)) return -APKE_ADB_LIMIT;
adb_reset(&dt->idb[dt->nestdb]); adb_reset(&dt->idb[dt->nestdb]);
dt->idb[dt->nestdb].hdr.schema = htole32(schema->schema_id); dt->idb[dt->nestdb].schema = schema->schema_id;
dt->objs[dt->nest].db = &dt->idb[dt->nestdb]; dt->objs[dt->nest].db = &dt->idb[dt->nestdb];
dt->nestdb++; dt->nestdb++;
} }