Commit Graph

2023 Commits

Author SHA1 Message Date
Nicolas Williams
4f0249cd94 hx509/kdc: Move KDC CA utility function into hx509
This is part of the program to move REST services like bx509d out of
kdc/.
2020-09-08 00:25:24 -05:00
Nicolas Williams
fdc13c4aac Fix switch fallthrough warnings/errors 2020-09-07 22:04:59 -05:00
Nicolas Williams
7d50445d1b Generic: Fix warnings (fallthrough mosty) 2020-09-07 22:04:59 -05:00
Nicolas Williams
d15b39dd14 hx509: Add log/warn/debug dest set functions 2020-09-07 22:04:59 -05:00
Nicolas Williams
af9bb46a78 hx509: Add hx509_enomem() 2020-09-07 22:04:59 -05:00
Jeffrey Altman
226a3ba7d7 hx509: hx509_context_free do not leak configuration files
hx509_context_free() must call heim_config_file_free() on
the hx509_context.cf section binding.  Otherwise the memory
is leaked.

Change-Id: Ib3350a5be67203904fc1aee727c342c3ed552978
2020-06-29 11:40:48 -04:00
Jeffrey Altman
aa47b5f1a3 Don't override the PATH_SEP definition if present
PATH_SEP is declared on Windows to be ";" and not ":"
by include/config.h.w32.

lib/base/context.c and lib/hx509.c must not override an existing
setting.  Otherwise, file lists cannot be separated and will be
treated as a single file name.

Change-Id: I5521188faca36e41fbae95fbb8942970eab261c8
2020-05-26 11:48:45 -05:00
Nicolas Williams
d6e9584d6c hx509: Fix hx509_context_free() leak 2020-04-24 16:02:35 -05:00
Nicolas Williams
679bcb6872 hx509: Add hx509.conf support
Just like krb5.conf, but hx509.conf, with all the same default locations
on Windows, OS X, and elsewhere, and HX509_CONFIG as the environment
variable equivalent of KRB5_CONFIG.
2020-04-24 16:02:33 -05:00
Nicolas Williams
64d5f86ec3 Fix warnings (some bugs, some spurious)
Many spurious VC warnings not quieted though.
2020-03-12 21:02:09 -05:00
Nicolas Williams
bc9054d4b5 hx509: Better handle OpenSSL diffs in test_req 2019-12-20 16:14:00 -06:00
Nicolas Williams
73a9c95b7b hx509: Avoid double-free on CSR parse error 2019-12-20 15:50:22 -06:00
Nicolas Williams
e515745996 hx509: private key exclusion options
Add two ways to exclude private keys when dealing with an hx509
certificate store.  One as a load option (load no private keys, never
add private keys), one as a store option (store no private keys).

This is useful for CA code so it can have a single store with the
issuer's credentials _and_ the chain for it, and copy those to a store
with the issued certificate and _not_ accidentally include the issuer's
private key.

It would be much safer still to flip the default for this flag, but that
could break out-of-tree libhx509 dependents.
2019-12-09 18:10:10 -06:00
Nicolas Williams
3c0d1258ce hx509: Fix unauthorized feature accounting 2019-12-09 18:10:10 -06:00
Nicolas Williams
a7a1d798c3 hx509: keep track of authorized CSR features
This commit adds a few functions for marking KU, EKUs, and SANs as
authorized, and for getting a count of unsupported certificate
extensions requested, and a count of authorized KU/EKUs/SANs.

The intent is to make it easier to build CSR authorization and CA code
that is robust in the face of future support for certificate extensions
and SAN types not currently supported.  An application could parse a
CSR, iterate all KU/EKUs/SANs, check a subject's authorization to them,
mark them authorized where authorized, then check if there are any
remaining unauthorized extensions or unsupported extensions requested.

Ultimately, if a CSR's KU/EKUs/SANs are all authorized, then they can
all be copied to a TBS, and a certificate can be issued.
2019-12-04 21:34:37 -06:00
Nicolas Williams
d519094117 hxtool: fix leak 2019-12-04 13:40:28 -06:00
Nicolas Williams
4f8577a988 hxtool: add cert type: https-negotiate-server 2019-12-04 13:40:28 -06:00
Roland C. Dowdeswell
cd297eb462 Declare variables at the beginning of a block.
Looks like this fixes the Appveyor build.
2019-11-20 18:14:44 -05:00
Nicolas Williams
4500a14f95 hx509: Show query expression parse errors 2019-11-19 23:00:41 -06:00
Nicolas Williams
944eae82cf asn1: Remove alias of id-kp-clientAuth 2019-11-19 23:00:41 -06:00
Nicolas Williams
fce3f16859 hx509: Add hx509_ca_tbs_get_name()
This is so we can check if a TBS gets an empty subject name, then refuse
to issue the certificate if it doesn't also have at least one SAN.
2019-11-06 19:51:21 -06:00
Nicolas Williams
9ca5d710f9 hx509: fix hx509_request_get_eku() 2019-11-04 14:11:40 -06:00
Nicolas Williams
66cde3e580 hx509: Fix hx509_request_get_exts() 2019-11-02 18:49:42 -05:00
Nicolas Williams
35c91324ed hx509: Add hx509_get_instance() 2019-11-02 18:49:42 -05:00
Nicolas Williams
427751a204 hxtool: Add "acert" (assert cert contents) command
This will prove useful in testing kx509.
2019-11-02 18:49:42 -05:00
Nicolas Williams
6612090ba0 hx509: Export missing symbols 2019-11-02 18:49:42 -05:00
Nicolas Williams
ddbc36d86b hx509: Store priv keys first in PEM stores
Most consumers of PEM files don't care about the order in which private
keys and certificates are stored.  However, Postfix does care when
multiple EE certs (and chains) are stored in a file, in which case it
requires that private keys come before their certificates.
2019-11-02 18:49:42 -05:00
Nicolas Williams
8af2d79d35 hx509: Add missing CSR extension request support
This is necessary in order to add proper support for CSRs in kx509,
where the KDC can examine all requested KUs/EKUs/SANs, check
authorization, and issue a certificate with all those extensions if
authorized.

This is the convention used by OpenSSL, of encoding all the KU, EKUs,
and SANs being requested as Extensions as they would appear in the
TBSCertificate, then putting those in as a single Attribute in the CSR's
Attributes list with attribute OID {id-pkcs-9, 14}.

 - expose all hx509_request_*() functions
 - finish support in hx509_request_parse*() for KU, EKU, and SAN CSR
   attributes
 - finish support in hx509_request_to_pkcs10() for encoding all
   requested KU, EKU, and SAN extensions as a CSR extReq (extension request)
 - add hx509_request_add_*() support for:
    - id-pkinit-san and ms-upn-pkinit-san
    - XMPP (Jabber) SAN
    - registeredID (useless but trivial)
 - add hxtool request-create options for all supported SANs
 - add hxtool request-create options for KeyUsage
 - add hxtool request-create options for ExtKeyUsage
 - add hxtool request-print support for all these things
 - fix bugs in existing id-pkinit-san handling

