Skip to main content
PyPi version Python version Code style: Black Read the docs CI workflow

This library is a backend addon for keyring, it provides a backend that manages OSX keychain key-class items.

Supported features:

  • Auto-generating keys via SecKeyCreateRandomKey

  • Importing existing keys of various formats via SecItemAdd

  • Storing keys in keychain, or generating transient keys

  • Storing keys in the secure enclave (T2 chip - ‘TPM’); for code-signed interpreters

  • Limiting key management to specific access groups; for code-signed interpreters

  • Making keys non-extractable, so that the key content could not be retrieved, but only used for signing or encryption

Installation

Run the following (on a darwin machine):

$ pip install keyrings.osx_keychain_keys

Using This Backend

One can utilize this backend both programatically, or from the CLI.

import keyring
from keyrings.osx_keychain_keys.backend import OSXKeychainKeysBackend, OSXKeychainKeyType, OSXKeyChainKeyClassType

backend = OSXKeychainKeysBackend(
    key_type=OSXKeychainKeyType.RSA, # Key type, e.g. RSA, RC, DSA, ...
    key_class_type=OSXKeyChainKeyClassType.Private, # Private key, Public key, Symmetric-key
    key_size_in_bits=4096,
    is_permanent=True, # If set, saves the key in keychain; else, returns a transient key
    use_secure_enclave=False, # Saves the key in the T2 (TPM) chip, requires a code-signed interpreter
    access_group=None, # Limits key management and retrieval to set group, requires a code-signed interpreter
    is_extractable=True # If set, private key is extractable; else, it can't be retrieved, but only operated against
)

keyring.set_keyring(backend)

# If password is not set - a key is generated
keyring.set_password('some-label', 'some-tag', password=None)

# If password is set - it could be a file path to a key to import to keychain
keyring.set_password('some-label', 'some-tag', '/tmp/my-private.key')
# It could also be the key-data itself
keyring.set_password('some-label', 'some-tag', '-----BEGIN RSA PRIVATE KEY----\n....')

# Returns a python-wrapped (using hazmat cryptography lib) private / public key
keyring.get_password('some-label', 'some-tag')

# Deletes a key from keychain
keyring.delete_password('some-label', 'some-tag')

See more examples in keyrings/osx_keychain_keys/examples and keyrings/osx_keychain_keys/tests.

Command-line Utility

One can also use the keyring CLI to operate against this backend:

$ keyring -b keyrings.osx_keychain_keys.backend.OSXKeychainKeysBackend set "some-label" "some-tag"

Security Considerations

Using mac’s keychain has some caveats that should be noted, namely:

  • Some keychain APIs require the invoking application (the python interpreter, in this case) to be code-signed with specific Apple entitlements, namely:

    • Saving the key to the secure enclave (T2 / TPM chip)

    • Limiting access via access controls (i.e. requiring touch-id / password before key retrieval)

    • Limiting key management to specific access groups

  • By default, all inserted keys are accessible to the runnable executable, meaning the interpreter you use can manage the generated or imported keys.

    If you use a virtualenv, you may create one with $> venv --copies to limit accessibility to the specific venv python binary.

Making Releases

A CI/CD pipeline is setup on github - once a PR is merged to master, a pre-release will be automatically deployed to github; When a release is tagged, it will be automatically deployed to pypi.

Running Tests

To run the tests locally (a darwin machine is required), install and invoke tox.

Release files for keyrings.osx-keychain-keys 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for keyrings.osx-keychain-keys 1.0.1
File Size Uploaded
keyrings.osx_keychain_keys-1.0.1.tar.gz 22.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for keyrings.osx-keychain-keys 1.0.1
File Interpreter ABI Platform
keyrings.osx_keychain_keys-1.0.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 39.5 kB

Release files / keyrings.osx_keychain_keys-1.0.1.tar.gz

Download URL keyrings.osx_keychain_keys-1.0.1.tar.gz
Size 22.1 kB
Tags Source
SHA-256 checksum
How to use checksums
f3e6a4bcf0ed708e97d2b8cabd161b1519900752a6aa47cd2b5dbc5f59a91dab
BLAKE2b-256 checksum
How to use checksums
8546a5d1b57d7680e9c89383cbc618d86394816d9b341b4c5079c758af82f9af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.6

Release files / keyrings.osx_keychain_keys-1.0.1-py2.py3-none-any.whl

Download URL keyrings.osx_keychain_keys-1.0.1-py2.py3-none-any.whl
Size 17.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
b1c57ddeddaa9eb271c8ec872df94dfc3941c435baacccafcd262ed701631f5f
BLAKE2b-256 checksum
How to use checksums
4cb2e6693b8c81b644c1d491b37c7a89f17313cc1553bfb8e091b57fbaf36d45
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.6

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page