PageRenderTime 27ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ansible/parsing/vault/__init__.py

https://github.com/debfx/ansible
Python | 1332 lines | 1251 code | 30 blank | 51 comment | 18 complexity | be8fe15b95f0fcb7c5253c9fb2a2a391 MD5 | raw file
Possible License(s): GPL-3.0
  1. # (c) 2014, James Tanner <tanner.jc@gmail.com>
  2. # (c) 2016, Adrian Likins <alikins@redhat.com>
  3. # (c) 2016 Toshio Kuratomi <tkuratomi@ansible.com>
  4. #
  5. # Ansible is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # Ansible is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
  17. # Make coding more python3-ish
  18. from __future__ import (absolute_import, division, print_function)
  19. __metaclass__ = type
  20. import os
  21. import random
  22. import shlex
  23. import shutil
  24. import subprocess
  25. import sys
  26. import tempfile
  27. import warnings
  28. from binascii import hexlify
  29. from binascii import unhexlify
  30. from binascii import Error as BinasciiError
  31. HAS_CRYPTOGRAPHY = False
  32. HAS_PYCRYPTO = False
  33. HAS_SOME_PYCRYPTO = False
  34. CRYPTOGRAPHY_BACKEND = None
  35. try:
  36. with warnings.catch_warnings():
  37. warnings.simplefilter("ignore", DeprecationWarning)
  38. from cryptography.exceptions import InvalidSignature
  39. from cryptography.hazmat.backends import default_backend
  40. from cryptography.hazmat.primitives import hashes, padding
  41. from cryptography.hazmat.primitives.hmac import HMAC
  42. from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
  43. from cryptography.hazmat.primitives.ciphers import (
  44. Cipher as C_Cipher, algorithms, modes
  45. )
  46. CRYPTOGRAPHY_BACKEND = default_backend()
  47. HAS_CRYPTOGRAPHY = True
  48. except ImportError:
  49. pass
  50. try:
  51. from Crypto.Cipher import AES as AES_pycrypto
  52. HAS_SOME_PYCRYPTO = True
  53. # Note: Only used for loading obsolete VaultAES files. All files are written
  54. # using the newer VaultAES256 which does not require md5
  55. from Crypto.Hash import SHA256 as SHA256_pycrypto
  56. from Crypto.Hash import HMAC as HMAC_pycrypto
  57. # Counter import fails for 2.0.1, requires >= 2.6.1 from pip
  58. from Crypto.Util import Counter as Counter_pycrypto
  59. # KDF import fails for 2.0.1, requires >= 2.6.1 from pip
  60. from Crypto.Protocol.KDF import PBKDF2 as PBKDF2_pycrypto
  61. HAS_PYCRYPTO = True
  62. except ImportError:
  63. pass
  64. from ansible.errors import AnsibleError, AnsibleAssertionError
  65. from ansible import constants as C
  66. from ansible.module_utils.six import PY3, binary_type
  67. # Note: on py2, this zip is izip not the list based zip() builtin
  68. from ansible.module_utils.six.moves import zip
  69. from ansible.module_utils._text import to_bytes, to_text, to_native
  70. from ansible.utils.display import Display
  71. from ansible.utils.path import makedirs_safe
  72. display = Display()
  73. b_HEADER = b'$ANSIBLE_VAULT'
  74. CIPHER_WHITELIST = frozenset((u'AES', u'AES256'))
  75. CIPHER_WRITE_WHITELIST = frozenset((u'AES256',))
  76. # See also CIPHER_MAPPING at the bottom of the file which maps cipher strings
  77. # (used in VaultFile header) to a cipher class
  78. NEED_CRYPTO_LIBRARY = "ansible-vault requires either the cryptography library (preferred) or"
  79. if HAS_SOME_PYCRYPTO:
  80. NEED_CRYPTO_LIBRARY += " a newer version of"
  81. NEED_CRYPTO_LIBRARY += " pycrypto in order to function."
  82. class AnsibleVaultError(AnsibleError):
  83. pass
  84. class AnsibleVaultPasswordError(AnsibleVaultError):
  85. pass
  86. class AnsibleVaultFormatError(AnsibleError):
  87. pass
  88. def is_encrypted(data):
  89. """ Test if this is vault encrypted data blob
  90. :arg data: a byte or text string to test whether it is recognized as vault
  91. encrypted data
  92. :returns: True if it is recognized. Otherwise, False.
  93. """
  94. try:
  95. # Make sure we have a byte string and that it only contains ascii
  96. # bytes.
  97. b_data = to_bytes(to_text(data, encoding='ascii', errors='strict', nonstring='strict'), encoding='ascii', errors='strict')
  98. except (UnicodeError, TypeError):
  99. # The vault format is pure ascii so if we failed to encode to bytes
  100. # via ascii we know that this is not vault data.
  101. # Similarly, if it's not a string, it's not vault data
  102. return False
  103. if b_data.startswith(b_HEADER):
  104. return True
  105. return False
  106. def is_encrypted_file(file_obj, start_pos=0, count=-1):
  107. """Test if the contents of a file obj are a vault encrypted data blob.
  108. :arg file_obj: A file object that will be read from.
  109. :kwarg start_pos: A byte offset in the file to start reading the header
  110. from. Defaults to 0, the beginning of the file.
  111. :kwarg count: Read up to this number of bytes from the file to determine
  112. if it looks like encrypted vault data. The default is -1, read to the
  113. end of file.
  114. :returns: True if the file looks like a vault file. Otherwise, False.
  115. """
  116. # read the header and reset the file stream to where it started
  117. current_position = file_obj.tell()
  118. try:
  119. file_obj.seek(start_pos)
  120. return is_encrypted(file_obj.read(count))
  121. finally:
  122. file_obj.seek(current_position)
  123. def _parse_vaulttext_envelope(b_vaulttext_envelope, default_vault_id=None):
  124. b_tmpdata = b_vaulttext_envelope.splitlines()
  125. b_tmpheader = b_tmpdata[0].strip().split(b';')
  126. b_version = b_tmpheader[1].strip()
  127. cipher_name = to_text(b_tmpheader[2].strip())
  128. vault_id = default_vault_id
  129. # Only attempt to find vault_id if the vault file is version 1.2 or newer
  130. # if self.b_version == b'1.2':
  131. if len(b_tmpheader) >= 4:
  132. vault_id = to_text(b_tmpheader[3].strip())
  133. b_ciphertext = b''.join(b_tmpdata[1:])
  134. return b_ciphertext, b_version, cipher_name, vault_id
  135. def parse_vaulttext_envelope(b_vaulttext_envelope, default_vault_id=None, filename=None):
  136. """Parse the vaulttext envelope
  137. When data is saved, it has a header prepended and is formatted into 80
  138. character lines. This method extracts the information from the header
  139. and then removes the header and the inserted newlines. The string returned
  140. is suitable for processing by the Cipher classes.
  141. :arg b_vaulttext: byte str containing the data from a save file
  142. :kwarg default_vault_id: The vault_id name to use if the vaulttext does not provide one.
  143. :kwarg filename: The filename that the data came from. This is only
  144. used to make better error messages in case the data cannot be
  145. decrypted. This is optional.
  146. :returns: A tuple of byte str of the vaulttext suitable to pass to parse_vaultext,
  147. a byte str of the vault format version,
  148. the name of the cipher used, and the vault_id.
  149. :raises: AnsibleVaultFormatError: if the vaulttext_envelope format is invalid
  150. """
  151. # used by decrypt
  152. default_vault_id = default_vault_id or C.DEFAULT_VAULT_IDENTITY
  153. try:
  154. return _parse_vaulttext_envelope(b_vaulttext_envelope, default_vault_id)
  155. except Exception as exc:
  156. msg = "Vault envelope format error"
  157. if filename:
  158. msg += ' in %s' % (filename)
  159. msg += ': %s' % exc
  160. raise AnsibleVaultFormatError(msg)
  161. def format_vaulttext_envelope(b_ciphertext, cipher_name, version=None, vault_id=None):
  162. """ Add header and format to 80 columns
  163. :arg b_ciphertext: the encrypted and hexlified data as a byte string
  164. :arg cipher_name: unicode cipher name (for ex, u'AES256')
  165. :arg version: unicode vault version (for ex, '1.2'). Optional ('1.1' is default)
  166. :arg vault_id: unicode vault identifier. If provided, the version will be bumped to 1.2.
  167. :returns: a byte str that should be dumped into a file. It's
  168. formatted to 80 char columns and has the header prepended
  169. """
  170. if not cipher_name:
  171. raise AnsibleError("the cipher must be set before adding a header")
  172. version = version or '1.1'
  173. # If we specify a vault_id, use format version 1.2. For no vault_id, stick to 1.1
  174. if vault_id and vault_id != u'default':
  175. version = '1.2'
  176. b_version = to_bytes(version, 'utf-8', errors='strict')
  177. b_vault_id = to_bytes(vault_id, 'utf-8', errors='strict')
  178. b_cipher_name = to_bytes(cipher_name, 'utf-8', errors='strict')
  179. header_parts = [b_HEADER,
  180. b_version,
  181. b_cipher_name]
  182. if b_version == b'1.2' and b_vault_id:
  183. header_parts.append(b_vault_id)
  184. header = b';'.join(header_parts)
  185. b_vaulttext = [header]
  186. b_vaulttext += [b_ciphertext[i:i + 80] for i in range(0, len(b_ciphertext), 80)]
  187. b_vaulttext += [b'']
  188. b_vaulttext = b'\n'.join(b_vaulttext)
  189. return b_vaulttext
  190. def _unhexlify(b_data):
  191. try:
  192. return unhexlify(b_data)
  193. except (BinasciiError, TypeError) as exc:
  194. raise AnsibleVaultFormatError('Vault format unhexlify error: %s' % exc)
  195. def _parse_vaulttext(b_vaulttext):
  196. b_vaulttext = _unhexlify(b_vaulttext)
  197. b_salt, b_crypted_hmac, b_ciphertext = b_vaulttext.split(b"\n", 2)
  198. b_salt = _unhexlify(b_salt)
  199. b_ciphertext = _unhexlify(b_ciphertext)
  200. return b_ciphertext, b_salt, b_crypted_hmac
  201. def parse_vaulttext(b_vaulttext):
  202. """Parse the vaulttext
  203. :arg b_vaulttext: byte str containing the vaulttext (ciphertext, salt, crypted_hmac)
  204. :returns: A tuple of byte str of the ciphertext suitable for passing to a
  205. Cipher class's decrypt() function, a byte str of the salt,
  206. and a byte str of the crypted_hmac
  207. :raises: AnsibleVaultFormatError: if the vaulttext format is invalid
  208. """
  209. # SPLIT SALT, DIGEST, AND DATA
  210. try:
  211. return _parse_vaulttext(b_vaulttext)
  212. except AnsibleVaultFormatError:
  213. raise
  214. except Exception as exc:
  215. msg = "Vault vaulttext format error: %s" % exc
  216. raise AnsibleVaultFormatError(msg)
  217. def verify_secret_is_not_empty(secret, msg=None):
  218. '''Check the secret against minimal requirements.
  219. Raises: AnsibleVaultPasswordError if the password does not meet requirements.
  220. Currently, only requirement is that the password is not None or an empty string.
  221. '''
  222. msg = msg or 'Invalid vault password was provided'
  223. if not secret:
  224. raise AnsibleVaultPasswordError(msg)
  225. class VaultSecret:
  226. '''Opaque/abstract objects for a single vault secret. ie, a password or a key.'''
  227. def __init__(self, _bytes=None):
  228. # FIXME: ? that seems wrong... Unset etc?
  229. self._bytes = _bytes
  230. @property
  231. def bytes(self):
  232. '''The secret as a bytestring.
  233. Sub classes that store text types will need to override to encode the text to bytes.
  234. '''
  235. return self._bytes
  236. def load(self):
  237. return self._bytes
  238. class PromptVaultSecret(VaultSecret):
  239. default_prompt_formats = ["Vault password (%s): "]
  240. def __init__(self, _bytes=None, vault_id=None, prompt_formats=None):
  241. super(PromptVaultSecret, self).__init__(_bytes=_bytes)
  242. self.vault_id = vault_id
  243. if prompt_formats is None:
  244. self.prompt_formats = self.default_prompt_formats
  245. else:
  246. self.prompt_formats = prompt_formats
  247. @property
  248. def bytes(self):
  249. return self._bytes
  250. def load(self):
  251. self._bytes = self.ask_vault_passwords()
  252. def ask_vault_passwords(self):
  253. b_vault_passwords = []
  254. for prompt_format in self.prompt_formats:
  255. prompt = prompt_format % {'vault_id': self.vault_id}
  256. try:
  257. vault_pass = display.prompt(prompt, private=True)
  258. except EOFError:
  259. raise AnsibleVaultError('EOFError (ctrl-d) on prompt for (%s)' % self.vault_id)
  260. verify_secret_is_not_empty(vault_pass)
  261. b_vault_pass = to_bytes(vault_pass, errors='strict', nonstring='simplerepr').strip()
  262. b_vault_passwords.append(b_vault_pass)
  263. # Make sure the passwords match by comparing them all to the first password
  264. for b_vault_password in b_vault_passwords:
  265. self.confirm(b_vault_passwords[0], b_vault_password)
  266. if b_vault_passwords:
  267. return b_vault_passwords[0]
  268. return None
  269. def confirm(self, b_vault_pass_1, b_vault_pass_2):
  270. # enforce no newline chars at the end of passwords
  271. if b_vault_pass_1 != b_vault_pass_2:
  272. # FIXME: more specific exception
  273. raise AnsibleError("Passwords do not match")
  274. def script_is_client(filename):
  275. '''Determine if a vault secret script is a client script that can be given --vault-id args'''
  276. # if password script is 'something-client' or 'something-client.[sh|py|rb|etc]'
  277. # script_name can still have '.' or could be entire filename if there is no ext
  278. script_name, dummy = os.path.splitext(filename)
  279. # TODO: for now, this is entirely based on filename
  280. if script_name.endswith('-client'):
  281. return True
  282. return False
  283. def get_file_vault_secret(filename=None, vault_id=None, encoding=None, loader=None):
  284. this_path = os.path.realpath(os.path.expanduser(filename))
  285. if not os.path.exists(this_path):
  286. raise AnsibleError("The vault password file %s was not found" % this_path)
  287. if loader.is_executable(this_path):
  288. if script_is_client(filename):
  289. display.vvvv('The vault password file %s is a client script.' % filename)
  290. # TODO: pass vault_id_name to script via cli
  291. return ClientScriptVaultSecret(filename=this_path, vault_id=vault_id,
  292. encoding=encoding, loader=loader)
  293. # just a plain vault password script. No args, returns a byte array
  294. return ScriptVaultSecret(filename=this_path, encoding=encoding, loader=loader)
  295. return FileVaultSecret(filename=this_path, encoding=encoding, loader=loader)
  296. # TODO: mv these classes to a separate file so we don't pollute vault with 'subprocess' etc
  297. class FileVaultSecret(VaultSecret):
  298. def __init__(self, filename=None, encoding=None, loader=None):
  299. super(FileVaultSecret, self).__init__()
  300. self.filename = filename
  301. self.loader = loader
  302. self.encoding = encoding or 'utf8'
  303. # We could load from file here, but that is eventually a pain to test
  304. self._bytes = None
  305. self._text = None
  306. @property
  307. def bytes(self):
  308. if self._bytes:
  309. return self._bytes
  310. if self._text:
  311. return self._text.encode(self.encoding)
  312. return None
  313. def load(self):
  314. self._bytes = self._read_file(self.filename)
  315. def _read_file(self, filename):
  316. """
  317. Read a vault password from a file or if executable, execute the script and
  318. retrieve password from STDOUT
  319. """
  320. # TODO: replace with use of self.loader
  321. try:
  322. f = open(filename, "rb")
  323. vault_pass = f.read().strip()
  324. f.close()
  325. except (OSError, IOError) as e:
  326. raise AnsibleError("Could not read vault password file %s: %s" % (filename, e))
  327. b_vault_data, dummy = self.loader._decrypt_if_vault_data(vault_pass, filename)
  328. vault_pass = b_vault_data.strip(b'\r\n')
  329. verify_secret_is_not_empty(vault_pass,
  330. msg='Invalid vault password was provided from file (%s)' % filename)
  331. return vault_pass
  332. def __repr__(self):
  333. if self.filename:
  334. return "%s(filename='%s')" % (self.__class__.__name__, self.filename)
  335. return "%s()" % (self.__class__.__name__)
  336. class ScriptVaultSecret(FileVaultSecret):
  337. def _read_file(self, filename):
  338. if not self.loader.is_executable(filename):
  339. raise AnsibleVaultError("The vault password script %s was not executable" % filename)
  340. command = self._build_command()
  341. stdout, stderr, p = self._run(command)
  342. self._check_results(stdout, stderr, p)
  343. vault_pass = stdout.strip(b'\r\n')
  344. empty_password_msg = 'Invalid vault password was provided from script (%s)' % filename
  345. verify_secret_is_not_empty(vault_pass,
  346. msg=empty_password_msg)
  347. return vault_pass
  348. def _run(self, command):
  349. try:
  350. # STDERR not captured to make it easier for users to prompt for input in their scripts
  351. p = subprocess.Popen(command, stdout=subprocess.PIPE)
  352. except OSError as e:
  353. msg_format = "Problem running vault password script %s (%s)." \
  354. " If this is not a script, remove the executable bit from the file."
  355. msg = msg_format % (self.filename, e)
  356. raise AnsibleError(msg)
  357. stdout, stderr = p.communicate()
  358. return stdout, stderr, p
  359. def _check_results(self, stdout, stderr, popen):
  360. if popen.returncode != 0:
  361. raise AnsibleError("Vault password script %s returned non-zero (%s): %s" %
  362. (self.filename, popen.returncode, stderr))
  363. def _build_command(self):
  364. return [self.filename]
  365. class ClientScriptVaultSecret(ScriptVaultSecret):
  366. VAULT_ID_UNKNOWN_RC = 2
  367. def __init__(self, filename=None, encoding=None, loader=None, vault_id=None):
  368. super(ClientScriptVaultSecret, self).__init__(filename=filename,
  369. encoding=encoding,
  370. loader=loader)
  371. self._vault_id = vault_id
  372. display.vvvv('Executing vault password client script: %s --vault-id %s' % (filename, vault_id))
  373. def _run(self, command):
  374. try:
  375. p = subprocess.Popen(command,
  376. stdout=subprocess.PIPE,
  377. stderr=subprocess.PIPE)
  378. except OSError as e:
  379. msg_format = "Problem running vault password client script %s (%s)." \
  380. " If this is not a script, remove the executable bit from the file."
  381. msg = msg_format % (self.filename, e)
  382. raise AnsibleError(msg)
  383. stdout, stderr = p.communicate()
  384. return stdout, stderr, p
  385. def _check_results(self, stdout, stderr, popen):
  386. if popen.returncode == self.VAULT_ID_UNKNOWN_RC:
  387. raise AnsibleError('Vault password client script %s did not find a secret for vault-id=%s: %s' %
  388. (self.filename, self._vault_id, stderr))
  389. if popen.returncode != 0:
  390. raise AnsibleError("Vault password client script %s returned non-zero (%s) when getting secret for vault-id=%s: %s" %
  391. (self.filename, popen.returncode, self._vault_id, stderr))
  392. def _build_command(self):
  393. command = [self.filename]
  394. if self._vault_id:
  395. command.extend(['--vault-id', self._vault_id])
  396. return command
  397. def __repr__(self):
  398. if self.filename:
  399. return "%s(filename='%s', vault_id='%s')" % \
  400. (self.__class__.__name__, self.filename, self._vault_id)
  401. return "%s()" % (self.__class__.__name__)
  402. def match_secrets(secrets, target_vault_ids):
  403. '''Find all VaultSecret objects that are mapped to any of the target_vault_ids in secrets'''
  404. if not secrets:
  405. return []
  406. matches = [(vault_id, secret) for vault_id, secret in secrets if vault_id in target_vault_ids]
  407. return matches
  408. def match_best_secret(secrets, target_vault_ids):
  409. '''Find the best secret from secrets that matches target_vault_ids
  410. Since secrets should be ordered so the early secrets are 'better' than later ones, this
  411. just finds all the matches, then returns the first secret'''
  412. matches = match_secrets(secrets, target_vault_ids)
  413. if matches:
  414. return matches[0]
  415. # raise exception?
  416. return None
  417. def match_encrypt_vault_id_secret(secrets, encrypt_vault_id=None):
  418. # See if the --encrypt-vault-id matches a vault-id
  419. display.vvvv('encrypt_vault_id=%s' % encrypt_vault_id)
  420. if encrypt_vault_id is None:
  421. raise AnsibleError('match_encrypt_vault_id_secret requires a non None encrypt_vault_id')
  422. encrypt_vault_id_matchers = [encrypt_vault_id]
  423. encrypt_secret = match_best_secret(secrets, encrypt_vault_id_matchers)
  424. # return the best match for --encrypt-vault-id
  425. if encrypt_secret:
  426. return encrypt_secret
  427. # If we specified a encrypt_vault_id and we couldn't find it, dont
  428. # fallback to using the first/best secret
  429. raise AnsibleVaultError('Did not find a match for --encrypt-vault-id=%s in the known vault-ids %s' % (encrypt_vault_id,
  430. [_v for _v, _vs in secrets]))
  431. def match_encrypt_secret(secrets, encrypt_vault_id=None):
  432. '''Find the best/first/only secret in secrets to use for encrypting'''
  433. display.vvvv('encrypt_vault_id=%s' % encrypt_vault_id)
  434. # See if the --encrypt-vault-id matches a vault-id
  435. if encrypt_vault_id:
  436. return match_encrypt_vault_id_secret(secrets,
  437. encrypt_vault_id=encrypt_vault_id)
  438. # Find the best/first secret from secrets since we didnt specify otherwise
  439. # ie, consider all of the available secrets as matches
  440. _vault_id_matchers = [_vault_id for _vault_id, dummy in secrets]
  441. best_secret = match_best_secret(secrets, _vault_id_matchers)
  442. # can be empty list sans any tuple
  443. return best_secret
  444. class VaultLib:
  445. def __init__(self, secrets=None):
  446. self.secrets = secrets or []
  447. self.cipher_name = None
  448. self.b_version = b'1.2'
  449. def encrypt(self, plaintext, secret=None, vault_id=None):
  450. """Vault encrypt a piece of data.
  451. :arg plaintext: a text or byte string to encrypt.
  452. :returns: a utf-8 encoded byte str of encrypted data. The string
  453. contains a header identifying this as vault encrypted data and
  454. formatted to newline terminated lines of 80 characters. This is
  455. suitable for dumping as is to a vault file.
  456. If the string passed in is a text string, it will be encoded to UTF-8
  457. before encryption.
  458. """
  459. if secret is None:
  460. if self.secrets:
  461. dummy, secret = match_encrypt_secret(self.secrets)
  462. else:
  463. raise AnsibleVaultError("A vault password must be specified to encrypt data")
  464. b_plaintext = to_bytes(plaintext, errors='surrogate_or_strict')
  465. if is_encrypted(b_plaintext):
  466. raise AnsibleError("input is already encrypted")
  467. if not self.cipher_name or self.cipher_name not in CIPHER_WRITE_WHITELIST:
  468. self.cipher_name = u"AES256"
  469. try:
  470. this_cipher = CIPHER_MAPPING[self.cipher_name]()
  471. except KeyError:
  472. raise AnsibleError(u"{0} cipher could not be found".format(self.cipher_name))
  473. # encrypt data
  474. if vault_id:
  475. display.vvvvv('Encrypting with vault_id "%s" and vault secret %s' % (vault_id, secret))
  476. else:
  477. display.vvvvv('Encrypting without a vault_id using vault secret %s' % secret)
  478. b_ciphertext = this_cipher.encrypt(b_plaintext, secret)
  479. # format the data for output to the file
  480. b_vaulttext = format_vaulttext_envelope(b_ciphertext,
  481. self.cipher_name,
  482. vault_id=vault_id)
  483. return b_vaulttext
  484. def decrypt(self, vaulttext, filename=None):
  485. '''Decrypt a piece of vault encrypted data.
  486. :arg vaulttext: a string to decrypt. Since vault encrypted data is an
  487. ascii text format this can be either a byte str or unicode string.
  488. :kwarg filename: a filename that the data came from. This is only
  489. used to make better error messages in case the data cannot be
  490. decrypted.
  491. :returns: a byte string containing the decrypted data and the vault-id that was used
  492. '''
  493. plaintext, vault_id, vault_secret = self.decrypt_and_get_vault_id(vaulttext, filename=filename)
  494. return plaintext
  495. def decrypt_and_get_vault_id(self, vaulttext, filename=None):
  496. """Decrypt a piece of vault encrypted data.
  497. :arg vaulttext: a string to decrypt. Since vault encrypted data is an
  498. ascii text format this can be either a byte str or unicode string.
  499. :kwarg filename: a filename that the data came from. This is only
  500. used to make better error messages in case the data cannot be
  501. decrypted.
  502. :returns: a byte string containing the decrypted data and the vault-id vault-secret that was used
  503. """
  504. b_vaulttext = to_bytes(vaulttext, errors='strict', encoding='utf-8')
  505. if self.secrets is None:
  506. raise AnsibleVaultError("A vault password must be specified to decrypt data")
  507. if not is_encrypted(b_vaulttext):
  508. msg = "input is not vault encrypted data"
  509. if filename:
  510. msg += "%s is not a vault encrypted file" % to_native(filename)
  511. raise AnsibleError(msg)
  512. b_vaulttext, dummy, cipher_name, vault_id = parse_vaulttext_envelope(b_vaulttext,
  513. filename=filename)
  514. # create the cipher object, note that the cipher used for decrypt can
  515. # be different than the cipher used for encrypt
  516. if cipher_name in CIPHER_WHITELIST:
  517. this_cipher = CIPHER_MAPPING[cipher_name]()
  518. else:
  519. raise AnsibleError("{0} cipher could not be found".format(cipher_name))
  520. b_plaintext = None
  521. if not self.secrets:
  522. raise AnsibleVaultError('Attempting to decrypt but no vault secrets found')
  523. # WARNING: Currently, the vault id is not required to match the vault id in the vault blob to
  524. # decrypt a vault properly. The vault id in the vault blob is not part of the encrypted
  525. # or signed vault payload. There is no cryptographic checking/verification/validation of the
  526. # vault blobs vault id. It can be tampered with and changed. The vault id is just a nick
  527. # name to use to pick the best secret and provide some ux/ui info.
  528. # iterate over all the applicable secrets (all of them by default) until one works...
  529. # if we specify a vault_id, only the corresponding vault secret is checked and
  530. # we check it first.
  531. vault_id_matchers = []
  532. vault_id_used = None
  533. vault_secret_used = None
  534. if vault_id:
  535. display.vvvvv('Found a vault_id (%s) in the vaulttext' % (vault_id))
  536. vault_id_matchers.append(vault_id)
  537. _matches = match_secrets(self.secrets, vault_id_matchers)
  538. if _matches:
  539. display.vvvvv('We have a secret associated with vault id (%s), will try to use to decrypt %s' % (vault_id, to_text(filename)))
  540. else:
  541. display.vvvvv('Found a vault_id (%s) in the vault text, but we do not have a associated secret (--vault-id)' % (vault_id))
  542. # Not adding the other secrets to vault_secret_ids enforces a match between the vault_id from the vault_text and
  543. # the known vault secrets.
  544. if not C.DEFAULT_VAULT_ID_MATCH:
  545. # Add all of the known vault_ids as candidates for decrypting a vault.
  546. vault_id_matchers.extend([_vault_id for _vault_id, _dummy in self.secrets if _vault_id != vault_id])
  547. matched_secrets = match_secrets(self.secrets, vault_id_matchers)
  548. # for vault_secret_id in vault_secret_ids:
  549. for vault_secret_id, vault_secret in matched_secrets:
  550. display.vvvvv('Trying to use vault secret=(%s) id=%s to decrypt %s' % (vault_secret, vault_secret_id, to_text(filename)))
  551. try:
  552. # secret = self.secrets[vault_secret_id]
  553. display.vvvv('Trying secret %s for vault_id=%s' % (vault_secret, vault_secret_id))
  554. b_plaintext = this_cipher.decrypt(b_vaulttext, vault_secret)
  555. if b_plaintext is not None:
  556. vault_id_used = vault_secret_id
  557. vault_secret_used = vault_secret
  558. file_slug = ''
  559. if filename:
  560. file_slug = ' of "%s"' % filename
  561. display.vvvvv('Decrypt%s successful with secret=%s and vault_id=%s' % (to_text(file_slug), vault_secret, vault_secret_id))
  562. break
  563. except AnsibleVaultFormatError as exc:
  564. msg = "There was a vault format error"
  565. if filename:
  566. msg += ' in %s' % (to_text(filename))
  567. msg += ': %s' % exc
  568. display.warning(msg)
  569. raise
  570. except AnsibleError as e:
  571. display.vvvv('Tried to use the vault secret (%s) to decrypt (%s) but it failed. Error: %s' %
  572. (vault_secret_id, to_text(filename), e))
  573. continue
  574. else:
  575. msg = "Decryption failed (no vault secrets were found that could decrypt)"
  576. if filename:
  577. msg += " on %s" % to_native(filename)
  578. raise AnsibleVaultError(msg)
  579. if b_plaintext is None:
  580. msg = "Decryption failed"
  581. if filename:
  582. msg += " on %s" % to_native(filename)
  583. raise AnsibleError(msg)
  584. return b_plaintext, vault_id_used, vault_secret_used
  585. class VaultEditor:
  586. def __init__(self, vault=None):
  587. # TODO: it may be more useful to just make VaultSecrets and index of VaultLib objects...
  588. self.vault = vault or VaultLib()
  589. # TODO: mv shred file stuff to it's own class
  590. def _shred_file_custom(self, tmp_path):
  591. """"Destroy a file, when shred (core-utils) is not available
  592. Unix `shred' destroys files "so that they can be recovered only with great difficulty with
  593. specialised hardware, if at all". It is based on the method from the paper
  594. "Secure Deletion of Data from Magnetic and Solid-State Memory",
  595. Proceedings of the Sixth USENIX Security Symposium (San Jose, California, July 22-25, 1996).
  596. We do not go to that length to re-implement shred in Python; instead, overwriting with a block
  597. of random data should suffice.
  598. See https://github.com/ansible/ansible/pull/13700 .
  599. """
  600. file_len = os.path.getsize(tmp_path)
  601. if file_len > 0: # avoid work when file was empty
  602. max_chunk_len = min(1024 * 1024 * 2, file_len)
  603. passes = 3
  604. with open(tmp_path, "wb") as fh:
  605. for _ in range(passes):
  606. fh.seek(0, 0)
  607. # get a random chunk of data, each pass with other length
  608. chunk_len = random.randint(max_chunk_len // 2, max_chunk_len)
  609. data = os.urandom(chunk_len)
  610. for _ in range(0, file_len // chunk_len):
  611. fh.write(data)
  612. fh.write(data[:file_len % chunk_len])
  613. # FIXME remove this assert once we have unittests to check its accuracy
  614. if fh.tell() != file_len:
  615. raise AnsibleAssertionError()
  616. os.fsync(fh)
  617. def _shred_file(self, tmp_path):
  618. """Securely destroy a decrypted file
  619. Note standard limitations of GNU shred apply (For flash, overwriting would have no effect
  620. due to wear leveling; for other storage systems, the async kernel->filesystem->disk calls never
  621. guarantee data hits the disk; etc). Furthermore, if your tmp dirs is on tmpfs (ramdisks),
  622. it is a non-issue.
  623. Nevertheless, some form of overwriting the data (instead of just removing the fs index entry) is
  624. a good idea. If shred is not available (e.g. on windows, or no core-utils installed), fall back on
  625. a custom shredding method.
  626. """
  627. if not os.path.isfile(tmp_path):
  628. # file is already gone
  629. return
  630. try:
  631. r = subprocess.call(['shred', tmp_path])
  632. except (OSError, ValueError):
  633. # shred is not available on this system, or some other error occurred.
  634. # ValueError caught because macOS El Capitan is raising an
  635. # exception big enough to hit a limit in python2-2.7.11 and below.
  636. # Symptom is ValueError: insecure pickle when shred is not
  637. # installed there.
  638. r = 1
  639. if r != 0:
  640. # we could not successfully execute unix shred; therefore, do custom shred.
  641. self._shred_file_custom(tmp_path)
  642. os.remove(tmp_path)
  643. def _edit_file_helper(self, filename, secret,
  644. existing_data=None, force_save=False, vault_id=None):
  645. # Create a tempfile
  646. root, ext = os.path.splitext(os.path.realpath(filename))
  647. fd, tmp_path = tempfile.mkstemp(suffix=ext)
  648. os.close(fd)
  649. cmd = self._editor_shell_command(tmp_path)
  650. try:
  651. if existing_data:
  652. self.write_data(existing_data, tmp_path, shred=False)
  653. # drop the user into an editor on the tmp file
  654. subprocess.call(cmd)
  655. except Exception as e:
  656. # whatever happens, destroy the decrypted file
  657. self._shred_file(tmp_path)
  658. raise AnsibleError('Unable to execute the command "%s": %s' % (' '.join(cmd), to_native(e)))
  659. b_tmpdata = self.read_data(tmp_path)
  660. # Do nothing if the content has not changed
  661. if existing_data == b_tmpdata and not force_save:
  662. self._shred_file(tmp_path)
  663. return
  664. # encrypt new data and write out to tmp
  665. # An existing vaultfile will always be UTF-8,
  666. # so decode to unicode here
  667. b_ciphertext = self.vault.encrypt(b_tmpdata, secret, vault_id=vault_id)
  668. self.write_data(b_ciphertext, tmp_path)
  669. # shuffle tmp file into place
  670. self.shuffle_files(tmp_path, filename)
  671. display.vvvvv('Saved edited file "%s" encrypted using %s and vault id "%s"' % (filename, secret, vault_id))
  672. def _real_path(self, filename):
  673. # '-' is special to VaultEditor, dont expand it.
  674. if filename == '-':
  675. return filename
  676. real_path = os.path.realpath(filename)
  677. return real_path
  678. def encrypt_bytes(self, b_plaintext, secret, vault_id=None):
  679. b_ciphertext = self.vault.encrypt(b_plaintext, secret, vault_id=vault_id)
  680. return b_ciphertext
  681. def encrypt_file(self, filename, secret, vault_id=None, output_file=None):
  682. # A file to be encrypted into a vaultfile could be any encoding
  683. # so treat the contents as a byte string.
  684. # follow the symlink
  685. filename = self._real_path(filename)
  686. b_plaintext = self.read_data(filename)
  687. b_ciphertext = self.vault.encrypt(b_plaintext, secret, vault_id=vault_id)
  688. self.write_data(b_ciphertext, output_file or filename)
  689. def decrypt_file(self, filename, output_file=None):
  690. # follow the symlink
  691. filename = self._real_path(filename)
  692. ciphertext = self.read_data(filename)
  693. try:
  694. plaintext = self.vault.decrypt(ciphertext, filename=filename)
  695. except AnsibleError as e:
  696. raise AnsibleError("%s for %s" % (to_native(e), to_native(filename)))
  697. self.write_data(plaintext, output_file or filename, shred=False)
  698. def create_file(self, filename, secret, vault_id=None):
  699. """ create a new encrypted file """
  700. dirname = os.path.dirname(filename)
  701. if dirname and not os.path.exists(dirname):
  702. display.warning("%s does not exist, creating..." % dirname)
  703. makedirs_safe(dirname)
  704. # FIXME: If we can raise an error here, we can probably just make it
  705. # behave like edit instead.
  706. if os.path.isfile(filename):
  707. raise AnsibleError("%s exists, please use 'edit' instead" % filename)
  708. self._edit_file_helper(filename, secret, vault_id=vault_id)
  709. def edit_file(self, filename):
  710. vault_id_used = None
  711. vault_secret_used = None
  712. # follow the symlink
  713. filename = self._real_path(filename)
  714. b_vaulttext = self.read_data(filename)
  715. # vault or yaml files are always utf8
  716. vaulttext = to_text(b_vaulttext)
  717. try:
  718. # vaulttext gets converted back to bytes, but alas
  719. # TODO: return the vault_id that worked?
  720. plaintext, vault_id_used, vault_secret_used = self.vault.decrypt_and_get_vault_id(vaulttext)
  721. except AnsibleError as e:
  722. raise AnsibleError("%s for %s" % (to_native(e), to_native(filename)))
  723. # Figure out the vault id from the file, to select the right secret to re-encrypt it
  724. # (duplicates parts of decrypt, but alas...)
  725. dummy, dummy, cipher_name, vault_id = parse_vaulttext_envelope(b_vaulttext,
  726. filename=filename)
  727. # vault id here may not be the vault id actually used for decrypting
  728. # as when the edited file has no vault-id but is decrypted by non-default id in secrets
  729. # (vault_id=default, while a different vault-id decrypted)
  730. # Keep the same vault-id (and version) as in the header
  731. if cipher_name not in CIPHER_WRITE_WHITELIST:
  732. # we want to get rid of files encrypted with the AES cipher
  733. self._edit_file_helper(filename, vault_secret_used, existing_data=plaintext,
  734. force_save=True, vault_id=vault_id)
  735. else:
  736. self._edit_file_helper(filename, vault_secret_used, existing_data=plaintext,
  737. force_save=False, vault_id=vault_id)
  738. def plaintext(self, filename):
  739. b_vaulttext = self.read_data(filename)
  740. vaulttext = to_text(b_vaulttext)
  741. try:
  742. plaintext = self.vault.decrypt(vaulttext, filename=filename)
  743. return plaintext
  744. except AnsibleError as e:
  745. raise AnsibleVaultError("%s for %s" % (to_native(e), to_native(filename)))
  746. # FIXME/TODO: make this use VaultSecret
  747. def rekey_file(self, filename, new_vault_secret, new_vault_id=None):
  748. # follow the symlink
  749. filename = self._real_path(filename)
  750. prev = os.stat(filename)
  751. b_vaulttext = self.read_data(filename)
  752. vaulttext = to_text(b_vaulttext)
  753. display.vvvvv('Rekeying file "%s" to with new vault-id "%s" and vault secret %s' %
  754. (filename, new_vault_id, new_vault_secret))
  755. try:
  756. plaintext, vault_id_used, _dummy = self.vault.decrypt_and_get_vault_id(vaulttext)
  757. except AnsibleError as e:
  758. raise AnsibleError("%s for %s" % (to_native(e), to_native(filename)))
  759. # This is more or less an assert, see #18247
  760. if new_vault_secret is None:
  761. raise AnsibleError('The value for the new_password to rekey %s with is not valid' % filename)
  762. # FIXME: VaultContext...? could rekey to a different vault_id in the same VaultSecrets
  763. # Need a new VaultLib because the new vault data can be a different
  764. # vault lib format or cipher (for ex, when we migrate 1.0 style vault data to
  765. # 1.1 style data we change the version and the cipher). This is where a VaultContext might help
  766. # the new vault will only be used for encrypting, so it doesn't need the vault secrets
  767. # (we will pass one in directly to encrypt)
  768. new_vault = VaultLib(secrets={})
  769. b_new_vaulttext = new_vault.encrypt(plaintext, new_vault_secret, vault_id=new_vault_id)
  770. self.write_data(b_new_vaulttext, filename)
  771. # preserve permissions
  772. os.chmod(filename, prev.st_mode)
  773. os.chown(filename, prev.st_uid, prev.st_gid)
  774. display.vvvvv('Rekeyed file "%s" (decrypted with vault id "%s") was encrypted with new vault-id "%s" and vault secret %s' %
  775. (filename, vault_id_used, new_vault_id, new_vault_secret))
  776. def read_data(self, filename):
  777. try:
  778. if filename == '-':
  779. data = sys.stdin.read()
  780. else:
  781. with open(filename, "rb") as fh:
  782. data = fh.read()
  783. except Exception as e:
  784. msg = to_native(e)
  785. if not msg:
  786. msg = repr(e)
  787. raise AnsibleError('Unable to read source file (%s): %s' % (to_native(filename), msg))
  788. return data
  789. # TODO: add docstrings for arg types since this code is picky about that
  790. def write_data(self, data, filename, shred=True):
  791. """Write the data bytes to given path
  792. This is used to write a byte string to a file or stdout. It is used for
  793. writing the results of vault encryption or decryption. It is used for
  794. saving the ciphertext after encryption and it is also used for saving the
  795. plaintext after decrypting a vault. The type of the 'data' arg should be bytes,
  796. since in the plaintext case, the original contents can be of any text encoding
  797. or arbitrary binary data.
  798. When used to write the result of vault encryption, the val of the 'data' arg
  799. should be a utf-8 encoded byte string and not a text typ and not a text type..
  800. When used to write the result of vault decryption, the val of the 'data' arg
  801. should be a byte string and not a text type.
  802. :arg data: the byte string (bytes) data
  803. :arg filename: filename to save 'data' to.
  804. :arg shred: if shred==True, make sure that the original data is first shredded so that is cannot be recovered.
  805. :returns: None
  806. """
  807. # FIXME: do we need this now? data_bytes should always be a utf-8 byte string
  808. b_file_data = to_bytes(data, errors='strict')
  809. # get a ref to either sys.stdout.buffer for py3 or plain old sys.stdout for py2
  810. # We need sys.stdout.buffer on py3 so we can write bytes to it since the plaintext
  811. # of the vaulted object could be anything/binary/etc
  812. output = getattr(sys.stdout, 'buffer', sys.stdout)
  813. if filename == '-':
  814. output.write(b_file_data)
  815. else:
  816. if os.path.isfile(filename):
  817. if shred:
  818. self._shred_file(filename)
  819. else:
  820. os.remove(filename)
  821. with open(filename, "wb") as fh:
  822. fh.write(b_file_data)
  823. def shuffle_files(self, src, dest):
  824. prev = None
  825. # overwrite dest with src
  826. if os.path.isfile(dest):
  827. prev = os.stat(dest)
  828. # old file 'dest' was encrypted, no need to _shred_file
  829. os.remove(dest)
  830. shutil.move(src, dest)
  831. # reset permissions if needed
  832. if prev is not None:
  833. # TODO: selinux, ACLs, xattr?
  834. os.chmod(dest, prev.st_mode)
  835. os.chown(dest, prev.st_uid, prev.st_gid)
  836. def _editor_shell_command(self, filename):
  837. env_editor = os.environ.get('EDITOR', 'vi')
  838. editor = shlex.split(env_editor)
  839. editor.append(filename)
  840. return editor
  841. ########################################
  842. # CIPHERS #
  843. ########################################
  844. class VaultAES256:
  845. """
  846. Vault implementation using AES-CTR with an HMAC-SHA256 authentication code.
  847. Keys are derived using PBKDF2
  848. """
  849. # http://www.daemonology.net/blog/2009-06-11-cryptographic-right-answers.html
  850. # Note: strings in this class should be byte strings by default.
  851. def __init__(self):
  852. if not HAS_CRYPTOGRAPHY and not HAS_PYCRYPTO:
  853. raise AnsibleError(NEED_CRYPTO_LIBRARY)
  854. @staticmethod
  855. def _create_key_cryptography(b_password, b_salt, key_length, iv_length):
  856. kdf = PBKDF2HMAC(
  857. algorithm=hashes.SHA256(),
  858. length=2 * key_length + iv_length,
  859. salt=b_salt,
  860. iterations=10000,
  861. backend=CRYPTOGRAPHY_BACKEND)
  862. b_derivedkey = kdf.derive(b_password)
  863. return b_derivedkey
  864. @staticmethod
  865. def _pbkdf2_prf(p, s):
  866. hash_function = SHA256_pycrypto
  867. return HMAC_pycrypto.new(p, s, hash_function).digest()
  868. @classmethod
  869. def _create_key_pycrypto(cls, b_password, b_salt, key_length, iv_length):
  870. # make two keys and one iv
  871. b_derivedkey = PBKDF2_pycrypto(b_password, b_salt, dkLen=(2 * key_length) + iv_length,
  872. count=10000, prf=cls._pbkdf2_prf)
  873. return b_derivedkey
  874. @classmethod
  875. def _gen_key_initctr(cls, b_password, b_salt):
  876. # 16 for AES 128, 32 for AES256
  877. key_length = 32
  878. if HAS_CRYPTOGRAPHY:
  879. # AES is a 128-bit block cipher, so IVs and counter nonces are 16 bytes
  880. iv_length = algorithms.AES.block_size // 8
  881. b_derivedkey = cls._create_key_cryptography(b_password, b_salt, key_length, iv_length)
  882. b_iv = b_derivedkey[(key_length * 2):(key_length * 2) + iv_length]
  883. elif HAS_PYCRYPTO:
  884. # match the size used for counter.new to avoid extra work
  885. iv_length = 16
  886. b_derivedkey = cls._create_key_pycrypto(b_password, b_salt, key_length, iv_length)
  887. b_iv = hexlify(b_derivedkey[(key_length * 2):(key_length * 2) + iv_length])
  888. else:
  889. raise AnsibleError(NEED_CRYPTO_LIBRARY + '(Detected in initctr)')
  890. b_key1 = b_derivedkey[:key_length]
  891. b_key2 = b_derivedkey[key_length:(key_length * 2)]
  892. return b_key1, b_key2, b_iv
  893. @staticmethod
  894. def _encrypt_cryptography(b_plaintext, b_key1, b_key2, b_iv):
  895. cipher = C_Cipher(algorithms.AES(b_key1), modes.CTR(b_iv), CRYPTOGRAPHY_BACKEND)
  896. encryptor = cipher.encryptor()
  897. padder = padding.PKCS7(algorithms.AES.block_size).padder()
  898. b_ciphertext = encryptor.update(padder.update(b_plaintext) + padder.finalize())
  899. b_ciphertext += encryptor.finalize()
  900. # COMBINE SALT, DIGEST AND DATA
  901. hmac = HMAC(b_key2, hashes.SHA256(), CRYPTOGRAPHY_BACKEND)
  902. hmac.update(b_ciphertext)
  903. b_hmac = hmac.finalize()
  904. return to_bytes(hexlify(b_hmac), errors='surrogate_or_strict'), hexlify(b_ciphertext)
  905. @staticmethod
  906. def _encrypt_pycrypto(b_plaintext, b_key1, b_key2, b_iv):
  907. # PKCS#7 PAD DATA http://tools.ietf.org/html/rfc5652#section-6.3
  908. bs = AES_pycrypto.block_size
  909. padding_length = (bs - len(b_plaintext) % bs) or bs
  910. b_plaintext += to_bytes(padding_length * chr(padding_length), encoding='ascii', errors='strict')
  911. # COUNTER.new PARAMETERS
  912. # 1) nbits (integer) - Length of the counter, in bits.
  913. # 2) initial_value (integer) - initial value of the counter. "iv" from _gen_key_initctr
  914. ctr = Counter_pycrypto.new(128, initial_value=int(b_iv, 16))
  915. # AES.new PARAMETERS
  916. # 1) AES key, must be either 16, 24, or 32 bytes long -- "key" from _gen_key_initctr
  917. # 2) MODE_CTR, is the recommended mode
  918. # 3) counter=<CounterObject>
  919. cipher = AES_pycrypto.new(b_key1, AES_pycrypto.MODE_CTR, counter=ctr)
  920. # ENCRYPT PADDED DATA
  921. b_ciphertext = cipher.encrypt(b_plaintext)
  922. # COMBINE SALT, DIGEST AND DATA
  923. hmac = HMAC_pycrypto.new(b_key2, b_ciphertext, SHA256_pycrypto)
  924. return to_bytes(hmac.hexdigest(), errors='surrogate_or_strict'), hexlify(b_ciphertext)
  925. @classmethod
  926. def encrypt(cls, b_plaintext, secret):
  927. if secret is None:
  928. raise AnsibleVaultError('The secret passed to encrypt() was None')
  929. b_salt = os.urandom(32)
  930. b_password = secret.bytes
  931. b_key1, b_key2, b_iv = cls._gen_key_initctr(b_password, b_salt)
  932. if HAS_CRYPTOGRAPHY:
  933. b_hmac, b_ciphertext = cls._encrypt_cryptography(b_plaintext, b_key1, b_key2, b_iv)
  934. elif HAS_PYCRYPTO:
  935. b_hmac, b_ciphertext = cls._encrypt_pycrypto(b_plaintext, b_key1, b_key2, b_iv)
  936. else:
  937. raise AnsibleError(NEED_CRYPTO_LIBRARY + '(Detected in encrypt)')
  938. b_vaulttext = b'\n'.join([hexlify(b_salt), b_hmac, b_ciphertext])
  939. # Unnecessary but getting rid of it is a backwards incompatible vault
  940. # format change
  941. b_vaulttext = hexlify(b_vaulttext)
  942. return b_vaulttext
  943. @classmethod
  944. def _decrypt_cryptography(cls, b_ciphertext, b_crypted_hmac, b_key1, b_key2, b_iv):
  945. # b_key1, b_key2, b_iv = self._gen_key_initctr(b_password, b_salt)
  946. # EXIT EARLY IF DIGEST DOESN'T MATCH
  947. hmac = HMAC(b_key2, hashes.SHA256(), CRYPTOGRAPHY_BACKEND)
  948. hmac.update(b_ciphertext)
  949. try:
  950. hmac.verify(_unhexlify(b_crypted_hmac))
  951. except InvalidSignature as e:
  952. raise AnsibleVaultError('HMAC verification failed: %s' % e)
  953. cipher = C_Cipher(algorithms.AES(b_key1), modes.CTR(b_iv), CRYPTOGRAPHY_BACKEND)
  954. decryptor = cipher.decryptor()
  955. unpadder = padding.PKCS7(128).unpadder()
  956. b_plaintext = unpadder.update(
  957. decryptor.update(b_ciphertext) + decryptor.finalize()
  958. ) + unpadder.finalize()
  959. return b_plaintext
  960. @staticmethod
  961. def _is_equal(b_a, b_b):
  962. """
  963. Comparing 2 byte arrrays in constant time
  964. to avoid timing attacks.
  965. It would be nice if there was a library for this but
  966. hey.
  967. """
  968. if not (isinstance(b_a, binary_type) and isinstance(b_b, binary_type)):
  969. raise TypeError('_is_equal can only be used to compare two byte strings')
  970. # http://codahale.com/a-lesson-in-timing-attacks/
  971. if len(b_a) != len(b_b):
  972. return False
  973. result = 0
  974. for b_x, b_y in zip(b_a, b_b):
  975. if PY3:
  976. result |= b_x ^ b_y
  977. else:
  978. result |= ord(b_x) ^ ord(b_y)
  979. return result == 0
  980. @classmethod
  981. def _decrypt_pycrypto(cls, b_ciphertext, b_crypted_hmac, b_key1, b_key2, b_iv):
  982. # EXIT EARLY IF DIGEST DOESN'T MATCH
  983. hmac_decrypt = HMAC_pycrypto.new(b_key2, b_ciphertext, SHA256_pycrypto)
  984. if not cls._is_equal(b_crypted_hmac, to_bytes(hmac_decrypt.hexdigest())):
  985. return None
  986. # SET THE COUNTER AND THE CIPHER
  987. ctr = Counter_pycrypto.new(128, initial_value=int(b_iv, 16))
  988. cipher = AES_pycrypto.new(b_key1, AES_pycrypto.MODE_CTR, counter=ctr)
  989. # DECRYPT PADDED DATA
  990. b_plaintext = cipher.decrypt(b_ciphertext)
  991. # UNPAD DATA
  992. if PY3:
  993. padding_length = b_plaintext[-1]
  994. else:
  995. padding_length = ord(b_plaintext[-1])
  996. b_plaintext = b_plaintext[:-padding_length]
  997. return b_plaintext
  998. @classmethod
  999. def decrypt(cls, b_vaulttext, secret):
  1000. b_ciphertext, b_salt, b_crypted_hmac = parse_vaulttext(b_vaulttext)
  1001. # TODO: would be nice if a VaultSecret could be passed directly to _decrypt_*
  1002. # (move _gen_key_initctr() to a AES256 VaultSecret or VaultContext impl?)
  1003. # though, likely needs to be python cryptography specific impl that basically
  1004. # creates a Cipher() with b_key1, a Mode.CTR() with b_iv, and a HMAC() with sign key b_key2
  1005. b_password = secret.bytes
  1006. b_key1, b_key2, b_iv = cls._gen_key_initctr(b_password, b_salt)
  1007. if HAS_CRYPTOGRAPHY:
  1008. b_plaintext = cls._decrypt_cryptography(b_ciphertext, b_crypted_hmac, b_key1, b_key2, b_iv)
  1009. elif HAS_PYCRYPTO:
  1010. b_plaintext = cls._decrypt_pycrypto(b_ciphertext, b_crypted_hmac, b_key1, b_key2, b_iv)
  1011. else:
  1012. raise AnsibleError(NEED_CRYPTO_LIBRARY + '(Detected in decrypt)')
  1013. return b_plaintext
  1014. # Keys could be made bytes later if the code that gets the data is more
  1015. # naturally byte-oriented
  1016. CIPHER_MAPPING = {
  1017. u'AES256': VaultAES256,
  1018. }