Bouncy Castle Cryptography Library 1.81

org.bouncycastle.openpgp
Class PGPSecretKeyRing

java.lang.Object
  extended byorg.bouncycastle.openpgp.PGPKeyRing
      extended byorg.bouncycastle.openpgp.PGPSecretKeyRing
All Implemented Interfaces:
org.bouncycastle.util.Iterable
Direct Known Subclasses:
BcPGPSecretKeyRing, JcaPGPSecretKeyRing

public class PGPSecretKeyRing
extends PGPKeyRing
implements org.bouncycastle.util.Iterable

Class to hold a single master secret key and its subkeys.

Often PGP keyring files consist of multiple master keys, if you are trying to process or construct one of these you should use the PGPSecretKeyRingCollection class.


Constructor Summary
PGPSecretKeyRing(byte[] encoding, KeyFingerPrintCalculator fingerPrintCalculator)
           
PGPSecretKeyRing(java.io.InputStream in, KeyFingerPrintCalculator fingerPrintCalculator)
           
PGPSecretKeyRing(java.util.List secKeys)
          Base constructor from a list of keys representing a secret key ring (a master key and its associated sub-keys).
 
Method Summary
static PGPSecretKeyRing copyWithNewPassword(PGPSecretKeyRing ring, PBESecretKeyDecryptor oldKeyDecryptor, PBESecretKeyEncryptor newKeyEncryptor)
          Return a copy of the passed in secret key ring, with the private keys (where present) associated with the master key and sub keys are encrypted using a new password and the passed in algorithm.
 void encode(java.io.OutputStream outStream)
           
 byte[] getEncoded()
           
 byte[] getEncoded(PacketFormat format)
           
 java.util.Iterator getExtraPublicKeys()
          Return an iterator of the public keys in the secret key ring that have no matching private key.
 java.util.Iterator getKeysWithSignaturesBy(KeyIdentifier identifier)
           
 java.util.Iterator getKeysWithSignaturesBy(long keyID)
          Return any keys carrying a signature issued by the key represented by keyID.
 PGPPublicKey getPublicKey()
          Return the public key for the master key.
 PGPPublicKey getPublicKey(byte[] fingerprint)
          Return the public key with the passed in fingerprint if it is present.
 PGPPublicKey getPublicKey(KeyIdentifier identifier)
           
 PGPPublicKey getPublicKey(long keyID)
          Return the public key referred to by the passed in keyID if it is present.
 java.util.Iterator getPublicKeys()
          Return an iterator containing all the public keys.
 java.util.Iterator getPublicKeys(KeyIdentifier identifier)
           
 PGPSecretKey getSecretKey()
          Return the master private key.
 PGPSecretKey getSecretKey(byte[] fingerprint)
          Return the secret key associated with the passed in fingerprint if it is present.
 PGPSecretKey getSecretKey(KeyIdentifier identifier)
           
 PGPSecretKey getSecretKey(long keyID)
          Return the secret key referred to by the passed in keyID if it is present.
 java.util.Iterator getSecretKeys()
          Return an iterator containing all the secret keys.
 java.util.Iterator getSecretKeys(KeyIdentifier identifier)
           
static PGPSecretKeyRing insertOrReplacePublicKey(PGPSecretKeyRing secretRing, PGPPublicKey publicKey)
          Either replace the public key on the corresponding secret key in the key ring if present, or insert the public key as an extra public key in case that the secret ring does not contain the corresponding secret key.
static PGPSecretKeyRing insertSecretKey(PGPSecretKeyRing secRing, PGPSecretKey secKey)
          Returns a new key ring with the secret key passed in either added or replacing an existing one with the same key ID.
 java.util.Iterator iterator()
          Support method for Iterable where available.
static PGPSecretKeyRing removeSecretKey(PGPSecretKeyRing secRing, PGPSecretKey secKey)
          Returns a new key ring with the secret key passed in removed from the key ring.
static PGPSecretKeyRing replacePublicKeys(PGPSecretKeyRing secretRing, PGPPublicKeyRing publicRing)
          Replace the public key set on the secret ring with the corresponding key off the public ring.
 int size()
          Return the number of keys in the key ring.
 PGPPublicKeyRing toCertificate()
          Return the OpenPGP certificate (Transferable Public Key) of this key.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PGPSecretKeyRing

public PGPSecretKeyRing(java.util.List secKeys)
Base constructor from a list of keys representing a secret key ring (a master key and its associated sub-keys).

Parameters:
secKeys - the list of keys making up the ring.

PGPSecretKeyRing

public PGPSecretKeyRing(byte[] encoding,
                        KeyFingerPrintCalculator fingerPrintCalculator)
                 throws java.io.IOException,
                        PGPException

PGPSecretKeyRing

public PGPSecretKeyRing(java.io.InputStream in,
                        KeyFingerPrintCalculator fingerPrintCalculator)
                 throws java.io.IOException,
                        PGPException
Method Detail

getPublicKey

public PGPPublicKey getPublicKey()
Return the public key for the master key.

Specified by:
getPublicKey in class PGPKeyRing
Returns:
PGPPublicKey

getPublicKey

public PGPPublicKey getPublicKey(long keyID)
Return the public key referred to by the passed in keyID if it is present.

Specified by:
getPublicKey in class PGPKeyRing
Parameters:
keyID - the full keyID of the key of interest.
Returns:
PGPPublicKey with matching keyID, null if it is not present.

getPublicKey

public PGPPublicKey getPublicKey(byte[] fingerprint)
Return the public key with the passed in fingerprint if it is present.

