Skip to content

Commit d9e2c2e

Browse files
author
gefeili
committed
#1792 Add documentation to PGPSignature
2 parents 902266f + 0815787 commit d9e2c2e

File tree

1 file changed

+222
-0
lines changed

1 file changed

+222
-0
lines changed

pg/src/main/java/org/bouncycastle/openpgp/PGPSignature.java

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,161 @@
3434
public class PGPSignature
3535
extends PGPDefaultSignatureGenerator
3636
{
37+
/**
38+
* The signature is made over some binary data.
39+
* No preprocessing is applied.
40+
* <br>
41+
* This signature type is used to create data signatures.
42+
*
43+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-binary-signature-type-id-0x">
44+
* RFC9580 - Binary Signature of a Document</a>
45+
*/
3746
public static final int BINARY_DOCUMENT = 0x00;
47+
48+
/**
49+
* The signature is made over text data.
50+
* In a preprocessing step, the text data is canonicalized (line endings may be altered).
51+
* <br>
52+
* This signature type is used to create data signatures.
53+
*
54+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-text-signature-type-id-0x01">
55+
* RFC9580 - Text Signature of a Canonical Document</a>
56+
*/
3857
public static final int CANONICAL_TEXT_DOCUMENT = 0x01;
58+
59+
/**
60+
* The signature is made only over its own signature subpackets.
61+
*
62+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-standalone-signature-type-i">
63+
* RFC9580 - Standalone Signature</a>
64+
*/
3965
public static final int STAND_ALONE = 0x02;
4066

67+
/**
68+
* Generic certification over a user-id or user-attribute.
69+
* The issuer of a generic certification does not make any claims as to what extent they checked
70+
* the authenticity of the identity claim.
71+
* <br>
72+
* This signature type is used to bind user information to primary keys, or to certify the identity claim
73+
* of a third party.
74+
*
75+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-generic-certification-signa">
76+
* RFC9580 - Generic Certification Signature of a User ID and Public Key Packet</a>
77+
*/
4178
public static final int DEFAULT_CERTIFICATION = 0x10;
79+
80+
/**
81+
* Persona certification over a user-id or user-attribute.
82+
* The issuer of a persona certification did explicitly not check the authenticity of the identity claim.
83+
* <br>
84+
* This signature type is used to bind user information to primary keys, or to certify the identity claim
85+
* of a third party.
86+
*
87+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-persona-certification-signa">
88+
* RFC9580 - Persona Certification Signature of a User ID and Public Key Packet</a>
89+
*/
4290
public static final int NO_CERTIFICATION = 0x11;
91+
92+
/**
93+
* Casual certification over a user-id or user-attribute.
94+
* The issuer of a casual certification did some casual verification to check the authenticity of the
95+
* identity claim.
96+
* <br>
97+
* This signature type is used to bind user information to primary keys, or to certify the identity claim
98+
* of a third party.
99+
*
100+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-casual-certification-signat">
101+
* RFC9580 - Casual Certification of a User ID an Public Key Packet</a>
102+
*/
43103
public static final int CASUAL_CERTIFICATION = 0x12;
104+
105+
/**
106+
* Positive certification over a user-id or user-attribute.
107+
* The issuer of a positive certification did extensive effort to check the authenticity of the identity claim.
108+
* <br>
109+
* This signature type is used to bind user information to primary keys, or to certify the identity claim
110+
* of a third party.
111+
*
112+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-positive-certification-sign">
113+
* RFC9580 - Positive Certification Signature of a User ID and Public Key Packet</a>
114+
*/
44115
public static final int POSITIVE_CERTIFICATION = 0x13;
45116

117+
/**
118+
* Subkey Binding Signature to bind a subkey to a primary key.
119+
* This signature type is used to bind a subkey to the primary key of a certificate.
120+
*
121+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-subkey-binding-signature-ty">
122+
* RFC9580 - Subkey Binding Signature</a>
123+
*/
46124
public static final int SUBKEY_BINDING = 0x18;
125+
126+
/**
127+
* Primary-Key Binding Signature to bind a signing-capable subkey to a primary key.
128+
* This (back-) signature is used as an embedded signature in a {@link #SUBKEY_BINDING} signature and acts as
129+
* a claim by the subkey, stating that it is in fact a subkey of the primary key.
130+
*
131+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-primary-key-binding-signatu">
132+
* RFC9580 - Primary Key Binding Signature</a>
133+
*/
47134
public static final int PRIMARYKEY_BINDING = 0x19;
135+
136+
/**
137+
* The signature is made directly over a primary key.
138+
* If issued as a self-signature, its contents apply to the whole certificate, meaning this signature
139+
* is appropriate to set algorithm preferences which also apply to its subkeys.
140+
* Issued as a signature over a third-party certificate, it can be used to mark said certificate as a CA.
141+
*
142+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-direct-key-signature-type-i">
143+
* RFC9580 - Direct Key Signature</a>
144+
*/
48145
public static final int DIRECT_KEY = 0x1f;
146+
147+
/**
148+
* The signature is used to revoke a primary key (and in turn the whole certificate with all its subkeys).
149+
*
150+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-key-revocation-signature-ty">
151+
* RFC9580 - Key Revocation Signature</a>
152+
*/
49153
public static final int KEY_REVOCATION = 0x20;
154+
155+
/**
156+
* The signature is used to revoke the binding of a particular subkey.
157+
*
158+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-subkey-revocation-signature">
159+
* RFC9580 - Subkey Revocation Signature</a>
160+
*/
50161
public static final int SUBKEY_REVOCATION = 0x28;
162+
163+
/**
164+
* The signature is used to revoke a user-id certification signature
165+
* ({@link #DEFAULT_CERTIFICATION}, {@link #NO_CERTIFICATION}, {@link #CASUAL_CERTIFICATION},
166+
* {@link #POSITIVE_CERTIFICATION}) or {@link #DIRECT_KEY} signature.
167+
* Issued as a self-signature, it can be used to revoke an identity claim.
168+
* Issued over a third-party certificate, it revokes the attestation of the third-party's claim.
169+
*
170+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-certification-revocation-si">
171+
* RFC9580 - Certification Revocation Signature</a>
172+
*/
51173
public static final int CERTIFICATION_REVOCATION = 0x30;
174+
175+
/**
176+
* The signature is only meaningful for the timestamp contained in it.
177+
*
178+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-timestamp-signature-type-id">
179+
* RFC9580 - Timestamp Signature</a>
180+
*/
52181
public static final int TIMESTAMP = 0x40;
182+
183+
/**
184+
* This signature is issued over another signature and can act as an attestation of that signature.
185+
* This concept can be used to "approve" third-party certifications over the own key, allowing
186+
* third-party certifications to be published on key-servers that usually strip such signatures
187+
* to prevent certificate flooding.
188+
*
189+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-third-party-confirmation-si">
190+
* RFC9580 - Third-Party Confirmation Signature/a>
191+
*/
53192
public static final int THIRD_PARTY_CONFIRMATION = 0x50;
54193

55194
private final SignaturePacket sigPck;
@@ -67,6 +206,12 @@ private static SignaturePacket cast(Packet packet)
67206
return (SignaturePacket)packet;
68207
}
69208

