Top Description Fields Constructors Methods
org.bouncycastle.bcpg

public Class PublicKeyPacket

extends ContainedPacket
implements PublicKeyAlgorithmTags
Class Inheritance
All Implemented Interfaces
org.bouncycastle.bcpg.PublicKeyAlgorithmTags
Known Direct Subclasses
org.bouncycastle.bcpg.PublicSubkeyPacket
Imports
java.io.ByteArrayOutputStream, .IOException, java.util.Date, org.bouncycastle.util.Pack

Base class for OpenPGP public (primary) keys. The public key packet holds the public parameters of an OpenPGP key pair. An OpenPGP certificate (transferable public key) consists of one primary key and optionally multiple subkey packets.
See Also
RFC4880 - Public-Key Packet, RFC9580 - Public-Key Packet, LibrePGP - Public-Key Packet

Field Summary

Modifier and TypeField and Description
private int
private BCPGKey
public static final int
LIBREPGP_5

Non-Standard LibrePGP introduced v5, which is only supported by a subset of vendors.

private long
private int
private int
public static final int
VERSION_3

OpenPGP v3 keys are deprecated.

public static final int
VERSION_4

OpenPGP v4 keys are (at the time of writing) widely used, but are subject to some attacks.

public static final int
VERSION_6

OpenPGP v6 keys are newly introduced.

Constructor Summary

AccessConstructor and Description
pack-priv
PublicKeyPacket(BCPGInputStream
packet input stream
in
)

Parse a PublicKeyPacket from an OpenPGP BCPGInputStream.

pack-priv
PublicKeyPacket(BCPGInputStream
packet input stream
in
,
boolean
new packet format
newPacketFormat
)

Parse a PublicKeyPacket from an OpenPGP BCPGInputStream.

pack-priv
PublicKeyPacket(int
packet type ID
keyTag
,
BCPGInputStream
packet input stream
in
)

Parse a PublicKeyPacket or PublicSubkeyPacket from an OpenPGP BCPGInputStream.

pack-priv
PublicKeyPacket(int
packet type ID
keyTag
,
BCPGInputStream
packet input stream
in
,
boolean
packet format
newPacketFormat
)

Parse a PublicKeyPacket or PublicSubkeyPacket from an OpenPGP BCPGInputStream.

public
PublicKeyPacket(int
public key algorithm id
algorithm
,
Date
creation time
time
,
BCPGKey
key object
key
)
Deprecated use versioned PublicKeyPacket(int, int, Date, BCPGKey) instead

Construct version 4 public primary key packet.

public
PublicKeyPacket(int
packet version
version
,
int
public key algorithm id
algorithm
,
Date
creation time
time
,
BCPGKey
key object
key
)

Construct an OpenPGP public primary key packet.

pack-priv
PublicKeyPacket(int
public key packet type ID
keyTag
,
int
packet version
version
,
int
public key algorithm id
algorithm
,
Date
creation time
time
,
BCPGKey
key object
key
)

Construct an OpenPGP public key packet.

Method Summary

Modifier and TypeMethod and Description
public void
encode(BCPGOutputStream
packet output stream
out
)

Implements abstract org.bouncycastle.bcpg.ContainedPacket.encode.

Encode the packet to the OpenPGP BCPGOutputStream.

public int

Returns:

algorithm id
getAlgorithm
()

Return the algorithm id of the public key.

public byte[]

Returns:

encoded packet contents
getEncodedContents
()

Return the encoded packet contents without the packet frame.

public BCPGKey

Returns:

key
getKey
()

Return the key object.

public static long
getKeyID(PublicKeyPacket publicPk, byte[] fingerprint)

public Date

Returns:

creation time of the key
getTime
()

Return the keys creation time.

public int

Returns:

v3 key validity period in days since creation.
getValidDays
()
Deprecated v4 and v6 keys instead signal their expiration time via the org.bouncycastle.bcpg.sig.KeyExpirationTime signature subpacket.

Only for v3 keys - The time in days since the keys creation, during which the key is valid.

public int

Returns:

packet version
getVersion
()

Return the packet version.

private void
parseKey(BCPGInputStream
input stream which read just up to the public key material
in
,
int
public key algorithm ID
algorithmId
,
long
optional: Length of the public key material. -1 if not present.
optLen
)

Parse algorithm-specific public key material.

Inherited from org.bouncycastle.bcpg.ContainedPacket:
getEncodedgetEncoded

Field Detail

algorithmback to summary
private int algorithm
keyback to summary
private BCPGKey key
LIBREPGP_5back to summary
public static final int LIBREPGP_5

Non-Standard LibrePGP introduced v5, which is only supported by a subset of vendors.

timeback to summary
private long time
validDaysback to summary
private int validDays
versionback to summary
private int version
VERSION_3back to summary
public static final int VERSION_3

OpenPGP v3 keys are deprecated. They can only be used with RSA.

See Also
OpenPGP - Version 3 Public Keys
VERSION_4back to summary
public static final int VERSION_4

OpenPGP v4 keys are (at the time of writing) widely used, but are subject to some attacks.

See Also
OpenPGP - Version 4 Public Keys
VERSION_6back to summary
public static final int VERSION_6

OpenPGP v6 keys are newly introduced.

See Also
OpenPGP - Version 6 Public Keys

Constructor Detail

PublicKeyPacketback to summary
pack-priv PublicKeyPacket(BCPGInputStream in) throws IOException

Parse a PublicKeyPacket from an OpenPGP BCPGInputStream. The packet format is remembered as PacketFormat#LEGACY.

Parameters
in:BCPGInputStream

packet input stream

PublicKeyPacketback to summary
pack-priv PublicKeyPacket(BCPGInputStream in, boolean newPacketFormat) throws IOException

Parse a PublicKeyPacket from an OpenPGP BCPGInputStream. If

newPacketFormat
is true, the packet format is remembered as PacketFormat#CURRENT, otherwise as PacketFormat#LEGACY.
Parameters
in:BCPGInputStream

packet input stream

newPacketFormat:boolean

new packet format

PublicKeyPacketback to summary
pack-priv PublicKeyPacket(int keyTag, BCPGInputStream in) throws IOException

Parse a PublicKeyPacket or PublicSubkeyPacket from an OpenPGP BCPGInputStream. If

keyTag
is PUBLIC_KEY, the packet is a primary key. If instead it is PUBLIC_SUBKEY, it is a subkey packet. The packet format is remembered as PacketFormat#LEGACY.
Parameters
keyTag:int

packet type ID

in:BCPGInputStream

packet input stream

PublicKeyPacketback to summary
pack-priv PublicKeyPacket(int keyTag, BCPGInputStream in, boolean newPacketFormat) throws IOException

Parse a PublicKeyPacket or PublicSubkeyPacket from an OpenPGP BCPGInputStream. If

keyTag
is PUBLIC_KEY, the packet is a primary key. If instead it is PUBLIC_SUBKEY, it is a subkey packet. If
newPacketFormat
is true, the packet format is remembered as PacketFormat#CURRENT, otherwise as PacketFormat#LEGACY.
Parameters
keyTag:int

packet type ID

in:BCPGInputStream

packet input stream

newPacketFormat:boolean

packet format

Exceptions
IOException:
if the key packet cannot be parsed
See Also
OpenPGP - Version 3 Public Keys, OpenPGP - Version 4 Public Keys, OpenPGP - Version 6 Public Keys, LibrePGP - Public-Key Packet Formats
PublicKeyPacketback to summary
public PublicKeyPacket(int algorithm, Date time, BCPGKey key)

Deprecated

use versioned PublicKeyPacket(int, int, Date, BCPGKey) instead

Construct version 4 public primary key packet.

Parameters
algorithm:int

public key algorithm id

time:Date

creation time

key:BCPGKey

key object

Annotations
@Deprecated
PublicKeyPacketback to summary
public PublicKeyPacket(int version, int algorithm, Date time, BCPGKey key)

Construct an OpenPGP public primary key packet.

Parameters
version:int

packet version

algorithm:int

public key algorithm id

time:Date

creation time

key:BCPGKey

key object

PublicKeyPacketback to summary
pack-priv PublicKeyPacket(int keyTag, int version, int algorithm, Date time, BCPGKey key)

Construct an OpenPGP public key packet. If

keyTag
is PUBLIC_KEY, the packet is a primary key. If instead it is PUBLIC_SUBKEY, it is a subkey packet.
Parameters
keyTag:int

public key packet type ID

version:int

packet version

algorithm:int

public key algorithm id

time:Date

creation time

key:BCPGKey

key object

Method Detail

encodeback to summary
public void encode(BCPGOutputStream out) throws IOException

Implements abstract org.bouncycastle.bcpg.ContainedPacket.encode.

Encode the packet to the OpenPGP BCPGOutputStream. If the BCPGOutputStream packet format is set to PacketFormat#ROUNDTRIP, the result of hasNewPacketFormat() determines, which packet format is used to encode the packet. Otherwise, the BCPGOutputStream dictates which format to use.

Parameters
out:BCPGOutputStream

packet output stream

getAlgorithmback to summary
public int getAlgorithm()

Return the algorithm id of the public key.

Returns:int

algorithm id

getEncodedContentsback to summary
public byte[] getEncodedContents() throws IOException

Return the encoded packet contents without the packet frame.

Returns:byte[]

encoded packet contents

getKeyback to summary
public BCPGKey getKey()

Return the key object.

Returns:BCPGKey

key

getKeyIDback to summary
public static long getKeyID(PublicKeyPacket publicPk, byte[] fingerprint)
getTimeback to summary
public Date getTime()

Return the keys creation time.

Returns:Date

creation time of the key

getValidDaysback to summary
public int getValidDays()

Deprecated

v4 and v6 keys instead signal their expiration time via the org.bouncycastle.bcpg.sig.KeyExpirationTime signature subpacket.

Only for v3 keys - The time in days since the keys creation, during which the key is valid.

Returns:int

v3 key validity period in days since creation.

getVersionback to summary
public int getVersion()

Return the packet version.

Returns:int

packet version

parseKeyback to summary
private void parseKey(BCPGInputStream in, int algorithmId, long optLen) throws IOException

Parse algorithm-specific public key material.

Parameters
in:BCPGInputStream

input stream which read just up to the public key material

algorithmId:int

public key algorithm ID

optLen:long

optional: Length of the public key material. -1 if not present.

Exceptions
IOException:
if the pk material cannot be parsed