Skip to content

Add a PKINIT client implementing RFC 4556, RFC 5349, RFC 4557 and RFC 8070 - #2260

Merged
anadrianmanrique merged 10 commits into
fortra:masterfrom
azoxlpf:feat/add-pkinit-rfc4556
Oct 28, 2026
Merged

anadrianmanrique merged 10 commits into
fortra:masterfrom
azoxlpf:feat/add-pkinit-rfc4556

Conversation

@azoxlpf

@azoxlpf azoxlpf commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Well, I think this is the biggest PR I have ever opened. It has been about six months of reading RFCs, staring at Wireshark, and quietly enjoying myself far more than is reasonable for someone decoding ASN.1 by hand 😆

Here is the thing that started it. Every PKINIT implementation I could find, does PKINIT and then immediately follows it with a U2U exchange to pull the account NT hash out of the PAC, and authenticates with that hash. It is a perfectly good trick: you get an NT hash, which is useful on its own, and the code is short. But it is not what RFC 4556 actually describes. The whole point of PKINIT is that the AS reply key comes out of the certificate exchange itself, so the TGT you get back is a normal, directly usable TGT. No U2U, no NT hash, no second round trip.

So this PR implements the real thing.

I tried to reference the RFCs as much as I could throughout the code, section by section, so that any check or any odd looking piece of encoding can be traced back to the paragraph it comes from without having to take my word for it.

Disclosure: Claude Opus 4.6 helped me understand some parts of the specification,find and fix bugs in my code, and build the test suite.

  • getTGT with pfx :

image

  • getTGT with crt :
image
  • KDC certificate validation, with the right CA and with a wrong one :
image
  • ECDH key agreement, P-384 :
image
  • Diffie-Hellman with MODP group 16 :
image

@ThatTotallyRealMyth

ThatTotallyRealMyth commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Dang I implemented this a few months ago https://github.com/ThatTotallyRealMyth/impacket/tree/add-pku2u-protocol while waiting for the wrap up of #2213 : ( but your implementation is defo better and more complete so kudos :3

@anadrianmanrique anadrianmanrique self-assigned this Aug 21, 2026
@anadrianmanrique anadrianmanrique added the enhancement Implemented features can be improved or revised label Aug 21, 2026
@anadrianmanrique anadrianmanrique added the high High priority item label Sep 26, 2026
@azoxlpf
azoxlpf force-pushed the feat/add-pkinit-rfc4556 branch from b6bc48c to f524ea9 Compare October 10, 2026 16:50
@azoxlpf azoxlpf changed the title Add a PKINIT client implementing RFC 4556, RFC 5349 and RFC 4557 Add a PKINIT client implementing RFC 4556, RFC 5349, RFC 4557 and RFC 8070 Oct 17, 2026
@mpgn

mpgn commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Tested on goad lab, all good, @anadrianmanrique if you have time on this one 🙏

Every ✅ below is a TGT that I then used for a TGS-REQ and an SMB login (including the referral to the child domain):

Case Result
-cert-pfx, -cert-pem/-key-pem (also an encrypted key), PFX with a password, legacy RC2-40 PFX, PFX with chain ✅
No username (UPN fallback), ESC1 cert of a child domain user against the child DC, machine account cert (DNS SAN only) ✅
-dh-group 2/5/14/15/16, -key-exchange ecdh P-256/P-384/P-521, -service HOST/... ✅
-ca-cert with the real CA ✅

@mpgn

mpgn commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

I've checked the new commits, but I think I found a problem with -ca-cert: with a DER file, the KDC validation is silently skipped.

readPEMCertificates() only reads PEM blocks, so a DER CA file gives an empty list and verifyKDCCertificate() behaves as if -ca-cert was not given. The CA file AD CS publishes (CertEnroll\<host>_<CA>.crt) is DER, and a fake CA in DER is accepted too (GOAD, e931780):

$ openssl x509 -in fakeca_other.pem -outform DER -out fakeca_other.der
$ getTGT.py -cert-pfx jaime.pfx -ca-cert fakeca_other.der -dc-ip 10.4.10.10 sevenkingdoms.local/jaime.lannister -debug
[+] Trying to connect to KDC at 10.4.10.10:88
[+] KDC certificate has no id-pkinit-san SAN, accepted on its extended key usage (1.3.6.1.5.5.7.3.2, 1.3.6.1.5.5.7.3.1)
[+] No trusted CA certificate given, skipping the KDC certification path validation
[*] Saving ticket in jaime.lannister.ccache

