~ubuntu-branches/ubuntu/utopic/freerdp/utopic

« back to all changes in this revision

Viewing changes to libfreerdp-core/crypto.c

  • Committer: Package Import Robot
  • Author(s): Otavio Salvador, Jeremy Bicha
  • Date: 2012-02-11 10:34:05 UTC
  • mfrom: (1.1.7) (9.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20120211103405-mk0gjhjn70eeyxul
Tags: 1.0.1-1
[ Jeremy Bicha ]
* New upstream release. Closes: #659332.
* Updated symbols

Show diffs side-by-side

added added

removed removed

Lines of Context:
100
100
{
101
101
        int len;
102
102
        EVP_DecryptUpdate(&des3->des3_ctx, out_data, &len, in_data, length);
 
103
 
103
104
        if (length != len)
104
 
                abort();        // TODO
 
105
                abort(); /* TODO */
105
106
}
106
107
 
107
108
void crypto_des3_free(CryptoDes3 des3)
188
189
        return status;
189
190
}
190
191
 
191
 
void crypto_rsa_encrypt(const uint8* input, int length, uint32 key_length, const uint8* modulus, const uint8* exponent, uint8* output)
 
192
/*
 
193
 * Terminal Services Signing Keys.
 
194
 * Yes, Terminal Services Private Key is publicly available.
 
195
 */
 
196
 
 
197
const uint8 tssk_modulus[] =
 
198
{
 
199
        0x3d, 0x3a, 0x5e, 0xbd, 0x72, 0x43, 0x3e, 0xc9,
 
200
        0x4d, 0xbb, 0xc1, 0x1e, 0x4a, 0xba, 0x5f, 0xcb,
 
201
        0x3e, 0x88, 0x20, 0x87, 0xef, 0xf5, 0xc1, 0xe2,
 
202
        0xd7, 0xb7, 0x6b, 0x9a, 0xf2, 0x52, 0x45, 0x95,
 
203
        0xce, 0x63, 0x65, 0x6b, 0x58, 0x3a, 0xfe, 0xef,
 
204
        0x7c, 0xe7, 0xbf, 0xfe, 0x3d, 0xf6, 0x5c, 0x7d,
 
205
        0x6c, 0x5e, 0x06, 0x09, 0x1a, 0xf5, 0x61, 0xbb,
 
206
        0x20, 0x93, 0x09, 0x5f, 0x05, 0x6d, 0xea, 0x87
 
207
};
 
208
 
 
209
const uint8 tssk_privateExponent[] =
 
210
{
 
211
        0x87, 0xa7, 0x19, 0x32, 0xda, 0x11, 0x87, 0x55,
 
212
        0x58, 0x00, 0x16, 0x16, 0x25, 0x65, 0x68, 0xf8,
 
213
        0x24, 0x3e, 0xe6, 0xfa, 0xe9, 0x67, 0x49, 0x94,
 
214
        0xcf, 0x92, 0xcc, 0x33, 0x99, 0xe8, 0x08, 0x60,
 
215
        0x17, 0x9a, 0x12, 0x9f, 0x24, 0xdd, 0xb1, 0x24,
 
216
        0x99, 0xc7, 0x3a, 0xb8, 0x0a, 0x7b, 0x0d, 0xdd,
 
217
        0x35, 0x07, 0x79, 0x17, 0x0b, 0x51, 0x9b, 0xb3,
 
218
        0xc7, 0x10, 0x01, 0x13, 0xe7, 0x3f, 0xf3, 0x5f
 
219
};
 
220
 
 
221
const uint8 tssk_exponent[] =
 
222
{
 
223
        0x5b, 0x7b, 0x88, 0xc0
 
224
};
 
