~ubuntu-branches/ubuntu/intrepid/ruby1.9/intrepid-updates

« back to all changes in this revision

Viewing changes to ext/openssl/ossl_pkey_ec.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-09-04 16:01:17 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070904160117-i15zckg2nhxe9fyw
Tags: 1.9.0+20070830-2ubuntu1
* Sync from Debian; remaining changes:
  - Add -g to CFLAGS.
* Fixes build failure on ia64.
* Fixes build failure with gcc-4.2 on lpia.
* Robustify check for target_os, fixing build failure on lpia.
* Set Ubuntu maintainer address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
209
209
 
210
210
                if ((ec = EC_KEY_new_by_curve_name(nid)) == NULL)
211
211
                    ossl_raise(eECError, "unable to create curve (%s)\n", name);
 
212
 
 
213
                EC_KEY_set_asn1_flag(ec, OPENSSL_EC_NAMED_CURVE);
 
214
                EC_KEY_set_conv_form(ec, POINT_CONVERSION_UNCOMPRESSED);
212
215
            }
213
216
        }
214
217
    }
569
572
    return str;
570
573
}
571
574
 
572
 
static VALUE ossl_ec_key_to_public_key(VALUE self)
573
 
{
574
 
    EC_KEY *ec;
575
 
    
576
 
    VALUE new_obj;
577
 
        
578
 
    Require_EC_KEY(self, ec);
579
 
 
580
 
    new_obj = rb_obj_alloc(cEC);
581
 
 
582
 
/* BUG: finish .to_public_key */
583
 
rb_notimplement();
584
 
    
585
 
 
586
 
    return new_obj;
587
 
}
588
 
 
589
575
/*
590
576
 *  call-seq:
591
577
 *     key.generate_key   => self
664
650
static VALUE ossl_ec_key_dsa_sign_asn1(VALUE self, VALUE data)
665
651
{
666
652
    EC_KEY *ec;
667
 
    int buf_len;
 
653
    unsigned int buf_len;
668
654
    VALUE str;
669
655
 
670
656
    Require_EC_KEY(self, ec);
674
660
        ossl_raise(eECError, "Private EC key needed!");
675
661
 
676
662
    str = rb_str_new(0, ECDSA_size(ec) + 16);
677
 
    if (ECDSA_sign(0, RSTRING_PTR(data), RSTRING_LEN(data), RSTRING_PTR(str), &buf_len, ec) != 1)
 
663
    if (ECDSA_sign(0, (unsigned char *) RSTRING_PTR(data), RSTRING_LEN(data), (unsigned char *) RSTRING_PTR(str), &buf_len, ec) != 1)
678
664
         ossl_raise(eECError, "ECDSA_sign");
679
665
 
680
666
    rb_str_resize(str, buf_len);
696
682
    StringValue(data);
697
683
    StringValue(sig);
698
684
 
699
 
    switch (ECDSA_verify(0, RSTRING_PTR(data), RSTRING_LEN(data), RSTRING_PTR(sig), RSTRING_LEN(sig), ec)) {
 
685
    switch (ECDSA_verify(0, (unsigned char *) RSTRING_PTR(data), RSTRING_LEN(data), (unsigned char *) RSTRING_PTR(sig), RSTRING_LEN(sig), ec)) {
700
686
    case 1:     return Qtrue;
701
687
    case 0:     return Qfalse;
702
688
    default:    break;
788
774
            BIO_free(in);
789
775
 
790
776
            if (!group) {
791
 
               const char *name = STR2CSTR(arg1);
792
 
               int nid = OBJ_sn2nid(name);
 
777
                const char *name = STR2CSTR(arg1);
 
778
                int nid = OBJ_sn2nid(name);
793
779
 
794
 
               if (nid == NID_undef)
795
 
                   ossl_raise(eEC_GROUP, "unknown curve name (%s)", name);
 
780
                if (nid == NID_undef)
 
781
                    ossl_raise(eEC_GROUP, "unknown curve name (%s)", name);
796
782
 
797
783
                group = EC_GROUP_new_by_curve_name(nid);
798
784
                if (group == NULL)
799
785
                    ossl_raise(eEC_GROUP, "unable to create curve (%s)", name);
 
786
 
 
787
                EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
 
788
                EC_GROUP_set_point_conversion_form(group, POINT_CONVERSION_UNCOMPRESSED);
800
789
            }
801
790
        }
802
791
 
837
826
}
838
827
 
839
828
/*  call-seq:
 
829
 *     group1 == group2   => true | false
 
830
 *
 
831
 */
 
832
static VALUE ossl_ec_group_eql(VALUE a, VALUE b)
 
