int zcert_save (zcert_t *self, char *format, ...) { assert (self); assert (format); va_list argptr; va_start (argptr, format); char *filename = zsys_vprintf (format, argptr); va_end (argptr); // Save public certificate using specified filename zcert_save_public (self, filename); // Now save secret certificate using filename with "_secret" suffix s_save_metadata_all (self); zconfig_comment (self->config, " ZeroMQ CURVE **Secret** Certificate"); zconfig_comment (self->config, " DO NOT PROVIDE THIS FILE TO OTHER USERS nor change its permissions."); zconfig_put (self->config, "/curve/public-key", self->public_txt); zconfig_put (self->config, "/curve/secret-key", self->secret_txt); char filename_secret [256]; snprintf (filename_secret, 256, "%s_secret", filename); zsys_file_mode_private (); int rc = zconfig_save (self->config, filename_secret); zsys_file_mode_default (); zstr_free (&filename); return rc; }
void zconfig_test (bool verbose) { printf (" * zconfig: "); // @selftest // Create temporary directory for test files # define TESTDIR ".test_zconfig" zsys_dir_create (TESTDIR); zconfig_t *root = zconfig_new ("root", NULL); zconfig_t *section, *item; section = zconfig_new ("headers", root); item = zconfig_new ("email", section); zconfig_set_value (item, "*****@*****.**"); item = zconfig_new ("name", section); zconfig_set_value (item, "Justin Kayce"); zconfig_put (root, "/curve/secret-key", "Top Secret"); zconfig_comment (root, " CURVE certificate"); zconfig_comment (root, " -----------------"); zconfig_save (root, TESTDIR "/test.cfg"); zconfig_destroy (&root); root = zconfig_load (TESTDIR "/test.cfg"); if (verbose) zconfig_save (root, "-"); char *email = zconfig_resolve (root, "/headers/email", NULL); assert (email); assert (streq (email, "*****@*****.**")); char *passwd = zconfig_resolve (root, "/curve/secret-key", NULL); assert (passwd); assert (streq (passwd, "Top Secret")); zconfig_save (root, TESTDIR "/test.cfg"); zconfig_destroy (&root); // Delete all test files zdir_t *dir = zdir_new (TESTDIR, NULL); zdir_remove (dir, true); zdir_destroy (&dir); // @end printf ("OK\n"); }
int zcert_save_public (zcert_t *self, char *format, ...) { assert (self); assert (format); va_list argptr; va_start (argptr, format); char *filename = zsys_vprintf (format, argptr); va_end (argptr); s_save_metadata_all (self); zconfig_comment (self->config, " ZeroMQ CURVE Public Certificate"); zconfig_comment (self->config, " Exchange securely, or use a secure mechanism to verify the contents"); zconfig_comment (self->config, " of this file after exchange. Store public certificates in your home"); zconfig_comment (self->config, " directory, in the .curve subdirectory."); zconfig_put (self->config, "/curve/public-key", self->public_txt); int rc = zconfig_save (self->config, filename); zstr_free (&filename); return rc; }
static void s_save_metadata_all (zcert_t *self) { zconfig_destroy (&self->config); self->config = zconfig_new ("root", NULL); zconfig_t *section = zconfig_new ("metadata", self->config); zhash_foreach (self->metadata, s_save_metadata, section); char *timestr = zclock_timestr (); zconfig_comment (self->config, " **** Generated on %s by CZMQ ****", timestr); zstr_free (×tr); }