001: package org.bouncycastle.bcpg;
002:
003: import java.io.*;
004:
005: /**
006: * basic packet for a PGP secret key
007: */
008: public class SecretKeyPacket extends ContainedPacket implements
009: PublicKeyAlgorithmTags {
010: public static final int USAGE_NONE = 0x00;
011: public static final int USAGE_CHECKSUM = 0xff;
012: public static final int USAGE_SHA1 = 0xfe;
013:
014: private PublicKeyPacket pubKeyPacket;
015: private byte[] secKeyData;
016: private int s2kUsage;
017: private int encAlgorithm;
018: private S2K s2k;
019: private byte[] iv;
020:
021: /**
022: *
023: * @param in
024: * @throws IOException
025: */
026: SecretKeyPacket(BCPGInputStream in) throws IOException {
027: pubKeyPacket = new PublicKeyPacket(in);
028:
029: s2kUsage = in.read();
030:
031: if (s2kUsage == USAGE_CHECKSUM || s2kUsage == USAGE_SHA1) {
032: encAlgorithm = in.read();
033: s2k = new S2K(in);
034: } else {
035: encAlgorithm = s2kUsage;
036: }
037:
038: if (!(s2k != null && s2k.getType() == S2K.GNU_DUMMY_S2K && s2k
039: .getProtectionMode() == 0x01)) {
040: if (s2kUsage != 0) {
041: if (encAlgorithm < 7) {
042: iv = new byte[8];
043: } else {
044: iv = new byte[16];
045: }
046: in.readFully(iv, 0, iv.length);
047: }
048: }
049:
050: if (in.available() != 0) {
051: secKeyData = new byte[in.available()];
052:
053: in.readFully(secKeyData);
054: }
055: }
056:
057: /**
058: *
059: * @param pubKeyPacket
060: * @param encAlgorithm
061: * @param s2k
062: * @param iv
063: * @param secKeyData
064: */
065: public SecretKeyPacket(PublicKeyPacket pubKeyPacket,
066: int encAlgorithm, S2K s2k, byte[] iv, byte[] secKeyData) {
067: this .pubKeyPacket = pubKeyPacket;
068: this .encAlgorithm = encAlgorithm;
069:
070: if (encAlgorithm != SymmetricKeyAlgorithmTags.NULL) {
071: this .s2kUsage = USAGE_CHECKSUM;
072: } else {
073: this .s2kUsage = USAGE_NONE;
074: }
075:
076: this .s2k = s2k;
077: this .iv = iv;
078: this .secKeyData = secKeyData;
079: }
080:
081: public SecretKeyPacket(PublicKeyPacket pubKeyPacket,
082: int encAlgorithm, int s2kUsage, S2K s2k, byte[] iv,
083: byte[] secKeyData) {
084: this .pubKeyPacket = pubKeyPacket;
085: this .encAlgorithm = encAlgorithm;
086: this .s2kUsage = s2kUsage;
087: this .s2k = s2k;
088: this .iv = iv;
089: this .secKeyData = secKeyData;
090: }
091:
092: public int getEncAlgorithm() {
093: return encAlgorithm;
094: }
095:
096: public int getS2KUsage() {
097: return s2kUsage;
098: }
099:
100: public byte[] getIV() {
101: return iv;
102: }
103:
104: public S2K getS2K() {
105: return s2k;
106: }
107:
108: public PublicKeyPacket getPublicKeyPacket() {
109: return pubKeyPacket;
110: }
111:
112: public byte[] getSecretKeyData() {
113: return secKeyData;
114: }
115:
116: public byte[] getEncodedContents() throws IOException {
117: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
118: BCPGOutputStream pOut = new BCPGOutputStream(bOut);
119:
120: pOut.write(pubKeyPacket.getEncodedContents());
121:
122: pOut.write(s2kUsage);
123:
124: if (s2kUsage == USAGE_CHECKSUM || s2kUsage == USAGE_SHA1) {
125: pOut.write(encAlgorithm);
126: pOut.writeObject(s2k);
127: }
128:
129: if (iv != null) {
130: pOut.write(iv);
131: }
132:
133: if (secKeyData != null) {
134: pOut.write(secKeyData);
135: }
136:
137: return bOut.toByteArray();
138: }
139:
140: public void encode(BCPGOutputStream out) throws IOException {
141: out.writePacket(SECRET_KEY, getEncodedContents(), true);
142: }
143: }
|