Suggested fix (fail closed with a clear message):

             with open(options.ca_cert, 'rb') as fd:
                 trustedCAs = readPEMCertificates(fd.read())
+            if not trustedCAs:
+                logging.critical('-ca-cert %s is not a PEM file (DER is not supported, convert it with: '
+                                 'openssl x509 -inform DER -in %s -out ca.pem)' % (options.ca_cert, options.ca_cert))
+                sys.exit(1)
$ getTGT.py -cert-pfx jaime.pfx -ca-cert fakeca_other.der -dc-ip 10.4.10.10 sevenkingdoms.local/jaime.lannister
[-] -ca-cert fakeca_other.der is not a PEM file (DER is not supported, convert it with: openssl x509 -inform DER -in fakeca_other.der -out ca.pem)

Another option would be to accept DER in -ca-cert too, but I haven't tested it.

@azoxlpf

azoxlpf commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

I've checked the new commits, but I think I found a problem with -ca-cert: with a DER file, the KDC validation is silently skipped.

readPEMCertificates() only reads PEM blocks, so a DER CA file gives an empty list and verifyKDCCertificate() behaves as if -ca-cert was not given. The CA file AD CS publishes (CertEnroll\<host>_<CA>.crt) is DER, and a fake CA in DER is accepted too (GOAD, e931780):

$ openssl x509 -in fakeca_other.pem -outform DER -out fakeca_other.der
$ getTGT.py -cert-pfx jaime.pfx -ca-cert fakeca_other.der -dc-ip 10.4.10.10 sevenkingdoms.local/jaime.lannister -debug
[+] Trying to connect to KDC at 10.4.10.10:88
[+] KDC certificate has no id-pkinit-san SAN, accepted on its extended key usage (1.3.6.1.5.5.7.3.2, 1.3.6.1.5.5.7.3.1)
[+] No trusted CA certificate given, skipping the KDC certification path validation
[*] Saving ticket in jaime.lannister.ccache

Suggested fix (fail closed with a clear message):

             with open(options.ca_cert, 'rb') as fd:
                 trustedCAs = readPEMCertificates(fd.read())
+            if not trustedCAs:
+                logging.critical('-ca-cert %s is not a PEM file (DER is not supported, convert it with: '
+                                 'openssl x509 -inform DER -in %s -out ca.pem)' % (options.ca_cert, options.ca_cert))
+                sys.exit(1)
$ getTGT.py -cert-pfx jaime.pfx -ca-cert fakeca_other.der -dc-ip 10.4.10.10 sevenkingdoms.local/jaime.lannister
[-] -ca-cert fakeca_other.der is not a PEM file (DER is not supported, convert it with: openssl x509 -inform DER -in fakeca_other.der -out ca.pem)

Another option would be to accept DER in -ca-cert too, but I haven't tested it.

Thanks! It’s fixed.

@mpgn

mpgn commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Retested on GOAD, LGTM 👍

@anadrianmanrique

Copy link
Copy Markdown
Collaborator

Malformed KDC replies containing oversized ASN.1 lengths could raise an uncaught OverflowError with pyasn1 0.6.1, which was still permitted by the project’s dependency requirements. CI
passed because it used pyasn1 0.6.4, which included the upstream fix introduced in 0.6.3.

OverflowError was added to PARSE_ERRORS so that these malformed inputs would consistently raise PKINITError. A deterministic regression test was also added.

@anadrianmanrique

Copy link
Copy Markdown
Collaborator

RSA PKINIT is exposed in getTGT.py, but the workflow is not complete

Test target:

• Windows Server 2022 DC

The stock PR was tested with:

python3 examples/getTGT.py
-debug
-cert-pem <CLIENT_CERT>
-key-pem <CLIENT_KEY>
-ca-cert <CA_CERT>
-key-exchange rsa
-dc-ip xx.xx.xx.xx
domain.local/user

