|
| 1 | +package org.bouncycastle.openpgp; |
| 2 | + |
| 3 | +import org.bouncycastle.bcpg.AEADEncDataPacket; |
| 4 | +import org.bouncycastle.bcpg.BCPGInputStream; |
| 5 | +import org.bouncycastle.bcpg.InputStreamPacket; |
| 6 | +import org.bouncycastle.bcpg.SymmetricEncIntegrityPacket; |
| 7 | +import org.bouncycastle.openpgp.operator.PGPDataDecryptor; |
| 8 | +import org.bouncycastle.openpgp.operator.SessionKeyDataDecryptorFactory; |
| 9 | +import org.bouncycastle.util.io.TeeInputStream; |
| 10 | + |
| 11 | +import java.io.EOFException; |
| 12 | +import java.io.InputStream; |
| 13 | + |
| 14 | +public class PGPSessionKeyEncryptedData extends PGPEncryptedData |
| 15 | +{ |
| 16 | + |
| 17 | + private final PGPSessionKey sessionKey; |
| 18 | + |
| 19 | + PGPSessionKeyEncryptedData(PGPSessionKey sessionKey, InputStreamPacket encData) |
| 20 | + { |
| 21 | + super(encData); |
| 22 | + this.sessionKey = sessionKey; |
| 23 | + } |
| 24 | + |
| 25 | + public PGPSessionKey getSessionKey() |
| 26 | + { |
| 27 | + return sessionKey; |
| 28 | + } |
| 29 | + |
| 30 | + public InputStream getDataStream( |
| 31 | + SessionKeyDataDecryptorFactory dataDecryptorFactory) |
| 32 | + throws PGPException |
| 33 | + { |
| 34 | + if (encData instanceof AEADEncDataPacket) |
| 35 | + { |
| 36 | + AEADEncDataPacket aeadData = (AEADEncDataPacket) encData; |
| 37 | + |
| 38 | + if (aeadData.getAlgorithm() != getAlgorithm()) |
| 39 | + { |
| 40 | + throw new PGPException("session key and AEAD algorithm mismatch"); |
| 41 | + } |
| 42 | + |
| 43 | + PGPDataDecryptor dataDecryptor = dataDecryptorFactory.createDataDecryptor(aeadData.getAEADAlgorithm(), aeadData.getIV(), aeadData.getChunkSize(), sessionKey.getAlgorithm(), sessionKey.getKey()); |
| 44 | + |
| 45 | + BCPGInputStream encIn = encData.getInputStream(); |
| 46 | + |
| 47 | + return new BCPGInputStream(dataDecryptor.getInputStream(encIn)); |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + try |
| 52 | + { |
| 53 | + boolean withIntegrityPacket = encData instanceof SymmetricEncIntegrityPacket; |
| 54 | + PGPDataDecryptor dataDecryptor = dataDecryptorFactory.createDataDecryptor(withIntegrityPacket, sessionKey.getAlgorithm(), sessionKey.getKey()); |
| 55 | + |
| 56 | + return getDataStream(withIntegrityPacket, dataDecryptor); |
| 57 | + } catch (PGPException e) |
| 58 | + { |
| 59 | + throw e; |
| 60 | + } catch (Exception e) |
| 61 | + { |
| 62 | + throw new PGPException("Exception creating cipher", e); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private InputStream getDataStream( |
| 68 | + boolean withIntegrityPacket, |
| 69 | + PGPDataDecryptor dataDecryptor) |
| 70 | + throws PGPException |
| 71 | + { |
| 72 | + try |
| 73 | + { |
| 74 | + BCPGInputStream encIn = encData.getInputStream(); |
| 75 | + encIn.mark(dataDecryptor.getBlockSize() + 2); // iv + 2 octets checksum |
| 76 | + |
| 77 | + encStream = new BCPGInputStream(dataDecryptor.getInputStream(encIn)); |
| 78 | + |
| 79 | + if (withIntegrityPacket) |
| 80 | + { |
| 81 | + truncStream = new TruncatedStream(encStream); |
| 82 | + |
| 83 | + integrityCalculator = dataDecryptor.getIntegrityCalculator(); |
| 84 | + |
| 85 | + encStream = new TeeInputStream(truncStream, integrityCalculator.getOutputStream()); |
| 86 | + } |
| 87 | + |
| 88 | + byte[] iv = new byte[dataDecryptor.getBlockSize()]; |
| 89 | + for (int i = 0; i != iv.length; i++) |
| 90 | + { |
| 91 | + int ch = encStream.read(); |
| 92 | + |
| 93 | + if (ch < 0) |
| 94 | + { |
| 95 | + throw new EOFException("unexpected end of stream."); |
| 96 | + } |
| 97 | + |
| 98 | + iv[i] = (byte)ch; |
| 99 | + } |
| 100 | + |
| 101 | + int v1 = encStream.read(); |
| 102 | + int v2 = encStream.read(); |
| 103 | + |
| 104 | + if (v1 < 0 || v2 < 0) |
| 105 | + { |
| 106 | + throw new EOFException("unexpected end of stream."); |
| 107 | + } |
| 108 | + |
| 109 | + |
| 110 | + // Note: the oracle attack on "quick check" bytes is not deemed |
| 111 | + // a security risk for PBE (see PGPPublicKeyEncryptedData) |
| 112 | + |
| 113 | + boolean repeatCheckPassed = iv[iv.length - 2] == (byte)v1 |
| 114 | + && iv[iv.length - 1] == (byte)v2; |
| 115 | + |
| 116 | + // Note: some versions of PGP appear to produce 0 for the extra |
| 117 | + // bytes rather than repeating the two previous bytes |
| 118 | + boolean zeroesCheckPassed = v1 == 0 && v2 == 0; |
| 119 | + |
| 120 | + if (!repeatCheckPassed && !zeroesCheckPassed) |
| 121 | + { |
| 122 | + encIn.reset(); |
| 123 | + throw new PGPDataValidationException("data check failed."); |
| 124 | + } |
| 125 | + |
| 126 | + return encStream; |
| 127 | + } |
| 128 | + catch (PGPException e) |
| 129 | + { |
| 130 | + throw e; |
| 131 | + } |
| 132 | + catch (Exception e) |
| 133 | + { |
| 134 | + throw new PGPException("Exception creating cipher", e); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public int getAlgorithm() |
| 140 | + { |
| 141 | + return sessionKey.getAlgorithm(); |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments