Replace "add did" command with improved "keyring add did"

Output is same format as "keyring add" command

Fixed minor bug in rolling a random DID -- last byte was not random

Some code improvements, including better support for sid_t
This commit is contained in:
Andrew Bettison 2013-02-14 16:06:01 +10:30
parent 433f49551a
commit c42260f5f9
6 changed files with 62 additions and 16 deletions

View File

@ -1638,8 +1638,8 @@ int app_keyring_set_did(const struct cli_parsed *parsed, void *context)
{
if (config.debug.verbose)
DEBUG_cli_parsed(parsed);
const char *sid, *did, *name;
cli_arg(parsed, "sid", &sid, str_is_subscriber_id, "");
const char *sidhex, *did, *name;
cli_arg(parsed, "sid", &sidhex, str_is_subscriber_id, "");
cli_arg(parsed, "did", &did, cli_optional_did, "");
cli_arg(parsed, "name", &name, NULL, "");
@ -1648,18 +1648,34 @@ int app_keyring_set_did(const struct cli_parsed *parsed, void *context)
if (!(keyring = keyring_open_instance_cli(parsed)))
return -1;
unsigned char packedSid[SID_SIZE];
stowSid(packedSid,0,(char *)sid);
sid_t sid;
if (str_to_sid_t(&sid, sidhex) == -1)
return WHY("str_to_sid_t() failed");
int cn=0,in=0,kp=0;
int r=keyring_find_sid(keyring,&cn,&in,&kp,packedSid);
int r=keyring_find_sid(keyring, &cn, &in, &kp, sid.binary);
if (!r) return WHY("No matching SID");
if (keyring_set_did(keyring->contexts[cn]->identities[in],
(char *)did,(char *)name))
if (keyring_set_did(keyring->contexts[cn]->identities[in], did, name))
return WHY("Could not set DID");
if (keyring_commit(keyring))
return WHY("Could not write updated keyring record");
cli_puts("sid");
cli_delim(":");
cli_printf("%s", alloca_tohex_sid_t(sid));
cli_delim("\n");
if (did) {
cli_puts("did");
cli_delim(":");
cli_puts(did);
cli_delim("\n");
}
if (name) {
cli_puts("name");
cli_delim(":");
cli_puts(name);
cli_delim("\n");
}
keyring_free(keyring);
return 0;
}
@ -2203,7 +2219,7 @@ struct cli_schema command_line_options[]={
"List identites in specified key ring that can be accessed using the specified PINs"},
{app_keyring_add,{"keyring","add" KEYRING_PIN_OPTIONS,"[<pin>]",NULL},CLIFLAG_STANDALONE,
"Create a new identity in the keyring protected by the provided PIN"},
{app_keyring_set_did,{"set","did" KEYRING_PIN_OPTIONS,"<sid>","<did>","<name>",NULL},CLIFLAG_STANDALONE,
{app_keyring_set_did,{"keyring", "set","did" KEYRING_PIN_OPTIONS,"<sid>","<did>","<name>",NULL},CLIFLAG_STANDALONE,
"Set the DID for the specified SID. Optionally supply PIN to unlock the SID record in the keyring."},
{app_id_self,{"id","self",NULL},0,
"Return my own identity(s) as URIs"},

View File

@ -22,6 +22,31 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "str.h"
#include <ctype.h>
int str_to_sid_t(sid_t *sid, const char *hex)
{
if (strcmp(hex, "broadcast") == 0) {
*sid = SID_BROADCAST;
return 0;
}
return fromhexstr(sid->binary, hex, sizeof sid->binary);
}
int strn_to_sid_t(sid_t *sid, const char *hex, const char **endp)
{
if (str_startswith(hex, "broadcast", endp) == 0) {
*sid = SID_BROADCAST;
return 0;
}
sid_t tmp;
int n = fromhex(tmp.binary, hex, sizeof tmp.binary);
if (n != sizeof tmp.binary)
return -1;
*sid = tmp;
if (endp)
*endp = hex + sizeof sid->binary * 2;
return 0;
}
int str_is_subscriber_id(const char *sid)
{
size_t len = 0;

View File

@ -990,7 +990,7 @@ int keyring_commit(keyring_file *k)
return errorCount;
}
int keyring_set_did(keyring_identity *id,char *did,char *name)
int keyring_set_did(keyring_identity *id, const char *did, const char *name)
{
if (!id) return WHY("id is null");
if (!did) return WHY("did is null");
@ -1373,9 +1373,9 @@ int keyring_seed(keyring_file *k)
return 0;
int i;
unsigned char did[65];
char did[65];
/* Securely generate random telephone number */
urandombytes((unsigned char *)did,10);
urandombytes((unsigned char *)did, 11);
/* Make DID start with 2 through 9, as 1 is special in many number spaces,
and 0 is commonly used for escaping to national or international dialling. */
did[0]='2'+(did[0]%8);
@ -1384,7 +1384,7 @@ int keyring_seed(keyring_file *k)
keyring_identity *id=keyring_create_identity(k,k->contexts[0],"");
if (!id) return WHY("Could not create new identity");
if (keyring_set_did(id,(char *)did,"")) return WHY("Could not set DID of new identity");
if (keyring_set_did(id, did, "")) return WHY("Could not set DID of new identity");
if (keyring_commit(k)) return WHY("Could not commit new identity to keyring file");
return 0;
}

View File

@ -268,7 +268,7 @@ keyring_file *keyring_open(char *file);
keyring_file *keyring_open_instance();
keyring_file *keyring_open_instance_cli(const struct cli_parsed *parsed);
int keyring_enter_pin(keyring_file *k, const char *pin);
int keyring_set_did(keyring_identity *id,char *did,char *name);
int keyring_set_did(keyring_identity *id, const char *did, const char *name);
int keyring_sanitise_position(const keyring_file *k,int *cn,int *in,int *kp);
int keyring_next_keytype(const keyring_file *k, int *cn, int *in, int *kp, int keytype);
int keyring_next_identity(const keyring_file *k,int *cn,int *in,int *kp);
@ -429,6 +429,11 @@ typedef struct sid_binary {
// is the SID entirely 0x00?
#define is_sid_any(SID) is_all_matching(SID, SID_SIZE, 0)
#define alloca_tohex_sid_t(sid) alloca_tohex((sid).binary, sizeof (*(sid_t*)0).binary)
int str_to_sid_t(sid_t *sid, const char *hex);
int strn_to_sid_t(sid_t *sid, const char *hex, const char **endp);
int str_is_subscriber_id(const char *sid);
int strn_is_subscriber_id(const char *sid, size_t *lenp);
int str_is_did(const char *did);

View File

@ -582,7 +582,7 @@ create_identities() {
# them, otherwise extract the DID and NAME automatically generated by
# servald.
if [ -n "${!didvar}" -o -n "${!namevar}" ]; then
executeOk_servald set did "${!sidvar}" "${!didvar}" "${!namevar}"
executeOk_servald keyring set did "${!sidvar}" "${!didvar}" "${!namevar}"
eval "$didvar=\${!didvar}"
eval "$namevar=\${!namevar}"
tfw_log "$didvar=$(shellarg "${!didvar}")"

View File

@ -51,7 +51,7 @@ setup_publish() {
assert_no_servald_processes
foreach_instance +A +B +C +D create_single_identity
set_instance +D
executeOk_servald set did $SIDD $DIDC "Agent D Smith"
executeOk_servald keyring set did $SIDD $DIDC "Agent D Smith"
DIDD1=$DIDC
NAMED1="Agent D Smith"
DIDD=$DIDC1