-
Notifications
You must be signed in to change notification settings - Fork 207
Thumbprint for certificate made optional #835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,25 +66,27 @@ def _str2bytes(raw): | |
except: | ||
return raw | ||
|
||
def _extract_cert_and_thumbprints(private_key, cert): | ||
# Cert concepts https://security.stackexchange.com/a/226758/125264 | ||
from cryptography.hazmat.primitives import hashes, serialization | ||
cert_pem = cert.public_bytes(encoding=serialization.Encoding.PEM).decode() | ||
x5c = [ | ||
'\n'.join(cert_pem.splitlines()[1:-1]) | ||
] | ||
sha256_thumbprint = cert.fingerprint(hashes.SHA256()).hex() | ||
sha1_thumbprint = cert.fingerprint(hashes.SHA1()).hex() | ||
return private_key, sha256_thumbprint, sha1_thumbprint, x5c | ||
|
||
def _parse_pfx(pfx_path, passphrase_bytes): | ||
# Cert concepts https://security.stackexchange.com/a/226758/125264 | ||
from cryptography.hazmat.primitives import hashes, serialization | ||
from cryptography.hazmat.primitives.serialization import pkcs12 | ||
with open(pfx_path, 'rb') as f: | ||
private_key, cert, _ = pkcs12.load_key_and_certificates( # cryptography 2.5+ | ||
# https://cryptography.io/en/latest/hazmat/primitives/asymmetric/serialization/#cryptography.hazmat.primitives.serialization.pkcs12.load_key_and_certificates | ||
f.read(), passphrase_bytes) | ||
if not (private_key and cert): | ||
raise ValueError("Your PFX file shall contain both private key and cert") | ||
cert_pem = cert.public_bytes(encoding=serialization.Encoding.PEM).decode() # cryptography 1.0+ | ||
x5c = [ | ||
'\n'.join(cert_pem.splitlines()[1:-1]) # Strip the "--- header ---" and "--- footer ---" | ||
] | ||
sha256_thumbprint = cert.fingerprint(hashes.SHA256()).hex() # cryptography 0.7+ | ||
sha1_thumbprint = cert.fingerprint(hashes.SHA1()).hex() # cryptography 0.7+ | ||
# https://cryptography.io/en/latest/x509/reference/#x-509-certificate-object | ||
return private_key, sha256_thumbprint, sha1_thumbprint, x5c | ||
return _extract_cert_and_thumbprints(private_key, cert) | ||
|
||
|
||
def _load_private_key_from_pem_str(private_key_pem_str, passphrase_bytes): | ||
|
@@ -306,7 +308,7 @@ def __init__( | |
|
||
{ | ||
"private_key": "...-----BEGIN PRIVATE KEY-----... in PEM format", | ||
"thumbprint": "A1B2C3D4E5F6...", | ||
"thumbprint": "A1B2C3D4E5F6...", (Optinal, if not provided, MSAL will calculate it. Added in version 1.34.0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you update the doc at line 288 as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bgavrilMS i'm not sure if it will work without public cert passed as we use it to calculate x5c, maybe method _extract_cert_and_thumbprints can be updated to check if public cert is passed and if not x5c will be set to None? |
||
"public_certificate": "...-----BEGIN CERTIFICATE-----...", | ||
"passphrase": "Passphrase if the private_key is encrypted (Optional. Added in version 1.6.0)", | ||
} | ||
|
@@ -815,6 +817,15 @@ def _build_client(self, client_credential, authority, skip_regional_client=False | |
passphrase_bytes) | ||
if client_credential.get("public_certificate") is True and x5c: | ||
headers["x5c"] = x5c | ||
elif (client_credential.get("private_key") | ||
and client_credential.get("public_certificate") | ||
and not client_credential.get("thumbprint")): # in case user does not pass thumbprint but only certificate and private key | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vi7us , so this implementation only works for the Subject Name/Issuer (SNI) scenario. Why not also make it work for non-SNI? Besides, this implementation does not seem to handle the passphrase in the Also, in case you are not aware of, currently MSAL Python's There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I know it is possible to use .pfx file but that means it must be saved locally which is not what we were suggested (as per email thread). For the
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vi7us , thanks for continuing with this PR!
|
||
private_key, sha256_thumbprint, sha1_thumbprint, x5c =( | ||
_extract_cert_and_thumbprints( | ||
client_credential['private_key'], | ||
client_credential['public_certificate'])) | ||
if x5c: | ||
headers["x5c"] = x5c | ||
elif ( | ||
client_credential.get("private_key") # PEM blob | ||
and client_credential.get("thumbprint")): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
""" | ||
|
||
# The __init__.py will import this. Not the other way around. | ||
__version__ = "1.33.0b1" | ||
__version__ = "1.34.0b1" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also revert this change. We will only bump version number when we cut a release, and decide the actual number at that time. Besides, bumping version number in each feature PR brings complications in case we would end up reverting the feature PR. |
||
SKU = "MSAL.Python" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought you said (in PR 833) that it is not fully deprecated. We will need to settle on the wording, one way or another.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated as per request
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rayluo - if the CX provides the public certificate, then thumbprint is not needed? MSAL can always compute it? In that case the property becomes indeed deprecated. Internally, MSAL will still continue to use SHA1 for ADFS.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I am not mistaken, the thumbprint is a hash of the private key, not the public certificate. So, whether the customer provides a public certificate (to use SNI feature), is irrelevant.
But you are right that MSAL could treat the absence of the thumbprint parameter as "compute the hash automatically". We may still want to keep the old
thumbprint="an sha-1 hash"
usage for ADFS, because MSAL historically just uses a magic string "https://contoso.com /adfs" to detect ADFS mode, and that might not always be reliable. Better keep the oldthumbprint="an sha-1 hash"
usage, so that ADFS customer may still opt in to use SHA-1 when needed.