Class: KmsTools::Decrypter

Inherits:
Base
  • Object
show all
Defined in:
lib/kms-tools/decrypter.rb

Overview

Provides low-level decryption functionality for kms-tools

Author:

  • Matt Kriegers

Constant Summary

Constants inherited from Base

Base::DEFAULT_REGION

Instance Attribute Summary

Attributes inherited from Base

#kms, #master_key

Instance Method Summary collapse

Methods inherited from Base

#available_aliases, #from_64, #initialize, #master_key_arn, #master_key_id, #region, #to_64, #to_s64, #use_key_alias=

Constructor Details

This class inherits a constructor from KmsTools::Base

Instance Method Details

#decrypt_string(str) ⇒ String

Decrypt base64 encoded ciphertext that was encrypted directly with a customer master key

Parameters:

  • str (String)

    Base64 encoded ciphertext

Returns:

  • (String)

    Binary plaintext



10
11
12
# File 'lib/kms-tools/decrypter.rb', line 10

def decrypt_string(str)
  kms.decrypt({:ciphertext_blob => from_64(str)}).plaintext
end

#decrypt_with_data_key(params) ⇒ String

Decrypt a blob using private keys

Parameters:

  • params (Hash)

Options Hash (params):

  • :cipher (String)

    OpenSSL cipher used for encryption

  • :encrypted_key (String)

    Encrypted private key

  • :encrypted_iv (String)

    Encrypted initialization vector

  • :encrypted_data (String)

    Ciphertext data blob

Returns:

  • (String)

    Binary plaintext



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kms-tools/decrypter.rb', line 22

def decrypt_with_data_key(params)
  cipher = OpenSSL::Cipher.new(params[:cipher])
  cipher.decrypt
  cipher.key = decrypt_string(params[:encrypted_key])
  cipher.iv = decrypt_string(params[:encrypted_iv])
  decrypted_data = cipher.update(params[:encrypted_data]) + cipher.final

  raise "File integrity check failed!" unless integrity_verified?(decrypted_data, params[:checksum])

  decrypted_data
end

#integrity_verified?(data, checksum) ⇒ Boolean

Verify data blob against known hash

Parameters:

  • data (String)

    Data to verify

  • checksum (String)

    Known SHA1 hash

Returns:

  • (Boolean)


77
78
79
# File 'lib/kms-tools/decrypter.rb', line 77

def integrity_verified?(data, checksum)
  Digest::SHA1.hexdigest(data).eql? checksum
end

#stream_decrypt_with_data_key(params) ⇒ Object

Decrypt a stream using private keys

Parameters:

  • params (Hash)

Options Hash (params):

  • :cipher (String)

    OpenSSL cipher used for encryption

  • :encrypted_key (String)

    Encrypted private key

  • :encrypted_iv (String)

    Encrypted initialization vector

  • :in (Stream)

    Input stream to read ciphertext data from

  • :position (Integer)

    Optional file position marking beginning of ciphertext

  • :out (Stream)

    Stream to to write plaintext output to

  • :checksum (String)

    Optional SHA1 checksum to verify integrity of decrypted data



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/kms-tools/decrypter.rb', line 44

def stream_decrypt_with_data_key(params)
  # set up cipher
  cipher = OpenSSL::Cipher.new(params[:cipher])
  cipher.decrypt
  cipher.key = decrypt_string(params[:encrypted_key])
  cipher.iv = decrypt_string(params[:encrypted_iv])

  sha1 = Digest::SHA1.new if params[:checksum]

  # write the output stream
  chunk = ""
  params[:in].seek(params[:position], IO::SEEK_SET) if params[:position]
  while params[:in].read(STREAM_CHUNK_SIZE, chunk)
    decrypted_chunk = cipher.update(chunk)
    sha1.update(decrypted_chunk) if params[:checksum]
    params[:out] << decrypted_chunk
  end

  final = cipher.final
  sha1.update(final) if params[:checksum]

  if params[:checksum]
    raise "Decrypted data stream failed checksum verification!" unless params[:checksum].eql? sha1.hexdigest
  end

  params[:out] << final
  true
end