55import java .util .Date ;
66
77/**
8- * basic packet for a PGP public key
8+ * Base class for OpenPGP public (primary) keys.
9+ * The public key packet holds the public parameters of an OpenPGP key pair.
10+ * An OpenPGP certificate (transferable public key) consists of one primary key and optionally multiple subkey packets.
11+ *
12+ * @see <a href="https://www.rfc-editor.org/rfc/rfc4880.html#section-5.5.1.1">
13+ * rfc4880 - Public-Key Packet</a>
14+ * @see <a href="https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-13.html#name-public-key-packet-type-id-6">
15+ * C-R - Public-Key Packet</a>
16+ * @see <a href="https://www.ietf.org/archive/id/draft-koch-librepgp-00.html#name-public-key-packet-tag-6">
17+ * LibrePGP - Public-Key Packet</a>
918 */
1019public class PublicKeyPacket
1120 extends ContainedPacket
1221 implements PublicKeyAlgorithmTags
1322{
23+ /**
24+ * OpenPGP v3 keys are deprecated.
25+ * They can only be used with RSA.
26+ *
27+ * @see <a href="https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-13.html#name-version-3-public-keys">
28+ * C-R - Version 3 Public Keys</a>
29+ */
1430 public static final int VERSION_3 = 3 ;
31+ /**
32+ * OpenPGP v4 keys are (at the time of writing) widely used, but are subject to some attacks.
33+ *
34+ * @see <a href="https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-13.html#name-version-4-public-keys">
35+ * C-R - Version 4 Public Keys</a>
36+ */
1537 public static final int VERSION_4 = 4 ;
38+ /**
39+ * Non-Standard LibrePGP introduced v5, which is only supported by a subset of vendors.
40+ */
1641 public static final int LIBREPGP_5 = 5 ;
42+ /**
43+ * OpenPGP v6 keys are newly introduced.
44+ *
45+ * @see <a href="https://www.ietf.org/archive/id/draft-ietf-openpgp-crypto-refresh-13.html#name-version-6-public-keys">
46+ * C-R - Version 6 Public Keys</a>
47+ */
1748 public static final int VERSION_6 = 6 ;
1849
1950 private int version ;
51+ // Creation time of the key stored as seconds since epoch
2052 private long time ;
2153 private int validDays ;
2254 private int algorithm ;
2355 private BCPGKey key ;
2456
57+ /**
58+ * Parse a {@link PublicKeyPacket} from an OpenPGP {@link BCPGInputStream}.
59+ * The packet format is remembered as {@link PacketFormat#LEGACY}.
60+ * @param in packet input stream
61+ * @throws IOException
62+ */
2563 PublicKeyPacket (
2664 BCPGInputStream in )
2765 throws IOException
2866 {
2967 this (in , false );
3068 }
69+
70+ /**
71+ * Parse a {@link PublicKeyPacket} from an OpenPGP {@link BCPGInputStream}.
72+ * If <pre>newPacketFormat</pre> is true, the packet format is remembered as {@link PacketFormat#CURRENT},
73+ * otherwise as {@link PacketFormat#LEGACY}.
74+ * @param in packet input stream
75+ * @param newPacketFormat new packet format
76+ * @throws IOException
77+ */
3178 PublicKeyPacket (
3279 BCPGInputStream in ,
3380 boolean newPacketFormat )
@@ -36,6 +83,15 @@ public class PublicKeyPacket
3683 this (PUBLIC_KEY , in , newPacketFormat );
3784 }
3885
86+ /**
87+ * Parse a {@link PublicKeyPacket} or {@link PublicSubkeyPacket} from an OpenPGP {@link BCPGInputStream}.
88+ * If <pre>keyTag</pre> is {@link #PUBLIC_KEY}, the packet is a primary key.
89+ * If instead it is {@link #PUBLIC_SUBKEY}, it is a subkey packet.
90+ * The packet format is remembered as {@link PacketFormat#LEGACY}.
91+ * @param keyTag packet type ID
92+ * @param in packet input stream
93+ * @throws IOException
94+ */
3995 PublicKeyPacket (
4096 int keyTag ,
4197 BCPGInputStream in )
@@ -46,7 +102,7 @@ public class PublicKeyPacket
46102
47103 /**
48104 * Parse a {@link PublicKeyPacket} or {@link PublicSubkeyPacket} from an OpenPGP {@link BCPGInputStream}.
49- * If <pre>packetTypeID </pre> is {@link #PUBLIC_KEY}, the packet is a primary key.
105+ * If <pre>keyTag </pre> is {@link #PUBLIC_KEY}, the packet is a primary key.
50106 * If instead it is {@link #PUBLIC_SUBKEY}, it is a subkey packet.
51107 * If <pre>newPacketFormat</pre> is true, the packet format is remembered as {@link PacketFormat#CURRENT},
52108 * otherwise as {@link PacketFormat#LEGACY}.
@@ -155,11 +211,11 @@ private void parseKey(BCPGInputStream in, int algorithmId, long optLen)
155211 }
156212
157213 /**
158- * Construct version 4 public key packet.
214+ * Construct version 4 public primary key packet.
159215 *
160- * @param algorithm
161- * @param time
162- * @param key
216+ * @param algorithm public key algorithm id
217+ * @param time creation time
218+ * @param key key object
163219 * @deprecated use versioned {@link #PublicKeyPacket(int, int, Date, BCPGKey)} instead
164220 */
165221 @ Deprecated
@@ -171,15 +227,32 @@ public PublicKeyPacket(
171227 this (VERSION_4 , algorithm , time , key );
172228 }
173229
230+ /**
231+ * Construct an OpenPGP public primary key packet.
232+ * @param version packet version
233+ * @param algorithm public key algorithm id
234+ * @param time creation time
235+ * @param key key object
236+ */
174237 public PublicKeyPacket (
175238 int version ,
176239 int algorithm ,
177240 Date time ,
178241 BCPGKey key )
179- {
180- this (PUBLIC_KEY , version , algorithm , time , key );
181- }
242+ {
243+ this (PUBLIC_KEY , version , algorithm , time , key );
244+ }
182245
246+ /**
247+ * Construct an OpenPGP public key packet.
248+ * If <pre>keyTag</pre> is {@link #PUBLIC_KEY}, the packet is a primary key.
249+ * If instead it is {@link #PUBLIC_SUBKEY}, it is a subkey packet.
250+ * @param keyTag public key packet type ID
251+ * @param version packet version
252+ * @param algorithm public key algorithm id
253+ * @param time creation time
254+ * @param key key object
255+ */
183256 PublicKeyPacket (int keyTag , int version , int algorithm , Date time , BCPGKey key )
184257 {
185258 super (keyTag );
@@ -190,32 +263,59 @@ public PublicKeyPacket(
190263 this .key = key ;
191264 }
192265
193-
266+ /**
267+ * Return the packet version.
268+ * @return packet version
269+ */
194270 public int getVersion ()
195271 {
196272 return version ;
197273 }
198274
275+ /**
276+ * Return the {@link PublicKeyAlgorithmTags algorithm id} of the public key.
277+ * @return algorithm id
278+ */
199279 public int getAlgorithm ()
200280 {
201281 return algorithm ;
202282 }
203283
284+ /**
285+ * Only for v3 keys - The time in days since the keys creation, during which the key is valid.
286+ *
287+ * @return v3 key validity period in days since creation.
288+ * @deprecated v4 and v6 keys instead signal their expiration time via the
289+ * {@link org.bouncycastle.bcpg.sig.KeyExpirationTime} signature subpacket.
290+ */
204291 public int getValidDays ()
205292 {
206293 return validDays ;
207294 }
208295
296+ /**
297+ * Return the keys creation time.
298+ * @return creation time of the key
299+ */
209300 public Date getTime ()
210301 {
211302 return new Date (time * 1000 );
212303 }
213304
305+ /**
306+ * Return the key object.
307+ * @return key
308+ */
214309 public BCPGKey getKey ()
215310 {
216311 return key ;
217312 }
218313
314+ /**
315+ * Return the encoded packet contents without the packet frame.
316+ * @return encoded packet contents
317+ * @throws IOException
318+ */
219319 public byte [] getEncodedContents ()
220320 throws IOException
221321 {
@@ -247,6 +347,14 @@ public byte[] getEncodedContents()
247347 return bOut .toByteArray ();
248348 }
249349
350+ /**
351+ * Encode the packet to the OpenPGP {@link BCPGOutputStream}.
352+ * If the {@link BCPGOutputStream} packet format is set to {@link PacketFormat#ROUNDTRIP}, the result
353+ * of {@link #hasNewPacketFormat()} determines, which packet format is used to encode the packet.
354+ * Otherwise, the {@link BCPGOutputStream} dictates which format to use.
355+ * @param out packet output stream
356+ * @throws IOException
357+ */
250358 public void encode (
251359 BCPGOutputStream out )
252360 throws IOException
0 commit comments