Skip to content
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

Caddy PKI without Root key #6290

Closed
apollo13 opened this issue May 2, 2024 · 11 comments · Fixed by #6298
Closed

Caddy PKI without Root key #6290

apollo13 opened this issue May 2, 2024 · 11 comments · Fixed by #6298
Labels
discussion 💬 The right solution needs to be found feature ⚙️ New feature or request

Comments

@apollo13
Copy link
Contributor

apollo13 commented May 2, 2024

Hi, according to the docs the caddy pki can be configured like this:

{
	pki {
		ca local {
			root {
				format pem_file
				cert /path/to/root.pem
				key /path/to/root.key
			}
			intermediate {
				format pem_file
				cert /path/to/intermediate.pem
				key /path/to/intermediate.key
			}
		}
	}
}

if I am solely using the intermediate to issue certificates, then there is no reason to supply the root key. Would it be possible to allow configuration of caddy without a root key?

@mholt
Copy link
Member

mholt commented May 2, 2024

If there is no root, how can there be an intermediate? 🤔 I guess I don't fully understand what you're asking.

@apollo13
Copy link
Contributor Author

apollo13 commented May 2, 2024 via email

@francislavoie
Copy link
Member

As a workaround, you could set your intermediate as the root and then set sign_with_root.

@apollo13
Copy link
Contributor Author

apollo13 commented May 2, 2024 via email

@francislavoie
Copy link
Member

Oh, yeah I think it wouldn't.

Also I just realized we never wired up sign_with_root via Caddyfile for the acme_server handler, it's only available via JSON config.

@mholt
Copy link
Member

mholt commented May 2, 2024

Ok, gotcha, so the root exists, you're talking about just not loading it into the config.

We could probably work on making that possible.

@mholt mholt added feature ⚙️ New feature or request discussion 💬 The right solution needs to be found labels May 2, 2024
@apollo13 apollo13 changed the title Caddy PKI without Root cert Caddy PKI without Root key May 3, 2024
@apollo13
Copy link
Contributor Author

apollo13 commented May 3, 2024

This simple diff makes it work for me.

diff --git a/modules/caddypki/crypto.go b/modules/caddypki/crypto.go
index 386ce629..24c04686 100644
--- a/modules/caddypki/crypto.go
+++ b/modules/caddypki/crypto.go
@@ -78,18 +78,21 @@ func (kp KeyPair) Load() (*x509.Certificate, crypto.Signer, error) {
                if err != nil {
                        return nil, nil, err
                }
-               keyData, err := os.ReadFile(kp.PrivateKey)
-               if err != nil {
-                       return nil, nil, err
-               }
-
                cert, err := pemDecodeSingleCert(certData)
                if err != nil {
                        return nil, nil, err
                }
-               key, err := certmagic.PEMDecodePrivateKey(keyData)
-               if err != nil {
-                       return nil, nil, err
+
+               var key crypto.Signer = nil
+               if kp.PrivateKey != "" {
+                       keyData, err := os.ReadFile(kp.PrivateKey)
+                       if err != nil {
+                               return nil, nil, err
+                       }
+                       key, err = certmagic.PEMDecodePrivateKey(keyData)
+                       if err != nil {
+                               return nil, nil, err
+                       }
                }
 
                return cert, key, nil

Now I was able to configure the acme server like this:

{
        http_port 8080
        https_port 8443

        skip_install_trust
	pki {
		ca caddy {
			name caddy
		}
		ca local {
			root {
				format pem_file
				cert /home/florian/sources/caddy/certs/ca.pem
			}
			intermediate {
				format pem_file
				cert /home/florian/sources/caddy/certs/intermediate.crt
				key /home/florian/sources/caddy/certs/intermediate.pem
			}
		}
	}
}

localhost {
	tls {
		issuer internal {
			ca caddy
		}
	}
	acme_server {
		ca local
	}
}

@mholt
Copy link
Member

mholt commented May 3, 2024

@apollo13 I think that would be okay. Want to submit a PR for review?

@apollo13
Copy link
Contributor Author

apollo13 commented May 3, 2024 via email

@mholt
Copy link
Member

mholt commented May 3, 2024

Sure, we can help out. (Might just take us some time, heh, quite busy!)

could I write a plugin that would call out to another CA to issue the certs instead of providing caddy with the private keys?

The pki app's job is literally to generate signing keys and issue certs. If your sites need certs from another CA, you can configure cert issuers: https://caddyserver.com/docs/caddyfile/options#cert-issuer

@apollo13
Copy link
Contributor Author

apollo13 commented May 3, 2024

Sure, we can help out. (Might just take us some time, heh, quite busy!)

No worries.

If your sites need certs from another CA, you can configure cert issuers: https://caddyserver.com/docs/caddyfile/options#cert-issuer

But those cannot be used from acme_server. Think of it like having your CA on a yubikey and then issuing certs via acme_server for the other caddy instances.

apollo13 added a commit to apollo13/caddy that referenced this issue May 3, 2024
@mholt mholt linked a pull request May 7, 2024 that will close this issue
mholt added a commit that referenced this issue May 7, 2024
* Allow usage of root CA without a key. Fixes #6290

* Update modules/caddypki/crypto.go

---------

Co-authored-by: Matt Holt <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion 💬 The right solution needs to be found feature ⚙️ New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants