2014-10-19 05:14:30 +00:00
# (c) 2014, James Tanner <tanner.jc@gmail.com>
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
2014-10-29 00:44:21 +00:00
# Make coding more python3-ish
from __future__ import ( absolute_import , division , print_function )
__metaclass__ = type
2014-10-19 05:14:30 +00:00
import os
import shlex
import shutil
import tempfile
from io import BytesIO
from subprocess import call
2015-07-11 18:24:00 +00:00
from ansible . errors import AnsibleError
2014-10-19 05:14:30 +00:00
from hashlib import sha256
from binascii import hexlify
from binascii import unhexlify
2015-08-24 22:49:55 +00:00
from six import PY3
2015-06-03 15:45:10 +00:00
2015-07-11 18:24:00 +00:00
# Note: Only used for loading obsolete VaultAES files. All files are written
# using the newer VaultAES256 which does not require md5
from hashlib import md5
2015-06-03 15:45:10 +00:00
try :
from six import byte2int
except ImportError :
# bytes2int added in six-1.4.0
2015-06-03 17:24:35 +00:00
if PY3 :
2015-06-03 15:45:10 +00:00
import operator
byte2int = operator . itemgetter ( 0 )
2015-06-03 17:24:35 +00:00
else :
def byte2int ( bs ) :
return ord ( bs [ 0 ] )
2015-06-03 15:45:10 +00:00
2015-04-15 18:08:53 +00:00
from ansible . utils . unicode import to_unicode , to_bytes
2014-10-19 05:14:30 +00:00
try :
from Crypto . Hash import SHA256 , HMAC
HAS_HASH = True
except ImportError :
HAS_HASH = False
# Counter import fails for 2.0.1, requires >= 2.6.1 from pip
try :
from Crypto . Util import Counter
HAS_COUNTER = True
except ImportError :
HAS_COUNTER = False
# KDF import fails for 2.0.1, requires >= 2.6.1 from pip
try :
from Crypto . Protocol . KDF import PBKDF2
HAS_PBKDF2 = True
except ImportError :
HAS_PBKDF2 = False
# AES IMPORTS
try :
from Crypto . Cipher import AES as AES
2015-04-15 18:08:53 +00:00
HAS_AES = True
2014-10-19 05:14:30 +00:00
except ImportError :
2015-04-15 18:08:53 +00:00
HAS_AES = False
2014-10-19 05:14:30 +00:00
Use PBKDF2HMAC() from cryptography for vault keys.
When stretching the key for vault files, use PBKDF2HMAC() from the
cryptography package instead of pycrypto. This will speed up the opening
of vault files by ~10x.
The problem is here in lib/ansible/utils/vault.py:
hash_function = SHA256
# make two keys and one iv
pbkdf2_prf = lambda p, s: HMAC.new(p, s, hash_function).digest()
derivedkey = PBKDF2(password, salt, dkLen=(2 * keylength) + ivlength,
count=10000, prf=pbkdf2_prf)
`PBKDF2()` calls a Python callback function (`pbkdf2_pr()`) 10000 times.
If one has several vault files, this will cause excessive start times
with `ansible` or `ansible-playbook` (we experience ~15 second startup
times).
Testing the original implementation in 1.9.2 with a vault file:
In [2]: %timeit v.decrypt(encrypted_data)
1 loops, best of 3: 265 ms per loop
Having a recent OpenSSL version and using the vault.py changes in this commit:
In [2]: %timeit v.decrypt(encrypted_data)
10 loops, best of 3: 23.2 ms per loop
2015-07-22 17:52:42 +00:00
# OpenSSL pbkdf2_hmac
HAS_PBKDF2HMAC = False
try :
from cryptography . hazmat . primitives . hashes import SHA256 as c_SHA256
from cryptography . hazmat . primitives . kdf . pbkdf2 import PBKDF2HMAC
from cryptography . hazmat . backends import default_backend
HAS_PBKDF2HMAC = True
except ImportError :
pass
HAS_ANY_PBKDF2HMAC = HAS_PBKDF2 or HAS_PBKDF2HMAC
2014-10-19 05:14:30 +00:00
CRYPTO_UPGRADE = " ansible-vault requires a newer version of pycrypto than the one installed on your platform. You may fix this with OS-specific commands such as: yum install python-devel; rpm -e --nodeps python-crypto; pip install pycrypto "
2015-08-24 22:49:55 +00:00
b_HEADER = b ' $ANSIBLE_VAULT '
CIPHER_WHITELIST = frozenset ( ( u ' AES ' , u ' AES256 ' ) )
CIPHER_WRITE_WHITELIST = frozenset ( ( u ' AES256 ' , ) )
2014-10-19 05:14:30 +00:00
2015-04-16 16:53:59 +00:00
2015-06-16 13:20:15 +00:00
def check_prereqs ( ) :
Use PBKDF2HMAC() from cryptography for vault keys.
When stretching the key for vault files, use PBKDF2HMAC() from the
cryptography package instead of pycrypto. This will speed up the opening
of vault files by ~10x.
The problem is here in lib/ansible/utils/vault.py:
hash_function = SHA256
# make two keys and one iv
pbkdf2_prf = lambda p, s: HMAC.new(p, s, hash_function).digest()
derivedkey = PBKDF2(password, salt, dkLen=(2 * keylength) + ivlength,
count=10000, prf=pbkdf2_prf)
`PBKDF2()` calls a Python callback function (`pbkdf2_pr()`) 10000 times.
If one has several vault files, this will cause excessive start times
with `ansible` or `ansible-playbook` (we experience ~15 second startup
times).
Testing the original implementation in 1.9.2 with a vault file:
In [2]: %timeit v.decrypt(encrypted_data)
1 loops, best of 3: 265 ms per loop
Having a recent OpenSSL version and using the vault.py changes in this commit:
In [2]: %timeit v.decrypt(encrypted_data)
10 loops, best of 3: 23.2 ms per loop
2015-07-22 17:52:42 +00:00
if not HAS_AES or not HAS_COUNTER or not HAS_ANY_PBKDF2HMAC or not HAS_HASH :
2015-07-11 18:24:00 +00:00
raise AnsibleError ( CRYPTO_UPGRADE )
2015-06-16 13:20:15 +00:00
2015-08-24 22:49:55 +00:00
class VaultLib :
2014-10-19 05:14:30 +00:00
def __init__ ( self , password ) :
2015-08-24 22:49:55 +00:00
self . b_password = to_bytes ( password , errors = ' strict ' , encoding = ' utf-8 ' )
2014-10-19 05:14:30 +00:00
self . cipher_name = None
2015-08-24 22:49:55 +00:00
self . b_version = b ' 1.1 '
2014-10-19 05:14:30 +00:00
2015-04-15 18:08:53 +00:00
def is_encrypted ( self , data ) :
2015-08-24 22:49:55 +00:00
""" Test if this is vault encrypted data
: arg data : a byte str or unicode string to test whether it is
recognized as vault encrypted data
: returns : True if it is recognized . Otherwise , False .
"""
if to_bytes ( data , errors = ' strict ' , encoding = ' utf-8 ' ) . startswith ( b_HEADER ) :
2014-10-19 05:14:30 +00:00
return True
2015-08-24 22:49:55 +00:00
return False
2014-10-19 05:14:30 +00:00
def encrypt ( self , data ) :
2015-08-24 22:49:55 +00:00
""" Vault encrypt a piece of data.
: arg data : a utf - 8 byte str or unicode string to encrypt .
: returns : a utf - 8 encoded byte str of encrypted data . The string
contains a header identifying this as vault encrypted data and
formatted to newline terminated lines of 80 characters . This is
suitable for dumping as is to a vault file .
"""
b_data = to_bytes ( data , errors = ' strict ' , encoding = ' utf-8 ' )
2014-10-19 05:14:30 +00:00
2015-08-24 22:49:55 +00:00
if self . is_encrypted ( b_data ) :
2015-07-11 18:24:00 +00:00
raise AnsibleError ( " data is already encrypted " )
2014-10-19 05:14:30 +00:00
2015-08-25 09:24:23 +00:00
if not self . cipher_name or self . cipher_name not in CIPHER_WRITE_WHITELIST :
2015-08-24 22:49:55 +00:00
self . cipher_name = u " AES256 "
2014-10-19 05:14:30 +00:00
2015-08-24 22:49:55 +00:00
cipher_class_name = u ' Vault {0} ' . format ( self . cipher_name )
2015-08-25 09:24:23 +00:00
if cipher_class_name in globals ( ) :
2015-08-24 22:49:55 +00:00
Cipher = globals ( ) [ cipher_class_name ]
this_cipher = Cipher ( )
2014-10-19 05:14:30 +00:00
else :
2015-08-24 22:49:55 +00:00
raise AnsibleError ( u " {0} cipher could not be found " . format ( self . cipher_name ) )
2014-10-19 05:14:30 +00:00
2015-08-24 22:49:55 +00:00
# encrypt data
b_enc_data = this_cipher . encrypt ( b_data , self . b_password )
2014-10-19 05:14:30 +00:00
2015-08-24 22:49:55 +00:00
# format the data for output to the file
b_tmp_data = self . _format_output ( b_enc_data )
return b_tmp_data
2014-10-19 05:14:30 +00:00
def decrypt ( self , data ) :
2015-08-24 22:49:55 +00:00
""" Decrypt a piece of vault encrypted data.
: arg data : a string to decrypt . Since vault encrypted data is an
ascii text format this can be either a byte str or unicode string .
: returns : a byte string containing the decrypted data
"""
b_data = to_bytes ( data , errors = ' strict ' , encoding = ' utf-8 ' )
2015-04-15 18:08:53 +00:00
2015-08-24 22:49:55 +00:00
if self . b_password is None :
2015-07-11 18:24:00 +00:00
raise AnsibleError ( " A vault password must be specified to decrypt data " )
2014-10-19 05:14:30 +00:00
2015-08-24 22:49:55 +00:00
if not self . is_encrypted ( b_data ) :
2015-07-11 18:24:00 +00:00
raise AnsibleError ( " data is not encrypted " )
2014-10-19 05:14:30 +00:00
# clean out header
2015-08-24 22:49:55 +00:00
b_data = self . _split_header ( b_data )
2014-10-19 05:14:30 +00:00
# create the cipher object
2015-08-24 22:49:55 +00:00
cipher_class_name = u ' Vault {0} ' . format ( self . cipher_name )
if cipher_class_name in globals ( ) and self . cipher_name in CIPHER_WHITELIST :
Cipher = globals ( ) [ cipher_class_name ]
this_cipher = Cipher ( )
2014-10-19 05:14:30 +00:00
else :
2015-08-24 22:49:55 +00:00
raise AnsibleError ( " {0} cipher could not be found " . format ( self . cipher_name ) )
2014-10-19 05:14:30 +00:00
# try to unencrypt data
2015-08-24 22:49:55 +00:00
b_data = this_cipher . decrypt ( b_data , self . b_password )
if b_data is None :
2015-07-11 18:24:00 +00:00
raise AnsibleError ( " Decryption failed " )
2014-10-19 05:14:30 +00:00
2015-08-24 22:49:55 +00:00
return b_data
def _format_output ( self , b_data ) :
""" Add header and format to 80 columns
2014-10-19 05:14:30 +00:00
2015-08-24 22:49:55 +00:00
: arg b_data : the encrypted and hexlified data as a byte string
: returns : a byte str that should be dumped into a file . It ' s
formatted to 80 char columns and has the header prepended
"""
2014-10-19 05:14:30 +00:00
if not self . cipher_name :
2015-07-11 18:24:00 +00:00
raise AnsibleError ( " the cipher must be set before adding a header " )
2014-10-19 05:14:30 +00:00
2015-08-24 22:49:55 +00:00
tmpdata = [ b ' %s \n ' % b_data [ i : i + 80 ] for i in range ( 0 , len ( b_data ) , 80 ) ]
tmpdata . insert ( 0 , b ' %s ; %s ; %s \n ' % ( b_HEADER , self . b_version ,
to_bytes ( self . cipher_name , errors = ' strict ' , encoding = ' utf-8 ' ) ) )
tmpdata = b ' ' . join ( tmpdata )
return tmpdata
2014-10-19 05:14:30 +00:00
2015-08-24 22:49:55 +00:00
def _split_header ( self , b_data ) :
""" Retrieve information about the Vault and clean the data
2014-10-19 05:14:30 +00:00
2015-08-24 22:49:55 +00:00
When data is saved , it has a header prepended and is formatted into 80
character lines . This method extracts the information from the header
and then removes the header and the inserted newlines . The string returned
is suitable for processing by the Cipher classes .
2014-10-19 05:14:30 +00:00
2015-08-24 22:49:55 +00:00
: arg b_data : byte str containing the data from a save file
: returns : a byte str suitable for passing to a Cipher class ' s
decrypt ( ) function .
"""
2014-10-19 05:14:30 +00:00
# used by decrypt
2015-08-24 22:49:55 +00:00
tmpdata = b_data . split ( b ' \n ' )
2015-04-15 18:08:53 +00:00
tmpheader = tmpdata [ 0 ] . strip ( ) . split ( b ' ; ' )
2014-10-19 05:14:30 +00:00
2015-08-24 22:49:55 +00:00
self . b_version = tmpheader [ 1 ] . strip ( )
2015-04-15 18:08:53 +00:00
self . cipher_name = to_unicode ( tmpheader [ 2 ] . strip ( ) )
2015-08-24 22:49:55 +00:00
clean_data = b ' ' . join ( tmpdata [ 1 : ] )
2014-10-19 05:14:30 +00:00
return clean_data
2015-08-24 22:49:55 +00:00
class VaultEditor :
2014-10-19 05:14:30 +00:00
2015-08-26 13:47:37 +00:00
def __init__ ( self , password ) :
2014-10-19 05:14:30 +00:00
self . password = password
2015-08-26 13:47:37 +00:00
def _edit_file_helper ( self , filename , existing_data = None , force_save = False ) :
2014-10-21 14:33:33 +00:00
# make sure the umask is set to a sane value
old_umask = os . umask ( 0o077 )
# Create a tempfile
_ , tmp_path = tempfile . mkstemp ( )
if existing_data :
2015-04-25 02:31:06 +00:00
self . write_data ( existing_data , tmp_path )
2014-10-21 14:33:33 +00:00
# drop the user into an editor on the tmp file
call ( self . _editor_shell_command ( tmp_path ) )
tmpdata = self . read_data ( tmp_path )
2014-08-13 12:58:17 +00:00
# Do nothing if the content has not changed
2015-08-24 22:49:55 +00:00
if existing_data == tmpdata and not force_save :
2015-08-25 21:51:32 +00:00
os . remove ( tmp_path )
2014-08-13 12:58:17 +00:00
return
2014-10-21 14:33:33 +00:00
# create new vault
this_vault = VaultLib ( self . password )
# encrypt new data and write out to tmp
enc_data = this_vault . encrypt ( tmpdata )
self . write_data ( enc_data , tmp_path )
# shuffle tmp file into place
2015-08-26 13:47:37 +00:00
self . shuffle_files ( tmp_path , filename )
2014-10-21 14:33:33 +00:00
# and restore umask
os . umask ( old_umask )
2015-08-26 13:47:37 +00:00
def create_file ( self , filename ) :
2014-10-19 05:14:30 +00:00
""" create a new encrypted file """
2015-06-16 13:20:15 +00:00
check_prereqs ( )
2014-10-19 05:14:30 +00:00
2015-08-26 13:47:37 +00:00
if os . path . isfile ( filename ) :
raise AnsibleError ( " %s exists, please use ' edit ' instead " % filename )
2014-10-19 05:14:30 +00:00
2014-10-21 14:33:33 +00:00
# Let the user specify contents and save file
2015-08-26 13:47:37 +00:00
self . _edit_file_helper ( filename )
2014-10-19 05:14:30 +00:00
2015-08-26 13:47:37 +00:00
def decrypt_file ( self , filename ) :
2014-10-19 05:14:30 +00:00
2015-06-16 13:20:15 +00:00
check_prereqs ( )
2014-10-19 05:14:30 +00:00
2015-08-26 13:47:37 +00:00
if not os . path . isfile ( filename ) :
raise AnsibleError ( " %s does not exist " % filename )
2014-10-21 14:33:33 +00:00
2015-08-26 13:47:37 +00:00
tmpdata = self . read_data ( filename )
2014-10-19 05:14:30 +00:00
this_vault = VaultLib ( self . password )
if this_vault . is_encrypted ( tmpdata ) :
dec_data = this_vault . decrypt ( tmpdata )
if dec_data is None :
2015-07-11 18:24:00 +00:00
raise AnsibleError ( " Decryption failed " )
2014-10-19 05:14:30 +00:00
else :
2015-08-26 13:47:37 +00:00
self . write_data ( dec_data , filename )
2014-10-19 05:14:30 +00:00
else :
2015-08-26 13:47:37 +00:00
raise AnsibleError ( " %s is not encrypted " % filename )
2014-10-19 05:14:30 +00:00
2015-08-26 13:47:37 +00:00
def edit_file ( self , filename ) :
2014-10-19 05:14:30 +00:00
2015-06-16 13:20:15 +00:00
check_prereqs ( )
2014-10-19 05:14:30 +00:00
# decrypt to tmpfile
2015-08-26 13:47:37 +00:00
tmpdata = self . read_data ( filename )
2014-10-19 05:14:30 +00:00
this_vault = VaultLib ( self . password )
dec_data = this_vault . decrypt ( tmpdata )
2014-10-21 14:33:33 +00:00
# let the user edit the data and save
2015-08-24 22:49:55 +00:00
if this_vault . cipher_name not in CIPHER_WRITE_WHITELIST :
# we want to get rid of files encrypted with the AES cipher
2015-08-26 13:47:37 +00:00
self . _edit_file_helper ( filename , existing_data = dec_data , force_save = True )
2015-08-24 22:49:55 +00:00
else :
2015-08-26 13:47:37 +00:00
self . _edit_file_helper ( filename , existing_data = dec_data , force_save = False )
2014-10-19 05:14:30 +00:00
2015-08-26 13:47:37 +00:00
def view_file ( self , filename ) :
2014-10-19 05:14:30 +00:00
2015-06-16 13:20:15 +00:00
check_prereqs ( )
2014-10-19 05:14:30 +00:00
# decrypt to tmpfile
2015-08-26 13:47:37 +00:00
tmpdata = self . read_data ( filename )
2014-10-19 05:14:30 +00:00
this_vault = VaultLib ( self . password )
dec_data = this_vault . decrypt ( tmpdata )
_ , tmp_path = tempfile . mkstemp ( )
self . write_data ( dec_data , tmp_path )
# drop the user into pager on the tmp file
call ( self . _pager_shell_command ( tmp_path ) )
os . remove ( tmp_path )
2015-08-26 13:47:37 +00:00
def encrypt_file ( self , filename ) :
2014-10-19 05:14:30 +00:00
2015-06-16 13:20:15 +00:00
check_prereqs ( )
2014-10-19 05:14:30 +00:00
2015-08-26 13:47:37 +00:00
if not os . path . isfile ( filename ) :
raise AnsibleError ( " %s does not exist " % filename )
2014-10-21 14:33:33 +00:00
2015-08-26 13:47:37 +00:00
tmpdata = self . read_data ( filename )
2014-10-19 05:14:30 +00:00
this_vault = VaultLib ( self . password )
if not this_vault . is_encrypted ( tmpdata ) :
enc_data = this_vault . encrypt ( tmpdata )
2015-08-26 13:47:37 +00:00
self . write_data ( enc_data , filename )
2014-10-19 05:14:30 +00:00
else :
2015-08-26 13:47:37 +00:00
raise AnsibleError ( " %s is already encrypted " % filename )
2014-10-19 05:14:30 +00:00
2015-08-26 14:24:59 +00:00
def rekey_file ( self , filename , new_password ) :
2014-10-19 05:14:30 +00:00
2015-06-16 13:20:15 +00:00
check_prereqs ( )
2014-10-19 05:14:30 +00:00
2015-04-15 18:08:53 +00:00
# decrypt
2015-08-26 13:47:37 +00:00
tmpdata = self . read_data ( filename )
2014-10-19 05:14:30 +00:00
this_vault = VaultLib ( self . password )
dec_data = this_vault . decrypt ( tmpdata )
# create new vault
new_vault = VaultLib ( new_password )
# re-encrypt data and re-write file
enc_data = new_vault . encrypt ( dec_data )
2015-08-26 13:47:37 +00:00
self . write_data ( enc_data , filename )
2014-10-19 05:14:30 +00:00
def read_data ( self , filename ) :
f = open ( filename , " rb " )
tmpdata = f . read ( )
f . close ( )
return tmpdata
def write_data ( self , data , filename ) :
2015-04-15 18:08:53 +00:00
if os . path . isfile ( filename ) :
2014-10-19 05:14:30 +00:00
os . remove ( filename )
f = open ( filename , " wb " )
2015-08-24 22:49:55 +00:00
f . write ( to_bytes ( data , errors = ' strict ' ) )
2014-10-19 05:14:30 +00:00
f . close ( )
def shuffle_files ( self , src , dest ) :
# overwrite dest with src
if os . path . isfile ( dest ) :
os . remove ( dest )
shutil . move ( src , dest )
def _editor_shell_command ( self , filename ) :
EDITOR = os . environ . get ( ' EDITOR ' , ' vim ' )
editor = shlex . split ( EDITOR )
editor . append ( filename )
return editor
def _pager_shell_command ( self , filename ) :
PAGER = os . environ . get ( ' PAGER ' , ' less ' )
pager = shlex . split ( PAGER )
pager . append ( filename )
return pager
2015-06-16 13:20:15 +00:00
class VaultFile ( object ) :
def __init__ ( self , password , filename ) :
self . password = password
self . filename = filename
if not os . path . isfile ( self . filename ) :
2015-07-11 18:24:00 +00:00
raise AnsibleError ( " %s does not exist " % self . filename )
2015-06-16 13:20:15 +00:00
try :
self . filehandle = open ( filename , " rb " )
2015-07-08 15:58:07 +00:00
except Exception as e :
2015-07-11 18:24:00 +00:00
raise AnsibleError ( " Could not open %s : %s " % ( self . filename , str ( e ) ) )
2015-06-16 13:20:15 +00:00
_ , self . tmpfile = tempfile . mkstemp ( )
2015-08-24 22:49:55 +00:00
### FIXME:
# __del__ can be problematic in python... For this use case, make
# VaultFile a context manager instead (implement __enter__ and __exit__)
2015-06-16 13:20:15 +00:00
def __del__ ( self ) :
self . filehandle . close ( )
os . unlink ( self . tmplfile )
def is_encrypted ( self ) :
2015-08-24 22:49:55 +00:00
peak = self . filehandle . readline ( )
if peak . startswith ( b_HEADER ) :
2015-06-16 13:20:15 +00:00
return True
else :
return False
def get_decrypted ( self ) :
check_prereqs ( )
if self . is_encrypted ( ) :
tmpdata = self . filehandle . read ( )
this_vault = VaultLib ( self . password )
dec_data = this_vault . decrypt ( tmpdata )
if dec_data is None :
2015-07-11 18:24:00 +00:00
raise AnsibleError ( " Decryption failed " )
2015-06-16 13:20:15 +00:00
else :
2015-08-24 22:49:55 +00:00
self . tmpfile . write ( dec_data )
2015-06-16 13:20:15 +00:00
return self . tmpfile
else :
return self . filename
2014-10-19 05:14:30 +00:00
########################################
# CIPHERS #
########################################
2015-08-24 22:49:55 +00:00
class VaultAES :
2014-10-19 05:14:30 +00:00
# this version has been obsoleted by the VaultAES256 class
# which uses encrypt-then-mac (fixing order) and also improving the KDF used
# code remains for upgrade purposes only
# http://stackoverflow.com/a/16761459
2015-08-24 22:49:55 +00:00
# Note: strings in this class should be byte strings by default.
2014-10-19 05:14:30 +00:00
def __init__ ( self ) :
if not HAS_AES :
2015-07-11 18:24:00 +00:00
raise AnsibleError ( CRYPTO_UPGRADE )
2014-10-19 05:14:30 +00:00
def aes_derive_key_and_iv ( self , password , salt , key_length , iv_length ) :
""" Create a key and an initialization vector """
2015-04-15 18:08:53 +00:00
d = d_i = b ' '
2014-10-19 05:14:30 +00:00
while len ( d ) < key_length + iv_length :
2015-08-24 22:49:55 +00:00
text = b " %s %s %s " % ( d_i , password , salt )
d_i = to_bytes ( md5 ( text ) . digest ( ) , errors = ' strict ' )
2014-10-19 05:14:30 +00:00
d + = d_i
key = d [ : key_length ]
iv = d [ key_length : key_length + iv_length ]
return key , iv
def encrypt ( self , data , password , key_length = 32 ) :
""" Read plaintext data from in_file and write encrypted to out_file """
# combine sha + data
2015-08-24 22:49:55 +00:00
this_sha = to_bytes ( sha256 ( data ) . hexdigest ( ) )
tmp_data = this_sha + b " \n " + data
2014-10-19 05:14:30 +00:00
2015-08-24 22:49:55 +00:00
in_file = BytesIO ( tmp_data )
2014-10-19 05:14:30 +00:00
in_file . seek ( 0 )
out_file = BytesIO ( )
bs = AES . block_size
2015-04-15 18:08:53 +00:00
# Get a block of random data. EL does not have Crypto.Random.new()
2014-10-19 05:14:30 +00:00
# so os.urandom is used for cross platform purposes
2015-08-24 22:49:55 +00:00
salt = os . urandom ( bs - len ( b ' Salted__ ' ) )
2014-10-19 05:14:30 +00:00
key , iv = self . aes_derive_key_and_iv ( password , salt , key_length , bs )
cipher = AES . new ( key , AES . MODE_CBC , iv )
2015-04-15 18:08:53 +00:00
full = to_bytes ( b ' Salted__ ' + salt )
out_file . write ( full )
2014-10-19 05:14:30 +00:00
finished = False
while not finished :
chunk = in_file . read ( 1024 * bs )
if len ( chunk ) == 0 or len ( chunk ) % bs != 0 :
padding_length = ( bs - len ( chunk ) % bs ) or bs
2015-08-24 22:49:55 +00:00
chunk + = to_bytes ( padding_length * chr ( padding_length ) , errors = ' strict ' , encoding = ' ascii ' )
2014-10-19 05:14:30 +00:00
finished = True
out_file . write ( cipher . encrypt ( chunk ) )
out_file . seek ( 0 )
enc_data = out_file . read ( )
tmp_data = hexlify ( enc_data )
return tmp_data
2015-04-15 18:08:53 +00:00
2014-10-19 05:14:30 +00:00
def decrypt ( self , data , password , key_length = 32 ) :
""" Read encrypted data from in_file and write decrypted to out_file """
# http://stackoverflow.com/a/14989032
data = unhexlify ( data )
in_file = BytesIO ( data )
in_file . seek ( 0 )
out_file = BytesIO ( )
bs = AES . block_size
2015-04-15 18:08:53 +00:00
tmpsalt = in_file . read ( bs )
2015-08-24 22:49:55 +00:00
salt = tmpsalt [ len ( b ' Salted__ ' ) : ]
2014-10-19 05:14:30 +00:00
key , iv = self . aes_derive_key_and_iv ( password , salt , key_length , bs )
cipher = AES . new ( key , AES . MODE_CBC , iv )
2015-04-15 18:08:53 +00:00
next_chunk = b ' '
2014-10-19 05:14:30 +00:00
finished = False
while not finished :
chunk , next_chunk = next_chunk , cipher . decrypt ( in_file . read ( 1024 * bs ) )
if len ( next_chunk ) == 0 :
2015-06-03 17:24:35 +00:00
if PY3 :
2015-04-15 18:08:53 +00:00
padding_length = chunk [ - 1 ]
2015-06-03 17:24:35 +00:00
else :
padding_length = ord ( chunk [ - 1 ] )
2015-04-15 18:08:53 +00:00
2014-10-19 05:14:30 +00:00
chunk = chunk [ : - padding_length ]
finished = True
2015-04-16 16:53:59 +00:00
2014-10-19 05:14:30 +00:00
out_file . write ( chunk )
2015-04-16 16:53:59 +00:00
out_file . flush ( )
2014-10-19 05:14:30 +00:00
# reset the stream pointer to the beginning
out_file . seek ( 0 )
2015-04-16 16:53:59 +00:00
out_data = out_file . read ( )
out_file . close ( )
2014-10-19 05:14:30 +00:00
# split out sha and verify decryption
2015-08-24 22:49:55 +00:00
split_data = out_data . split ( b " \n " , 1 )
2014-10-19 05:14:30 +00:00
this_sha = split_data [ 0 ]
2015-08-24 22:49:55 +00:00
this_data = split_data [ 1 ]
test_sha = to_bytes ( sha256 ( this_data ) . hexdigest ( ) )
2014-10-19 05:14:30 +00:00
if this_sha != test_sha :
2015-07-11 18:24:00 +00:00
raise AnsibleError ( " Decryption failed " )
2014-10-19 05:14:30 +00:00
return this_data
2015-08-24 22:49:55 +00:00
class VaultAES256 :
2014-10-19 05:14:30 +00:00
"""
2015-04-15 18:08:53 +00:00
Vault implementation using AES - CTR with an HMAC - SHA256 authentication code .
2014-10-19 05:14:30 +00:00
Keys are derived using PBKDF2
"""
# http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html
2015-08-24 22:49:55 +00:00
# Note: strings in this class should be byte strings by default.
2014-10-19 05:14:30 +00:00
def __init__ ( self ) :
2015-06-16 13:20:15 +00:00
check_prereqs ( )
2014-10-19 05:14:30 +00:00
Use PBKDF2HMAC() from cryptography for vault keys.
When stretching the key for vault files, use PBKDF2HMAC() from the
cryptography package instead of pycrypto. This will speed up the opening
of vault files by ~10x.
The problem is here in lib/ansible/utils/vault.py:
hash_function = SHA256
# make two keys and one iv
pbkdf2_prf = lambda p, s: HMAC.new(p, s, hash_function).digest()
derivedkey = PBKDF2(password, salt, dkLen=(2 * keylength) + ivlength,
count=10000, prf=pbkdf2_prf)
`PBKDF2()` calls a Python callback function (`pbkdf2_pr()`) 10000 times.
If one has several vault files, this will cause excessive start times
with `ansible` or `ansible-playbook` (we experience ~15 second startup
times).
Testing the original implementation in 1.9.2 with a vault file:
In [2]: %timeit v.decrypt(encrypted_data)
1 loops, best of 3: 265 ms per loop
Having a recent OpenSSL version and using the vault.py changes in this commit:
In [2]: %timeit v.decrypt(encrypted_data)
10 loops, best of 3: 23.2 ms per loop
2015-07-22 17:52:42 +00:00
def create_key ( self , password , salt , keylength , ivlength ) :
2014-10-19 05:14:30 +00:00
hash_function = SHA256
# make two keys and one iv
pbkdf2_prf = lambda p , s : HMAC . new ( p , s , hash_function ) . digest ( )
2015-04-15 18:08:53 +00:00
derivedkey = PBKDF2 ( password , salt , dkLen = ( 2 * keylength ) + ivlength ,
2014-10-19 05:14:30 +00:00
count = 10000 , prf = pbkdf2_prf )
Use PBKDF2HMAC() from cryptography for vault keys.
When stretching the key for vault files, use PBKDF2HMAC() from the
cryptography package instead of pycrypto. This will speed up the opening
of vault files by ~10x.
The problem is here in lib/ansible/utils/vault.py:
hash_function = SHA256
# make two keys and one iv
pbkdf2_prf = lambda p, s: HMAC.new(p, s, hash_function).digest()
derivedkey = PBKDF2(password, salt, dkLen=(2 * keylength) + ivlength,
count=10000, prf=pbkdf2_prf)
`PBKDF2()` calls a Python callback function (`pbkdf2_pr()`) 10000 times.
If one has several vault files, this will cause excessive start times
with `ansible` or `ansible-playbook` (we experience ~15 second startup
times).
Testing the original implementation in 1.9.2 with a vault file:
In [2]: %timeit v.decrypt(encrypted_data)
1 loops, best of 3: 265 ms per loop
Having a recent OpenSSL version and using the vault.py changes in this commit:
In [2]: %timeit v.decrypt(encrypted_data)
10 loops, best of 3: 23.2 ms per loop
2015-07-22 17:52:42 +00:00
return derivedkey
def gen_key_initctr ( self , password , salt ) :
# 16 for AES 128, 32 for AES256
keylength = 32
# match the size used for counter.new to avoid extra work
ivlength = 16
if HAS_PBKDF2HMAC :
backend = default_backend ( )
kdf = PBKDF2HMAC (
algorithm = c_SHA256 ( ) ,
length = 2 * keylength + ivlength ,
salt = salt ,
iterations = 10000 ,
backend = backend )
derivedkey = kdf . derive ( password )
else :
derivedkey = self . create_key ( password , salt , keylength , ivlength )
2014-10-19 05:14:30 +00:00
key1 = derivedkey [ : keylength ]
key2 = derivedkey [ keylength : ( keylength * 2 ) ]
iv = derivedkey [ ( keylength * 2 ) : ( keylength * 2 ) + ivlength ]
return key1 , key2 , hexlify ( iv )
def encrypt ( self , data , password ) :
salt = os . urandom ( 32 )
key1 , key2 , iv = self . gen_key_initctr ( password , salt )
# PKCS#7 PAD DATA http://tools.ietf.org/html/rfc5652#section-6.3
bs = AES . block_size
padding_length = ( bs - len ( data ) % bs ) or bs
2015-08-24 22:49:55 +00:00
data + = to_bytes ( padding_length * chr ( padding_length ) , encoding = ' ascii ' , errors = ' strict ' )
2014-10-19 05:14:30 +00:00
# COUNTER.new PARAMETERS
# 1) nbits (integer) - Length of the counter, in bits.
# 2) initial_value (integer) - initial value of the counter. "iv" from gen_key_initctr
2014-10-29 00:27:14 +00:00
ctr = Counter . new ( 128 , initial_value = int ( iv , 16 ) )
2014-10-19 05:14:30 +00:00
# AES.new PARAMETERS
# 1) AES key, must be either 16, 24, or 32 bytes long -- "key" from gen_key_initctr
# 2) MODE_CTR, is the recommended mode
# 3) counter=<CounterObject>
cipher = AES . new ( key1 , AES . MODE_CTR , counter = ctr )
# ENCRYPT PADDED DATA
2015-04-15 18:08:53 +00:00
cryptedData = cipher . encrypt ( data )
2014-10-19 05:14:30 +00:00
# COMBINE SALT, DIGEST AND DATA
hmac = HMAC . new ( key2 , cryptedData , SHA256 )
2015-08-24 22:49:55 +00:00
message = b ' %s \n %s \n %s ' % ( hexlify ( salt ) , to_bytes ( hmac . hexdigest ( ) ) , hexlify ( cryptedData ) )
2014-10-19 05:14:30 +00:00
message = hexlify ( message )
return message
def decrypt ( self , data , password ) :
# SPLIT SALT, DIGEST, AND DATA
data = unhexlify ( data )
2015-04-15 18:08:53 +00:00
salt , cryptedHmac , cryptedData = data . split ( b " \n " , 2 )
2014-10-19 05:14:30 +00:00
salt = unhexlify ( salt )
cryptedData = unhexlify ( cryptedData )
key1 , key2 , iv = self . gen_key_initctr ( password , salt )
2015-04-15 18:08:53 +00:00
# EXIT EARLY IF DIGEST DOESN'T MATCH
2014-10-19 05:14:30 +00:00
hmacDecrypt = HMAC . new ( key2 , cryptedData , SHA256 )
2015-04-15 18:08:53 +00:00
if not self . is_equal ( cryptedHmac , to_bytes ( hmacDecrypt . hexdigest ( ) ) ) :
2014-10-19 05:14:30 +00:00
return None
# SET THE COUNTER AND THE CIPHER
2014-10-29 00:27:14 +00:00
ctr = Counter . new ( 128 , initial_value = int ( iv , 16 ) )
2014-10-19 05:14:30 +00:00
cipher = AES . new ( key1 , AES . MODE_CTR , counter = ctr )
# DECRYPT PADDED DATA
decryptedData = cipher . decrypt ( cryptedData )
# UNPAD DATA
2015-04-15 18:08:53 +00:00
try :
padding_length = ord ( decryptedData [ - 1 ] )
except TypeError :
padding_length = decryptedData [ - 1 ]
2014-10-19 05:14:30 +00:00
decryptedData = decryptedData [ : - padding_length ]
2015-08-24 22:49:55 +00:00
return decryptedData
2014-10-19 05:14:30 +00:00
def is_equal ( self , a , b ) :
2015-04-15 18:08:53 +00:00
"""
Comparing 2 byte arrrays in constant time
to avoid timing attacks .
It would be nice if there was a library for this but
hey .
"""
2014-10-19 05:14:30 +00:00
# http://codahale.com/a-lesson-in-timing-attacks/
if len ( a ) != len ( b ) :
return False
2015-04-15 18:08:53 +00:00
2014-10-19 05:14:30 +00:00
result = 0
for x , y in zip ( a , b ) :
2015-06-03 17:24:35 +00:00
if PY3 :
2015-04-15 18:08:53 +00:00
result | = x ^ y
2015-06-03 17:24:35 +00:00
else :
result | = ord ( x ) ^ ord ( y )
2015-04-15 18:08:53 +00:00
return result == 0
2015-07-11 18:24:00 +00:00