209+
/**
210+
* Parse a {@link PGPSignature} from an OpenPGP packet input stream.
211+
* @param pIn packet input stream
212+
* @throws IOException
213+
* @throws PGPException
214+
*/
70215
public PGPSignature(
71216
BCPGInputStream pIn)
72217
throws IOException, PGPException
@@ -149,6 +294,13 @@ public boolean isCertification()
149294
return isCertification(getSignatureType());
150295
}
151296

297+
/**
298+
* Initialize the signature for verification.
299+
*
300+
* @param verifierBuilderProvider provide the implementation for signature verification
301+
* @param pubKey issuer public key
302+
* @throws PGPException
303+
*/
152304
public void init(PGPContentVerifierBuilderProvider verifierBuilderProvider, PGPPublicKey pubKey)
153305
throws PGPException
154306
{
@@ -202,6 +354,14 @@ private void updateWithSalt()
202354
}
203355
}
204356

357+
/**
358+
* Finish the verification and return true if the signature is "correct".
359+
* Note: The fact that this method returned <pre>true</pre> does not yet mean that the signature is valid.
360+
* A correct signature may very well be expired, the issuer key may be revoked, etc.
361+
* All these constraints are not checked by this method.
362+
* @return true if the signature is correct
363+
* @throws PGPException
364+
*/
205365
public boolean verify()
206366
throws PGPException
207367
{
@@ -414,6 +574,13 @@ boolean doVerifyCertification(
414574
return verifier.verify(this.getSignature());
415575
}
416576

577+
/**
578+
* Return the type id of the signature.
579+
* @see <a href="https://www.rfc-editor.org/rfc/rfc9580.html#name-signature-types">
580+
* RFC9580 - Signature Types</a>
581+
*
582+
* @return type id
583+
*/
417584
public int getSignatureType()
418585
{
419586
return sigPck.getSignatureType();
@@ -454,11 +621,28 @@ public boolean hasSubpackets()
454621
return sigPck.getHashedSubPackets() != null || sigPck.getUnhashedSubPackets() != null;
455622
}
456623

624+
/**
625+
* Return the hashed subpackets of the signature.
626+
* Hashed signature subpackets are covered by the signature.
627+
*
628+
* @return hashed signature subpackets
629+
*/
457630
public PGPSignatureSubpacketVector getHashedSubPackets()
458631
{
459632
return createSubpacketVector(sigPck.getHashedSubPackets());
460633
}
461634

635+
/**
636+
* Return the unhashed subpackets of the signature.
637+
* As unhashed signature subpackets are NOT covered by the signature, an attacker might inject false
638+
* information after the fact, therefore only "self-authenticating" information from this area can
639+
* be trusted.
640+
* Self-authenticating information are for example the {@link org.bouncycastle.bcpg.sig.IssuerKeyID}
641+
* or {@link org.bouncycastle.bcpg.sig.IssuerFingerprint}, whose authenticity can be confirmed by
642+
* verifying the signature using the declared key.
643+
*
644+
* @return unhashed signature subpackets
645+
*/
462646
public PGPSignatureSubpacketVector getUnhashedSubPackets()
463647
{
464648
return createSubpacketVector(sigPck.getUnhashedSubPackets());
@@ -474,11 +658,21 @@ private PGPSignatureSubpacketVector createSubpacketVector(SignatureSubpacket[] p
474658
return null;
475659
}
476660

661+
/**
662+
* Return the salt of a v6 signature.
663+
* @return salt
664+
*/
477665
byte[] getSalt()
478666
{
479667
return sigPck.getSalt();
480668
}
481669

670+
/**
671+
* Return the cryptographic raw signature contained in the OpenPGP signature packet.
672+
* The value is dependent on the signing algorithm.
673+
* @return cryptographic signature
674+
* @throws PGPException
675+
*/
482676
public byte[] getSignature()
483677
throws PGPException
484678
{
@@ -532,6 +726,11 @@ else if (getKeyAlgorithm() == PublicKeyAlgorithmTags.EDDSA_LEGACY)
532726
return signature;
533727
}
534728

729+
/**
730+
* Return the OpenPGP packet encoding of the signature.
731+
* @return OpenPGP packet encoding
732+
* @throws IOException
733+
*/
535734
public byte[] getEncoded()
536735
throws IOException
537736
{
@@ -559,6 +758,12 @@ public byte[] getEncoded(boolean forTransfer)
559758
return bOut.toByteArray();
560759
}
561760

761+
/**
762+
* Encode the signature to an OpenPGP packet stream.
763+
* This method does not strip out any trust packets.
764+
* @param outStream packet stream
765+
* @throws IOException
766+
*/
562767
public void encode(
563768
OutputStream outStream)
564769
throws IOException
@@ -607,11 +812,28 @@ public static boolean isCertification(int signatureType)
607812
|| PGPSignature.POSITIVE_CERTIFICATION == signatureType;
608813
}
609814

815+
/**
816+
* Return true, if the cryptographic signature encoding of the two signatures match.
817+
* @param sig1 first signature
818+
* @param sig2 second signature
819+
* @return true if both signatures contain the same cryptographic signature
820+
*/
610821
public static boolean isSignatureEncodingEqual(PGPSignature sig1, PGPSignature sig2)
611822
{
612823
return Arrays.areEqual(sig1.sigPck.getSignatureBytes(), sig2.sigPck.getSignatureBytes());
613824
}
614825

826+
/**
827+
* Join two copies of the same signature.
828+
* As an entity might append additional information to an existing signatures unhashed subpacket area
829+
* (e.g. an embedded {@link #THIRD_PARTY_CONFIRMATION} signature), an implementation might want to
830+
* join an existing instance of a signature with an updated copy, e.g. retrieved from a key server.
831+
* This method merges both signature instances by joining unhashed subpackets.
832+
* @param sig1 first signature
833+
* @param sig2 second signature
834+
* @return merged signature
835+
* @throws PGPException
836+
*/
615837
public static PGPSignature join(PGPSignature sig1, PGPSignature sig2)
616838
throws PGPException
617839
{

0 commit comments

Comments
 (0)