225
 
 
226
static void crypto_rsa_common(const uint8* input, int length, uint32 key_length, const uint8* modulus, const uint8* exponent, int exponent_size, uint8* output)
192
227
{
193
228
        BN_CTX* ctx;
194
229
        int output_length;
197
232
        uint8* exponent_reverse;
198
233
        BIGNUM mod, exp, x, y;
199
234
 
200
 
        input_reverse = (uint8*) xmalloc(2 * MODULUS_MAX_SIZE + EXPONENT_MAX_SIZE);
201
 
        modulus_reverse = input_reverse + MODULUS_MAX_SIZE;
202
 
        exponent_reverse = modulus_reverse + MODULUS_MAX_SIZE;
 
235
        input_reverse = (uint8*) xmalloc(2 * key_length + exponent_size);
 
236
        modulus_reverse = input_reverse + key_length;
 
237
        exponent_reverse = modulus_reverse + key_length;
203
238
 
204
239
        memcpy(modulus_reverse, modulus, key_length);
205
240
        crypto_reverse(modulus_reverse, key_length);
206
 
        memcpy(exponent_reverse, exponent, EXPONENT_MAX_SIZE);
207
 
        crypto_reverse(exponent_reverse, EXPONENT_MAX_SIZE);
 
241
        memcpy(exponent_reverse, exponent, exponent_size);
 
242
        crypto_reverse(exponent_reverse, exponent_size);
208
243
        memcpy(input_reverse, input, length);
209
244
        crypto_reverse(input_reverse, length);
210
245
 
215
250
        BN_init(&y);
216
251
 
217
252
        BN_bin2bn(modulus_reverse, key_length, &mod);
218
 
        BN_bin2bn(exponent_reverse, EXPONENT_MAX_SIZE, &exp);
 
253
        BN_bin2bn(exponent_reverse, exponent_size, &exp);
219
254
        BN_bin2bn(input_reverse, length, &x);
220
255
        BN_mod_exp(&y, &x, &exp, &mod, ctx);
221
256
 
233
268
        xfree(input_reverse);
234
269
}
235
270
 
 
271
static void crypto_rsa_public(const uint8* input, int length, uint32 key_length, const uint8* modulus, const uint8* exponent, uint8* output)
 
272
{
 
273
        crypto_rsa_common(input, length, key_length, modulus, exponent, EXPONENT_MAX_SIZE, output);
 
274
}
 
275
 
 
276
static void crypto_rsa_private(const uint8* input, int length, uint32 key_length, const uint8* modulus, const uint8* private_exponent, uint8* output)
 
277
{
 
278
 
 
279
        crypto_rsa_common(input, length, key_length, modulus, private_exponent, key_length, output);
 
280
}
 
281
 
 
282
void crypto_rsa_public_encrypt(const uint8* input, int length, uint32 key_length, const uint8* modulus, const uint8* exponent, uint8* output)
 
283
{
 
284
 
 
285
        crypto_rsa_public(input, length, key_length, modulus, exponent, output);
 
286
}
 
287
 
 
288
void crypto_rsa_public_decrypt(const uint8* input, int length, uint32 key_length, const uint8* modulus, const uint8* exponent, uint8* output)
 
289
{
 
290
 
 
291
        crypto_rsa_public(input, length, key_length, modulus, exponent, output);
 
292
}
 
293
 
 
294
void crypto_rsa_private_encrypt(const uint8* input, int length, uint32 key_length, const uint8* modulus, const uint8* private_exponent, uint8* output)
 
295
{
 
296
 
 
297
        crypto_rsa_private(input, length, key_length, modulus, private_exponent, output);
 
298
}
 
299
 
 
300
void crypto_rsa_private_decrypt(const uint8* input, int length, uint32 key_length, const uint8* modulus, const uint8* private_exponent, uint8* output)
 
301
{
 
302
 
 
303
        crypto_rsa_private(input, length, key_length, modulus, private_exponent, output);
 
304
}
 
305
 
 
306
void crypto_rsa_decrypt(const uint8* input, int length, uint32 key_length, const uint8* modulus, const uint8* private_exponent, uint8* output)
 
307
{
 
308
 
 
309
        crypto_rsa_common(input, length, key_length, modulus, private_exponent, key_length, output);
 
310
}
 
311
 
