code: public static CertifiedPublicKey getCertificate(CertificateProvider provider, SignerInformation signer,
          CertificateFactory factory)
      {
          SignerId id = signer.getSID();
  
          if (provider instanceof BcStoreX509CertificateProvider) {
              X509CertificateHolder cert = ((BcStoreX509CertificateProvider) provider).getCertificate(id);
              return (cert != null) ? BcUtils.convertCertificate(factory, cert) : null;
          }
  
          X500Name bcIssuer = id.getIssuer();
          BigInteger serial = id.getSerialNumber();
          byte[] keyId = id.getSubjectKeyIdentifier();
  
          if (bcIssuer != null) {
              PrincipalIndentifier issuer = new DistinguishedName(bcIssuer);
              if (keyId != null) {
                  return provider.getCertificate(issuer, serial, keyId);
              }
              return provider.getCertificate(issuer, serial);
          }
  
          if (keyId != null) {
              return provider.getCertificate(keyId);
          }
  
          return null;
      }
docs: Retrieve the certificate matching the given signer from the certificate provider.
  
  @param provider a certificate provider.
  @param signer the signer for which you want to retrieve the certificate.
  @param factory a certificate factory to convert the certificate.
  @return a certified public key.