~alobbs/+junk/libaccounts-glib

« back to all changes in this revision

Viewing changes to libaccounts-glib/ag-account.c

  • Committer: Aurel Popirtac
  • Date: 2011-06-14 13:47:31 UTC
  • Revision ID: git-v1:4ca3cc97eb39099d472fec5650512805937b1692
Fixes: NB#Account::verify and Account::verifyWithTokens() returns TRUE all the time irrespective of whether Account::sign() called or not.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
#include "ag-service.h"
42
42
#include "ag-util.h"
43
43
 
 
44
#include "config.h"
 
45
 
 
46
#ifdef HAVE_AEGISCRYPTO
 
47
  #include <aegis_crypto.h>
 
48
#endif
 
49
 
44
50
#include <string.h>
45
51
 
46
52
#define SERVICE_GLOBAL "global"
303
309
    return watch;
304
310
}
305
311
 
 
312
#ifdef HAVE_AEGISCRYPTO
306
313
static gboolean
307
314
got_account_signature (sqlite3_stmt *stmt, AgSignature *sgn)
308
315
{
311
318
 
312
319
    return TRUE;
313
320
}
 
321
#endif
314
322
 
315
323
static gboolean
316
324
got_account_setting (sqlite3_stmt *stmt, GHashTable *settings)
2085
2093
    return TRUE;
2086
2094
}
2087
2095
 
 
2096
#ifdef HAVE_AEGISCRYPTO
2088
2097
static gboolean
2089
2098
store_data (gpointer key, gpointer value, gpointer data)
2090
2099
{
2169
2178
 
2170
2179
    return g_string_free (data, FALSE);
2171
2180
}
 
2181
#endif
 
2182
 
2172
2183
/**
2173
2184
 * ag_account_sign:
2174
2185
 * @key: the name of the key or prefix of the keys to be signed.
2175
 
 * @token: token for creating signature.
 
2186
 * @token: aegis token (NULL teminated string) or NULL in order to use the
 
2187
           application aegis ID token, for creating the signature. The
 
2188
           application must possess (request) the token.
2176
2189
 *
2177
 
 * Creates signature of the @key with given @token.
 
2190
 * Creates signature of the @key with given @token. The account must be 
 
2191
 * stored prior to calling this function.
2178
2192
 */
2179
2193
void
2180
2194
ag_account_sign (AgAccount *account, const gchar *key, const gchar *token)
2181
2195
{
 
2196
#ifdef HAVE_AEGISCRYPTO
2182
2197
    AgSignature *sgn;
2183
2198
    AgAccountPrivate *priv;
2184
2199
    AgServiceChanges *sc;
2185
2200
    gchar *data;
 
2201
    struct aegis_signature_t signature;
 
2202
    gchar *signature_string;
2186
2203
 
2187
2204
    g_return_if_fail (key != NULL);
2188
2205
    g_return_if_fail (token != NULL);
2192
2209
 
2193
2210
    g_return_if_fail (data != NULL);
2194
2211
 
2195
 
    /* TODO: sign data with token - depends on libmaemosec */
 
2212
    aegis_crypto_result result_sign =
 
2213
            aegis_crypto_sign (data,
 
2214
                               strlen (data),
 
2215
                               token,
 
2216
                               &signature);
 
2217
    g_free (data);
 
2218
    g_return_if_fail (result_sign == aegis_crypto_ok);
2196
2219
 
2197
 
    priv = account->priv;
2198
 
    sc = account_service_changes_get (priv, priv->service, TRUE);
 
2220
    aegis_crypto_signature_to_string (&signature,
 
2221
                                      aegis_as_base64,
 
2222
                                      token,
 
2223
                                      &signature_string);
2199
2224
 
2200
2225
    sgn = g_slice_new (AgSignature);
2201
 
    sgn->signature = data; //signed_data;
 
2226
    sgn->signature = g_strdup (signature_string);
 
2227
    aegis_crypto_free (signature_string);
2202
2228
    sgn->token = g_strdup (token);
2203
2229
 
 
2230
    priv = account->priv;
 
2231
    sc = account_service_changes_get (priv, priv->service, TRUE);
 
2232
 
2204
2233
    g_hash_table_insert (sc->signatures,
2205
2234
                         g_strdup (key), sgn);
 
2235
                         
 
2236
    aegis_crypto_finish ();
 
2237
#else
 
2238
    g_warning ("ag_account_sign: aegis-crypto not found! Unable to sign the key.");
 
2239
#endif
2206
2240
}
2207
2241
 