833
{
 
834
    EC_GROUP *group1 = NULL, *group2 = NULL;
 
835
 
 
836
    Require_EC_GROUP(a, group1);
 
837
    SafeRequire_EC_GROUP(b, group2);
 
838
 
 
839
    if (EC_GROUP_cmp(group1, group2, ossl_bn_ctx) == 1)
 
840
       return Qfalse;
 
841
 
 
842
    return Qtrue;
 
843
}
 
844
 
 
845
/*  call-seq:
840
846
 *     group.generator   => ec_point
841
847
 *
842
848
 *  See the OpenSSL documentation for EC_GROUP_get0_generator()
1289
1295
 
1290
1296
/*
1291
1297
 *  call-seq:
 
1298
 *     point1 == point2 => true | false
 
1299
 *
 
1300
 */
 
1301
static VALUE ossl_ec_point_eql(VALUE a, VALUE b)
 
1302
{
 
1303
    EC_POINT *point1, *point2;
 
1304
    VALUE group_v1 = rb_iv_get(a, "@group");
 
1305
    VALUE group_v2 = rb_iv_get(b, "@group");
 
1306
    const EC_GROUP *group;
 
1307
 
 
1308
    if (ossl_ec_group_eql(group_v1, group_v2) == Qfalse)
 
1309
        return Qfalse;
 
1310
 
 
1311
    Require_EC_POINT(a, point1);
 
1312
    SafeRequire_EC_POINT(b, point2);
 
1313
    SafeRequire_EC_GROUP(group_v1, group);
 
1314
 
 
1315
    if (EC_POINT_cmp(group, point1, point2, ossl_bn_ctx) == 1)
 
1316
        return Qfalse;
 
1317
 
 
1318
    return Qtrue;
 
1319
}
 
1320
 
 
1321
/*
 
1322
 *  call-seq:
 
1323
 *     point.infinity? => true | false
 
1324
 *
 
1325
 */
 
1326
static VALUE ossl_ec_point_is_at_infinity(VALUE self)
 
1327
{
 
1328
    EC_POINT *point;
 
1329
    VALUE group_v = rb_iv_get(self, "@group");
 
1330
    const EC_GROUP *group;
 
1331
 
 
1332
    Require_EC_POINT(self, point);
 
1333
    SafeRequire_EC_GROUP(group_v, group);
 
1334
 
 
1335
    switch (EC_POINT_is_at_infinity(group, point)) {
 
1336
    case 1: return Qtrue;
 
1337
    case 0: return Qfalse;
 
1338
    default: ossl_raise(cEC_POINT, "EC_POINT_is_at_infinity");
 
1339
    }
 
1340
}
 
1341
 
 
1342
/*
 
1343
 *  call-seq:
 
1344
 *     point.on_curve? => true | false
 
1345
 *
 
1346
 */
 
1347
static VALUE ossl_ec_point_is_on_curve(VALUE self)
 
1348
{
 
1349
    EC_POINT *point;
 
1350
    VALUE group_v = rb_iv_get(self, "@group");
 
1351
    const EC_GROUP *group;
 
1352
 
 
1353
    Require_EC_POINT(self, point);
 
1354
    SafeRequire_EC_GROUP(group_v, group);
 
1355
 
 
1356
    switch (EC_POINT_is_on_curve(group, point, ossl_bn_ctx)) {
 
1357
    case 1: return Qtrue;
 
1358
    case 0: return Qfalse;
 
1359
    default: ossl_raise(cEC_POINT, "EC_POINT_is_on_curve");
 
1360
    }
 
1361
}
 
1362
 
 
1363
/*
 
1364
 *  call-seq:
 
1365
 *     point.make_affine! => self
 
1366
 *
 
1367
 */
 
1368
static VALUE ossl_ec_point_make_affine(VALUE self)
 
1369
{
 
1370
    EC_POINT *point;
 
1371
    VALUE group_v = rb_iv_get(self, "@group");
 
1372
    const EC_GROUP *group;
 
1373
 
 
1374
    Require_EC_POINT(self, point);
 
1375
    SafeRequire_EC_GROUP(group_v, group);
 
1376
 
 
1377
    if (EC_POINT_make_affine(group, point, ossl_bn_ctx) != 1)
 
1378
        ossl_raise(cEC_POINT, "EC_POINT_make_affine");
 
1379
 
 
1380
    return self;
 
1381
}
 
1382
 
 
1383
/*
 
1384
 *  call-seq:
 
1385
 *     point.invert! => self
 
1386
 *
 
1387
 */
 
1388
static VALUE ossl_ec_point_invert(VALUE self)
 