236
312
void crypto_reverse(uint8* data, int length)
237
313
{
238
314
        int i, j;
279
355
        char* buffer = NULL;
280
356
        BIO* outBIO = BIO_new(BIO_s_mem());
281
357
        
282
 
        if(X509_NAME_print_ex(outBIO, name, 0, XN_FLAG_ONELINE) > 0) 
 
358
        if (X509_NAME_print_ex(outBIO, name, 0, XN_FLAG_ONELINE) > 0)
283
359
        {
284
360
                unsigned long size = BIO_number_written(outBIO);
285
361
                buffer = xzalloc(size + 1);
297
373
        return crypto_print_name(X509_get_subject_name(xcert));
298
374
}
299
375
 
 
376
char* crypto_cert_subject_common_name(X509* xcert, int* length)
 
377
{
 
378
        int index;
 
379
        uint8* common_name;
 
380
        X509_NAME* subject_name;
 
381
        X509_NAME_ENTRY* entry;
 
382
        ASN1_STRING* entry_data;
 
383
 
 
384
        subject_name = X509_get_subject_name(xcert);
 
385
 
 
386
        if (subject_name == NULL)
 
387
                return NULL;
 
388
 
 
389
        index = X509_NAME_get_index_by_NID(subject_name, NID_commonName, -1);
 
390
 
 
391
        if (index < 0)
 
392
                return NULL;
 
393
 
 
394
        entry = X509_NAME_get_entry(subject_name, index);
 
395
 
 
396
        if (entry == NULL)
 
397
                return NULL;
 
398
 
 
399
        entry_data = X509_NAME_ENTRY_get_data(entry);
 
400
 
 
401
        if (entry_data == NULL)
 
402
                return NULL;
 
403
 
 
404
        *length = ASN1_STRING_to_UTF8(&common_name, entry_data);
 
405
 
 
406
        if (*length < 0)
 
407
                return NULL;
 
408
 
 
409
        return (char*) common_name;
 
410
}
 
411
 
 
412
char** crypto_cert_subject_alt_name(X509* xcert, int* count, int** lengths)
 
413
{
 
414
        int index;
 
415
        int length;
 
416
        char** strings;
 
417
        uint8* string;
 
418
        int num_subject_alt_names;
 
419
        GENERAL_NAMES* subject_alt_names;
 
420
        GENERAL_NAME* subject_alt_name;
 
421
 
 
422
        *count = 0;
 
423
        subject_alt_names = X509_get_ext_d2i(xcert, NID_subject_alt_name, 0, 0);
 
424
 
 
425
        if (!subject_alt_names)
 
426
                return NULL;
 
427
 
 
428
        num_subject_alt_names = sk_GENERAL_NAME_num(subject_alt_names);
 
429
        strings = (char**) malloc(sizeof(char*) * num_subject_alt_names);
 
430
        *lengths = (int*) malloc(sizeof(int*) * num_subject_alt_names);
 
431
 
 
432
        for (index = 0; index < num_subject_alt_names; ++index)
 
433
        {
 
434
                subject_alt_name = sk_GENERAL_NAME_value(subject_alt_names, index);
 
435
 
 
436
                if (subject_alt_name->type == GEN_DNS)
 
437
                {
 
438
                        length = ASN1_STRING_to_UTF8(&string, subject_alt_name->d.dNSName);
 
439
                        strings[*count] = (char*) string;
 
440
                        *lengths[*count] = length;
 
441
                        (*count)++;
 
442
                }
 
443
        }
 
444
 
 
445
        if (*count < 1)
 
446
                return NULL;
 
447
 
 
448
        return strings;
 
449
}
 
450
 
300
451
char* crypto_cert_issuer(X509* xcert)
301
452
{
302
453
        return crypto_print_name(X509_get_issuer_name(xcert));
303
454
}
304
455
 
305
 
boolean x509_verify_cert(CryptoCert cert, rdpSettings* settings)
 
456
boolean x509_verify_certificate(CryptoCert cert, char* certificate_store_path)
306
457
{
307
 
        char* cert_loc;
308
458
        X509_STORE_CTX* csc;
309
459
        boolean status = false;
310
460
        X509_STORE* cert_ctx = NULL;
328
478
                goto end;
329
479
 
330
480
        X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
331
 
        cert_loc = get_local_certloc(settings->home_path);
332
481
 
333
 
        if(cert_loc != NULL)
 
482
        if (certificate_store_path != NULL)
334
483
        {
335
 
                X509_LOOKUP_add_dir(lookup, cert_loc, X509_FILETYPE_ASN1);
336
 
                xfree(cert_loc);
 
484
                X509_LOOKUP_add_dir(lookup, certificate_store_path, X509_FILETYPE_ASN1);
337
485
        }
338
486
 
339
487
        csc = X509_STORE_CTX_new();
343
491
 
344
492
        X509_STORE_set_flags(cert_ctx, 0);
345
493
 
346
 
        if(!X509_STORE_CTX_init(csc, cert_ctx, xcert, 0))
 
494
        if (!X509_STORE_CTX_init(csc, cert_ctx, xcert, 0))
347
495
                goto end;
348
496
 
349
497
        if (X509_verify_cert(csc) == 1)
356
504
        return status;
357
505
}
358
506
 
359
 
rdpCertData* crypto_get_cert_data(X509* xcert, char* hostname)
 
507
rdpCertificateData* crypto_get_certificate_data(X509* xcert, char* hostname)
360
508
{
361
509
        char* fp;
362
 
        rdpCertData* certdata;
 
510
        rdpCertificateData* certdata;
363
511
 
364
512
        fp = crypto_cert_fingerprint(xcert);
365
 
        certdata = certdata_new(hostname, fp);
 
513
        certdata = certificate_data_new(hostname, fp);
366
514
        xfree(fp);
367
515
 
368
516
        return certdata;