Skip to content

Commit 8709be5

Browse files
committed
factor out prefixed buffer encryption
1 parent 298a924 commit 8709be5

1 file changed

Lines changed: 17 additions & 31 deletions

File tree

incrypt-plugin.c

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ char* writemeta(char* output);
5252
int encrypt_buffer_gpg(struct strbuf *buffer, struct strbuf *output,
5353
struct string_list *recipients);
5454
void myupdaterefs(const char* refname, const char* oid);
55+
void encryptstrbuf2obj(struct strbuf* in, struct object_id* obj);
5556

5657
/*static*/ const char* CRYPTREADME = "# 401 Unauthorized\n\n"
5758
"This is an encrypted git repository. You can clone it, but you will not be\n"
@@ -331,6 +332,19 @@ char* decryptrefname(const char* input, char* output) {
331332
return output;
332333
}
333334

335+
void encryptstrbuf2obj(struct strbuf* in, struct object_id* obj) {
336+
struct strbuf prefixed = STRBUF_INIT;
337+
unsigned char* encrypted = NULL;
338+
size_t encryptedlen = 0;
339+
hashdatabuf(&prefixed, in);
340+
strbuf_addbuf(&prefixed, in);
341+
encrypted = malloc(prefixed.len+16);
342+
encryptdata((const unsigned char*)prefixed.buf, prefixed.len, encrypted, &encryptedlen);
343+
odb_write_object(the_repository->objects, encrypted, encryptedlen, OBJ_BLOB, obj);
344+
strbuf_release(&prefixed);
345+
free(encrypted);
346+
}
347+
334348
unsigned char* hashdata(const unsigned char* input, size_t inputlen,
335349
unsigned char* output) {
336350
struct git_hash_ctx c;
@@ -412,15 +426,8 @@ struct object_id obj_def;
412426
void metainit(void) {
413427
struct strbuf keybuf = STRBUF_INIT;
414428
struct strbuf output_buf = STRBUF_INIT;
415-
//char* key[48];
416429
struct string_list recipients = STRING_LIST_INIT_NODUP;
417-
struct strbuf templateprefixed = STRBUF_INIT;
418-
unsigned char* templateencrypted = NULL;
419-
size_t templateencryptedlen = 0;
420430
struct strbuf defaultbranch = STRBUF_INIT;
421-
struct strbuf defaultbranchprefixed = STRBUF_INIT;
422-
unsigned char* defaultbranchencrypted = NULL;
423-
size_t defaultbranchencryptedlen = 0;
424431
odb_write_object(the_repository->objects, ver, strlen(ver), OBJ_BLOB, &obj_ver);
425432
strbuf_add(&keybuf, keyver, 15);
426433
getrandom(key, 48, 0);
@@ -433,21 +440,9 @@ void metainit(void) {
433440
odb_write_object(the_repository->objects, output_buf.buf, output_buf.len, OBJ_BLOB, &obj_key);
434441
strbuf_release(&output_buf);
435442
odb_write_object(the_repository->objects, NULL, 0, OBJ_TREE, &obj_sig);
436-
hashdatabuf(&templateprefixed, &template);
437-
strbuf_add(&templateprefixed, template.buf, template.len);
438-
templateencrypted = malloc(templateprefixed.len+16);
439-
encryptdata((const unsigned char*)templateprefixed.buf, templateprefixed.len, templateencrypted, &templateencryptedlen);
440-
odb_write_object(the_repository->objects, templateencrypted, templateencryptedlen, OBJ_BLOB, &obj_msg);
441-
strbuf_release(&templateprefixed);
442-
free(templateencrypted);
443+
encryptstrbuf2obj(&template, &obj_msg);
443444
strbuf_addf(&defaultbranch, "refs/heads/%s", "master");
444-
hashdatabuf(&defaultbranchprefixed, &defaultbranch);
445-
strbuf_add(&defaultbranchprefixed, defaultbranch.buf, defaultbranch.len);
446-
defaultbranchencrypted = malloc(defaultbranchprefixed.len+16);
447-
encryptdata((const unsigned char*)defaultbranchprefixed.buf, defaultbranchprefixed.len, defaultbranchencrypted, &defaultbranchencryptedlen);
448-
odb_write_object(the_repository->objects, defaultbranchencrypted, defaultbranchencryptedlen, OBJ_BLOB, &obj_def);
449-
strbuf_release(&defaultbranchprefixed);
450-
free(defaultbranchencrypted);
445+
encryptstrbuf2obj(&defaultbranch, &obj_def);
451446
}
452447

453448
static void secretcommit(struct object_id* tid, struct object_id* oid) {
@@ -463,9 +458,6 @@ char* writemeta(char* output) {
463458
struct object_id tid;
464459
struct strbuf tb = STRBUF_INIT;
465460
struct strbuf map = STRBUF_INIT;
466-
struct strbuf mapprefixed = STRBUF_INIT;
467-
unsigned char* mapencrypted = NULL;
468-
size_t mapencryptedlen = 0;
469461
struct object_id obj_readme;
470462
struct object_id obj_map;
471463
struct strbuf refname = STRBUF_INIT;
@@ -476,13 +468,7 @@ char* writemeta(char* output) {
476468
strbuf_add(&tb, obj_def.hash, the_hash_algo->rawsz);
477469
strbuf_addf(&tb, "%o %s%c", 0100644, "key", '\0');
478470
strbuf_add(&tb, obj_key.hash, the_hash_algo->rawsz);
479-
hashdatabuf(&mapprefixed, &map);
480-
strbuf_add(&mapprefixed, map.buf, map.len);
481-
mapencrypted = malloc(mapprefixed.len+16);
482-
encryptdata((const unsigned char*)mapprefixed.buf, mapprefixed.len, mapencrypted, &mapencryptedlen);
483-
odb_write_object(the_repository->objects, mapencrypted, mapencryptedlen, OBJ_BLOB, &obj_map);
484-
strbuf_release(&mapprefixed);
485-
free(mapencrypted);
471+
encryptstrbuf2obj(&map, &obj_map);
486472
strbuf_addf(&tb, "%o %s%c", 0100644, "map", '\0');
487473
strbuf_add(&tb, obj_map.hash, the_hash_algo->rawsz);
488474
strbuf_addf(&tb, "%o %s%c", 0100644, "msg", '\0');

0 commit comments

Comments
 (0)