Skip to content

Commit 7c958e4

Browse files
committed
PGP updates from bc-java
1 parent 6df85c6 commit 7c958e4

File tree

4 files changed

+60
-24
lines changed

4 files changed

+60
-24
lines changed

crypto/src/openpgp/PgpEncryptedDataGenerator.cs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,13 @@ private class PubMethod
8282
: EncMethod
8383
{
8484
internal PgpPublicKey pubKey;
85+
internal bool sessionKeyObfuscation;
8586
internal byte[][] data;
8687

87-
internal PubMethod(PgpPublicKey pubKey)
88+
internal PubMethod(PgpPublicKey pubKey, bool sessionKeyObfuscation)
8889
{
8990
this.pubKey = pubKey;
91+
this.sessionKeyObfuscation = sessionKeyObfuscation;
9092
}
9193

9294
public override void AddSessionInfo(
@@ -144,7 +146,7 @@ private byte[] EncryptSessionInfo(byte[] sessionInfo, SecureRandom random)
144146
IWrapper w = PgpUtilities.CreateWrapper(ecKey.SymmetricKeyAlgorithm);
145147
w.Init(true, new ParametersWithRandom(key, random));
146148

147-
byte[] paddedSessionData = PgpPad.PadSessionData(sessionInfo);
149+
byte[] paddedSessionData = PgpPad.PadSessionData(sessionInfo, sessionKeyObfuscation);
148150

149151
byte[] C = w.Wrap(paddedSessionData, 0, paddedSessionData.Length);
150152
byte[] VB = new MPInteger(new BigInteger(1, ephPub.Q.GetEncoded(false))).GetEncoded();
@@ -317,18 +319,22 @@ internal void DoAddMethod(byte[] rawPassPhrase, bool clearPassPhrase, HashAlgori
317319
}
318320

319321
/// <summary>Add a public key encrypted session key to the encrypted object.</summary>
320-
public void AddMethod(
321-
PgpPublicKey key)
322+
public void AddMethod(PgpPublicKey key)
322323
{
323-
if (!key.IsEncryptionKey)
324+
AddMethod(key, true);
325+
}
326+
327+
public void AddMethod(PgpPublicKey key, bool sessionKeyObfuscation)
328+
{
329+
if (!key.IsEncryptionKey)
324330
{
325331
throw new ArgumentException("passed in key not an encryption key!");
326332
}
327333

328-
methods.Add(new PubMethod(key));
334+
methods.Add(new PubMethod(key, sessionKeyObfuscation));
329335
}
330336

331-
private void AddCheckSum(
337+
private void AddCheckSum(
332338
byte[] sessionInfo)
333339
{
334340
Debug.Assert(sessionInfo != null);

crypto/src/openpgp/PgpPad.cs

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,55 @@ private PgpPad()
1111

1212
public static byte[] PadSessionData(byte[] sessionInfo)
1313
{
14-
byte[] result = new byte[40];
15-
16-
Array.Copy(sessionInfo, 0, result, 0, sessionInfo.Length);
14+
return PadSessionData(sessionInfo, true);
15+
}
1716

18-
byte padValue = (byte)(result.Length - sessionInfo.Length);
17+
public static byte[] PadSessionData(byte[] sessionInfo, bool obfuscate)
18+
{
19+
int length = sessionInfo.Length;
20+
int paddedLength = ((length >> 3) + 1) << 3;
1921

20-
for (int i = sessionInfo.Length; i != result.Length; i++)
22+
if (obfuscate)
2123
{
22-
result[i] = padValue;
24+
paddedLength = System.Math.Max(40, paddedLength);
2325
}
2426

27+
int padCount = paddedLength - length;
28+
byte padByte = (byte)padCount;
29+
30+
byte[] result = new byte[paddedLength];
31+
Array.Copy(sessionInfo, 0, result, 0, length);
32+
for (int i = length; i < paddedLength; ++i)
33+
{
34+
result[i] = padByte;
35+
}
2536
return result;
2637
}
2738

2839
public static byte[] UnpadSessionData(byte[] encoded)
2940
{
30-
byte padValue = encoded[encoded.Length - 1];
41+
int paddedLength = encoded.Length;
42+
byte padByte = encoded[paddedLength - 1];
43+
int padCount = padByte;
44+
int length = paddedLength - padCount;
45+
int last = length - 1;
3146

32-
for (int i = encoded.Length - padValue; i != encoded.Length; i++)
47+
int diff = 0;
48+
for (int i = 0; i < paddedLength; ++i)
3349
{
34-
if (encoded[i] != padValue)
35-
throw new PgpException("bad padding found in session data");
50+
int mask = (last - i) >> 31;
51+
diff |= (padByte ^ encoded[i]) & mask;
3652
}
3753

38-
byte[] taggedKey = new byte[encoded.Length - padValue];
54+
diff |= paddedLength & 7;
55+
diff |= (40 - paddedLength) >> 31;
3956

40-
Array.Copy(encoded, 0, taggedKey, 0, taggedKey.Length);
57+
if (diff != 0)
58+
throw new PgpException("bad padding found in session data");
4159

42-
return taggedKey;
60+
byte[] result = new byte[length];
61+
Array.Copy(encoded, 0, result, 0, length);
62+
return result;
4363
}
4464
}
4565
}

crypto/src/openpgp/PgpPublicKey.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ private static void UpdateDigest(IDigest d, BigInteger b)
7272
PgpSignature.PositiveCertification,
7373
PgpSignature.CasualCertification,
7474
PgpSignature.NoCertification,
75-
PgpSignature.DefaultCertification
75+
PgpSignature.DefaultCertification,
76+
PgpSignature.DirectKey,
7677
};
7778

7879
private long keyId;
@@ -369,6 +370,12 @@ public long GetValidSeconds()
369370
{
370371
return seconds;
371372
}
373+
374+
seconds = GetExpirationTimeFromSig(false, PgpSignature.DirectKey);
375+
if (seconds >= 0)
376+
{
377+
return seconds;
378+
}
372379
}
373380

374381
return 0;
@@ -388,6 +395,9 @@ private long GetExpirationTimeFromSig(bool selfSigned, int signatureType)
388395
if (hashed == null)
389396
continue;
390397

398+
if (!hashed.HasSubpacket(SignatureSubpacketTag.KeyExpireTime))
399+
continue;
400+
391401
long current = hashed.GetKeyExpirationTime();
392402

393403
if (sig.KeyId == this.KeyId)
@@ -447,10 +457,10 @@ public bool IsEncryptionKey
447457
}
448458
}
449459

450-
/// <summary>True, if this is a master key.</summary>
460+
/// <summary>True, if this could be a master key.</summary>
451461
public bool IsMasterKey
452462
{
453-
get { return subSigs == null; }
463+
get { return (subSigs == null) && !(this.IsEncryptionKey && publicPk.Algorithm != PublicKeyAlgorithmTag.RsaGeneral); }
454464
}
455465

456466
/// <summary>The algorithm code associated with the public key.</summary>

crypto/test/src/openpgp/examples/ClearSignedFileProcessor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Org.BouncyCastle.Bcpg.OpenPgp.Examples
1414
* To sign a file: ClearSignedFileProcessor -s fileName secretKey passPhrase.
1515
* </p>
1616
* <p>
17-
* To decrypt: ClearSignedFileProcessor -v fileName signatureFile publicKeyFile.
17+
* To decrypt: ClearSignedFileProcessor -v signatureFile publicKeyFile.
1818
* </p>
1919
*/
2020
public sealed class ClearSignedFileProcessor

0 commit comments

Comments
 (0)