2208
2242
/**
2209
2243
 * ag_account_verify:
2210
2244
 * @key: the name of the key or prefix of the keys to be verified.
2211
 
 * @token: location to receive the pointer to token.
 
2245
 * @token: location to receive the pointer to aegis token.
2212
2246
 *
2213
2247
 * Verify if the key is signed and the signature matches the value
2214
 
 * and provides the token which was used for signing the @key.
 
2248
 * and provides the aegis token which was used for signing the @key.
2215
2249
 *
2216
2250
 * Returns: %TRUE if the key is signed and the signature matches
2217
2251
 * the value.
2219
2253
gboolean
2220
2254
ag_account_verify (AgAccount *account, const gchar *key, const gchar **token)
2221
2255
{
 
2256
#ifdef HAVE_AEGISCRYPTO
2222
2257
    AgAccountPrivate *priv;
2223
2258
    AgServiceSettings *ss;
2224
2259
    guint service_id;
2225
2260
    gchar *data;
2226
2261
    gchar *sql;
2227
2262
    AgSignature sgn;
 
2263
    GString *sql_str;
 
2264
    aegis_system_mode_t made_in_mode;
 
2265
    aegis_crypto_result result_verify;
 
2266
    aegis_crypto_result result_convert;
 
2267
    struct aegis_signature_t signature;
 
2268
    char *token_name;
2228
2269
 
2229
2270
    g_return_val_if_fail (AG_IS_ACCOUNT (account), FALSE);
2230
2271
 
2235
2276
 
2236
2277
    service_id = (priv->service != NULL) ? priv->service->id : 0;
2237
2278
 
2238
 
    GString *sql_str;
2239
2279
    sql_str = g_string_sized_new (512);
2240
2280
    _ag_string_append_printf (sql_str,
2241
2281
                              "SELECT signature, token FROM Signatures "
2246
2286
                            (AgQueryCallback)got_account_signature,
2247
2287
                            &sgn, sql);
2248
2288
 
2249
 
    g_free(sql);
2250
 
    data = signature_data(account, key);
2251
 
 
2252
 
    /* TODO: verify data with sgn->signature - depends on libmaemosec */
 
2289
    g_free (sql);
 
2290
    data = signature_data (account, key);
 
2291
 
 
2292
    aegis_crypto_init();
 
2293
 
 
2294
    token_name = NULL;
 
2295
    result_convert =  aegis_crypto_string_to_signature (sgn.signature,
 
2296
                                                        &signature,
 
2297
                                                        &token_name);
 
2298
 
 
2299
    if (result_convert != aegis_crypto_ok) {
 
2300
        *token = NULL;
 
2301
        aegis_crypto_finish ();
 
2302
        g_free (data);
 
2303
        return FALSE;
 
2304
    }
 
2305
 
 
2306
    result_verify = aegis_crypto_verify (&signature,
 
2307
                                         token_name,
 
2308
                                         data,
 
2309
                                         strlen (data),
 
2310
                                         &made_in_mode);
 
2311
 
 
2312
    if (result_verify != aegis_crypto_ok) {
 
2313
        *token = NULL;
 
2314
        aegis_crypto_free (token_name);
 
2315
        aegis_crypto_finish ();
 
2316
        g_free (data);
 
2317
        return FALSE;
 
2318
    }
 
2319
 
 
2320
    *token = g_strdup (token_name);
 
2321
    if (token_name)
 
2322
        aegis_crypto_free (token_name);
 
2323
 
 
2324
    aegis_crypto_finish ();
2253
2325
 
2254
2326
    g_free (data);
2255
2327
 
2256
 
    /* temporary solution */
2257
 
    *token = "token";
2258
2328
    return TRUE;
 
2329
#else
 
2330
    g_warning ("ag_account_verify: aegis-crypto not found! Unable to verify the key.");
 
2331
    return FALSE;
 
2332
#endif
2259
2333
}
2260
2334
 
2261
2335
/**
2262
 
 * ag_account_verify_with_token:
 
2336
 * ag_account_verify_with_tokens:
2263
2337
 * @key: the name of the key or prefix of the keys to be verified.
2264
 
 * @tokens: array of tokens.
 
2338
 * @tokens: array of aegis tokens.
2265
2339
 *
2266
 
 * Verify if the @key is signed with any of the token from the @tokens
 
2340
 * Verify if the @key is signed with any of the tokens from the @tokens
2267
2341
 * and the signature is valid.
2268
2342
 *
2269
 
 * Returns: %TRUE if the key is signed with any of the given token
 
2343
 * Returns: %TRUE if the key is signed with any of the given tokens
2270
2344
 * and the signature is valid.
2271
2345
 */
2272
2346
gboolean