All Implemented Interfaces:
Serializable, DataStructure
Direct Known Subclasses:
Destination, RouterIdentity

public class KeysAndCertextends DataStructureImpl
Container for cryptographic keys and certificate as used by I2P identities.

KeysAndCert provides the fundamental cryptographic identity components:

  • Public Key: Encryption key for encrypted communication
  • Signing Public Key: Key for verifying signatures and identity
  • Certificate: Optional metadata about the keys and identity
  • Cached Hash: Pre-computed SHA-256 hash for efficient identification

Structure:

  • Public key (variable length, typically 256 bytes for ElGamal)
  • Signing public key (variable length, typically 128 bytes for DSA, 32 bytes for Ed25519)
  • Certificate (type + length + payload)
  • Optional padding for consistent serialization

Usage:

  • Base Class: Extended by Destination and RouterIdentity
  • Identity: Forms the core of I2P cryptographic identities
  • Verification: Provides keys for signature verification
  • Encryption: Contains encryption key for secure communication

Key Types:

  • Encryption: ElGamal 2048-bit (legacy) or ECIES X25519 (modern)
  • Signing: DSA-SHA1 (legacy), ECDSA-P256, or EdDSA-Ed25519 (modern)
  • Certificates: NULL, HIDDEN, SIGNED, MULTIPLE, or KEY types

Immutability:

  • As of 0.9.9, instances are immutable after keys and certificate are set
  • Attempts to modify will throw IllegalStateException
  • Ensures thread safety and prevents accidental corruption

History:

  • Implemented in 0.8.2 and retrofitted over existing Destination and RouterIdentity classes
  • No functional difference between Destination and RouterIdentity at this level
  • Provides unified interface for identity management across I2P