Specified by:
getPublicKey in class PGPKeyRing
Parameters:
fingerprint - the full fingerprint of the key of interest.
Returns:
PGPPublicKey with the matching fingerprint, null if it is not present.

getPublicKey

public PGPPublicKey getPublicKey(KeyIdentifier identifier)
Specified by:
getPublicKey in class PGPKeyRing

getPublicKeys

public java.util.Iterator getPublicKeys(KeyIdentifier identifier)
Specified by:
getPublicKeys in class PGPKeyRing

getSecretKey

public PGPSecretKey getSecretKey(KeyIdentifier identifier)

getSecretKeys

public java.util.Iterator getSecretKeys(KeyIdentifier identifier)

getKeysWithSignaturesBy

public java.util.Iterator getKeysWithSignaturesBy(long keyID)
Return any keys carrying a signature issued by the key represented by keyID.

Specified by:
getKeysWithSignaturesBy in class PGPKeyRing
Parameters:
keyID - the key id to be matched against.
Returns:
an iterator (possibly empty) of PGPPublicKey objects carrying signatures from keyID.

getKeysWithSignaturesBy

public java.util.Iterator getKeysWithSignaturesBy(KeyIdentifier identifier)
Specified by:
getKeysWithSignaturesBy in class PGPKeyRing

getPublicKeys

public java.util.Iterator getPublicKeys()
Return an iterator containing all the public keys.

Specified by:
getPublicKeys in class PGPKeyRing
Returns:
Iterator

getSecretKey

public PGPSecretKey getSecretKey()
Return the master private key.

Returns:
PGPSecretKey

getSecretKeys

public java.util.Iterator getSecretKeys()
Return an iterator containing all the secret keys.

Returns:
Iterator

getSecretKey

public PGPSecretKey getSecretKey(long keyID)
Return the secret key referred to by the passed in keyID if it is present.

Parameters:
keyID - the full keyID of the key of interest.
Returns:
PGPSecretKey with matching keyID, null if it is not present.

getSecretKey

public PGPSecretKey getSecretKey(byte[] fingerprint)
Return the secret key associated with the passed in fingerprint if it is present.

Parameters:
fingerprint - the full fingerprint of the key of interest.
Returns:
PGPSecretKey with the matching fingerprint, null if it is not present.

getExtraPublicKeys

public java.util.Iterator getExtraPublicKeys()
Return an iterator of the public keys in the secret key ring that have no matching private key. At the moment only personal certificate data appears in this fashion.

Returns:
iterator of unattached, or extra, public keys.

size

public int size()
Return the number of keys in the key ring.

Specified by:
size in class PGPKeyRing
Returns:
number of keys (master key + subkey).

toCertificate

public PGPPublicKeyRing toCertificate()
Return the OpenPGP certificate (Transferable Public Key) of this key.

Returns:
certificate

getEncoded

public byte[] getEncoded()
                  throws java.io.IOException
Specified by:
getEncoded in class PGPKeyRing
Throws:
java.io.IOException

getEncoded

public byte[] getEncoded(PacketFormat format)
                  throws java.io.IOException
Specified by:
getEncoded in class PGPKeyRing
Throws:
java.io.IOException

encode

public void encode(java.io.OutputStream outStream)
            throws java.io.IOException
Specified by:
encode in class PGPKeyRing
Throws:
java.io.IOException

iterator

public java.util.Iterator iterator()
Support method for Iterable where available.

Specified by:
iterator in interface org.bouncycastle.util.Iterable

replacePublicKeys

public static PGPSecretKeyRing replacePublicKeys(PGPSecretKeyRing secretRing,
                                                 PGPPublicKeyRing publicRing)
Replace the public key set on the secret ring with the corresponding key off the public ring.

Parameters:
secretRing - secret ring to be changed.
publicRing - public ring containing the new public key set.

insertOrReplacePublicKey

public static PGPSecretKeyRing insertOrReplacePublicKey(PGPSecretKeyRing secretRing,
                                                        PGPPublicKey publicKey)
Either replace the public key on the corresponding secret key in the key ring if present, or insert the public key as an extra public key in case that the secret ring does not contain the corresponding secret key.

Parameters:
secretRing - secret key ring
publicKey - public key to insert or replace
Returns:
secret key ring

copyWithNewPassword

public static PGPSecretKeyRing copyWithNewPassword(PGPSecretKeyRing ring,
                                                   PBESecretKeyDecryptor oldKeyDecryptor,
                                                   PBESecretKeyEncryptor newKeyEncryptor)
                                            throws PGPException
Return a copy of the passed in secret key ring, with the private keys (where present) associated with the master key and sub keys are encrypted using a new password and the passed in algorithm.

Parameters:
ring - the PGPSecretKeyRing to be copied.
oldKeyDecryptor - the current decryptor based on the current password for key.
newKeyEncryptor - a new encryptor based on a new password for encrypting the secret key material.
Returns:
the updated key ring.
Throws:
PGPException

insertSecretKey

public static PGPSecretKeyRing insertSecretKey(PGPSecretKeyRing secRing,
                                               PGPSecretKey secKey)
Returns a new key ring with the secret key passed in either added or replacing an existing one with the same key ID.

Parameters:
secRing - the secret key ring to be modified.
secKey - the secret key to be added.
Returns:
a new secret key ring.

removeSecretKey

public static PGPSecretKeyRing removeSecretKey(PGPSecretKeyRing secRing,
                                               PGPSecretKey secKey)
Returns a new key ring with the secret key passed in removed from the key ring.

Parameters:
secRing - the secret key ring to be modified.
secKey - the secret key to be removed.
Returns:
a new secret key ring, or null if secKey is not found.

Bouncy Castle Cryptography Library 1.81