> For the complete documentation index, see [llms.txt](https://ref.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ref.gitbook.io/notes/java/certificates.md).

# Certificates

Demystifying digital certificates

**Three things:**

* *Certificate generator* - It has the `public key` and the `private key`
* *Self-signed certificate* - It has a copy of the `public key`, and it has `signature` generated by itself. It has *issuer* equal to *subject*. All certificates starts life as self-signed.&#x20;
* *Signed certificate* - It has copy of `public key`, and a `signature` that is generated by another certificate. The *issuer* is not equal to *subject*.

A signer certificate must posses the **CA extension** and this is done during construction. The last certificate known as leaf certificate does not have the **CA extension**, and thus cannot be used to sign another certificate. &#x20;

```java
CertAndKeyGen generator 
  = new CertAndKeyGen("RSA", "SHA1WithRSA", null);
generator.generate(1024);
PrivateKey privkey = generator.getPrivateKey();
byte[] buf = privkey.getEncoded();
buf = generator.getPublicKey().getEncoded();

X509Certificate cert = keyGen.getSelfCertificate(
  new X500Name("CN=ROOT"), (long)365 * 24 * 60 * 60);

buf = cert.getSignature();
buf = cert.getPublicKey().getEncoded();
System.out.println(cert.getIssuerDN().toString());
System.out.println(cert.getSubjectDN().toString());
```

Observe in where places to get the private key, the public key and the signature. Both the private key and the public key can be found in `generator`. The `generator` creates the `cert` and the public key is copied into the cert.&#x20;

When one certificate signs another certificate looks like this:

```java
userCertificate = createSignedCertificate(
      userCertificate,
      signerCertificate,
      signerPrivateKey);
```

Notice the first param input and the output. So in essence, the `createSignedCertificate( )` in above snippet does a transform to `userCertificate`. After the transformation, the `signature` in the certificate is value produced by the `signerCertificate`. Of most important is the private key needed to compute the signature. The **signerCertificate** is passed in the param to extract the signer's name, and embed it into the signed certificate.&#x20;

In X.509 trust chain: `A -> B -> C`

`A` is the root CA and `B` is an intermediate CA. In this scenario, the `C` certificate can only be verified by the *pubkey* of `B` and not `A` .&#x20;

### Keystore

Store private keys into a keystore. Each private key has an associated certificate chain. A private key alone and without a certificate chain has no value because it cannot prove itself. The private key is the link to identity and the associated chain is the proof of this identity. Sure, the public key can also prove identity, but the certificate chain of X.509 root of trusts are in every browsers and phones that forms a hierarchy. Imagine a world where every user publishes his own public key. So every one will have to find where to download these public keys.&#x20;