The RSA workflow fails at three consecutive points.

  1. Windows rejects the initial RSA request

    The client only advertises Kerberos enctypes AES256, AES128 and RC4—(18, 17, 23)—at pkinit.py lines 952–956 (https://github.com/azoxlpf/impacket/blob/54269e18bc6049b84370058eed8d8583f5dfbc6f/impacket/krb5/pkinit.py#L952). That list is copied directly into the AS-REQ at pkinit.py line 1030 (https://github.com/azoxlpf/impacket/blob/54269e18bc6049b84370058eed8d8583f5dfbc6f/impacket/krb5/pkinit.py#L1030).

The tested Windows KDC returned:

 KDC_ERR_ETYPE_NOSUPP

The RSA request also needs to advertise CMS identifiers (13, 15, 11) for RSA key transport, 3DES content encryption and RSA/SHA-1 signatures.

  1. The RSA response is parsed with the wrong CMS structure

After the CMS identifiers were added diagnostically, Windows returned PA-PK-AS-REP.encKeyPack. The client decrypts it and passes the result directly to verifySignedData() a pkinit.py lines 1261–1262 (https://github.com/azoxlpf/impacket/blob/54269e18bc6049b84370058eed8d8583f5dfbc6f/impacket/krb5/pkinit.py#L1261).
The decrypted value contains the contents of SignedData, but verifySignedData() expects a complete ContentInfo. Parsing therefore failed with:

 Could not parse the CMS ContentInfo:
 INTEGER tag received where an OBJECT IDENTIFIER was expected
  1. Windows checksum type 14 is rejected

After correcting the CMS parsing boundary diagnostically, processing reached ReplyKeyPack.asChecksum. Windows returned checksum type 14, but the implementation only accepts the checksum mapped from the reply-key enctype at pkinit.py lines 1272–1279 (https://github.com/azoxlpf/impacket/blob/54269e18bc6049b84370058eed8d8583f5dfbc6f/impacket/krb5/pkinit.py#L1272).

Processing stopped with:

 The asChecksum uses checksum type 14,
 which is not the required one for the reply key

Why the tests do not catch this

The mock KDC builds the RSA response at test_pkinit.py lines 251–300 (https://github.com/azoxlpf/impacket/blob/54269e18bc6049b84370058eed8d8583f5dfbc6f/tests/misc/test_pkinit.py#L251). It encrypts the complete ContentInfo returned by buildSignedData(), rather than the SignedData value used by the real Windows exchange.

The RSA tests at test_pkinit.py lines 689–698 (https://github.com/azoxlpf/impacket/blob/54269e18bc6049b84370058eed8d8583f5dfbc6f/tests/misc/test_pkinit.py#L689) also do not reproduce Windows’ CMS negotiation or checksum-type-14 behavior. As a result, the tests pass even though the public RSA workflow fails against Microsoft AD.

@anadrianmanrique

Copy link
Copy Markdown
Collaborator

Changes:

• getTGT.py (examples/getTGT.py:126): clarified RSA/CMS help text; no diagnostic option was added.

• pkinit.py (impacket/krb5/pkinit.py:101):
• Automatically advertises CMS identifiers (13, 15, 11) for RSA.
• Reconstructs SignedData correctly before CMS verification.
• Verifies Windows checksum type 14 using SHA-1 over the AS-REQ.

• test_pkinit.py (tests/misc/test_pkinit.py:61):
• Corrected the mock KDC’s RSA wire encoding.
• Tests RSA CMS advertisement.
• Tests valid and invalid Windows type-14 checksums.

@anadrianmanrique

Copy link
Copy Markdown
Collaborator

The commit updated PKINIT certificate-path validation so that every matching trust anchor and intermediate certificate is evaluated instead of selecting the first certificate with a matching subject name.
This prevents valid KDC certificates from being rejected when a CA bundle contains multiple same-subject certificates with different keys, such as after CA renewal.

@anadrianmanrique

Copy link
Copy Markdown
Collaborator

regression on GOAD for regular cert authentication, shadow creds and adcs wf passed ok. PR is ready to be merged now.
This is a great new feature, great work @azoxlpf ! thanks!

@anadrianmanrique
anadrianmanrique merged commit 40b1eb7 into fortra:master Oct 28, 2026
8 checks passed
@azoxlpf
azoxlpf deleted the feat/add-pkinit-rfc4556 branch October 28, 2026 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Implemented features can be improved or revised high High priority item

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants