certificate: fix memory leak

When the certificate test cases were ran under ASAN, there was reported
to be a large memory leak in the certificate code.

The understanding documented previously in the code with respect to the
relationship between `X509_STORE` and `X509_LOOKUP` did not match the
reality of the OpenSSL source.  `LOOKUP` is part-of the `STORE`, not an
owner-of it.  It is not appropriate to `X509_LOOKUP_free` the `LOOKUP`
that has become part of the `STORE`, because it is the responsibility of
the `STORE` to do that.

Invert the relationship (and holding std::unique_ptrs) so that the
`X509_STORE` becomes the RAII object and everything contained in it can
be freed when it goes out of scope.

Further explanation of the OpenSSL source is as follows:

* The `X509_LOOKUP_free` only releases the memory held by itself and
  calls `method->free`[1].  The `X509_LOOKUP_file` type has no
  `method->free`[2] (and confirmed with GDB).  This means that the
  `X509_LOOKUP_free` does not end up freeing much of any memory and
  causes a leak of everything it put into the `X509_STORE`, so
  `X509_LOOKUP_free` does not belong as the RAII cleanup function.

* The `X509_STORE_add_lookup` allocates the `X509_LOOKUP`, assigns
  itself to the `LOOKUP` as the store context, and adds the `LOOKUP`
  onto a stack of held `LOOKUP` objects[3].  When `X509_STORE_free` is
  called, all of the held `LOOKUP` objects are freed[4] by calling
  `X509_LOOKUP_free`.

Therefore, assigning `X509_STORE_free` as the RAII cleanup function
allows both the `X509_STORE` (and all certificate data inserted into it)
as well as the created `X509_LOOKUP` to be freed.

Tested: ASAN now passes, along with testcases, when ran.

1. https://github.com/openssl/openssl/blob/master/crypto/x509/x509_lu.c#L39
2. https://github.com/openssl/openssl/blob/master/crypto/x509/by_file.c#L30
3. https://github.com/openssl/openssl/blob/master/crypto/x509/x509_lu.c#L285
4. https://github.com/openssl/openssl/blob/master/crypto/x509/x509_lu.c#L238

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: Ie350c47a2c01c5a47ed538d01e1f71274ece1fc8
1 file changed
tree: 0b65956b82fedccbcb05f8f196c3726da317c399
  1. bmc-vmi-ca/
  2. dist/
  3. test/
  4. .clang-format
  5. .gitignore
  6. .lcovrc
  7. argument.cpp
  8. argument.hpp
  9. bootstrap.sh
  10. certificate.cpp
  11. certificate.hpp
  12. certs_manager.cpp
  13. certs_manager.hpp
  14. configure.ac
  15. csr.cpp
  16. csr.hpp
  17. LICENSE
  18. mainapp.cpp
  19. MAINTAINERS
  20. Makefile.am
  21. Makefile.am.include
  22. OWNERS
  23. README.md
  24. watch.cpp
  25. watch.hpp
README.md

phosphor-certificate-manager

Certificate management allows to replace the existing certificate and private key file with another (possibly CA signed) Certificate key file. Certificate management allows the user to install both the server and client certificates.

To Build

To build this package, do the following steps:

    1. ./bootstrap.sh
    2. ./configure ${CONFIGURE_FLAGS}
    3. make

To clean the repository run `./bootstrap.sh clean`.

To Run

Multiple instances of phosphor-certificate-manager are usually run on the bmc to support management of different types of certificates.

Usage: ./phosphor-certificate-manager [options]
Options:
    --help            Print this menu
    --type            certificate type
                      Valid types: client,server,authority
    --endpoint        d-bus endpoint
    --path            certificate file path
    --unit=<name>     Optional systemd unit need to reload

Https certificate management

Purpose: Server https certificate

./phosphor-certificate-manager --type=server --endpoint=https \
    --path=/etc/ssl/certs/https/server.pem --unit=bmcweb.service

CA certificate management

Purpose: Client certificate validation

./phosphor-certificate-manager --type=authority --endpoint=ldap \
    --path=/etc/ssl/certs/authority --unit=bmcweb.service

LDAP client certificate management

Purpose: LDAP client certificate validation

./phosphor-certificate-manager --type=client --endpoint=ldap \
    --path=/etc/nslcd/certs/cert.pem

D-Bus Interface

phosphor-certificate-manager is an implementation of the D-Bus interface defined in this document.

D-Bus service name is constructed by "xyz.openbmc_project.Certs.Manager.{Type}.{Endpoint}" and D-Bus object path is constructed by "/xyz/openbmc_project/certs/{type}/{endpoint}".

Take https certificate management as an example.

./phosphor-certificate-manager --type=server --endpoint=https \
    --path=/etc/ssl/certs/https/server.pem --unit=bmcweb.service

D-Bus service name is "xyz.openbmc_project.Certs.Manager.Server.Https" and D-Bus object path is "/xyz/openbmc_project/certs/server/https".

Usage in openbmc/bmcweb

OpenBMC bmcweb exposes various REST APIs for certificate management on the BMC, which leverages functionalities of phosphor-certificate-manager via D-Bus.