2 === Advanced CFErrorRef usage ===
 
   4 Make SecError a macro with an extra argument (formatOptions) and make the arguments proper CFStringRef
 
   5 CFCopyDescription takes formatOptions as well, which it passes down to us.
 
   6 Make sure we plumb though formatOptions everywhere
 
   7 Have every cftype implement a proper copyDescriotion function that looks at a kSecDebugFormatOption key and use it to do debug printing.
 
   8 Have the CFGiblis macros automatically implement copyDebugDescription function that calls
 
  11 == Content Protection ==
 
  13 * Notice that the system keystore is unavailible during upgrade and
 
  14   fail without removing old keychain since it won't work after an
 
  17 * cert table should be AlwaysAvailable + migratable...
 
  21 * security export - dump all classes and caches in unencrypted plist
 
  22   genp, inet, cert, keys, ocsp, cair, crls.
 
  23   security export genp, inet, cert, keys, ocsp, cair
 
  25 * security dh command should support decodePEM.
 
  30 Test ssl server and ssl client against certs with proper and wrong EKU.  Also
 
  31 test this for EV certs.
 
  33 Add some ipsec certs with proper ipsec eku extensions and without them and
 
  34 ensure that the ipsec policy rejects (done in si-20-sectrust) and accepts
 
  35 each kind as it should.
 
  37 == AUDIT of securityd_server_request ==
 
  39 in_args are a valid plist.
 
  42     _SecItemAdd() argument 1 is a dictionary, but its contents have not been checked.
 
  43 sec_item_copy_matching_id
 
  44     _SecItemCopyMatching() argument 1 is a dictionary, but its contents have not been checked.
 
  46     _SecItemDelete() argument 1 is a dictionary, but its contents have not been checked.
 
  48     _SecItemUpdate() argument 1 and 2 are dictionaries, but their contents have not been checked.
 
  49 sec_trust_store_contains_id (ok)
 
  50     SecTrustStoreForDomainName() argument 1 is a string of any length (might be 0).
 
  51     SecTrustStoreContainsCertificateWithDigest() argument 1 might be NULL, argument 2 is a data of any length (might be 0).
 
  52 sec_trust_store_set_trust_settings_id (ok)
 
  53     SecCertificateCreateWithData() argument 2 is a data of any length (might be 0) (done)
 
  54     _SecTrustStoreSetTrustSettings() argument 1 might be NULL, argument 3 is either NULL, a dictionary or an array, but its contents have not been checked.
 
  55 sec_trust_store_remove_certificate_id (ok)
 
  56     SecTrustStoreRemoveCertificateWithDigest() argument 2 is a data of any length (might be 0) an its bound to a statement in sqlite.
 
  57 sec_delete_all_id (ok)
 
  58     Audit done (args_in not used and its value is ignored).
 
  60     SecTrustServerEvaluateAsync() argument 1 is a dictionary, but its contents have not been checked.
 
  61    The options field of each policy provided ends up in policy->_options unchecked, so every access
 
  62    of policy->_options in SecPolicyServer.c needs to be sanitized.
 
  63 sec_restore_keychain (ok)
 
  64     Audit done (args_in not used and its value is ignored).
 
  68 Function naming conventions:
 
  69  - Use Sec@@@Finalaize only if the function does not free the thing it's finalizing.
 
  70  - use Sec@@@Destroy otherwise.
 
  72 Move EVRoots.plist and system TrustStore.sqlite3 into perfect hashtable inside securityd.
 
  74 Stop using framework relative resources in securityd since it uses a few kb of ram just to get to them.
 
  76 Change the interfaces to securityd to use real argument lists and do all the encoding/decoding inside client.c and server.c.
 
  78 Remove SecCertificateCreate() now that <rdar://problem/5701851> iap submits a binary is fixed.
 
  80 SecKeyRawSign/SecKeyRawVerify should get better error codes.
 
  84 Consider calling int sqlite3_release_memory(int); (after committing outstanding transactions); when we are low on ram.
 
  86 change api to asynchttp.h to take 2 arguments, a boolean flag to use http GET is the URL size is less than 256 bytes, two dictionary arguments of header name,values pairs to add to the GET and POST requests.  If only GET is present GET will always be used, if only POST is present only POST will be used.  If both are present, we use GET only if the URL size is less than 256 bytes and POST otherwise.  The callback should have a CFHTTPMessage() argument, which will go away when the callback returns.
 
  88 When receiving a stale response refetch by adding a Cache-Control: no-cache header.
 
  89 Implement a timeout and retry count (perhaps after timeout go to next responder, but possibly retry responders again if they all timeout).
 
  90 If id-pkix-ocsp-nocheck is not present in the responder cert, check revocation status of the responder cert itself (probably via crls, otherwise watch out for infinite recursion).
 
  92 Verify responder cert chain. (done)
 
  93 Verify singleResponse validity at usage time. (done)
 
  94 Verify signature of response. (done)
 
  95 Verify responder id. (done)
 
  99 Check extended keyUsage and keyUsage of leaf.
 
 105 <rdar://problem/4831694> Implement DSA using libGiants
 
 106 <rdar://problem/4831689> Add support for DSA ciphersuites in SecureTransport
 
 107 <rdar://problem/4831710> Implement DSS (DSA) Support for SecKeys
 
 108 <rdar://problem/4831700> Add support for DSS (DSA) signed certificates
 
 110 <rdar://problem/4831731> Support CRL Fetching and caching
 
 111 <rdar://problem/4831751> Support URL based certificate issuer fetching
 
 113 <rdar://problem/4831794> Suite B support for Blackberry feature parody
 
 117 * Trust settings dialog (P2 for P3)
 
 118 * Trust settings ui (P2 for P3)
 
 122 * SecItemDelete for identities should be server-side so it's done in an exclusive transaction
 
 124 * SecKeyRef for DH public/private keys (P3)
 
 126    - SecKey AES support?
 
 129 Notes on TrustStore trustsettings db.
 
 131             // Version table has one record
 
 132             CREATE TABLE version(version INTEGER);
 
 133             CREATE TABLE cert{sha1 BLOB(20) UNIQUE PRIMARY KEY,subj BLOB,cert BLOB,mdat REAL);
 
 134                 CREATE INDEX isubj ON cert(subj);
 
 135             CREATE TABLE trust(sha1 BLOB(20) UNIQUE PRIMARY KEY,
 
 141                 allowedError INTEGER,
 
 145             SELECT domain,policyString,allowedError,result FROM trust WHERE
 
 146                 (sha1=? OR sha1=defaut) AND
 
 148                 (policy=? OR policy ISNULL) AND
 
 149                 (application=? OR application ISNULL) AND
 
 151                 ORDER BY COLLATE domain DESC;
 
 154             INSERT OR REPLACE INTO cert(sha1,subj,cert,mdat)VALUES(?,?,?,?);
 
 155             DELETE FROM trust WHERE sha1=? AND domain=?
 
 156             INSERT INTO trust(sha1,domain,
 
 157                 policy,application,keyUsage,policyString,allowedError,result)
 
 158                 VALUES(?,?,?,?,?,?,?,?);
 
 160 To adopt libdispatch in SecTrustServer.c, we should make a queue per builder.
 
 161 That builders queue can then have
 
 162 The chain builder will look something like
 
 163     dispatch_async(queue, builder, SecPathBuilderNext);
 
 164     inside SecPathBuilderNext we dispatch_async(queue, builder, state);
 
 165     where state is whatever the new state to be run is.
 
 166     where today we invoke the client callback and free the builder in
 
 167     SecPathBuilderStep(), that will be replaced by doing it in the level
 
 168     that today sets builder->state to NULL, which of course just doesn't
 
 169     dispatch any new blocks since invoking the callback completes the API.
 
 170     If we want to allow the caller to use dispatch himself (for sending
 
 171     the reply on a reply queue of some kind that can reschedule jobs when
 
 172     a port_notify_send_ok (or whatever it's called) is received.
 
 173     If async io is done it uses a dispatch_source to run (or it can use a
 
 174     plain cfrunloop callback if the callback are low latency).  when the
 
 175     async io operation has completed succesfully a callback is invoked,
 
 176     this callback then pushes the new state onto the queue using
 
 177     dispatch_async(queue, builder, builder->state);
 
 178     so the new state runs after we return from the callback.
 
 180     IO can be on the global hi priority queue if it's truely async.
 
 181     Since only one runnable job is ever posted on a queue at a time for a
 
 182     given request, all of the job can be run on the high (dispatching new
 
 183     requests) normal (request handeling) and low (signature
 
 185     priority global queues as we see fit.
 
 186     If multiple requests where received, they would end up being
 
 187     executed round robin on a single core, or in parallel on multi core
 
 188     machines, since each request would get queued by the dispatcher as they
 
 189     arrived, and then jobs for the queues would add their next job to the end
 
 190     of the queue behind the other one again.
 
 192     During parts of the code where we write to a databsse or read from a
 
 193     database both of which could potentially block, we need to see if
 
 194     sqlite3 has async APIs otherwise we might end up having to serialize all
 
 195     db accesses to a single serial queue, which stops us from taking advantage
 
 196     of the multiple reader / single writer paradigm.  We just might have to
 
 197     create a sqlite3 io thread or threads.
 
 200 /* Idea sketch for state_engine to replace SecPathBuilderStep() one. */
 
 202 /* State engine api */
 
 203 typedef struct state_engine_s state_engine_t;
 
 204 typedef void(*state_engine_function_t)(void *);
 
 206 typedef void(^state_engine_block_t)(void);
 
 207 void state_engine_init(state_engine_t *engine, state_engine_block_t completed);
 
 208 void state_engine_next(state_engine_t *engine, state_engine_block_t state);
 
 210 void state_engine_init_f(state_engine_t *engine,
 
 211     void *context, state_engine_function_t completed);
 
 213 void state_engine_next_f(state_engine_t *engine,
 
 214     void *context, state_engine_function_t state);
 
 215 bool state_engine_step(state_engine_t **engine);
 
 216 void state_engine_push(state_engine_t **engine, state_engine_t *child);
 
 218 /* Complete engine, return parent, . */
 
 219 state_engine_t *state_engine_pop(state_engine_t *engine);
 
 221 /* State engine spi */
 
 222 struct state_engine_s {
 
 223     state_engine_t *parent;
 
 225     state_engine_function_t completed_f;
 
 227     state_engine_function_t state_f;
 
 230 //static state_engine_t *current_engine;
 
 232 void state_engine_next_f(state_engine_t *engine,
 
 233     void *state, state_engine_function_t state_f) {
 
 234     engine->state_f = state_f;
 
 235     engine->state = state;
 
 238 bool state_engine_step(state_engine_t **engine) {
 
 244         if (!(*engine)->state_f) {
 
 245             *engine = state_engine_pop(*engine);
 
 247             (*engine)->state_f((*engine)->state);
 
 252 void state_engine_push(state_engine_t **engine,
 
 253     state_engine_t *child) {
 
 254     child->parent = *engine;
 
 258 state_engine_t *state_engine_pop(state_engine_t *engine) {
 
 259     state_engine_t *parent = engine->parent;
 
 260     if (engine->completed_f)
 
 261         engine->completed_f(engine->completed);