Skip to content

Commit 13ce3ad

Browse files
committed
Merge branch 'pgpainless-pgpsecretkeyV6directkeysig'
2 parents 657b7d0 + 9eab8ed commit 13ce3ad

File tree

1 file changed

+39
-33
lines changed

1 file changed

+39
-33
lines changed

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

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,28 @@ public PGPSecretKey(
7878
* @param privKey the private key component.
7979
* @param pubKey the public key component.
8080
* @param checksumCalculator a calculator for the private key checksum
81-
* @param isMasterKey true if the key is a master key, false otherwise.
81+
* @param isPrimaryKey true if the key is a primary key, false otherwise.
8282
* @param keyEncryptor an encryptor for the key if required (null otherwise).
8383
* @throws PGPException if there is an issue creating the secret key packet.
8484
*/
8585
public PGPSecretKey(
8686
PGPPrivateKey privKey,
8787
PGPPublicKey pubKey,
8888
PGPDigestCalculator checksumCalculator,
89-
boolean isMasterKey,
89+
boolean isPrimaryKey,
9090
PBESecretKeyEncryptor keyEncryptor)
9191
throws PGPException
9292
{
93-
this.pub = buildPublicKey(isMasterKey, pubKey);
94-
this.secret = buildSecretKeyPacket(isMasterKey, privKey, pubKey, keyEncryptor, checksumCalculator);
93+
this.pub = buildPublicKey(isPrimaryKey, pubKey);
94+
this.secret = buildSecretKeyPacket(isPrimaryKey, privKey, pubKey, keyEncryptor, checksumCalculator);
9595
}
9696

97-
private static PGPPublicKey buildPublicKey(boolean isMasterKey, PGPPublicKey pubKey)
97+
private static PGPPublicKey buildPublicKey(boolean isPrimaryKey, PGPPublicKey pubKey)
9898
{
9999
PublicKeyPacket pubPacket = pubKey.publicPk;
100100

101101
// make sure we can actually do what's wanted
102-
if (isMasterKey && !(pubKey.isEncryptionKey() && pubPacket.getAlgorithm() != PublicKeyAlgorithmTags.RSA_GENERAL))
102+
if (isPrimaryKey && !(pubKey.isEncryptionKey() && pubPacket.getAlgorithm() != PublicKeyAlgorithmTags.RSA_GENERAL))
103103
{
104104
PGPPublicKey mstKey = new PGPPublicKey(pubKey);
105105
mstKey.publicPk = new PublicKeyPacket(pubPacket.getVersion(), pubPacket.getAlgorithm(), pubPacket.getTime(), pubPacket.getKey());
@@ -113,14 +113,14 @@ private static PGPPublicKey buildPublicKey(boolean isMasterKey, PGPPublicKey pub
113113
}
114114
}
115115

116-
private static SecretKeyPacket buildSecretKeyPacket(boolean isMasterKey, PGPPrivateKey privKey, PGPPublicKey pubKey, PBESecretKeyEncryptor keyEncryptor, PGPDigestCalculator checksumCalculator)
116+
private static SecretKeyPacket buildSecretKeyPacket(boolean isPrimaryKey, PGPPrivateKey privKey, PGPPublicKey pubKey, PBESecretKeyEncryptor keyEncryptor, PGPDigestCalculator checksumCalculator)
117117
throws PGPException
118118
{
119119
BCPGObject secKey = (BCPGObject)privKey.getPrivateKeyDataPacket();
120120

121121
if (secKey == null)
122122
{
123-
return generateSecretKeyPacket(isMasterKey, pubKey.publicPk, SymmetricKeyAlgorithmTags.NULL, new byte[0]);
123+
return generateSecretKeyPacket(isPrimaryKey, pubKey.publicPk, SymmetricKeyAlgorithmTags.NULL, new byte[0]);
124124
}
125125

126126
try
@@ -149,7 +149,7 @@ private static SecretKeyPacket buildSecretKeyPacket(boolean isMasterKey, PGPPriv
149149
if (keyEncryptor.getAeadAlgorithm() != 0)
150150
{
151151
s2kUsage = SecretKeyPacket.USAGE_AEAD;
152-
return generateSecretKeyPacket(isMasterKey, pubKey.publicPk, encAlgorithm, keyEncryptor.getAeadAlgorithm(), s2kUsage, s2k, iv, encData);
152+
return generateSecretKeyPacket(isPrimaryKey, pubKey.publicPk, encAlgorithm, keyEncryptor.getAeadAlgorithm(), s2kUsage, s2k, iv, encData);
153153
}
154154

155155
if (checksumCalculator != null)
@@ -165,13 +165,13 @@ private static SecretKeyPacket buildSecretKeyPacket(boolean isMasterKey, PGPPriv
165165
s2kUsage = SecretKeyPacket.USAGE_CHECKSUM;
166166
}
167167

168-
return generateSecretKeyPacket(isMasterKey, pubKey.publicPk, encAlgorithm, s2kUsage, s2k, iv, encData);
168+
return generateSecretKeyPacket(isPrimaryKey, pubKey.publicPk, encAlgorithm, s2kUsage, s2k, iv, encData);
169169
}
170170
else if (pubKey.getVersion() != PublicKeyPacket.VERSION_6)
171171
{
172172
pOut.write(checksum(null, keyData, keyData.length));
173173
}
174-
return generateSecretKeyPacket(isMasterKey, pubKey.publicPk, encAlgorithm, bOut.toByteArray());
174+
return generateSecretKeyPacket(isPrimaryKey, pubKey.publicPk, encAlgorithm, bOut.toByteArray());
175175
}
176176
catch (PGPException e)
177177
{
@@ -183,9 +183,9 @@ else if (pubKey.getVersion() != PublicKeyPacket.VERSION_6)
183183
}
184184
}
185185

186-
private static SecretKeyPacket generateSecretKeyPacket(boolean isMasterKey, PublicKeyPacket pubKey, int encAlgorithm, byte[] secKeyData)
186+
private static SecretKeyPacket generateSecretKeyPacket(boolean isPrimaryKey, PublicKeyPacket pubKey, int encAlgorithm, byte[] secKeyData)
187187
{
188-
if (isMasterKey)
188+
if (isPrimaryKey)
189189
{
190190
return new SecretKeyPacket(pubKey, encAlgorithm, null, null, secKeyData);
191191
}
@@ -195,9 +195,9 @@ private static SecretKeyPacket generateSecretKeyPacket(boolean isMasterKey, Publ
195195
}
196196
}
197197

198-
private static SecretKeyPacket generateSecretKeyPacket(boolean isMasterKey, PublicKeyPacket pubKey, int encAlgorithm, int s2kusage, S2K s2k, byte[] iv, byte[] secKeyData)
198+
private static SecretKeyPacket generateSecretKeyPacket(boolean isPrimaryKey, PublicKeyPacket pubKey, int encAlgorithm, int s2kusage, S2K s2k, byte[] iv, byte[] secKeyData)
199199
{
200-
if (isMasterKey)
200+
if (isPrimaryKey)
201201
{
202202
return new SecretKeyPacket(pubKey, encAlgorithm, s2kusage, s2k, iv, secKeyData);
203203
}
@@ -207,9 +207,9 @@ private static SecretKeyPacket generateSecretKeyPacket(boolean isMasterKey, Publ
207207
}
208208
}
209209

210-
private static SecretKeyPacket generateSecretKeyPacket(boolean isMasterKey, PublicKeyPacket pubKey, int encAlgorithm, int aeadAlgorithm, int s2kUsage, S2K s2K, byte[] iv, byte[] secKeyData)
210+
private static SecretKeyPacket generateSecretKeyPacket(boolean isPrimaryKey, PublicKeyPacket pubKey, int encAlgorithm, int aeadAlgorithm, int s2kUsage, S2K s2K, byte[] iv, byte[] secKeyData)
211211
{
212-
if (isMasterKey)
212+
if (isPrimaryKey)
213213
{
214214
return new SecretKeyPacket(pubKey, encAlgorithm, aeadAlgorithm, s2kUsage, s2K, iv, secKeyData);
215215
}
@@ -221,8 +221,11 @@ private static SecretKeyPacket generateSecretKeyPacket(boolean isMasterKey, Publ
221221

222222
/**
223223
* Construct a PGPSecretKey using the passed in private/public key pair and binding it to the passed in id
224-
* using a generated certification of certificationLevel.The secret key checksum is calculated using the original
224+
* using a generated certification of certificationLevel. The secret key checksum is calculated using the original
225225
* non-digest based checksum.
226+
* <p>
227+
* Note: In case of a version 6 OpenPGP key, you need to manually add a direct-key self-signature on the primary
228+
* key in order for it to be considered valid.
226229
*
227230
* @param certificationLevel the type of certification to be added.
228231
* @param keyPair the public/private keys to use.
@@ -247,32 +250,32 @@ public PGPSecretKey(
247250
}
248251

249252
/**
250-
* Construct a PGPSecretKey sub-key using the passed in private/public key pair and binding it to the master key pair.
253+
* Construct a PGPSecretKey sub-key using the passed in private/public key pair and binding it to the primary key pair.
251254
* The secret key checksum is calculated using the passed in checksum calculator.
252255
*
253-
* @param masterKeyPair the master public/private keys for the new subkey.
256+
* @param primaryKeyPair the primary public/private keys for the new subkey.
254257
* @param keyPair the public/private keys to use.
255258
* @param checksumCalculator a calculator for the private key checksum
256259
* @param certificationSignerBuilder the builder for generating the certification.
257260
* @param keyEncryptor an encryptor for the key if required (null otherwise).
258261
* @throws PGPException if there is an issue creating the secret key packet or the certification.
259262
*/
260263
public PGPSecretKey(
261-
PGPKeyPair masterKeyPair,
264+
PGPKeyPair primaryKeyPair,
262265
PGPKeyPair keyPair,
263266
PGPDigestCalculator checksumCalculator,
264267
PGPContentSignerBuilder certificationSignerBuilder,
265268
PBESecretKeyEncryptor keyEncryptor)
266269
throws PGPException
267270
{
268-
this(masterKeyPair, keyPair, checksumCalculator, null, null, certificationSignerBuilder, keyEncryptor);
271+
this(primaryKeyPair, keyPair, checksumCalculator, null, null, certificationSignerBuilder, keyEncryptor);
269272
}
270273

271274
/**
272-
* Construct a PGPSecretKey sub-key using the passed in private/public key pair and binding it to the master key pair.
275+
* Construct a PGPSecretKey sub-key using the passed in private/public key pair and binding it to the primary key pair.
273276
* The secret key checksum is calculated using the passed in checksum calculator.
274277
*
275-
* @param masterKeyPair the master public/private keys for the new subkey.
278+
* @param primaryKeyPair the primary public/private keys for the new subkey.
276279
* @param keyPair the public/private keys to use.
277280
* @param checksumCalculator calculator for PGP key checksums.
278281
* @param hashedPcks the hashed packets to be added to the certification.
@@ -282,7 +285,7 @@ public PGPSecretKey(
282285
* @throws PGPException if there is an issue creating the secret key packet or the certification.
283286
*/
284287
public PGPSecretKey(
285-
PGPKeyPair masterKeyPair,
288+
PGPKeyPair primaryKeyPair,
286289
PGPKeyPair keyPair,
287290
PGPDigestCalculator checksumCalculator,
288291
PGPSignatureSubpacketVector hashedPcks,
@@ -294,9 +297,9 @@ public PGPSecretKey(
294297
//
295298
// generate the certification
296299
//
297-
PGPSignatureGenerator sGen = new PGPSignatureGenerator(certificationSignerBuilder, masterKeyPair.getPublicKey());
300+
PGPSignatureGenerator sGen = new PGPSignatureGenerator(certificationSignerBuilder, primaryKeyPair.getPublicKey());
298301

299-
sGen.init(PGPSignature.SUBKEY_BINDING, masterKeyPair.getPrivateKey());
302+
sGen.init(PGPSignature.SUBKEY_BINDING, primaryKeyPair.getPrivateKey());
300303

301304
// do some basic checking if we are a signing key.
302305
if (!keyPair.getPublicKey().isEncryptionKey())
@@ -311,7 +314,7 @@ public PGPSecretKey(
311314

312315
try
313316
{
314-
subGen.addEmbeddedSignature(false, signatureGenerator.generateCertification(masterKeyPair.getPublicKey(), keyPair.getPublicKey()));
317+
subGen.addEmbeddedSignature(false, signatureGenerator.generateCertification(primaryKeyPair.getPublicKey(), keyPair.getPublicKey()));
315318

316319
hashedPcks = subGen.generate();
317320
}
@@ -331,7 +334,7 @@ else if (!hashedPcks.hasSubpacket(SignatureSubpacketTags.EMBEDDED_SIGNATURE))
331334

332335
List<PGPSignature> subSigs = new ArrayList<PGPSignature>();
333336

334-
subSigs.add(sGen.generateCertification(masterKeyPair.getPublicKey(), keyPair.getPublicKey()));
337+
subSigs.add(sGen.generateCertification(primaryKeyPair.getPublicKey(), keyPair.getPublicKey()));
335338

336339
// replace the public key packet structure with a public subkey one.
337340
PGPPublicKey pubSubKey = new PGPPublicKey(keyPair.getPublicKey(), null, subSigs);
@@ -345,9 +348,12 @@ else if (!hashedPcks.hasSubpacket(SignatureSubpacketTags.EMBEDDED_SIGNATURE))
345348
/**
346349
* Construct a PGPSecretKey using the passed in private/public key pair and binding it to the passed in id
347350
* using a generated certification of certificationLevel.
351+
* <p>
352+
* Note: In case of a version 6 OpenPGP key, you need to manually add a direct-key self-signature on the primary
353+
* key in order for it to be considered valid.
348354
*
349355
* @param certificationLevel the type of certification to be added.
350-
* @param keyPair the public/private keys to use.
356+
* @param keyPair the primary public/private keys to use.
351357
* @param id the id to bind to the key.
352358
* @param checksumCalculator a calculator for the private key checksum.
353359
* @param hashedPcks the hashed packets to be added to the certification.
@@ -424,9 +430,9 @@ public boolean isSigningKey()
424430
}
425431

426432
/**
427-
* Return true if this is a master key.
433+
* Return true if this is a primary key.
428434
*
429-
* @return true if a master key.
435+
* @return true if a primary key.
430436
*/
431437
public boolean isMasterKey()
432438
{
@@ -436,7 +442,7 @@ public boolean isMasterKey()
436442
/**
437443
* Detect if the Secret Key's Private Key is empty or not
438444
*
439-
* @return boolean whether or not the private key is empty
445+
* @return boolean whether the private key is empty
440446
*/
441447
public boolean isPrivateKeyEmpty()
442448
{

0 commit comments

Comments
 (0)