1389
{
 
1390
    EC_POINT *point;
 
1391
    VALUE group_v = rb_iv_get(self, "@group");
 
1392
    const EC_GROUP *group;
 
1393
 
 
1394
    Require_EC_POINT(self, point);
 
1395
    SafeRequire_EC_GROUP(group_v, group);
 
1396
 
 
1397
    if (EC_POINT_invert(group, point, ossl_bn_ctx) != 1)
 
1398
        ossl_raise(cEC_POINT, "EC_POINT_invert");
 
1399
 
 
1400
    return self;
 
1401
}
 
1402
 
 
1403
/*
 
1404
 *  call-seq:
 
1405
 *     point.set_to_infinity! => self
 
1406
 *
 
1407
 */
 
1408
static VALUE ossl_ec_point_set_to_infinity(VALUE self)
 
1409
{
 
1410
    EC_POINT *point;
 
1411
    VALUE group_v = rb_iv_get(self, "@group");
 
1412
    const EC_GROUP *group;
 
1413
 
 
1414
    Require_EC_POINT(self, point);
 
1415
    SafeRequire_EC_GROUP(group_v, group);
 
1416
 
 
1417
    if (EC_POINT_set_to_infinity(group, point) != 1)
 
1418
        ossl_raise(cEC_POINT, "EC_POINT_set_to_infinity");
 
1419
 
 
1420
    return self;
 
1421
}
 
1422
 
 
1423
/*
 
1424
 *  call-seq:
1292
1425
 *     point.to_bn   => OpenSSL::BN
1293
1426
 *
1294
1427
 *  See the OpenSSL documentation for EC_POINT_point2bn()
1350
1483
    ID_compressed = rb_intern("compressed");
1351
1484
    ID_hybrid = rb_intern("hybrid");
1352
1485
 
 
1486
#ifdef OPENSSL_EC_NAMED_CURVE
 
1487
    rb_define_const(cEC, "NAMED_CURVE", ULONG2NUM(OPENSSL_EC_NAMED_CURVE));
 
1488
#endif
 
1489
 
1353
1490
    rb_define_singleton_method(cEC, "builtin_curves", ossl_s_builtin_curves, 0);
1354
1491
 
1355
1492
    rb_define_method(cEC, "initialize", ossl_ec_key_initialize, -1);
1373
1510
    rb_define_method(cEC, "generate_key", ossl_ec_key_generate_key, 0);
1374
1511
    rb_define_method(cEC, "check_key", ossl_ec_key_check_key, 0);
1375
1512
 
1376
 
    rb_define_method(cEC, "dh_compute_key", ossl_ec_key_dh_compute_key, 2);
 
1513
    rb_define_method(cEC, "dh_compute_key", ossl_ec_key_dh_compute_key, 1);
1377
1514
    rb_define_method(cEC, "dsa_sign_asn1", ossl_ec_key_dsa_sign_asn1, 1);
1378
1515
    rb_define_method(cEC, "dsa_verify_asn1", ossl_ec_key_dsa_verify_asn1, 2);
1379
1516
/* do_sign/do_verify */
1385
1522
 
1386
1523
    rb_define_alloc_func(cEC_GROUP, ossl_ec_group_alloc);
1387
1524
    rb_define_method(cEC_GROUP, "initialize", ossl_ec_group_initialize, -1);
 
1525
    rb_define_method(cEC_GROUP, "eql?", ossl_ec_group_eql, 1);
 
1526
    rb_define_alias(cEC_GROUP, "==", "eql?");
1388
1527
/* copy/dup/cmp */
1389
1528
 
1390
1529
    rb_define_method(cEC_GROUP, "generator", ossl_ec_group_get_generator, 0);
1419
1558
    rb_define_alloc_func(cEC_POINT, ossl_ec_point_alloc);
1420
1559
    rb_define_method(cEC_POINT, "initialize", ossl_ec_point_initialize, -1);
1421
1560
    rb_attr(cEC_POINT, rb_intern("group"), 1, 0, 0);
 
1561
    rb_define_method(cEC_POINT, "eql?", ossl_ec_point_eql, 1);
 
1562
    rb_define_alias(cEC_POINT, "==", "eql?");
 
1563
 
 
1564
    rb_define_method(cEC_POINT, "infinity?", ossl_ec_point_is_at_infinity, 0);
 
1565
    rb_define_method(cEC_POINT, "on_curve?", ossl_ec_point_is_on_curve, 0);
 
1566
    rb_define_method(cEC_POINT, "make_affine!", ossl_ec_point_make_affine, 0);
 
1567
    rb_define_method(cEC_POINT, "invert!", ossl_ec_point_invert, 0);
 
1568
    rb_define_method(cEC_POINT, "set_to_infinity!", ossl_ec_point_set_to_infinity, 0);
1422
1569
/* all the other methods */
1423
1570
 
1424
1571
    rb_define_method(cEC_POINT, "to_bn", ossl_ec_point_to_bn, 0);