0001: package org.bouncycastle.openpgp.test;
0002:
0003: import org.bouncycastle.bcpg.BCPGOutputStream;
0004: import org.bouncycastle.bcpg.HashAlgorithmTags;
0005: import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
0006: import org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags;
0007: import org.bouncycastle.jce.provider.BouncyCastleProvider;
0008: import org.bouncycastle.openpgp.PGPCompressedData;
0009: import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
0010: import org.bouncycastle.openpgp.PGPEncryptedDataGenerator;
0011: import org.bouncycastle.openpgp.PGPEncryptedDataList;
0012: import org.bouncycastle.openpgp.PGPKeyPair;
0013: import org.bouncycastle.openpgp.PGPLiteralData;
0014: import org.bouncycastle.openpgp.PGPLiteralDataGenerator;
0015: import org.bouncycastle.openpgp.PGPObjectFactory;
0016: import org.bouncycastle.openpgp.PGPOnePassSignature;
0017: import org.bouncycastle.openpgp.PGPOnePassSignatureList;
0018: import org.bouncycastle.openpgp.PGPPBEEncryptedData;
0019: import org.bouncycastle.openpgp.PGPPrivateKey;
0020: import org.bouncycastle.openpgp.PGPPublicKey;
0021: import org.bouncycastle.openpgp.PGPPublicKeyEncryptedData;
0022: import org.bouncycastle.openpgp.PGPPublicKeyRing;
0023: import org.bouncycastle.openpgp.PGPSecretKey;
0024: import org.bouncycastle.openpgp.PGPSecretKeyRing;
0025: import org.bouncycastle.openpgp.PGPSignature;
0026: import org.bouncycastle.openpgp.PGPSignatureGenerator;
0027: import org.bouncycastle.openpgp.PGPSignatureList;
0028: import org.bouncycastle.openpgp.PGPUtil;
0029: import org.bouncycastle.openpgp.PGPV3SignatureGenerator;
0030: import org.bouncycastle.util.encoders.Base64;
0031: import org.bouncycastle.util.encoders.Hex;
0032: import org.bouncycastle.util.test.SimpleTest;
0033: import org.bouncycastle.util.test.UncloseableOutputStream;
0034:
0035: import javax.crypto.Cipher;
0036: import java.io.ByteArrayInputStream;
0037: import java.io.ByteArrayOutputStream;
0038: import java.io.IOException;
0039: import java.io.InputStream;
0040: import java.io.OutputStream;
0041: import java.security.KeyPair;
0042: import java.security.KeyPairGenerator;
0043: import java.security.PublicKey;
0044: import java.security.SecureRandom;
0045: import java.security.Security;
0046: import java.util.Date;
0047: import java.util.Iterator;
0048:
0049: public class PGPRSATest extends SimpleTest {
0050: byte[] testPubKey = Base64
0051: .decode("mIsEPz2nJAEEAOTVqWMvqYE693qTgzKv/TJpIj3hI8LlYPC6m1dk0z3bDLwVVk9F"
0052: + "FAB+CWS8RdFOWt/FG3tEv2nzcoNdRvjv9WALyIGNawtae4Ml6oAT06/511yUzXHO"
0053: + "k+9xK3wkXN5jdzUhf4cA2oGpLSV/pZlocsIDL+jCUQtumUPwFodmSHhzAAYptC9F"
0054: + "cmljIEVjaGlkbmEgKHRlc3Qga2V5KSA8ZXJpY0Bib3VuY3ljYXN0bGUub3JnPoi4"
0055: + "BBMBAgAiBQI/PackAhsDBQkAg9YABAsHAwIDFQIDAxYCAQIeAQIXgAAKCRA1WGFG"
0056: + "/fPzc8WMA/9BbjuB8E48QAlxoiVf9U8SfNelrz/ONJA/bMvWr/JnOGA9PPmFD5Uc"
0057: + "+kV/q+i94dEMjsC5CQ1moUHWSP2xlQhbOzBP2+oPXw3z2fBs9XJgnTH6QWMAAvLs"
0058: + "3ug9po0loNHLobT/D/XdXvcrb3wvwvPT2FptZqrtonH/OdzT9JdfrA==");
0059:
0060: byte[] testPrivKey = Base64
0061: .decode("lQH8BD89pyQBBADk1aljL6mBOvd6k4Myr/0yaSI94SPC5WDwuptXZNM92wy8FVZP"
0062: + "RRQAfglkvEXRTlrfxRt7RL9p83KDXUb47/VgC8iBjWsLWnuDJeqAE9Ov+ddclM1x"
0063: + "zpPvcSt8JFzeY3c1IX+HANqBqS0lf6WZaHLCAy/owlELbplD8BaHZkh4cwAGKf4D"
0064: + "AwKbLeIOVYTEdWD5v/YgW8ERs0pDsSIfBTvsJp2qA798KeFuED6jGsHUzdi1M990"
0065: + "6PRtplQgnoYmYQrzEc6DXAiAtBR4Kuxi4XHx0ZR2wpVlVxm2Ypgz7pbBNWcWqzvw"
0066: + "33inl7tR4IDsRdJOY8cFlN+1tSCf16sDidtKXUVjRjZNYJytH18VfSPlGXMeYgtw"
0067: + "3cSGNTERwKaq5E/SozT2MKTiORO0g0Mtyz+9MEB6XVXFavMun/mXURqbZN/k9BFb"
0068: + "z+TadpkihrLD1xw3Hp+tpe4CwPQ2GdWKI9KNo5gEnbkJgLrSMGgWalPhknlNHRyY"
0069: + "bSq6lbIMJEE3LoOwvYWwweR1+GrV9farJESdunl1mDr5/d6rKru+FFDwZM3na1IF"
0070: + "4Ei4FpqhivZ4zG6pN5XqLy+AK85EiW4XH0yAKX1O4YlbmDU4BjxhiwTdwuVMCjLO"
0071: + "5++jkz5BBQWdFX8CCMA4FJl36G70IbGzuFfOj07ly7QvRXJpYyBFY2hpZG5hICh0"
0072: + "ZXN0IGtleSkgPGVyaWNAYm91bmN5Y2FzdGxlLm9yZz6IuAQTAQIAIgUCPz2nJAIb"
0073: + "AwUJAIPWAAQLBwMCAxUCAwMWAgECHgECF4AACgkQNVhhRv3z83PFjAP/QW47gfBO"
0074: + "PEAJcaIlX/VPEnzXpa8/zjSQP2zL1q/yZzhgPTz5hQ+VHPpFf6voveHRDI7AuQkN"
0075: + "ZqFB1kj9sZUIWzswT9vqD18N89nwbPVyYJ0x+kFjAALy7N7oPaaNJaDRy6G0/w/1"
0076: + "3V73K298L8Lz09habWaq7aJx/znc0/SXX6w=");
0077:
0078: byte[] testPubKeyV3 = Base64
0079: .decode("mQCNAz+zvlEAAAEEAMS22jgXbOZ/D3xWgM2kauSdzrwlU7Ms5hDW05ObqQyO"
0080: + "FfQoKKMhfupyoa7J3x04VVBKu6Eomvr1es+VImH0esoeWFFahNOYq/I+jRRB"
0081: + "woOhAGZ5UB2/hRd7rFmxqp6sCXi8wmLO2tAorlTzAiNNvl7xF4cQZpc0z56F"
0082: + "wdi2fBUJAAURtApGSVhDSVRZX1FBiQCVAwUQP7O+UZ6Fwdi2fBUJAQFMwwQA"
0083: + "qRnFsdg4xQnB8Y5d4cOpXkIn9AZgYS3cxtuSJB84vG2CgC39nfv4c+nlLkWP"
0084: + "4puG+mZuJNgVoE84cuAF4I//1anKjlU7q1M6rFQnt5S4uxPyG3dFXmgyU1b4"
0085: + "PBOnA0tIxjPzlIhJAMsPCGGA5+5M2JP0ad6RnzqzE3EENMX+GqY=");
0086:
0087: byte[] testPrivKeyV3 = Base64
0088: .decode("lQHfAz+zvlEAAAEEAMS22jgXbOZ/D3xWgM2kauSdzrwlU7Ms5hDW05ObqQyO"
0089: + "FfQoKKMhfupyoa7J3x04VVBKu6Eomvr1es+VImH0esoeWFFahNOYq/I+jRRB"
0090: + "woOhAGZ5UB2/hRd7rFmxqp6sCXi8wmLO2tAorlTzAiNNvl7xF4cQZpc0z56F"
0091: + "wdi2fBUJAAURAXWwRBZQHNikA/f0ScLLjrXi4s0hgQecg+dkpDow94eu5+AR"
0092: + "0DzZnfurpgfUJCNiDi5W/5c3Zj/xyrfMAgkbCgJ1m6FZqAQh7Mq73l7Kfu4/"
0093: + "XIkyDF3tDgRuZNezB+JuElX10tV03xumHepp6M6CfhXqNJ15F33F99TA5hXY"
0094: + "CPYD7SiSOpIhQkCOAgDAA63imxbpuKE2W7Y4I1BUHB7WQi8ZdkZd04njNTv+"
0095: + "rFUuOPapQVfbWG0Vq8ld3YmJB4QWsa2mmqn+qToXbwufAgBpXkjvqK5yPiHF"
0096: + "Px2QbFc1VqoCJB6PO5JRIqEiUZBFGdDlLxt3VSyqz7IZ/zEnxZq+tPCGGGSm"
0097: + "/sAGiMvENcHVAfy0kTXU42TxEAYJyyNyqjXOobDJpEV1mKhFskRXt7tbMfOS"
0098: + "Yf91oX8f6xw6O2Nal+hU8dS0Bmfmk5/enHmvRLHQocO0CkZJWENJVFlfUUE=");
0099:
0100: byte[] sig1 = Base64
0101: .decode("owGbwMvMwMRoGpHo9vfz52LGNTJJnBmpOTn5eiUVJfb23JvAHIXy/KKcFEWuToap"
0102: + "zKwMIGG4Bqav0SwMy3yParsEKi2LMGI9xhh65sBxb05n5++ZLcWNJ/eLFKdWbm95"
0103: + "tHbDV7GMwj/tUctUpFUXWPYFCLdNsDiVNuXbQvZtdXV/5xzY+9w1nCnijH9JoNiJ"
0104: + "22n2jo0zo30/TZLo+jDl2vTzIvPeLEsPM3ZUE/1Ytqs4SG2TxIQbH7xf3uzcYXq2"
0105: + "5Fw9AA==");
0106:
0107: byte[] sig1crc = Base64.decode("+3i0");
0108:
0109: byte[] subKey = Base64
0110: .decode("lQH8BD89pyQBBADk1aljL6mBOvd6k4Myr/0yaSI94SPC5WDwuptXZNM92wy8FVZP"
0111: + "RRQAfglkvEXRTlrfxRt7RL9p83KDXUb47/VgC8iBjWsLWnuDJeqAE9Ov+ddclM1x"
0112: + "zpPvcSt8JFzeY3c1IX+HANqBqS0lf6WZaHLCAy/owlELbplD8BaHZkh4cwAGKf4D"
0113: + "AwKt6ZC7iqsQHGDNn2ZAuhS+ZwiFC+BToW9Vq6rwggWjgM/SThv55rfDk7keiXUT"
0114: + "MyUcZVeYBe4Jttb4fAAm83hNztFu6Jvm9ITcm7YvnasBtVQjppaB+oYZgsTtwK99"
0115: + "LGC3mdexnriCLxPN6tDFkGhzdOcYZfK6py4Ska8Dmq9nOZU9Qtv7Pm3qa5tuBvYw"
0116: + "myTxeaJYifZTu/sky3Gj+REb8WonbgAJX/sLNBPUt+vYko+lxU8uqZpVEMU//hGG"
0117: + "Rns2gIHdbSbIe1vGgIRUEd7Z0b7jfVQLUwqHDyfh5DGvAUhvtJogjUyFIXZzpU+E"
0118: + "9ES9t7LZKdwNZSIdNUjM2eaf4g8BpuQobBVkj/GUcotKyeBjwvKxHlRefL4CCw28"
0119: + "DO3SnLRKxd7uBSqeOGUKxqasgdekM/xIFOrJ85k7p89n6ncLQLHCPGVkzmVeRZro"
0120: + "/T7zE91J57qBGZOUAP1vllcYLty1cs9PCc5oWnj3XbQvRXJpYyBFY2hpZG5hICh0"
0121: + "ZXN0IGtleSkgPGVyaWNAYm91bmN5Y2FzdGxlLm9yZz6IuAQTAQIAIgUCPz2nJAIb"
0122: + "AwUJAIPWAAQLBwMCAxUCAwMWAgECHgECF4AACgkQNVhhRv3z83PFjAP/QW47gfBO"
0123: + "PEAJcaIlX/VPEnzXpa8/zjSQP2zL1q/yZzhgPTz5hQ+VHPpFf6voveHRDI7AuQkN"
0124: + "ZqFB1kj9sZUIWzswT9vqD18N89nwbPVyYJ0x+kFjAALy7N7oPaaNJaDRy6G0/w/1"
0125: + "3V73K298L8Lz09habWaq7aJx/znc0/SXX6y0JEVyaWMgRWNoaWRuYSA8ZXJpY0Bi"
0126: + "b3VuY3ljYXN0bGUub3JnPoi4BBMBAgAiBQI/RxQNAhsDBQkAg9YABAsHAwIDFQID"
0127: + "AxYCAQIeAQIXgAAKCRA1WGFG/fPzc3O6A/49tXFCiiP8vg77OXvnmbnzPBA1G6jC"
0128: + "RZNP1yIXusOjpHqyLN5K9hw6lq/o4pNiCuiq32osqGRX3lv/nDduJU1kn2Ow+I2V"
0129: + "ci+ojMXdCGdEqPwZfv47jHLwRrIUJ22OOoWsORtgvSeRUd4Izg8jruaFM7ufr5hr"
0130: + "jEl1cuLW1Hr8Lp0B/AQ/RxxQAQQA0J2BIdqb8JtDGKjvYxrju0urJVVzyI1CnCjA"
0131: + "p7CtLoHQJUQU7PajnV4Jd12ukfcoK7MRraYydQEjxh2MqPpuQgJS3dgQVrxOParD"
0132: + "QYBFrZNd2tZxOjYakhErvUmRo6yWFaxChwqMgl8XWugBNg1Dva+/YcoGQ+ly+Jg4"
0133: + "RWZoH88ABin+AwMCldD/2v8TyT1ghK70IuFs4MZBhdm6VgyGR8DQ/Ago6IAjA4BY"
0134: + "Sol3lJb7+IIGsZaXwEuMRUvn6dWfa3r2I0p1t75vZb1Ng1YK32RZ5DNzl4Xb3L8V"
0135: + "D+1Fiz9mHO8wiplAwDudB+RmQMlth3DNi/UsjeCTdEJAT+TTC7D40DiHDb1bR86Y"
0136: + "2O5Y7MQ3SZs3/x0D/Ob6PStjfQ1kiqbruAMROKoavG0zVgxvspkoKN7h7BapnwJM"
0137: + "6yf4qN/aByhAx9sFvADxu6z3SVcxiFw3IgAmabyWYb85LP8AsTYAG/HBoC6yob47"
0138: + "Mt+GEDeyPifzzGXBWYIH4heZbSQivvA0eRwY5VZsMsBkbY5VR0FLVWgplbuO21bS"
0139: + "rPS1T0crC+Zfj7FQBAkTfsg8RZQ8MPaHng01+gnFd243DDFvTAHygvm6a2X2fiRw"
0140: + "5epAST4wWfY/BZNOxmfSKH6QS0oQMRscw79He6vGTB7vunLrKQYD4veInwQYAQIA"
0141: + "CQUCP0ccUAIbDAAKCRA1WGFG/fPzczmFA/wMg5HhN5NkqmjnHUFfeXNXdHzmekyw"
0142: + "38RnuCMKmfc43AiDs+FtJ62gpQ6PEsZF4o9S5fxcjVk3VSg00XMDtQ/0BsKBc5Gx"
0143: + "hJTq7G+/SoeM433WG19uoS0+5Lf/31wNoTnpv6npOaYpcTQ7L9LCnzwAF4H0hJPE"
0144: + "6bhmW2CMcsE/IZUB4QQ/Rwc1EQQAs5MUQlRiYOfi3fQ1OF6Z3eCwioDKu2DmOxot"
0145: + "BICvdoG2muvs0KEBas9bbd0FJqc92FZJv8yxEgQbQtQAiFxoIFHRTFK+SPO/tQm+"
0146: + "r83nwLRrfDeVVdRfzF79YCc+Abuh8sS/53H3u9Y7DYWr9IuMgI39nrVhY+d8yukf"
0147: + "jo4OR+sAoKS/f7V1Xxj/Eqhb8qzf+N+zJRUlBACDd1eo/zFJZcq2YJa7a9vkViME"
0148: + "axvwApqxeoU7oDpeHEMWg2DXJ7V24ZU5SbPTMY0x98cc8pcoqwsqux8xicWc0reh"
0149: + "U3odQxWM4Se0LmEdca0nQOmNJlL9IsQ+QOJzx47qUOUAqhxnkXxQ/6B8w+M6gZya"
0150: + "fwSdy70OumxESZipeQP+Lo9x6FcaW9L78hDX0aijJhgSEsnGODKB+bln29txX37E"
0151: + "/a/Si+pyeLMi82kUdIL3G3I5HPWd3qSO4K94062+HfFj8bA20/1tbb/WxvxB2sKJ"
0152: + "i3IobblFOvFHo+v8GaLdVyartp0JZLue/jP1dl9ctulSrIqaJT342uLsgTjsr2z+"
0153: + "AwMCAyAU8Vo5AhhgFkDto8vQk7yxyRKEzu5qB66dRcTlaUPIiR8kamcy5ZTtujs4"
0154: + "KIW4j2M/LvagrpWfV5+0M0VyaWMgRWNoaWRuYSAoRFNBIFRlc3QgS2V5KSA8ZXJp"
0155: + "Y0Bib3VuY3ljYXN0bGUub3JnPohZBBMRAgAZBQI/Rwc1BAsHAwIDFQIDAxYCAQIe"
0156: + "AQIXgAAKCRDNI/XpxMo0QwJcAJ40447eezSiIMspuzkwsMyFN8YBaQCdFTuZuT30"
0157: + "CphiUYWnsC0mQ+J15B4=");
0158:
0159: byte[] enc1 = Base64
0160: .decode("hIwDKwfQexPJboABA/4/7prhYYMORTiQ5avQKx0XYpCLujzGefYjnyuWZnx3Iev8"
0161: + "Pmsguumm+OLLvtXhhkXQmkJRXbIg6Otj2ubPYWflRPgpJSgOrNOreOl5jeABOrtw"
0162: + "bV6TJb9OTtZuB7cTQSCq2gmYiSZkluIiDjNs3R3mEanILbYzOQ3zKSggKpzlv9JQ"
0163: + "AZUqTyDyJ6/OUbJF5fI5uiv76DCsw1zyMWotUIu5/X01q+AVP5Ly3STzI7xkWg/J"
0164: + "APz4zUHism7kSYz2viAQaJx9/bNnH3AM6qm1Fuyikl4=");
0165:
0166: byte[] enc1crc = Base64.decode("lv4o");
0167:
0168: byte[] enc2 = Base64
0169: .decode("hIwDKwfQexPJboABBAC62jcJH8xKnKb1neDVmiovYON04+7VQ2v4BmeHwJrdag1g"
0170: + "Ya++6PeBlQ2Q9lSGBwLobVuJmQ7cOnPUJP727JeSGWlMyFtMbBSHekOaTenT5lj7"
0171: + "Zk7oRHxMp/hByzlMacIDzOn8LPSh515RHM57eDLCOwqnAxGQwk67GRl8f5dFH9JQ"
0172: + "Aa7xx8rjCqPbiIQW6t5LqCNvPZOiSCmftll6+se1XJhFEuq8WS4nXtPfTiJ3vib4"
0173: + "3soJdHzGB6AOs+BQ6aKmmNTVAxa5owhtSt1Z/6dfSSk=");
0174:
0175: byte[] subPubKey = Base64
0176: .decode("mIsEPz2nJAEEAOTVqWMvqYE693qTgzKv/TJpIj3hI8LlYPC6m1dk0z3bDLwVVk9F"
0177: + "FAB+CWS8RdFOWt/FG3tEv2nzcoNdRvjv9WALyIGNawtae4Ml6oAT06/511yUzXHO"
0178: + "k+9xK3wkXN5jdzUhf4cA2oGpLSV/pZlocsIDL+jCUQtumUPwFodmSHhzAAYptC9F"
0179: + "cmljIEVjaGlkbmEgKHRlc3Qga2V5KSA8ZXJpY0Bib3VuY3ljYXN0bGUub3JnPoi4"
0180: + "BBMBAgAiBQI/PackAhsDBQkAg9YABAsHAwIDFQIDAxYCAQIeAQIXgAAKCRA1WGFG"
0181: + "/fPzc8WMA/9BbjuB8E48QAlxoiVf9U8SfNelrz/ONJA/bMvWr/JnOGA9PPmFD5Uc"
0182: + "+kV/q+i94dEMjsC5CQ1moUHWSP2xlQhbOzBP2+oPXw3z2fBs9XJgnTH6QWMAAvLs"
0183: + "3ug9po0loNHLobT/D/XdXvcrb3wvwvPT2FptZqrtonH/OdzT9JdfrIhMBBARAgAM"
0184: + "BQI/RxooBYMAemL8AAoJEM0j9enEyjRDiBgAn3RcLK+gq90PvnQFTw2DNqdq7KA0"
0185: + "AKCS0EEIXCzbV1tfTdCUJ3hVh3btF7QkRXJpYyBFY2hpZG5hIDxlcmljQGJvdW5j"
0186: + "eWNhc3RsZS5vcmc+iLgEEwECACIFAj9HFA0CGwMFCQCD1gAECwcDAgMVAgMDFgIB"
0187: + "Ah4BAheAAAoJEDVYYUb98/Nzc7oD/j21cUKKI/y+Dvs5e+eZufM8EDUbqMJFk0/X"
0188: + "Ihe6w6OkerIs3kr2HDqWr+jik2IK6KrfaiyoZFfeW/+cN24lTWSfY7D4jZVyL6iM"
0189: + "xd0IZ0So/Bl+/juMcvBGshQnbY46haw5G2C9J5FR3gjODyOu5oUzu5+vmGuMSXVy"
0190: + "4tbUevwuiEwEEBECAAwFAj9HGigFgwB6YvwACgkQzSP16cTKNEPwBQCdHm0Amwza"
0191: + "NmVmDHm3rmqI7rp2oQ0An2YbiP/H/kmBNnmTeH55kd253QOhuIsEP0ccUAEEANCd"
0192: + "gSHam/CbQxio72Ma47tLqyVVc8iNQpwowKewrS6B0CVEFOz2o51eCXddrpH3KCuz"
0193: + "Ea2mMnUBI8YdjKj6bkICUt3YEFa8Tj2qw0GARa2TXdrWcTo2GpIRK71JkaOslhWs"
0194: + "QocKjIJfF1roATYNQ72vv2HKBkPpcviYOEVmaB/PAAYpiJ8EGAECAAkFAj9HHFAC"
0195: + "GwwACgkQNVhhRv3z83M5hQP8DIOR4TeTZKpo5x1BX3lzV3R85npMsN/EZ7gjCpn3"
0196: + "ONwIg7PhbSetoKUOjxLGReKPUuX8XI1ZN1UoNNFzA7UP9AbCgXORsYSU6uxvv0qH"
0197: + "jON91htfbqEtPuS3/99cDaE56b+p6TmmKXE0Oy/Swp88ABeB9ISTxOm4ZltgjHLB"
0198: + "PyGZAaIEP0cHNREEALOTFEJUYmDn4t30NThemd3gsIqAyrtg5jsaLQSAr3aBtprr"
0199: + "7NChAWrPW23dBSanPdhWSb/MsRIEG0LUAIhcaCBR0UxSvkjzv7UJvq/N58C0a3w3"
0200: + "lVXUX8xe/WAnPgG7ofLEv+dx97vWOw2Fq/SLjICN/Z61YWPnfMrpH46ODkfrAKCk"
0201: + "v3+1dV8Y/xKoW/Ks3/jfsyUVJQQAg3dXqP8xSWXKtmCWu2vb5FYjBGsb8AKasXqF"
0202: + "O6A6XhxDFoNg1ye1duGVOUmz0zGNMffHHPKXKKsLKrsfMYnFnNK3oVN6HUMVjOEn"
0203: + "tC5hHXGtJ0DpjSZS/SLEPkDic8eO6lDlAKocZ5F8UP+gfMPjOoGcmn8Encu9Drps"
0204: + "REmYqXkD/i6PcehXGlvS+/IQ19GooyYYEhLJxjgygfm5Z9vbcV9+xP2v0ovqcniz"
0205: + "IvNpFHSC9xtyORz1nd6kjuCveNOtvh3xY/GwNtP9bW2/1sb8QdrCiYtyKG25RTrx"
0206: + "R6Pr/Bmi3Vcmq7adCWS7nv4z9XZfXLbpUqyKmiU9+Nri7IE47K9stDNFcmljIEVj"
0207: + "aGlkbmEgKERTQSBUZXN0IEtleSkgPGVyaWNAYm91bmN5Y2FzdGxlLm9yZz6IWQQT"
0208: + "EQIAGQUCP0cHNQQLBwMCAxUCAwMWAgECHgECF4AACgkQzSP16cTKNEMCXACfauui"
0209: + "bSwyG59Yrm8hHCDuCPmqwsQAni+dPl08FVuWh+wb6kOgJV4lcYae");
0210:
0211: byte[] subPubCrc = Base64.decode("rikt");
0212:
0213: byte[] pgp8Key = Base64
0214: .decode("lQIEBEBXUNMBBADScQczBibewnbCzCswc/9ut8R0fwlltBRxMW0NMdKJY2LF"
0215: + "7k2COeLOCIU95loJGV6ulbpDCXEO2Jyq8/qGw1qD3SCZNXxKs3GS8Iyh9Uwd"
0216: + "VL07nMMYl5NiQRsFB7wOb86+94tYWgvikVA5BRP5y3+O3GItnXnpWSJyREUy"
0217: + "6WI2QQAGKf4JAwIVmnRs4jtTX2DD05zy2mepEQ8bsqVAKIx7lEwvMVNcvg4Y"
0218: + "8vFLh9Mf/uNciwL4Se/ehfKQ/AT0JmBZduYMqRU2zhiBmxj4cXUQ0s36ysj7"
0219: + "fyDngGocDnM3cwPxaTF1ZRBQHSLewP7dqE7M73usFSz8vwD/0xNOHFRLKbsO"
0220: + "RqDlLA1Cg2Yd0wWPS0o7+qqk9ndqrjjSwMM8ftnzFGjShAdg4Ca7fFkcNePP"
0221: + "/rrwIH472FuRb7RbWzwXA4+4ZBdl8D4An0dwtfvAO+jCZSrLjmSpxEOveJxY"
0222: + "GduyR4IA4lemvAG51YHTHd4NXheuEqsIkn1yarwaaj47lFPnxNOElOREMdZb"
0223: + "nkWQb1jfgqO24imEZgrLMkK9bJfoDnlF4k6r6hZOp5FSFvc5kJB4cVo1QJl4"
0224: + "pwCSdoU6luwCggrlZhDnkGCSuQUUW45NE7Br22NGqn4/gHs0KCsWbAezApGj"
0225: + "qYUCfX1bcpPzUMzUlBaD5rz2vPeO58CDtBJ0ZXN0ZXIgPHRlc3RAdGVzdD6I"
0226: + "sgQTAQIAHAUCQFdQ0wIbAwQLBwMCAxUCAwMWAgECHgECF4AACgkQs8JyyQfH"
0227: + "97I1QgP8Cd+35maM2cbWV9iVRO+c5456KDi3oIUSNdPf1NQrCAtJqEUhmMSt"
0228: + "QbdiaFEkPrORISI/2htXruYn0aIpkCfbUheHOu0sef7s6pHmI2kOQPzR+C/j"
0229: + "8D9QvWsPOOso81KU2axUY8zIer64Uzqc4szMIlLw06c8vea27RfgjBpSCryw"
0230: + "AgAA");
0231:
0232: char[] pgp8Pass = "2002 Buffalo Sabres".toCharArray();
0233:
0234: char[] pass = { 'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l',
0235: 'd' };
0236:
0237: byte[] fingerprintKey = Base64
0238: .decode("mQEPA0CiJdUAAAEIAMI+znDlPd2kQoEcnxqxLcRz56Z7ttFKHpnYp0UkljZdquVc"
0239: + "By1jMfXGVV64xN1IvMcyenLXUE0IUeUBCQs6tHunFRAPSeCxJ3FdFe1B5MpqQG8A"
0240: + "BnEpAds/hAUfRDZD5y/lolk1hjvFMrRh6WXckaA/QQ2t00NmTrJ1pYUpkw9tnVQb"
0241: + "LUjWJhfZDBBcN0ADtATzgkugxMtcDxR6I5x8Ndn+IilqIm23kxGIcmMd/BHOec4c"
0242: + "jRwJXXDb7u8tl+2knAf9cwhPHp3+Zy4uGSQPdzQnXOhBlA+4WDa0RROOevWgq8uq"
0243: + "8/9Xp/OlTVL+OoIzjsI6mJP1Joa4qmqAnaHAmXcAEQEAAbQoQk9BM1JTS1kgPEJP"
0244: + "QSBNb25pdG9yaW5nIEAgODg4LTI2OS01MjY2PokBFQMFEECiJdWqaoCdocCZdwEB"
0245: + "0RsH/3HPxoUZ3G3K7T3jgOnJUckTSHWU3XspHzMVgqOxjTrcexi5IsAM5M+BulfW"
0246: + "T2aO+Kqf5w8cKTKgW02DNpHUiPjHx0nzDE+Do95zbIErGeK+Twkc4O/aVsvU9GGO"
0247: + "81VFI6WMvDQ4CUAUnAdk03MRrzI2nAuhn4NJ5LQS+uJrnqUJ4HmFAz6CQZQKd/kS"
0248: + "Xgq+A6i7aI1LG80YxWa9ooQgaCrb9dwY/kPQ+yC22zQ3FExtv+Fv3VtAKTilO3vn"
0249: + "BA4Y9uTHuObHfI+1yxUS2PrlRUX0m48ZjpIX+cEN3QblGBJudI/A1QSd6P0LZeBr"
0250: + "7F1Z1aF7ZDo0KzgiAIBvgXkeTpw=");
0251:
0252: byte[] fingerprintCheck = Base64.decode("CTv2");
0253:
0254: byte[] expiry60and30daysSig13Key = Base64
0255: .decode("mQGiBENZt/URBAC5JccXiwe4g6MuviEC8NI/x0NaVkGFAOY04d5E4jeIycBP"
0256: + "SrpOPrjETuigqhrj8oqed2+2yUqfnK4nhTsTAjyeJ3PpWC1pGAKzJgYmJk+K"
0257: + "9aTLq0BQWiXDdv5RG6fDmeq1umvOfcXBqGFAguLPZC+U872bSLnfe3lqGNA8"
0258: + "jvmY7wCgjhzVQVm10NN5ST8nemPEcSjnBrED/R494gHL6+r5OgUgXnNCDejA"
0259: + "4InoDImQCF+g7epp5E1MB6CMYSg2WSY2jHFuHpwnUb7AiOO0ZZ3UBqM9rYnK"
0260: + "kDvxkFCxba7Ms+aFj9blRNmy3vG4FewDcTdxzCtjUk6dRfu6UoARpqlTE/q7"
0261: + "Xo6EQP1ncwJ+UTlcHkTBvg/usI/yBACGjBqX8glb5VfNaZgNHMeS/UIiUiuV"
0262: + "SVFojiSDOHcnCe/6y4M2gVm38zz1W9qhoLfLpiAOFeL0yj6wzXvsjjXQiKQ8"
0263: + "nBE4Mf+oeH2qiQ/LfzQrGpI5eNcMXrzK9nigmz2htYO2GjQfupEnu1RHBTH8"
0264: + "NjofD2AShL9IO73plRuExrQgVGVzdCBLZXkgPHRlc3RAYm91bmN5Y2FzdGxl"
0265: + "Lm9yZz6IZAQTEQIAJAIbAwYLCQgHAwIDFQIDAxYCAQIeAQIXgAUCQ1m4DgUJ"
0266: + "AE8aGQAKCRD8QP1QuU7Kqw+eAJ0dZ3ZAqr73X61VmCkbyPoszLQMAQCfdFs2"
0267: + "YMDeUvX34Q/8Ba0KgO5f3RSwAgADuM0EQ1m39hADAIHpVGcLqS9UkmQaWBvH"
0268: + "WP6TnN7Y1Ha0TJOuxpbFjBW+CmVh/FjcsnavFXDXpo2zc742WT+vrHBSa/0D"
0269: + "1QEBsnCaX5SRRVp7Mqs8q+aDhjcHMIP8Sdxf7GozXDORkrRaJwADBQL9HLYm"
0270: + "7Rr5iYWDcvs+Pi6O1zUyb1tjkxEGaV/rcozl2MMmr2mzJ6x/Bz8SuhZEJS0m"
0271: + "bB2CvAA39aQi9jHlV7q0SV73NOkd2L/Vt2UZhzlUdvrJ37PgYDv+Wd9Ufz6g"
0272: + "MzLSiE8EGBECAA8FAkNZt/YCGwwFCQAnjQAACgkQ/ED9ULlOyqsTqQCcDnAZ"
0273: + "7YymCfhm1yJiuFQg3qiX6Z4An19OSEgeSKugVcH49g1sxUB0zNdIsAIAAw==");
0274:
0275: private void fingerPrintTest() throws Exception {
0276: //
0277: // version 3
0278: //
0279: PGPPublicKeyRing pgpPub = new PGPPublicKeyRing(fingerprintKey);
0280:
0281: PGPPublicKey pubKey = pgpPub.getPublicKey();
0282:
0283: if (!areEqual(pubKey.getFingerprint(), Hex
0284: .decode("4FFB9F0884266C715D1CEAC804A3BBFA"))) {
0285: fail("version 3 fingerprint test failed");
0286: }
0287:
0288: //
0289: // version 4
0290: //
0291: pgpPub = new PGPPublicKeyRing(testPubKey);
0292:
0293: pubKey = pgpPub.getPublicKey();
0294:
0295: if (!areEqual(pubKey.getFingerprint(), Hex
0296: .decode("3062363c1046a01a751946bb35586146fdf3f373"))) {
0297: fail("version 4 fingerprint test failed");
0298: }
0299: }
0300:
0301: private void mixedTest(PGPPrivateKey pgpPrivKey,
0302: PGPPublicKey pgpPubKey) throws Exception {
0303: byte[] text = { (byte) 'h', (byte) 'e', (byte) 'l', (byte) 'l',
0304: (byte) 'o', (byte) ' ', (byte) 'w', (byte) 'o',
0305: (byte) 'r', (byte) 'l', (byte) 'd', (byte) '!',
0306: (byte) '\n' };
0307:
0308: //
0309: // literal data
0310: //
0311: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
0312: PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
0313: OutputStream lOut = lGen.open(bOut, PGPLiteralData.BINARY,
0314: PGPLiteralData.CONSOLE, text.length, new Date());
0315:
0316: lOut.write(text);
0317:
0318: lGen.close();
0319:
0320: byte[] bytes = bOut.toByteArray();
0321:
0322: PGPObjectFactory f = new PGPObjectFactory(bytes);
0323: checkLiteralData((PGPLiteralData) f.nextObject(), text);
0324:
0325: ByteArrayOutputStream bcOut = new ByteArrayOutputStream();
0326:
0327: PGPEncryptedDataGenerator encGen = new PGPEncryptedDataGenerator(
0328: SymmetricKeyAlgorithmTags.AES_128, true,
0329: new SecureRandom(), "BC");
0330:
0331: encGen.addMethod(pgpPubKey);
0332:
0333: encGen.addMethod("password".toCharArray());
0334:
0335: OutputStream cOut = encGen.open(bcOut, bytes.length);
0336:
0337: cOut.write(bytes);
0338:
0339: cOut.close();
0340:
0341: byte[] encData = bcOut.toByteArray();
0342:
0343: //
0344: // asymmetric
0345: //
0346: PGPObjectFactory pgpF = new PGPObjectFactory(encData);
0347:
0348: PGPEncryptedDataList encList = (PGPEncryptedDataList) pgpF
0349: .nextObject();
0350:
0351: PGPPublicKeyEncryptedData encP = (PGPPublicKeyEncryptedData) encList
0352: .get(0);
0353:
0354: InputStream clear = encP.getDataStream(pgpPrivKey, "BC");
0355:
0356: PGPObjectFactory pgpFact = new PGPObjectFactory(clear);
0357:
0358: checkLiteralData((PGPLiteralData) pgpFact.nextObject(), text);
0359:
0360: //
0361: // PBE
0362: //
0363: pgpF = new PGPObjectFactory(encData);
0364:
0365: encList = (PGPEncryptedDataList) pgpF.nextObject();
0366:
0367: PGPPBEEncryptedData encPbe = (PGPPBEEncryptedData) encList
0368: .get(1);
0369:
0370: clear = encPbe.getDataStream("password".toCharArray(), "BC");
0371:
0372: pgpF = new PGPObjectFactory(clear);
0373:
0374: checkLiteralData((PGPLiteralData) pgpF.nextObject(), text);
0375: }
0376:
0377: private void checkLiteralData(PGPLiteralData ld, byte[] data)
0378: throws IOException {
0379: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
0380:
0381: if (!ld.getFileName().equals(PGPLiteralData.CONSOLE)) {
0382: throw new RuntimeException("wrong filename in packet");
0383: }
0384:
0385: InputStream inLd = ld.getDataStream();
0386: int ch;
0387:
0388: while ((ch = inLd.read()) >= 0) {
0389: bOut.write(ch);
0390: }
0391:
0392: if (!areEqual(bOut.toByteArray(), data)) {
0393: fail("wrong plain text in decrypted packet");
0394: }
0395: }
0396:
0397: public void performTest() throws Exception {
0398: PublicKey pubKey = null;
0399:
0400: //
0401: // Read the public key
0402: //
0403: PGPPublicKeyRing pgpPub = new PGPPublicKeyRing(testPubKey);
0404:
0405: pubKey = pgpPub.getPublicKey().getKey("BC");
0406:
0407: Iterator it = pgpPub.getPublicKey().getUserIDs();
0408:
0409: String uid = (String) it.next();
0410:
0411: it = pgpPub.getPublicKey().getSignaturesForID(uid);
0412:
0413: PGPSignature sig = (PGPSignature) it.next();
0414:
0415: sig.initVerify(pgpPub.getPublicKey(), "BC");
0416:
0417: if (!sig.verifyCertification(uid, pgpPub.getPublicKey())) {
0418: fail("failed to verify certification");
0419: }
0420:
0421: //
0422: // write a public key
0423: //
0424: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
0425: BCPGOutputStream pOut = new BCPGOutputStream(bOut);
0426:
0427: pgpPub.encode(pOut);
0428:
0429: if (!areEqual(bOut.toByteArray(), testPubKey)) {
0430: fail("public key rewrite failed");
0431: }
0432:
0433: //
0434: // Read the public key
0435: //
0436: PGPPublicKeyRing pgpPubV3 = new PGPPublicKeyRing(testPubKeyV3);
0437: PublicKey pubKeyV3 = pgpPub.getPublicKey().getKey("BC");
0438:
0439: //
0440: // write a V3 public key
0441: //
0442: bOut = new ByteArrayOutputStream();
0443: pOut = new BCPGOutputStream(bOut);
0444:
0445: pgpPubV3.encode(pOut);
0446:
0447: //
0448: // Read a v3 private key
0449: //
0450: char[] passP = "FIXCITY_QA".toCharArray();
0451:
0452: PGPSecretKeyRing pgpPriv = new PGPSecretKeyRing(testPrivKeyV3);
0453:
0454: PGPPrivateKey pgpPrivKey = pgpPriv.getSecretKey()
0455: .extractPrivateKey(passP, "BC");
0456:
0457: //
0458: // write a v3 private key
0459: //
0460: bOut = new ByteArrayOutputStream();
0461: pOut = new BCPGOutputStream(bOut);
0462:
0463: pgpPriv.encode(pOut);
0464:
0465: if (!areEqual(bOut.toByteArray(), testPrivKeyV3)) {
0466: fail("private key V3 rewrite failed");
0467: }
0468:
0469: //
0470: // Read the private key
0471: //
0472: pgpPriv = new PGPSecretKeyRing(testPrivKey);
0473:
0474: pgpPrivKey = pgpPriv.getSecretKey().extractPrivateKey(pass,
0475: "BC");
0476:
0477: //
0478: // write a private key
0479: //
0480: bOut = new ByteArrayOutputStream();
0481: pOut = new BCPGOutputStream(bOut);
0482:
0483: pgpPriv.encode(pOut);
0484:
0485: if (!areEqual(bOut.toByteArray(), testPrivKey)) {
0486: fail("private key rewrite failed");
0487: }
0488:
0489: //
0490: // test encryption
0491: //
0492: Cipher c = Cipher.getInstance("RSA", "BC");
0493:
0494: c.init(Cipher.ENCRYPT_MODE, pubKey);
0495:
0496: byte[] in = "hello world".getBytes();
0497:
0498: byte[] out = c.doFinal(in);
0499:
0500: c.init(Cipher.DECRYPT_MODE, pgpPrivKey.getKey());
0501:
0502: out = c.doFinal(out);
0503:
0504: if (!areEqual(in, out)) {
0505: fail("decryption failed.");
0506: }
0507:
0508: //
0509: // test signature message
0510: //
0511: PGPObjectFactory pgpFact = new PGPObjectFactory(sig1);
0512:
0513: PGPCompressedData c1 = (PGPCompressedData) pgpFact.nextObject();
0514:
0515: pgpFact = new PGPObjectFactory(c1.getDataStream());
0516:
0517: PGPOnePassSignatureList p1 = (PGPOnePassSignatureList) pgpFact
0518: .nextObject();
0519:
0520: PGPOnePassSignature ops = p1.get(0);
0521:
0522: PGPLiteralData p2 = (PGPLiteralData) pgpFact.nextObject();
0523:
0524: InputStream dIn = p2.getInputStream();
0525: int ch;
0526:
0527: ops.initVerify(pgpPub.getPublicKey(ops.getKeyID()), "BC");
0528:
0529: while ((ch = dIn.read()) >= 0) {
0530: ops.update((byte) ch);
0531: }
0532:
0533: PGPSignatureList p3 = (PGPSignatureList) pgpFact.nextObject();
0534:
0535: if (!ops.verify(p3.get(0))) {
0536: fail("Failed signature check");
0537: }
0538:
0539: //
0540: // encrypted message - read subkey
0541: //
0542: pgpPriv = new PGPSecretKeyRing(subKey);
0543:
0544: //
0545: // encrypted message
0546: //
0547: byte[] text = { (byte) 'h', (byte) 'e', (byte) 'l', (byte) 'l',
0548: (byte) 'o', (byte) ' ', (byte) 'w', (byte) 'o',
0549: (byte) 'r', (byte) 'l', (byte) 'd', (byte) '!',
0550: (byte) '\n' };
0551:
0552: PGPObjectFactory pgpF = new PGPObjectFactory(enc1);
0553:
0554: PGPEncryptedDataList encList = (PGPEncryptedDataList) pgpF
0555: .nextObject();
0556:
0557: PGPPublicKeyEncryptedData encP = (PGPPublicKeyEncryptedData) encList
0558: .get(0);
0559:
0560: pgpPrivKey = pgpPriv.getSecretKey(encP.getKeyID())
0561: .extractPrivateKey(pass, "BC");
0562:
0563: InputStream clear = encP.getDataStream(pgpPrivKey, "BC");
0564:
0565: pgpFact = new PGPObjectFactory(clear);
0566:
0567: c1 = (PGPCompressedData) pgpFact.nextObject();
0568:
0569: pgpFact = new PGPObjectFactory(c1.getDataStream());
0570:
0571: PGPLiteralData ld = (PGPLiteralData) pgpFact.nextObject();
0572:
0573: bOut = new ByteArrayOutputStream();
0574:
0575: if (!ld.getFileName().equals("test.txt")) {
0576: throw new RuntimeException("wrong filename in packet");
0577: }
0578:
0579: InputStream inLd = ld.getDataStream();
0580:
0581: while ((ch = inLd.read()) >= 0) {
0582: bOut.write(ch);
0583: }
0584:
0585: if (!areEqual(bOut.toByteArray(), text)) {
0586: fail("wrong plain text in decrypted packet");
0587: }
0588:
0589: //
0590: // encrypt - short message
0591: //
0592: byte[] shortText = { (byte) 'h', (byte) 'e', (byte) 'l',
0593: (byte) 'l', (byte) 'o' };
0594:
0595: ByteArrayOutputStream cbOut = new ByteArrayOutputStream();
0596: PGPEncryptedDataGenerator cPk = new PGPEncryptedDataGenerator(
0597: SymmetricKeyAlgorithmTags.CAST5, new SecureRandom(),
0598: "BC");
0599: PGPPublicKey puK = pgpPriv.getSecretKey(encP.getKeyID())
0600: .getPublicKey();
0601:
0602: cPk.addMethod(puK);
0603:
0604: OutputStream cOut = cPk.open(
0605: new UncloseableOutputStream(cbOut), shortText.length);
0606:
0607: cOut.write(shortText);
0608:
0609: cOut.close();
0610:
0611: pgpF = new PGPObjectFactory(cbOut.toByteArray());
0612:
0613: encList = (PGPEncryptedDataList) pgpF.nextObject();
0614:
0615: encP = (PGPPublicKeyEncryptedData) encList.get(0);
0616:
0617: pgpPrivKey = pgpPriv.getSecretKey(encP.getKeyID())
0618: .extractPrivateKey(pass, "BC");
0619:
0620: clear = encP.getDataStream(pgpPrivKey, "BC");
0621:
0622: bOut.reset();
0623:
0624: while ((ch = clear.read()) >= 0) {
0625: bOut.write(ch);
0626: }
0627:
0628: out = bOut.toByteArray();
0629:
0630: if (!areEqual(out, shortText)) {
0631: fail("wrong plain text in generated short text packet");
0632: }
0633:
0634: //
0635: // encrypt
0636: //
0637: cbOut = new ByteArrayOutputStream();
0638: cPk = new PGPEncryptedDataGenerator(
0639: SymmetricKeyAlgorithmTags.CAST5, new SecureRandom(),
0640: "BC");
0641: puK = pgpPriv.getSecretKey(encP.getKeyID()).getPublicKey();
0642:
0643: cPk.addMethod(puK);
0644:
0645: cOut = cPk
0646: .open(new UncloseableOutputStream(cbOut), text.length);
0647:
0648: cOut.write(text);
0649:
0650: cOut.close();
0651:
0652: pgpF = new PGPObjectFactory(cbOut.toByteArray());
0653:
0654: encList = (PGPEncryptedDataList) pgpF.nextObject();
0655:
0656: encP = (PGPPublicKeyEncryptedData) encList.get(0);
0657:
0658: pgpPrivKey = pgpPriv.getSecretKey(encP.getKeyID())
0659: .extractPrivateKey(pass, "BC");
0660:
0661: clear = encP.getDataStream(pgpPrivKey, "BC");
0662:
0663: bOut.reset();
0664:
0665: while ((ch = clear.read()) >= 0) {
0666: bOut.write(ch);
0667: }
0668:
0669: out = bOut.toByteArray();
0670:
0671: if (!areEqual(out, text)) {
0672: fail("wrong plain text in generated packet");
0673: }
0674:
0675: //
0676: // read public key with sub key.
0677: //
0678: pgpF = new PGPObjectFactory(subPubKey);
0679: Object o;
0680:
0681: while ((o = pgpFact.nextObject()) != null) {
0682: // System.out.println(o);
0683: }
0684:
0685: //
0686: // key pair generation - CAST5 encryption
0687: //
0688: char[] passPhrase = "hello".toCharArray();
0689:
0690: KeyPairGenerator kpg = KeyPairGenerator
0691: .getInstance("RSA", "BC");
0692:
0693: kpg.initialize(1024);
0694:
0695: KeyPair kp = kpg.generateKeyPair();
0696:
0697: PGPSecretKey secretKey = new PGPSecretKey(
0698: PGPSignature.DEFAULT_CERTIFICATION,
0699: PublicKeyAlgorithmTags.RSA_GENERAL, kp.getPublic(), kp
0700: .getPrivate(), new Date(), "fred",
0701: SymmetricKeyAlgorithmTags.CAST5, passPhrase, null,
0702: null, new SecureRandom(), "BC");
0703:
0704: PGPPublicKey key = secretKey.getPublicKey();
0705:
0706: it = key.getUserIDs();
0707:
0708: uid = (String) it.next();
0709:
0710: it = key.getSignaturesForID(uid);
0711:
0712: sig = (PGPSignature) it.next();
0713:
0714: sig.initVerify(key, "BC");
0715:
0716: if (!sig.verifyCertification(uid, key)) {
0717: fail("failed to verify certification");
0718: }
0719:
0720: pgpPrivKey = secretKey.extractPrivateKey(passPhrase, "BC");
0721:
0722: key = PGPPublicKey.removeCertification(key, uid, sig);
0723:
0724: if (key == null) {
0725: fail("failed certification removal");
0726: }
0727:
0728: byte[] keyEnc = key.getEncoded();
0729:
0730: key = PGPPublicKey.addCertification(key, uid, sig);
0731:
0732: keyEnc = key.getEncoded();
0733:
0734: PGPSignatureGenerator sGen = new PGPSignatureGenerator(
0735: PublicKeyAlgorithmTags.RSA_GENERAL,
0736: HashAlgorithmTags.SHA1, "BC");
0737:
0738: sGen.initSign(PGPSignature.KEY_REVOCATION, secretKey
0739: .extractPrivateKey(passPhrase, "BC"));
0740:
0741: sig = sGen.generateCertification(key);
0742:
0743: key = PGPPublicKey.addCertification(key, sig);
0744:
0745: keyEnc = key.getEncoded();
0746:
0747: PGPPublicKeyRing tmpRing = new PGPPublicKeyRing(keyEnc);
0748:
0749: key = tmpRing.getPublicKey();
0750:
0751: Iterator sgIt = key
0752: .getSignaturesOfType(PGPSignature.KEY_REVOCATION);
0753:
0754: sig = (PGPSignature) sgIt.next();
0755:
0756: sig.initVerify(key, "BC");
0757:
0758: if (!sig.verifyCertification(key)) {
0759: fail("failed to verify revocation certification");
0760: }
0761:
0762: //
0763: // use of PGPKeyPair
0764: //
0765: PGPKeyPair pgpKp = new PGPKeyPair(PGPPublicKey.RSA_GENERAL, kp
0766: .getPublic(), kp.getPrivate(), new Date(), "BC");
0767:
0768: PGPPublicKey k1 = pgpKp.getPublicKey();
0769:
0770: PGPPrivateKey k2 = pgpKp.getPrivateKey();
0771:
0772: k1.getEncoded();
0773:
0774: mixedTest(k2, k1);
0775:
0776: //
0777: // key pair generation - AES_256 encryption.
0778: //
0779: kp = kpg.generateKeyPair();
0780:
0781: secretKey = new PGPSecretKey(
0782: PGPSignature.DEFAULT_CERTIFICATION,
0783: PublicKeyAlgorithmTags.RSA_GENERAL, kp.getPublic(), kp
0784: .getPrivate(), new Date(), "fred",
0785: SymmetricKeyAlgorithmTags.AES_256, passPhrase, null,
0786: null, new SecureRandom(), "BC");
0787:
0788: secretKey.extractPrivateKey(passPhrase, "BC");
0789:
0790: secretKey.encode(new ByteArrayOutputStream());
0791:
0792: //
0793: // secret key password changing.
0794: //
0795: String newPass = "newPass";
0796:
0797: secretKey = PGPSecretKey.copyWithNewPassword(secretKey,
0798: passPhrase, newPass.toCharArray(), secretKey
0799: .getKeyEncryptionAlgorithm(),
0800: new SecureRandom(), "BC");
0801:
0802: secretKey.extractPrivateKey(newPass.toCharArray(), "BC");
0803:
0804: secretKey.encode(new ByteArrayOutputStream());
0805:
0806: key = secretKey.getPublicKey();
0807:
0808: key.encode(new ByteArrayOutputStream());
0809:
0810: it = key.getUserIDs();
0811:
0812: uid = (String) it.next();
0813:
0814: it = key.getSignaturesForID(uid);
0815:
0816: sig = (PGPSignature) it.next();
0817:
0818: sig.initVerify(key, "BC");
0819:
0820: if (!sig.verifyCertification(uid, key)) {
0821: fail("failed to verify certification");
0822: }
0823:
0824: pgpPrivKey = secretKey.extractPrivateKey(newPass.toCharArray(),
0825: "BC");
0826:
0827: //
0828: // signature generation
0829: //
0830: String data = "hello world!";
0831:
0832: bOut = new ByteArrayOutputStream();
0833:
0834: ByteArrayInputStream testIn = new ByteArrayInputStream(data
0835: .getBytes());
0836:
0837: sGen = new PGPSignatureGenerator(
0838: PublicKeyAlgorithmTags.RSA_GENERAL,
0839: HashAlgorithmTags.SHA1, "BC");
0840:
0841: sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
0842:
0843: PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(
0844: PGPCompressedData.ZIP);
0845:
0846: BCPGOutputStream bcOut = new BCPGOutputStream(cGen
0847: .open(new UncloseableOutputStream(bOut)));
0848:
0849: sGen.generateOnePassVersion(false).encode(bcOut);
0850:
0851: PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator();
0852:
0853: Date testDate = new Date(
0854: (System.currentTimeMillis() / 1000) * 1000);
0855: OutputStream lOut = lGen.open(
0856: new UncloseableOutputStream(bcOut),
0857: PGPLiteralData.BINARY, "_CONSOLE",
0858: data.getBytes().length, testDate);
0859:
0860: while ((ch = testIn.read()) >= 0) {
0861: lOut.write(ch);
0862: sGen.update((byte) ch);
0863: }
0864:
0865: lOut.close();
0866:
0867: sGen.generate().encode(bcOut);
0868:
0869: bcOut.close();
0870:
0871: //
0872: // verify generated signature
0873: //
0874: pgpFact = new PGPObjectFactory(bOut.toByteArray());
0875:
0876: c1 = (PGPCompressedData) pgpFact.nextObject();
0877:
0878: pgpFact = new PGPObjectFactory(c1.getDataStream());
0879:
0880: p1 = (PGPOnePassSignatureList) pgpFact.nextObject();
0881:
0882: ops = p1.get(0);
0883:
0884: p2 = (PGPLiteralData) pgpFact.nextObject();
0885: if (!p2.getModificationTime().equals(testDate)) {
0886: fail("Modification time not preserved: "
0887: + p2.getModificationTime() + " " + testDate);
0888: }
0889:
0890: dIn = p2.getInputStream();
0891:
0892: ops.initVerify(secretKey.getPublicKey(), "BC");
0893:
0894: while ((ch = dIn.read()) >= 0) {
0895: ops.update((byte) ch);
0896: }
0897:
0898: p3 = (PGPSignatureList) pgpFact.nextObject();
0899:
0900: if (!ops.verify(p3.get(0))) {
0901: fail("Failed generated signature check");
0902: }
0903:
0904: //
0905: // signature generation - version 3
0906: //
0907: bOut = new ByteArrayOutputStream();
0908:
0909: testIn = new ByteArrayInputStream(data.getBytes());
0910: PGPV3SignatureGenerator sGenV3 = new PGPV3SignatureGenerator(
0911: PGPPublicKey.RSA_GENERAL, PGPUtil.SHA1, "BC");
0912:
0913: sGen.initSign(PGPSignature.BINARY_DOCUMENT, pgpPrivKey);
0914:
0915: cGen = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
0916:
0917: bcOut = new BCPGOutputStream(cGen.open(bOut));
0918:
0919: sGen.generateOnePassVersion(false).encode(bcOut);
0920:
0921: lGen = new PGPLiteralDataGenerator();
0922: lOut = lGen.open(new UncloseableOutputStream(bcOut),
0923: PGPLiteralData.BINARY, "_CONSOLE",
0924: data.getBytes().length, testDate);
0925:
0926: while ((ch = testIn.read()) >= 0) {
0927: lOut.write(ch);
0928: sGen.update((byte) ch);
0929: }
0930:
0931: sGen.generate().encode(bcOut);
0932:
0933: lOut.close();
0934:
0935: bcOut.close();
0936:
0937: //
0938: // verify generated signature
0939: //
0940: pgpFact = new PGPObjectFactory(bOut.toByteArray());
0941:
0942: c1 = (PGPCompressedData) pgpFact.nextObject();
0943:
0944: pgpFact = new PGPObjectFactory(c1.getDataStream());
0945:
0946: p1 = (PGPOnePassSignatureList) pgpFact.nextObject();
0947:
0948: ops = p1.get(0);
0949:
0950: p2 = (PGPLiteralData) pgpFact.nextObject();
0951: if (!p2.getModificationTime().equals(testDate)) {
0952: fail("Modification time not preserved");
0953: }
0954:
0955: dIn = p2.getInputStream();
0956:
0957: ops.initVerify(secretKey.getPublicKey(), "BC");
0958:
0959: while ((ch = dIn.read()) >= 0) {
0960: ops.update((byte) ch);
0961: }
0962:
0963: p3 = (PGPSignatureList) pgpFact.nextObject();
0964:
0965: if (!ops.verify(p3.get(0))) {
0966: fail("Failed v3 generated signature check");
0967: }
0968:
0969: //
0970: // extract PGP 8 private key
0971: //
0972: pgpPriv = new PGPSecretKeyRing(pgp8Key);
0973:
0974: secretKey = pgpPriv.getSecretKey();
0975:
0976: pgpPrivKey = secretKey.extractPrivateKey(pgp8Pass, "BC");
0977:
0978: //
0979: // expiry
0980: //
0981: testExpiry(expiry60and30daysSig13Key, 60, 30);
0982:
0983: fingerPrintTest();
0984: }
0985:
0986: private void testExpiry(byte[] encodedRing, int masterDays,
0987: int subKeyDays) throws Exception {
0988: PGPPublicKeyRing pubRing = new PGPPublicKeyRing(encodedRing);
0989: PGPPublicKey k = pubRing.getPublicKey();
0990:
0991: if (k.getValidDays() != masterDays) {
0992: fail("mismatch on master valid days.");
0993: }
0994:
0995: Iterator it = pubRing.getPublicKeys();
0996:
0997: it.next();
0998:
0999: k = (PGPPublicKey) it.next();
1000:
1001: if (k.getValidDays() != subKeyDays) {
1002: fail("mismatch on subkey valid days.");
1003: }
1004: }
1005:
1006: public String getName() {
1007: return "PGPRSATest";
1008: }
1009:
1010: public static void main(String[] args) {
1011: Security.addProvider(new BouncyCastleProvider());
1012:
1013: runTest(new PGPRSATest());
1014: }
1015: }
|