Line data Source code
1 : // Copyright (C) 2015 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.gpg.testing; 16 : 17 : import static com.google.gerrit.gpg.PublicKeyStore.keyIdToString; 18 : 19 : import java.io.ByteArrayInputStream; 20 : import java.io.IOException; 21 : import org.bouncycastle.bcpg.ArmoredInputStream; 22 : import org.bouncycastle.openpgp.PGPException; 23 : import org.bouncycastle.openpgp.PGPPrivateKey; 24 : import org.bouncycastle.openpgp.PGPPublicKey; 25 : import org.bouncycastle.openpgp.PGPPublicKeyRing; 26 : import org.bouncycastle.openpgp.PGPSecretKey; 27 : import org.bouncycastle.openpgp.PGPSecretKeyRing; 28 : import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; 29 : import org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder; 30 : import org.bouncycastle.openpgp.operator.bc.BcPGPDigestCalculatorProvider; 31 : import org.eclipse.jgit.lib.Constants; 32 : 33 : public class TestKey { 34 : private final String pubArmored; 35 : private final String secArmored; 36 : private final PGPPublicKeyRing pubRing; 37 : private final PGPSecretKeyRing secRing; 38 : 39 3 : public TestKey(String pubArmored, String secArmored) { 40 3 : this.pubArmored = pubArmored; 41 3 : this.secArmored = secArmored; 42 3 : BcKeyFingerprintCalculator fc = new BcKeyFingerprintCalculator(); 43 : try { 44 3 : this.pubRing = new PGPPublicKeyRing(newStream(pubArmored), fc); 45 3 : this.secRing = new PGPSecretKeyRing(newStream(secArmored), fc); 46 0 : } catch (PGPException | IOException e) { 47 0 : throw new AssertionError(e); 48 3 : } 49 3 : } 50 : 51 : public String getPublicKeyArmored() { 52 3 : return pubArmored; 53 : } 54 : 55 : public String getSecretKeyArmored() { 56 0 : return secArmored; 57 : } 58 : 59 : public PGPPublicKeyRing getPublicKeyRing() { 60 1 : return pubRing; 61 : } 62 : 63 : public PGPPublicKey getPublicKey() { 64 3 : return pubRing.getPublicKey(); 65 : } 66 : 67 : public PGPSecretKey getSecretKey() { 68 1 : return secRing.getSecretKey(); 69 : } 70 : 71 : public long getKeyId() { 72 2 : return getPublicKey().getKeyID(); 73 : } 74 : 75 : public String getKeyIdString() { 76 2 : return keyIdToString(getPublicKey().getKeyID()); 77 : } 78 : 79 : public String getFirstUserId() { 80 2 : return getPublicKey().getUserIDs().next(); 81 : } 82 : 83 : public PGPPrivateKey getPrivateKey() throws PGPException { 84 1 : return getSecretKey() 85 1 : .extractPrivateKey( 86 : new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()) 87 : // All test keys have no passphrase. 88 1 : .build(new char[0])); 89 : } 90 : 91 : private static ArmoredInputStream newStream(String armored) throws IOException { 92 3 : return new ArmoredInputStream(new ByteArrayInputStream(Constants.encode(armored))); 93 : } 94 : }