Index: creddy/creddy.h
===================================================================
--- creddy/creddy.h	(revision 39fed7cfca3152c1d7385e267e7873c003b07590)
+++ creddy/creddy.h	(revision 04f5da17f2be6ba1952dc3780a1cc4ede5755112)
@@ -1,64 +1,86 @@
-#ifndef __CREDDY_H__
-#define __CREDDY_H__
+#ifndef __LIBCREDDY_H__
+#define __LIBCREDDY_H__
 
-#include <library.h>
-#include <credentials/certificates/certificate.h>
-#include <credentials/certificates/x509.h>
+#include <stdio.h>
 
-typedef struct _subject_t {
-    char *cert;
-    char *id;
-    char *role;
-} subject_t;
+#define CREDDY_SUCCESS                      0
+#define CREDDY_GENERATE_INVALID_CN          -1
+#define CREDDY_GENERATE_INVALID_VALIDITY    -2
+#define CREDDY_ATTRIBUTE_ISSUER_NOKEY       -3
+#define CREDDY_ATTRIBUTE_INVALID_ROLE       -4
+#define CREDDY_ATTRIBUTE_INVALID_VALIDITY   -5
 
-typedef struct _options_t {
-    int help;
-    int mode;
+/**
+ * Creddy identifiers.
+ */
 
-    char *cert;
+typedef struct _creddy_id_t creddy_id_t;
 
-    // generate options
-    char *cn;
-    int validity;
+// create an ID from an X.509 certificate
+creddy_id_t *creddy_id_from_file(char *filename);
 
-    // attribute options
-    char *issuer;
-    char *key;
-    char *role;
-    subject_t *subjects;
-    int num_subjects;
-    char *out;
+// load an X.509 private key for an from a file
+// handles keys with a password
+int creddy_id_load_privkey(creddy_id_t *id, char *filename);
 
-    // verify options
-    char *attrcert;
-} options_t;
+// generate an ID
+// returns one of CREDDY_SUCCESS or CREDDY_GENERATE_* (see top)
+int creddy_id_generate(creddy_id_t **ret, char *cn, int validity);
 
-#define MODE_GENERATE   1
-#define MODE_VERIFY     2
-#define MODE_KEYID      3
-#define MODE_ATTRIBUTE  4
-#define MODE_ROLES      5
-#define MODE_VERSION    6
+// get the SHA1 keyid, pointer is valid for the lifetime of the object
+char *creddy_id_keyid(creddy_id_t *id);
 
-// returns true if a name starts with a letter and is otherwise alphanumeric
-int clean_name(char *string);
+// default filename for the cert: ${CN}_ID.pem
+// caller must free the returned string
+char *creddy_id_cert_filename(creddy_id_t *id);
 
-// load an attr cert from file (aborts on fail)
-certificate_t *cert_from_file(char *filename);
+// write the cert fo an open file pointer
+void creddy_id_write_cert(creddy_id_t *id, FILE *out);
 
-// generate a random serial
-chunk_t creddy_generate_serial();
+// default filename for the private key: ${CN}_key.pem
+// caller must free the return value
+char *creddy_id_privkey_filename(creddy_id_t *id);
 
-void usage(options_t *opts);
-void *xmalloc(size_t len);
-void *xrealloc(void *ptr, size_t size);
-char *xstrdup(char *string);
+// write the private key to a file
+// it is recommended that you open this file mode 0600
+void creddy_id_write_privkey(creddy_id_t *id, FILE *out);
 
-// sub programs
-void generate_main(options_t *opts);
-void keyid_main(options_t *opts);
-void attribute_main(options_t *opts);
-void roles_main(options_t *opts);
-void verify_main(options_t *opts);
+// destroy the id
+void creddy_id_free(creddy_id_t *id);
 
-#endif /* __CREDDY_H__ */
+/**
+ * Attribute cert
+ */
+typedef struct _creddy_attribute_t creddy_attribute_t;
+
+//
+// Here's the skinny:
+//  Attribute cert objects don't contain an actual cert until they're baked.
+//  First you construct the object using creddy_attribute_create, then you add
+//  subjects to it using creddy_attribute_{principal,role,linking_role}.
+//  Finally you bake it. Once you've done that, you can access the DER encoding
+//  or write it to a file.
+//
+
+// create an attribute cert
+// validity is in days
+// returns one of CREDDY_SUCCESS or CREDDY_ATTRIBUTE_* (see top)
+int creddy_attribute_create(creddy_attribute_t **attr, creddy_id_t *issuer, char *role, int validity);
+
+// add a principal subject to the cert
+int creddy_attribute_principal(creddy_attribute_t *attr, char *keyid);
+
+// add a role subject
+int creddy_attribute_role(creddy_attribute_t *attr, char *keyid, char *role);
+
+// add a linking role subject
+int creddy_attribute_linking_role(creddy_attribute_t *attr, char *keyid, char *role, char *linked);
+
+// create the attribute cert once all the subjects have been added
+// can return 0 if there are no subjects or there's a problem building the cert
+int creddy_attribute_bake(creddy_attribute_t *attr);
+
+// destroy the cert
+void creddy_attribute_free(creddy_attribute_t *attr);
+
+#endif /* __LIBCREDDY_H__ */
