mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-01-18 02:39:44 +00:00
Clean up keyring_open_with_pins() error reporting
This commit is contained in:
parent
5ae14ad7bc
commit
0f65028a0b
@ -1084,12 +1084,8 @@ int app_rhizome_add_file(int argc, const char *const *argv, struct command_line_
|
|||||||
cli_arg(argc, argv, o, "author_sid", &authorSid, cli_optional_sid, "");
|
cli_arg(argc, argv, o, "author_sid", &authorSid, cli_optional_sid, "");
|
||||||
cli_arg(argc, argv, o, "pin", &pin, NULL, "");
|
cli_arg(argc, argv, o, "pin", &pin, NULL, "");
|
||||||
cli_arg(argc, argv, o, "manifestpath", &manifestpath, NULL, "");
|
cli_arg(argc, argv, o, "manifestpath", &manifestpath, NULL, "");
|
||||||
|
if (!keyring_open_with_pins(pin))
|
||||||
keyring=keyring_open_with_pins(pin);
|
return -1;
|
||||||
if (!keyring) { WHY("keyring add: Failed to create/open keyring file");
|
|
||||||
return -1; }
|
|
||||||
|
|
||||||
|
|
||||||
/* Ensure the Rhizome database exists and is open */
|
/* Ensure the Rhizome database exists and is open */
|
||||||
if (create_serval_instance_dir() == -1)
|
if (create_serval_instance_dir() == -1)
|
||||||
return -1;
|
return -1;
|
||||||
@ -1266,8 +1262,8 @@ int app_keyring_create(int argc, const char *const *argv, struct command_line_op
|
|||||||
{
|
{
|
||||||
const char *pin;
|
const char *pin;
|
||||||
cli_arg(argc, argv, o, "pin,pin ...", &pin, NULL, "");
|
cli_arg(argc, argv, o, "pin,pin ...", &pin, NULL, "");
|
||||||
keyring_file *k=keyring_open_with_pins(pin);
|
if (!keyring_open_with_pins(pin))
|
||||||
if (!k) WHY("keyring create: Failed to create/open keyring file");
|
return -1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1275,7 +1271,9 @@ int app_keyring_list(int argc, const char *const *argv, struct command_line_opti
|
|||||||
{
|
{
|
||||||
const char *pin;
|
const char *pin;
|
||||||
cli_arg(argc, argv, o, "pin,pin ...", &pin, NULL, "");
|
cli_arg(argc, argv, o, "pin,pin ...", &pin, NULL, "");
|
||||||
keyring_file *k=keyring_open_with_pins(pin);
|
keyring_file *k = keyring_open_with_pins(pin);
|
||||||
|
if (!k)
|
||||||
|
return -1;
|
||||||
|
|
||||||
int cn=0;
|
int cn=0;
|
||||||
int in=0;
|
int in=0;
|
||||||
@ -1307,11 +1305,9 @@ int app_keyring_add(int argc, const char *const *argv, struct command_line_optio
|
|||||||
{
|
{
|
||||||
const char *pin;
|
const char *pin;
|
||||||
cli_arg(argc, argv, o, "pin", &pin, NULL, "");
|
cli_arg(argc, argv, o, "pin", &pin, NULL, "");
|
||||||
|
keyring_file *k = keyring_open_with_pins("");
|
||||||
keyring_file *k=keyring_open_with_pins("");
|
if (!k)
|
||||||
if (!k) { WHY("keyring add: Failed to create/open keyring file");
|
return -1;
|
||||||
return -1; }
|
|
||||||
|
|
||||||
if (keyring_create_identity(k,k->contexts[0],(char *)pin)==NULL)
|
if (keyring_create_identity(k,k->contexts[0],(char *)pin)==NULL)
|
||||||
return setReason("Could not create new identity (keyring_create_identity() failed)");
|
return setReason("Could not create new identity (keyring_create_identity() failed)");
|
||||||
if (keyring_commit(k))
|
if (keyring_commit(k))
|
||||||
@ -1331,8 +1327,9 @@ int app_keyring_set_did(int argc, const char *const *argv, struct command_line_o
|
|||||||
if (strlen(did)>31) return WHY("DID too long (31 digits max)");
|
if (strlen(did)>31) return WHY("DID too long (31 digits max)");
|
||||||
if (strlen(name)>63) return WHY("Name too long (31 char max)");
|
if (strlen(name)>63) return WHY("Name too long (31 char max)");
|
||||||
|
|
||||||
keyring=keyring_open_with_pins((char *)pin);
|
keyring = keyring_open_with_pins((char *)pin);
|
||||||
if (!keyring) return WHY("Could not open keyring file");
|
if (!keyring)
|
||||||
|
return -1;
|
||||||
|
|
||||||
unsigned char packedSid[SID_SIZE];
|
unsigned char packedSid[SID_SIZE];
|
||||||
stowSid(packedSid,0,(char *)sid);
|
stowSid(packedSid,0,(char *)sid);
|
||||||
|
118
keyring.c
118
keyring.c
@ -21,35 +21,36 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|||||||
|
|
||||||
static int urandomfd = -1;
|
static int urandomfd = -1;
|
||||||
|
|
||||||
int urandombytes(unsigned char *x,unsigned long long xlen)
|
int urandombytes(unsigned char *x, unsigned long long xlen)
|
||||||
{
|
{
|
||||||
int i;
|
int tries = 0;
|
||||||
int t=0;
|
|
||||||
|
|
||||||
if (urandomfd == -1) {
|
if (urandomfd == -1) {
|
||||||
for (i=0;i<4;i++) {
|
for (tries = 0; tries < 4; ++tries) {
|
||||||
urandomfd = open("/dev/urandom",O_RDONLY);
|
urandomfd = open("/dev/urandom",O_RDONLY);
|
||||||
if (urandomfd != -1) break;
|
if (urandomfd != -1) break;
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
if (i==4) return -1;
|
if (urandomfd == -1) {
|
||||||
|
WHY_perror("open(/dev/urandom)");
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
tries = 0;
|
||||||
while (xlen > 0) {
|
while (xlen > 0) {
|
||||||
if (xlen < 1048576) i = xlen; else i = 1048576;
|
int i = (xlen < 1048576) ? xlen : 1048576;
|
||||||
|
i = read(urandomfd, x, i);
|
||||||
i = read(urandomfd,x,i);
|
if (i == -1) {
|
||||||
if (i < 1) {
|
if (++tries > 4) {
|
||||||
|
WHY_perror("read(/dev/urandom)");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
sleep(1);
|
sleep(1);
|
||||||
t++;
|
} else {
|
||||||
if (t>4) return -1;
|
tries = 0;
|
||||||
continue;
|
|
||||||
} else t=0;
|
|
||||||
|
|
||||||
x += i;
|
x += i;
|
||||||
xlen -= i;
|
xlen -= i;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,49 +61,52 @@ keyring_file *keyring_open(char *file)
|
|||||||
{
|
{
|
||||||
/* Allocate structure */
|
/* Allocate structure */
|
||||||
keyring_file *k=calloc(sizeof(keyring_file),1);
|
keyring_file *k=calloc(sizeof(keyring_file),1);
|
||||||
if (!k) { WHY("calloc() failed"); return NULL; }
|
if (!k) {
|
||||||
|
WHY_perror("calloc");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
/* Open keyring file read-write if we can, else use it read-only */
|
/* Open keyring file read-write if we can, else use it read-only */
|
||||||
k->file=fopen(file,"r+");
|
k->file=fopen(file,"r+");
|
||||||
if (!k->file) k->file=fopen(file,"r");
|
if (!k->file) k->file=fopen(file,"r");
|
||||||
if (!k->file) k->file=fopen(file,"w+");
|
if (!k->file) k->file=fopen(file,"w+");
|
||||||
if (!k->file) {
|
if (!k->file) {
|
||||||
WHY("Could not open keyring file");
|
WHY_perror("fopen");
|
||||||
fprintf(stderr,"file='%s'\n",file);
|
WHYF("Could not open keyring file %s", file);
|
||||||
keyring_free(k);
|
keyring_free(k);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (fseeko(k->file,0,SEEK_END))
|
if (fseeko(k->file,0,SEEK_END)) {
|
||||||
{
|
WHY_perror("fseeko");
|
||||||
WHY("Could not seek to end of keyring file");
|
WHYF("Could not seek to end of keyring file %s", file);
|
||||||
keyring_free(k);
|
keyring_free(k);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
k->file_size=ftello(k->file);
|
k->file_size=ftello(k->file);
|
||||||
|
|
||||||
if (k->file_size<KEYRING_PAGE_SIZE) {
|
if (k->file_size<KEYRING_PAGE_SIZE) {
|
||||||
/* Uninitialised, so write 2KB of zeroes,
|
/* Uninitialised, so write 2KB of zeroes,
|
||||||
followed by 2KB of random bytes as salt. */
|
followed by 2KB of random bytes as salt. */
|
||||||
if (fseeko(k->file,0,SEEK_SET)) {
|
if (fseeko(k->file,0,SEEK_SET)) {
|
||||||
WHY("Could not seek to start of file to write header");
|
WHY_perror("fseeko");
|
||||||
|
WHYF("Could not seek to start of keyring file %s", file);
|
||||||
keyring_free(k);
|
keyring_free(k);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
unsigned char buffer[KEYRING_PAGE_SIZE];
|
unsigned char buffer[KEYRING_PAGE_SIZE];
|
||||||
bzero(&buffer[0],KEYRING_BAM_BYTES);
|
bzero(&buffer[0],KEYRING_BAM_BYTES);
|
||||||
if (fwrite(&buffer[0],2048,1,k->file)!=1) {
|
if (fwrite(&buffer[0],2048,1,k->file)!=1) {
|
||||||
WHY("Could not write empty bitmap in fresh keyring file");
|
WHY_perror("fwrite");
|
||||||
|
WHYF("Could not write empty bitmap in fresh keyring file %s", file);
|
||||||
keyring_free(k);
|
keyring_free(k);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (urandombytes(&buffer[0],KEYRING_PAGE_SIZE-KEYRING_BAM_BYTES))
|
if (urandombytes(&buffer[0],KEYRING_PAGE_SIZE-KEYRING_BAM_BYTES)) {
|
||||||
{
|
WHYF("Could not get random keyring salt to put in fresh keyring file %s", file);
|
||||||
WHY("Could not get random keyring salt to put in fresh keyring file");
|
|
||||||
keyring_free(k);
|
keyring_free(k);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (fwrite(&buffer[0],KEYRING_PAGE_SIZE-KEYRING_BAM_BYTES,1,k->file)!=1) {
|
if (fwrite(&buffer[0],KEYRING_PAGE_SIZE-KEYRING_BAM_BYTES,1,k->file) != 1) {
|
||||||
WHY("Could not write keyring salt in fresh keyring file");
|
WHY_perror("fwrite");
|
||||||
|
WHYF("Could not write keyring salt in fresh keyring file %s", file);
|
||||||
keyring_free(k);
|
keyring_free(k);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -115,25 +119,25 @@ keyring_file *keyring_open(char *file)
|
|||||||
while(offset<k->file_size) {
|
while(offset<k->file_size) {
|
||||||
/* Read bitmap from slab.
|
/* Read bitmap from slab.
|
||||||
Also, if offset is zero, read the salt */
|
Also, if offset is zero, read the salt */
|
||||||
if (fseeko(k->file,offset,SEEK_SET))
|
if (fseeko(k->file,offset,SEEK_SET)) {
|
||||||
{
|
WHY_perror("fseeko");
|
||||||
WHY("Could not seek to BAM in keyring file");
|
WHYF("Could not seek to BAM in keyring file %s", file);
|
||||||
keyring_free(k);
|
keyring_free(k);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
*b=calloc(sizeof(keyring_bam),1);
|
*b=calloc(sizeof(keyring_bam),1);
|
||||||
if (!(*b))
|
if (!(*b)) {
|
||||||
{
|
WHY_perror("calloc");
|
||||||
WHY("Could not allocate keyring_bam structure for key ring file");
|
WHYF("Could not allocate keyring_bam structure for key ring file %s", file);
|
||||||
keyring_free(k);
|
keyring_free(k);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
(*b)->file_offset=offset;
|
(*b)->file_offset=offset;
|
||||||
/* Read bitmap */
|
/* Read bitmap */
|
||||||
int r=fread(&(*b)->bitmap[0],KEYRING_BAM_BYTES,1,k->file);
|
int r=fread(&(*b)->bitmap[0],KEYRING_BAM_BYTES,1,k->file);
|
||||||
if (r!=1)
|
if (r!=1) {
|
||||||
{
|
WHY_perror("fread");
|
||||||
WHY("Could not read BAM from keyring file");
|
WHYF("Could not read BAM from keyring file %s", file);
|
||||||
keyring_free(k);
|
keyring_free(k);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -144,26 +148,25 @@ keyring_file *keyring_open(char *file)
|
|||||||
multiple contexts being loaded) */
|
multiple contexts being loaded) */
|
||||||
if (!offset) {
|
if (!offset) {
|
||||||
k->contexts[0]=calloc(sizeof(keyring_context),1);
|
k->contexts[0]=calloc(sizeof(keyring_context),1);
|
||||||
if (!k->contexts[0])
|
if (!k->contexts[0]) {
|
||||||
{
|
WHY_perror("calloc");
|
||||||
WHY("Could not allocate keyring_context for keyring file");
|
WHYF("Could not allocate keyring_context for keyring file %s", file);
|
||||||
keyring_free(k);
|
keyring_free(k);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
k->contexts[0]->KeyRingPin=strdup(""); /* Implied empty PIN if none provided */
|
k->contexts[0]->KeyRingPin=strdup(""); /* Implied empty PIN if none provided */
|
||||||
k->contexts[0]->KeyRingSaltLen=KEYRING_PAGE_SIZE-KEYRING_BAM_BYTES;
|
k->contexts[0]->KeyRingSaltLen=KEYRING_PAGE_SIZE-KEYRING_BAM_BYTES;
|
||||||
k->contexts[0]->KeyRingSalt=malloc(k->contexts[0]->KeyRingSaltLen);
|
k->contexts[0]->KeyRingSalt=malloc(k->contexts[0]->KeyRingSaltLen);
|
||||||
if (!k->contexts[0]->KeyRingSalt)
|
if (!k->contexts[0]->KeyRingSalt) {
|
||||||
{
|
WHY_perror("malloc");
|
||||||
WHY("Could not allocate keyring_context->salt for keyring file");
|
WHYF("Could not allocate keyring_context->salt for keyring file %s", file);
|
||||||
keyring_free(k);
|
keyring_free(k);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
r=fread(&k->contexts[0]->KeyRingSalt[0],k->contexts[0]->KeyRingSaltLen,1,k->file);
|
r=fread(&k->contexts[0]->KeyRingSalt[0],k->contexts[0]->KeyRingSaltLen,1,k->file);
|
||||||
if (r!=1)
|
if (r!=1) {
|
||||||
{
|
WHY_perror("fread");
|
||||||
WHY("Could not read salt from keyring file");
|
WHYF("Could not read salt from keyring file %s", file);
|
||||||
keyring_free(k);
|
keyring_free(k);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -1356,17 +1359,14 @@ int keyring_enter_pins(keyring_file *k, const char *pinlist)
|
|||||||
|
|
||||||
keyring_file *keyring_open_with_pins(const char *pinlist)
|
keyring_file *keyring_open_with_pins(const char *pinlist)
|
||||||
{
|
{
|
||||||
keyring_file *k=NULL;
|
keyring_file *k = NULL;
|
||||||
|
|
||||||
if (create_serval_instance_dir() == -1)
|
if (create_serval_instance_dir() == -1)
|
||||||
return NULL;
|
return NULL;
|
||||||
const char *instancePath = serval_instancepath();
|
|
||||||
char keyringFile[1024];
|
char keyringFile[1024];
|
||||||
snprintf(keyringFile,1024,"%s/serval.keyring",instancePath);
|
if (!FORM_SERVAL_INSTANCE_PATH(keyringFile, "serval.keyring"))
|
||||||
if ((k=keyring_open(keyringFile))==NULL)
|
return NULL;
|
||||||
{ fprintf(stderr,"keyring list:Failed to create/open keyring file\n");
|
if ((k = keyring_open(keyringFile)) == NULL)
|
||||||
return NULL; }
|
return NULL;
|
||||||
|
|
||||||
keyring_enter_pins(k,pinlist);
|
keyring_enter_pins(k,pinlist);
|
||||||
return k;
|
return k;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user