See Also:
  • Field Details

    • _publicKey

      protected PublicKey _publicKey
      Public encryption key.
    • _signingKey

      protected SigningPublicKey _signingKey
      Public signing key.
    • _certificate

      protected Certificate _certificate
      Certificate specifying key types and metadata.
    • __calculatedHash

      private Hash __calculatedHash
      Cached calculated hash value.
    • __keyCertificate

      private volatile KeyCertificate __keyCertificate
      Cached key certificate, created lazily from _certificate when that is a plain CERTIFICATE_TYPE_KEY Certificate. null when there is nothing to cache. Volatile so a racy double-creation is harmless.
    • _padding

      private byte[] _padding
      If compressed, 32 bytes only.
    • _paddingBlocks

      protected int _paddingBlocks
      If compressed, the padding size / 32, else 0.
    • PAD_COMP_LEN

      private static final int PAD_COMP_LEN
      Compressed padding block length in bytes.
      See Also:
    • B32_PREFIX_BYTES

      private static final int B32_PREFIX_BYTES
      Hash bytes needed for the 8-char b32 hash prefix in toString(). 8 b32 chars carry 40 bits, and 5 bytes is the smallest input that produces a full 8 chars.
      See Also:
    • _log

      private static final Log _log
      Logger instance.
  • Constructor Details

    • KeysAndCert

      public KeysAndCert()
      Constructor. An empty key and certificate set, ready for readBytes(InputStream) or for the subclasses to populate field by field.
  • Method Details

    • getCertificate

      public Certificate getCertificate()
      Certificate for this identity.
      Returns:
      the certificate authorizing this identity's keys
    • setCertificate

      public void setCertificate(Certificate cert)
      Certificate to set.
      Parameters:
      cert - the certificate to store, which also supplies the signature and encryption types
      Throws:
      IllegalStateException - if was already set
    • getSigType

      public SigType getSigType()
      Signature type from the certificate.
      Returns:
      null if not set or unknown
    • getEncType

      public EncType getEncType()
      Encryption type from the certificate.
      Returns:
      null if not set or unknown
    • getKeyCertificate

      private KeyCertificate getKeyCertificate(Certificate cert)
      Up-convert the certificate to a KeyCertificate, caching the result. The cache avoids a new KeyCertificate allocation on every identity-type query for identities holding a plain CERTIFICATE_TYPE_KEY Certificate (KeyCertificate.toKeyCertificate() already returns itself).
      Parameters:
      cert - the non-null certificate
      Returns:
      the key certificate, or null if the certificate is not of type CERTIFICATE_TYPE_KEY or its payload is too short
    • getPublicKey

      public PublicKey getPublicKey()
      Valid for RouterIdentities. May contain random padding for Destinations.
      Returns:
      the public key
    • setPublicKey

      public void setPublicKey(PublicKey key)
      Public key to set.
      Parameters:
      key - the public key to store
      Throws:
      IllegalStateException - if was already set
    • getSigningPublicKey

      public SigningPublicKey getSigningPublicKey()
      Signing public key for this identity.
      Returns:
      the signing public key
    • setSigningPublicKey

      public void setSigningPublicKey(SigningPublicKey key)
      Signing public key to set.
      Parameters:
      key - the signing public key to store
      Throws:
      IllegalStateException - if was already set
    • getPadding

      public byte[] getPadding()
      Padding bytes for key alignment.
      Returns:
      the stored padding, null when none was set, otherwise the compressed form when more than one block was stored
    • setPadding

      public void setPadding(byte[] padding)
      Padding bytes to set.
      Parameters:
      padding - the padding to store, whose length must be 32 or a multiple of 32 for isCompressible() to become true
      Throws:
      IllegalStateException - if was already set
    • isCompressible

      public boolean isCompressible()
      Is there compressible padding?
      Returns:
      true when more than one padding block was stored, so the padding can be recompressed on write
    • readBytes

      public void readBytes(InputStream in) throws DataFormatException, IOException
      Reads the keys and certificate from an input stream.
      Parameters:
      in - stream to read from
      Throws:
      IllegalStateException - if data already set
      DataFormatException - if the data is improperly formatted
      IOException - if there was a problem reading the stream
    • combinePadding

      protected static byte[] combinePadding(byte[] pad1, byte[] pad2)
      Combines two padding arrays.
      Parameters:
      pad1 - the first padding array, whose bytes lead the result
      pad2 - the second padding array, whose bytes follow those of pad1
      Returns:
      null if both are null
    • compressPadding

      private void compressPadding()
      This only does the padding, does not compress the unused 256 byte LS public key. Savings is 288 bytes for RI and 64 bytes for LS.
    • writePaddingBytes

      protected int writePaddingBytes(byte[] target, int off)
      For Destination.writeBytes().
      Parameters:
      target - the buffer receiving the padding
      off - the offset in target at which to start writing
      Returns:
      the new offset
    • writeBytes

      public void writeBytes(OutputStream out) throws DataFormatException, IOException
      Writes the keys and certificate to an output stream.
      Parameters:
      out - stream to write to
      Throws:
      DataFormatException - if the data was incomplete or not yet ready to be written
      IOException - if there was a problem writing to the stream
    • equals

      public boolean equals(Object object)
      Compares this KeysAndCert with another object for equality.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      The signing key has enough randomness in it to use it by itself for speed.
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Returns a string representation of this KeysAndCert.
      Overrides:
      toString in class Object
    • calculateHash

      public Hash calculateHash()
      Throws IllegalStateException if keys and cert are not initialized, as of 0.9.12. Prior to that, returned null.
      Specified by:
      calculateHash in interface DataStructure
      Overrides:
      calculateHash in class DataStructureImpl
      Returns:
      SHA256 hash, or null if there were problems (data format or io errors)
      Throws:
      IllegalStateException - if keys and cert are not initialized
    • getHash

      public Hash getHash()
      Throws IllegalStateException if keys and cert are not initialized, as of 0.9.12. Prior to that, returned null.
      Returns:
      the SHA-256 of the serialized keys and cert, cached after the first calculation
      Throws:
      IllegalStateException - if keys and cert are not initialized