Possible future improvements

 - add HX509_TRACE env var and support (it would be nice to be able to
   observe why some certificate is rejected, or not matched in a query)
 - add testing that CSR creating and printing round-trip for all KUs,
   EKUs, and SANs
   (probably in tests/kdc/check-pkinit.in)
 - add testing that OpenSSL can print a CSR made by hxtool and
   vice-versa
 - hxtool ca: add KU sanity checking (via hx509_ca_sign() and/or friends)
   (don't allow encrypt for signing-only algs)
   (don't allow encrypt for RSA at all, or for RSA with small e exponents)
 - hxtool request-print: warn about all unknown attributes and
   extensions
 - hxtool ca: MAYBE add support for adding requested extensions from the
   --req=CSR
   ("Maybe" because CA operators should really verify and authorize all
    requested attributes, and should acknowledge that they have, and the
    simplest way to do this is to make them add all the corresponding
    CLI arguments to the hxtool ca command, but too, that is
    error-prone, thus it's not clear yet which approach is best.
    Perhaps interactively prompt for yes/no for each attribute.)
 - add additional SAN types:
    - iPAddress                 (useless?)
    - dNSSrv                    (useful!)
    - directoryName             (useless, but trivial)
    - uniformResourceIdentifier (useful)
 - it would be nice if the ASN.1 compiler could generate print
   functions..., and/or even better, to-JSON functions
 - it would be nice if we had a known-OID db, including the names of the
   types they refer to in certificate extensions, otherName SANs and CSR
   attributes, then we could generate a CSR and certificate printer for
   all known options even when they are not supported by the rest of
   Heimdal
    - and we could also get friendly names for OIDs, and we could
      resolve their arc names
    - longer term, we could also stand to add some ASN.1 information
      object system functionality, just enough to make
      lib/hx509/asn1_print awesome by being able to automatically decode
      all heim_any and OCTET STRING content (better than its current
      --inner option)
2019-10-08 22:20:40 -05:00
Nicolas Williams
d31dd9e00b hx509: make file store writes atomic
Now we'll use mkostemp() and rename() into place to make
hx509_certs_store() atomic for FILE/DER-FILE/PEM-FILE stores.

This is not ideal, as it can leave temp files in place if a process
crashes in between the mkostemp() and the rename into place.

On Linux we'll eventually make use of O_TMPFILE and linkat().  The idea
will be to first create an anonymous, zero-link file in the directory
that will contain the file at the end, write the file, then linkat() the
file into place as a .new file, then rename() the .new into place.  That
will limit the amount of junk that may be left behind to just one file.
(If the linkat() fails, then unlink() the .new and try again.  If the
rename() fails that just means the caller raced with another and the
operation is complete.)

We should really make a lib/roken interface that does this.
2019-10-08 20:58:04 -05:00
Nicolas Williams
89b076c543 hx509: match docs for hx509_certs_init() 2019-10-07 21:32:00 -05:00
Nicolas Williams
fbacb5f5fe hx509: set errno in hx509_cert_init_data() 2019-10-07 21:32:00 -05:00
Nicolas Williams
25dbe28291 hx509: SANs are critical if DN is empty 2019-10-07 21:32:00 -05:00
Nicolas Williams
038ed5ec31 hx509: check Name RDN attribute size bounds 2019-10-07 21:32:00 -05:00
Nicolas Williams
93e48f8fba hxtool: Add OID symbol resolution 2019-10-07 21:32:00 -05:00
Nicolas Williams
6471fcaa54 Move ASN.1 modules from lib/hx509 to lib/asn1
This will help us generate a directory of OIDs from all the ASN.1
modules in lib/asn1, which will then help us create an hx509 API for
resolving OIDs to/from friendly names, which ultimately will help us
make hxtool more user-friendly.
2019-10-07 21:32:00 -05:00
Nicolas Williams
44677c01ae hxtool print: fix output device bugs 2019-10-03 13:09:18 -05:00
Nicolas Williams
63ccdca137 hx509: _hx509_certs_keys_free() should not require gate 2019-10-03 13:09:18 -05:00
Nicolas Williams
d13f89cb11 hx509: expose hx509_request_parse*() 2019-10-03 13:09:18 -05:00
Nicolas Williams
87eb05df6a hx509: always verify CSR signature 2019-10-03 13:09:18 -05:00
Nicolas Williams
f2776ce841 hx509: make hx509_get_error_string(NULL, ...) work
If hx509_context_init() fails we should still be able to do something
with its return code.
2019-10-03 13:09:18 -05:00
Nicolas Williams
eaebb892b8 hx509: hx509_context_free(&null_var) should work 2019-10-03 13:09:18 -05:00
Nicolas Williams
41fcafd20c hx509: add hx509_certs_destroy() 2019-10-03 13:09:18 -05:00
Nicolas Williams
e4b35ba13f hx509: create files with safe mode_t 2019-10-03 13:09:18 -05:00
Nicolas Williams
5a0d85e423 hx509: Add PKCS#8 private key format option 2019-10-03 13:09:18 -05:00
Nicolas Williams
63116100a8 hx509: do not crash on missing FILE: name 2019-10-03 13:09:18 -05:00
Nicolas Williams
cc8a5c59a8 hxtool: add generate-key command
This will help us not commit private keys in-tree for testing as we can now
more easily generate them as needed.
2019-10-03 13:09:18 -05:00
Nicolas Williams
049d817c66 Fix hx509_ca_tbs_free() segfault 2019-10-03 13:09:18 -05:00
Nicolas Williams
859c587dc2 Add more hxtool EKU options, and KeyUsage too
This is necessary in order to have more control over, e.g., template
certificates for kx509.  But also it's good to have this more generally.

Some batteries not included.  Specifically: no attempt is made to validate that
given KeyUsage values are compatible with the subjectPublicKey's alrogithm and
parameters.
2019-10-03 13:09:18 -05:00
Nicolas Williams
005ba36b83 hxtool request-create: fix --key argument handling
```
    hxtool request-create --subject=... \
                          --generate-key=... \
                          --key-bits=... \
                          --key=STORE \
                          store
```

wants to generate a key, store it in the location specified by the --key
argument, then read it back, then generate the CSR, and store it in
`store`.

But it didn't work because for generating a key this really wants the
`--key` argument to be a file path into which a raw DER-encoded RSA key
will be written (only RSA is supported, ay!), but for reading the key
back it uses `hx509_certs_init()`, which wants `TYPE:name` keystore
specification.  A deadly embrace.

Now that we have improved libhx509 functionality for reading/writing
private keys from/to PEM files we use this functionality and require a
store type in the `--key=STORE` argument.
2019-10-03 13:09:18 -05:00
Nicolas Williams
252487dfe4 hx509: support reading private keys from PEM files
This commit adds:

 - hx509_cert_init_private_key() for creating an hx509_cert object that
   has just a private key

 - hx509_cert_have_private_key_only() for checking whether an hx509_cert
   object has just a private key

This also generalizes the get_key() internal function in hxtool, which
is tasked with reding or generating a private key for use in signing
CSRs.  Now hxtool request-create can read/write private keys to/from PEM
files, not just DER files.

This is needed to support key types other than just RSA for CSRs and
certificates.
2019-10-03 13:09:18 -05:00