~ubuntu-branches/ubuntu/raring/ruby1.9.1/raring-security

« back to all changes in this revision

Viewing changes to ext/openssl/ossl_cipher.c

  • Committer: Package Import Robot
  • Author(s): Antonio Terceiro, Lucas Nussbaum, Daigo Moriwaki, James Healy, Antonio Terceiro
  • Date: 2012-06-02 07:42:28 UTC
  • mfrom: (21.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20120602074228-09t2jgg1ozrsnnfo
Tags: 1.9.3.194-1
[ Lucas Nussbaum ]
* Add hurd-path-max.diff. Fixes FTBFS on Hurd. (Closes: #648055)

[ Daigo Moriwaki ]
* Removed debian/patches/debian/patches/sparc-continuations.diff,
  which the upstream has applied.
* debian/rules:
  - Bumped up tcltk_ver to 8.5.
  - Used chrpath for tcltklib.so to fix a lintian error,
    binary-or-shlib-defines-rpath.
* debian/control:
  - Suggests ruby-switch. (Closes: #654312)
  - Build-Depends: chrpath.
* debian/libruby1.9.1.symbols: Added a new symbol for
  rb_str_modify_expand@Base.
* debian/run-test-suites.bash:
  - Corrected options for test-all.
  - Enabled timeout to allow hang tests to be aborted.

[ James Healy ]
* New upstream release: 1.9.3p194 (Closes: #669582)
  + This release includes a fix for CVE-2011-0188 (Closes: #628451)
  + This release also does not segfault when running the test suite under
    amd64 (Closes: #674347)
* Enable hardened build flags (Closes: #667964)
* debian/control:
  - depend on specific version on coreutils
  - update policy version (no changes)

[ Antonio Terceiro ]
* debian/ruby1.9.1.postinst:
  + bump alternatives priority for `ruby` to 51 so that Ruby 1.9 has a
    higher priority than Ruby 1.8 (50).
  + bump alternatives priority for `gem` to 181 so that the Rubygems
    provided by Ruby 1.9 has priority over the one provided by the rubygems
    package.
* debian/control: added myself to Uploaders:
* debian/libruby1.9.1.symbols: update with new symbols added in 1.9.3p194
  upstream release.
* debian/manpages/*: fix references to command names with s/1.9/1.9.1/
* debian/rules: skip running DRB tests, since they seem to make the build
  hang. This should close #647296, but let's way and see. Also, with this do
  not need to timeout the test suite anymore.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Id: ossl_cipher.c 32724 2011-07-28 13:52:57Z nahi $
 
2
 * $Id: ossl_cipher.c 34505 2012-02-09 03:25:07Z nobu $
3
3
 * 'OpenSSL for Ruby' project
4
4
 * Copyright (C) 2001-2002  Michal Rokos <m.rokos@sh.cvut.cz>
5
5
 * All rights reserved.
126
126
 
127
127
    return self;
128
128
}
 
129
 
129
130
static VALUE
130
131
ossl_cipher_copy(VALUE self, VALUE other)
131
132
{
181
182
 *  call-seq:
182
183
 *     cipher.reset -> self
183
184
 *
 
185
 *  Fully resets the internal state of the Cipher. By using this, the same
 
186
 *  Cipher instance may be used several times for en- or decryption tasks.
 
187
 *
184
188
 *  Internally calls EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, -1).
185
189
 */
186
190
static VALUE
243
247
 *  call-seq:
244
248
 *     cipher.encrypt -> self
245
249
 *
246
 
 *  Make sure to call .encrypt or .decrypt before using any of the following methods:
 
250
 *  Initializes the Cipher for encryption.
 
251
 *
 
252
 *  Make sure to call Cipher#encrypt or Cipher#decrypt before using any of the
 
253
 *  following methods:
247
254
 *  * [key=, iv=, random_key, random_iv, pkcs5_keyivgen]
248
255
 *
249
256
 *  Internally calls EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, 1).
258
265
 *  call-seq:
259
266
 *     cipher.decrypt -> self
260
267
 *
261
 
 *  Make sure to call .encrypt or .decrypt before using any of the following methods:
 
268
 *  Initializes the Cipher for decryption.
 
269
 *
 
270
 *  Make sure to call Cipher#encrypt or Cipher#decrypt before using any of the
 
271
 *  following methods:
262
272
 *  * [key=, iv=, random_key, random_iv, pkcs5_keyivgen]
263
273
 *
264
274
 *  Internally calls EVP_CipherInit_ex(ctx, NULL, NULL, NULL, NULL, 0).
273
283
 *  call-seq:
274
284
 *     cipher.pkcs5_keyivgen(pass [, salt [, iterations [, digest]]] ) -> nil
275
285
 *
276
 
 *  Generates and sets the key/iv based on a password.
 
286
 *  Generates and sets the key/IV based on a password.
277
287
 *
278
 
 *  WARNING: This method is only PKCS5 v1.5 compliant when using RC2, RC4-40, or DES
279
 
 *  with MD5 or SHA1.  Using anything else (like AES) will generate the key/iv using an
280
 
 *  OpenSSL specific method.  Use a PKCS5 v2 key generation method instead.
 
288
 *  WARNING: This method is only PKCS5 v1.5 compliant when using RC2, RC4-40,
 
289
 *  or DES with MD5 or SHA1. Using anything else (like AES) will generate the
 
290
 *  key/iv using an OpenSSL specific method. This method is deprecated and
 
291
 *  should no longer be used. Use a PKCS5 v2 key generation method from
 
292
 *  OpenSSL::PKCS5 instead.
281
293
 *
282
294
 *  === Parameters
283
295
 *  +salt+ must be an 8 byte string if provided.
322
334
 *  call-seq:
323
335
 *     cipher.update(data [, buffer]) -> string or buffer
324
336
 *
 
337
 *  Encrypts data in a streaming fashion. Hand consecutive blocks of data
 
338
 *  to the +update+ method in order to encrypt it. Returns the encrypted
 
339
 *  data chunk. When done, the output of Cipher#final should be additionally
 
340
 *  added to the result.
 
341
 *
325
342
 *  === Parameters
326
343
 *  +data+ is a nonempty string.
327
344
 *  +buffer+ is an optional string to store the result.
360
377
 
361
378
/*
362
379
 *  call-seq:
363
 
 *     cipher.final -> aString
 
380
 *     cipher.final -> string
364
381
 *
365
 
 *  Returns the remaining data held in the cipher object.  Further calls to update() or final() will return garbage.
 
382
 *  Returns the remaining data held in the cipher object.  Further calls to
 
383
 *  Cipher#update or Cipher#final will return garbage.
366
384
 *
367
385
 *  See EVP_CipherFinal_ex for further information.
368
386
 */
387
405
 *  call-seq:
388
406
 *     cipher.name -> string
389
407
 *
390
 
 *  Returns the name of the cipher which may differ slightly from the original name provided.
 
408
 *  Returns the name of the cipher which may differ slightly from the original
 
409
 *  name provided.
391
410
 */
392
411
static VALUE
393
412
ossl_cipher_name(VALUE self)
403
422
 *  call-seq:
404
423
 *     cipher.key = string -> string
405
424
 *
406
 
 *  Sets the cipher key.
 
425
 *  Sets the cipher key. To generate a key, you should either use a secure
 
426
 *  random byte string or, if the key is to be derived from a password, you
 
427
 *  should rely on PBKDF2 functionality provided by OpenSSL::PKCS5. To
 
428
 *  generate a secure random-based key, Cipher#random_key may be used.
407
429
 *
408
 
 *  Only call this method after calling cipher.encrypt or cipher.decrypt.
 
430
 *  Only call this method after calling Cipher#encrypt or Cipher#decrypt.
409
431
 */
410
432
static VALUE
411
433
ossl_cipher_set_key(VALUE self, VALUE key)
428
450
 *  call-seq:
429
451
 *     cipher.iv = string -> string
430
452
 *
431
 
 *  Sets the cipher iv.
432
 
 *
433
 
 *  Only call this method after calling cipher.encrypt or cipher.decrypt.
 
453
 *  Sets the cipher IV. Please note that since you should never be using ECB
 
454
 *  mode, an IV is always explicitly required and should be set prior to
 
455
 *  encryption. The IV itself can be safely transmitted in public, but it
 
456
 *  should be unpredictable to prevent certain kinds of attacks. You may use
 
457
 *  Cipher#random_iv to create a secure random IV.
 
458
 *
 
459
 *  Only call this method after calling Cipher#encrypt or Cipher#decrypt.
 
460
 *
 
461
 *  If not explicitly set, the OpenSSL default of an all-zeroes ("\\0") IV is
 
462
 *  used.
434
463
 */
435
464
static VALUE
436
465
ossl_cipher_set_iv(VALUE self, VALUE iv)
454
483
 *  call-seq:
455
484
 *     cipher.key_len = integer -> integer
456
485
 *
457
 
 *  Sets the key length of the cipher.  If the cipher is a fixed length cipher then attempting to set the key
458
 
 *  length to any value other than the fixed value is an error.
 
486
 *  Sets the key length of the cipher.  If the cipher is a fixed length cipher
 
487
 *  then attempting to set the key length to any value other than the fixed
 
488
 *  value is an error.
459
489
 *
460
490
 *  Under normal circumstances you do not need to call this method (and probably shouldn't).
461
491
 *
508
538
        GetCipher(self, ctx);                                   \
509
539
        return INT2NUM(EVP_CIPHER_##func(EVP_CIPHER_CTX_cipher(ctx)));  \
510
540
    }
 
541
 
 
542
/*
 
543
 *  call-seq:
 
544
 *     cipher.key_len -> integer
 
545
 *
 
546
 *  Returns the key length in bytes of the Cipher.
 
547
 */
511
548
CIPHER_0ARG_INT(key_length)
 
549
/*
 
550
 *  call-seq:
 
551
 *     cipher.iv_len -> integer
 
552
 *
 
553
 *  Returns the expected length in bytes for an IV for this Cipher.
 
554
 */
512
555
CIPHER_0ARG_INT(iv_length)
 
556
/*
 
557
 *  call-seq:
 
558
 *     cipher.block_size -> integer
 
559
 *
 
560
 *  Returns the size in bytes of the blocks on which this Cipher operates on.
 
561
 */
513
562
CIPHER_0ARG_INT(block_size)
514
563
 
515
 
#if 0
516
 
/*
517
 
 *  call-seq:
518
 
 *     cipher.key_len -> integer
519
 
 *
520
 
 */
521
 
static VALUE ossl_cipher_key_length() { }
522
 
/*
523
 
 *  call-seq:
524
 
 *     cipher.iv_len -> integer
525
 
 *
526
 
 */
527
 
static VALUE ossl_cipher_iv_length() { }
528
 
/*
529
 
 *  call-seq:
530
 
 *     cipher.block_size -> integer
531
 
 *
532
 
 */
533
 
static VALUE ossl_cipher_block_size() { }
534
 
#endif
535
 
 
536
564
/*
537
565
 * INIT
538
566
 */
542
570
#if 0
543
571
    mOSSL = rb_define_module("OpenSSL"); /* let rdoc know about mOSSL */
544
572
#endif
 
573
 
 
574
    /* Document-class: OpenSSL::Cipher
 
575
     *
 
576
     * Provides symmetric algorithms for encryption and decryption. The
 
577
     * algorithms that are available depend on the particular version
 
578
     * of OpenSSL that is installed.
 
579
     *
 
580
     * === Listing all supported algorithms
 
581
     *
 
582
     * A list of supported algorithms can be obtained by
 
583
     *
 
584
     *   puts OpenSSL::Cipher.ciphers
 
585
     *
 
586
     * === Instantiating a Cipher
 
587
     *
 
588
     * There are several ways to create a Cipher instance. Generally, a
 
589
     * Cipher algorithm is categorized by its name, the key length in bits
 
590
     * and the cipher mode to be used. The most generic way to create a
 
591
     * Cipher is the following
 
592
     *
 
593
     *   cipher = OpenSSL::Cipher.new('<name>-<key length>-<mode>')
 
594
     *
 
595
     * That is, a string consisting of the hyphenated concatenation of the
 
596
     * individual components name, key length and mode. Either all uppercase
 
597
     * or all lowercase strings may be used, for example:
 
598
     *
 
599
     *  cipher = OpenSSL::Cipher.new('AES-128-CBC')
 
600
     *
 
601
     * For each algorithm supported, there is a class defined under the
 
602
     * Cipher class that goes by the name of the cipher, e.g. to obtain an
 
603
     * instance of AES, you could also use
 
604
     *
 
605
     *   # these are equivalent
 
606
     *   cipher = OpenSSL::Cipher::AES.new(128, :CBC)
 
607
     *   cipher = OpenSSL::Cipher::AES.new(128, 'CBC')
 
608
     *   cipher = OpenSSL::Cipher::AES.new('128-CBC')
 
609
     *
 
610
     * Finally, due to its wide-spread use, there are also extra classes
 
611
     * defined for the different key sizes of AES
 
612
     *
 
613
     *   cipher = OpenSSL::Cipher::AES128.new(:CBC)
 
614
     *   cipher = OpenSSL::Cipher::AES192.new(:CBC)
 
615
     *   cipher = OpenSSL::Cipher::AES256.new(:CBC)
 
616
     *
 
617
     * === Choosing either encryption or decryption mode
 
618
     *
 
619
     * Encryption and decryption are often very similar operations for
 
620
     * symmetric algorithms, this is reflected by not having to choose
 
621
     * different classes for either operation, both can be done using the
 
622
     * same class. Still, after obtaining a Cipher instance, we need to
 
623
     * tell the instance what it is that we intend to do with it, so we
 
624
     * need to call either
 
625
     *
 
626
     *   cipher.encrypt
 
627
     *
 
628
     * or
 
629
     *
 
630
     *   cipher.decrypt
 
631
     *
 
632
     * on the Cipher instance. This should be the first call after creating
 
633
     * the instance, otherwise configuration that has already been set could
 
634
     * get lost in the process.
 
635
     *
 
636
     * === Choosing a key
 
637
     *
 
638
     * Symmetric encryption requires a key that is the same for the encrypting
 
639
     * and for the decrypting party and after initial key establishment should
 
640
     * be kept as private information. There are a lot of ways to create
 
641
     * insecure keys, the most notable is to simply take a password as the key
 
642
     * without processing the password further. A simple and secure way to
 
643
     * create a key for a particular Cipher is
 
644
     *
 
645
     *  cipher = OpenSSL::AES256.new(:CFB)
 
646
     *  cipher.encrypt
 
647
     *  key = cipher.random_key # also sets the generated key on the Cipher
 
648
     *
 
649
     * If you absolutely need to use passwords as encryption keys, you
 
650
     * should use Password-Based Key Derivation Function 2 (PBKDF2) by
 
651
     * generating the key with the help of the functionality provided by
 
652
     * OpenSSL::PKCS5.pbkdf2_hmac_sha1 or OpenSSL::PKCS5.pbkdf2_hmac.
 
653
     *
 
654
     * Although there is Cipher#pkcs5_keyivgen, its use is deprecated and
 
655
     * it should only be used in legacy applications because it does not use
 
656
     * the newer PKCS#5 v2 algorithms.
 
657
     *
 
658
     * === Choosing an IV
 
659
     *
 
660
     * The cipher modes CBC, CFB, OFB and CTR all need an "initialization
 
661
     * vector", or short, IV. ECB mode is the only mode that does not require
 
662
     * an IV, but there is almost no legitimate use case for this mode
 
663
     * because of the fact that it does not sufficiently hide plaintext
 
664
     * patterns. Therefore
 
665
     *
 
666
     * <b>You should never use ECB mode unless you are absolutely sure that
 
667
     * you absolutely need it</b>
 
668
     *
 
669
     * Because of this, you will end up with a mode that explicitly requires
 
670
     * an IV in any case. Note that for backwards compatibility reasons,
 
671
     * setting an IV is not explicitly mandated by the Cipher API. If not
 
672
     * set, OpenSSL itself defaults to an all-zeroes IV ("\\0", not the
 
673
     * character). Although the IV can be seen as public information, i.e.
 
674
     * it may be transmitted in public once generated, it should still stay
 
675
     * unpredictable to prevent certain kinds of attacks. Therefore, ideally
 
676
     *
 
677
     * <b>Always create a secure random IV for every encryption of your
 
678
     * Cipher</b>
 
679
     *
 
680
     * A new, random IV should be created for every encryption of data. Think
 
681
     * of the IV as a nonce (number used once) - it's public but random and
 
682
     * unpredictable. A secure random IV can be created as follows
 
683
     *
 
684
     *  cipher = ...
 
685
     *  cipher.encrypt
 
686
     *  key = cipher.random_key
 
687
     *  iv = cipher.random_iv # also sets the generated IV on the Cipher
 
688
     *
 
689
     *  Although the key is generally a random value, too, it is a bad choice
 
690
     *  as an IV. There are elaborate ways how an attacker can take advantage
 
691
     *  of such an IV. As a general rule of thumb, exposing the key directly
 
692
     *  or indirectly should be avoided at all cost and exceptions only be
 
693
     *  made with good reason.
 
694
     *
 
695
     * === Calling Cipher#final
 
696
     *
 
697
     * ECB (which should not be used) and CBC are both block-based modes.
 
698
     * This means that unlike for the other streaming-based modes, they
 
699
     * operate on fixed-size blocks of data, and therefore they require a
 
700
     * "finalization" step to produce or correctly decrypt the last block of
 
701
     * data by appropriately handling some form of padding. Therefore it is
 
702
     * essential to add the output of OpenSSL::Cipher#final to your
 
703
     * encryption/decryption buffer or you will end up with decryption errors
 
704
     * or truncated data.
 
705
     *
 
706
     * Although this is not really necessary for streaming-mode ciphers, it is
 
707
     * still recommended to apply the same pattern of adding the output of
 
708
     * Cipher#final there as well - it also enables you to switch between
 
709
     * modes more easily in the future.
 
710
     *
 
711
     * === Encrypting and decrypting some data
 
712
     *
 
713
     *   data = "Very, very confidential data"
 
714
     *
 
715
     *   cipher = OpenSSL::Cipher::AES.new(128, :CBC)
 
716
     *   cipher.encrypt
 
717
     *   key = cipher.random_key
 
718
     *   iv = cipher.random_iv
 
719
     *
 
720
     *   encrypted = cipher.update(data) + cipher.final
 
721
     *   ...
 
722
     *   decipher = OpenSSL::Cipher::AES.new(128, :CBC)
 
723
     *   decipher.decrypt
 
724
     *   decipher.key = key
 
725
     *   decipher.iv = iv
 
726
     *
 
727
     *   plain = decipher.update(encrypted) + decipher.final
 
728
     *
 
729
     *   puts data == plain #=> true
 
730
     *
 
731
     */
545
732
    cCipher = rb_define_class_under(mOSSL, "Cipher", rb_cObject);
546
733
    eCipherError = rb_define_class_under(cCipher, "CipherError", eOSSLError);
547
734