2018-06-13 17:26:48 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Paul Schaub.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2018-07-18 18:23:06 +02:00
|
|
|
package org.pgpainless.key.generation;
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.Charset;
|
2018-06-04 14:50:09 +02:00
|
|
|
import java.security.InvalidAlgorithmParameterException;
|
2018-06-02 21:21:35 +02:00
|
|
|
import java.security.KeyPair;
|
|
|
|
import java.security.KeyPairGenerator;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.NoSuchProviderException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
|
2018-06-04 14:50:09 +02:00
|
|
|
import org.bouncycastle.bcpg.sig.KeyFlags;
|
2018-06-02 21:21:35 +02:00
|
|
|
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
|
|
|
import org.bouncycastle.openpgp.PGPEncryptedData;
|
|
|
|
import org.bouncycastle.openpgp.PGPException;
|
|
|
|
import org.bouncycastle.openpgp.PGPKeyPair;
|
|
|
|
import org.bouncycastle.openpgp.PGPKeyRingGenerator;
|
2018-07-12 11:07:53 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
2018-06-02 21:21:35 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
|
|
|
import org.bouncycastle.openpgp.PGPSignature;
|
2018-06-04 14:50:09 +02:00
|
|
|
import org.bouncycastle.openpgp.PGPSignatureSubpacketVector;
|
2018-06-02 21:21:35 +02:00
|
|
|
import org.bouncycastle.openpgp.operator.PBESecretKeyEncryptor;
|
|
|
|
import org.bouncycastle.openpgp.operator.PGPContentSignerBuilder;
|
|
|
|
import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
|
|
|
|
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder;
|
|
|
|
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder;
|
|
|
|
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPKeyPair;
|
|
|
|
import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder;
|
2018-07-18 18:23:06 +02:00
|
|
|
import org.pgpainless.algorithm.HashAlgorithm;
|
|
|
|
import org.pgpainless.algorithm.KeyFlag;
|
|
|
|
import org.pgpainless.key.collection.PGPKeyRing;
|
|
|
|
import org.pgpainless.key.generation.type.ECDH;
|
|
|
|
import org.pgpainless.key.generation.type.ECDSA;
|
|
|
|
import org.pgpainless.key.generation.type.KeyType;
|
|
|
|
import org.pgpainless.key.generation.type.RSA_GENERAL;
|
|
|
|
import org.pgpainless.key.generation.type.curve.EllipticCurve;
|
|
|
|
import org.pgpainless.key.generation.type.length.RsaLength;
|
|
|
|
import org.pgpainless.util.KeyRingSubKeyFix;
|
|
|
|
import org.pgpainless.util.Passphrase;
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
public class KeyRingBuilder implements KeyRingBuilderInterface {
|
|
|
|
|
|
|
|
private final Charset UTF8 = Charset.forName("UTF-8");
|
|
|
|
|
|
|
|
private List<KeySpec> keySpecs = new ArrayList<>();
|
2018-06-04 14:50:09 +02:00
|
|
|
private String userId;
|
2018-07-12 23:21:09 +02:00
|
|
|
private Passphrase passphrase;
|
2018-06-02 21:21:35 +02:00
|
|
|
|
2018-06-04 19:44:47 +02:00
|
|
|
/**
|
|
|
|
* Creates a simple RSA KeyPair of length {@code length} with user-id {@code userId}.
|
2018-06-28 15:08:30 +02:00
|
|
|
* The KeyPair consists of a single RSA master key which is used for signing, encryption and certification.
|
2018-06-04 19:44:47 +02:00
|
|
|
*
|
|
|
|
* @param userId user id.
|
|
|
|
* @param length length in bits.
|
|
|
|
* @return {@link PGPSecretKeyRing} containing the KeyPair.
|
|
|
|
* @throws PGPException
|
|
|
|
* @throws NoSuchAlgorithmException
|
|
|
|
* @throws NoSuchProviderException
|
|
|
|
* @throws InvalidAlgorithmParameterException
|
|
|
|
*/
|
2018-07-12 11:07:53 +02:00
|
|
|
public PGPKeyRing simpleRsaKeyRing(String userId, RsaLength length)
|
2018-06-04 14:50:09 +02:00
|
|
|
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
|
|
|
|
return withMasterKey(
|
2018-06-04 19:44:47 +02:00
|
|
|
KeySpec.getBuilder(RSA_GENERAL.withLength(length))
|
2018-06-04 14:50:09 +02:00
|
|
|
.withDefaultKeyFlags()
|
|
|
|
.withDefaultAlgorithms())
|
|
|
|
.withPrimaryUserId(userId)
|
|
|
|
.withoutPassphrase()
|
|
|
|
.build();
|
2018-06-02 21:21:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 15:08:30 +02:00
|
|
|
/**
|
|
|
|
* Creates a key ring consisting of an ECDSA master key and an ECDH sub-key.
|
|
|
|
* The ECDSA master key is used for signing messages and certifying the sub key.
|
|
|
|
* The ECDH sub-key is used for encryption of messages.
|
|
|
|
*
|
|
|
|
* @param userId user-id
|
|
|
|
* @return {@link PGPSecretKeyRing} containing the key pairs.
|
|
|
|
* @throws PGPException
|
|
|
|
* @throws NoSuchAlgorithmException
|
|
|
|
* @throws NoSuchProviderException
|
|
|
|
* @throws InvalidAlgorithmParameterException
|
|
|
|
*/
|
2018-07-12 11:07:53 +02:00
|
|
|
public PGPKeyRing simpleEcKeyRing(String userId)
|
2018-06-04 14:50:09 +02:00
|
|
|
throws PGPException, NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException {
|
|
|
|
return withSubKey(
|
2018-06-04 19:44:47 +02:00
|
|
|
KeySpec.getBuilder(ECDH.fromCurve(EllipticCurve._P256))
|
2018-06-04 14:50:09 +02:00
|
|
|
.withKeyFlags(KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS)
|
|
|
|
.withDefaultAlgorithms())
|
|
|
|
.withMasterKey(
|
2018-06-04 19:44:47 +02:00
|
|
|
KeySpec.getBuilder(ECDSA.fromCurve(EllipticCurve._P256))
|
2018-06-04 14:50:09 +02:00
|
|
|
.withKeyFlags(KeyFlag.AUTHENTICATION, KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA)
|
|
|
|
.withDefaultAlgorithms())
|
|
|
|
.withPrimaryUserId(userId)
|
|
|
|
.withoutPassphrase()
|
|
|
|
.build();
|
2018-06-02 21:21:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 14:50:09 +02:00
|
|
|
@Override
|
|
|
|
public KeyRingBuilderInterface withSubKey(KeySpec type) {
|
|
|
|
KeyRingBuilder.this.keySpecs.add(type);
|
|
|
|
return this;
|
2018-06-02 21:21:35 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 14:50:09 +02:00
|
|
|
@Override
|
|
|
|
public WithPrimaryUserId withMasterKey(KeySpec spec) {
|
|
|
|
if ((spec.getSubpackets().getKeyFlags() & KeyFlags.CERTIFY_OTHER) == 0) {
|
|
|
|
throw new IllegalArgumentException("Certification Key MUST have KeyFlag CERTIFY_OTHER");
|
2018-06-02 21:21:35 +02:00
|
|
|
}
|
2018-06-04 14:50:09 +02:00
|
|
|
KeyRingBuilder.this.keySpecs.add(0, spec);
|
|
|
|
return new WithPrimaryUserIdImpl();
|
2018-06-02 21:21:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class WithPrimaryUserIdImpl implements WithPrimaryUserId {
|
|
|
|
|
|
|
|
@Override
|
2018-06-04 14:50:09 +02:00
|
|
|
public WithPassphrase withPrimaryUserId(String userId) {
|
|
|
|
KeyRingBuilder.this.userId = userId;
|
|
|
|
return new WithPassphraseImpl();
|
2018-06-02 21:21:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-06-04 14:50:09 +02:00
|
|
|
public WithPassphrase withPrimaryUserId(byte[] userId) {
|
2018-06-02 21:21:35 +02:00
|
|
|
return withPrimaryUserId(new String(userId, UTF8));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class WithPassphraseImpl implements WithPassphrase {
|
|
|
|
|
|
|
|
@Override
|
2018-07-12 23:21:09 +02:00
|
|
|
public Build withPassphrase(Passphrase passphrase) {
|
2018-06-02 21:21:35 +02:00
|
|
|
KeyRingBuilder.this.passphrase = passphrase;
|
|
|
|
return new BuildImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Build withoutPassphrase() {
|
|
|
|
KeyRingBuilder.this.passphrase = null;
|
|
|
|
return new BuildImpl();
|
|
|
|
}
|
|
|
|
|
|
|
|
class BuildImpl implements Build {
|
|
|
|
|
|
|
|
@Override
|
2018-07-12 11:07:53 +02:00
|
|
|
public PGPKeyRing build() throws NoSuchAlgorithmException, PGPException, NoSuchProviderException,
|
2018-06-04 14:50:09 +02:00
|
|
|
InvalidAlgorithmParameterException {
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
// Hash Calculator
|
|
|
|
PGPDigestCalculator calculator = new JcaPGPDigestCalculatorProviderBuilder()
|
|
|
|
.setProvider(BouncyCastleProvider.PROVIDER_NAME)
|
|
|
|
.build()
|
2018-07-18 16:30:42 +02:00
|
|
|
.get(HashAlgorithm.SHA1.getAlgorithmId());
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
// Encryptor for encrypting secret keys
|
|
|
|
PBESecretKeyEncryptor encryptor = passphrase == null ?
|
|
|
|
null : // unencrypted key pair, otherwise AES-256 encrypted
|
|
|
|
new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, calculator)
|
|
|
|
.setProvider(BouncyCastleProvider.PROVIDER_NAME)
|
2018-07-12 23:21:09 +02:00
|
|
|
.build(passphrase != null ? passphrase.getChars() : null);
|
|
|
|
|
|
|
|
if (passphrase != null) {
|
|
|
|
passphrase.clear();
|
|
|
|
}
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
// First key is the Master Key
|
|
|
|
KeySpec certKeySpec = keySpecs.get(0);
|
2018-06-04 14:50:09 +02:00
|
|
|
// Remove master key, so that we later only add sub keys.
|
|
|
|
keySpecs.remove(0);
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
// Generate Master Key
|
|
|
|
PGPKeyPair certKey = generateKeyPair(certKeySpec);
|
|
|
|
|
|
|
|
// Signer for creating self-signature
|
|
|
|
PGPContentSignerBuilder signer = new JcaPGPContentSignerBuilder(
|
2018-07-18 16:30:42 +02:00
|
|
|
certKey.getPublicKey().getAlgorithm(), HashAlgorithm.SHA512.getAlgorithmId())
|
2018-06-04 14:50:09 +02:00
|
|
|
.setProvider(BouncyCastleProvider.PROVIDER_NAME);
|
2018-06-02 21:21:35 +02:00
|
|
|
|
2018-06-04 14:50:09 +02:00
|
|
|
PGPSignatureSubpacketVector hashedSubPackets = certKeySpec.getSubpackets();
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
// Generator which the user can get the key pair from
|
|
|
|
PGPKeyRingGenerator ringGenerator = new PGPKeyRingGenerator(
|
|
|
|
PGPSignature.POSITIVE_CERTIFICATION, certKey,
|
2018-06-04 14:50:09 +02:00
|
|
|
userId, calculator,
|
|
|
|
hashedSubPackets, null, signer, encryptor);
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
for (KeySpec subKeySpec : keySpecs) {
|
|
|
|
PGPKeyPair subKey = generateKeyPair(subKeySpec);
|
2018-06-04 14:50:09 +02:00
|
|
|
if (subKeySpec.isInheritedSubPackets()) {
|
|
|
|
ringGenerator.addSubKey(subKey);
|
|
|
|
} else {
|
|
|
|
ringGenerator.addSubKey(subKey, subKeySpec.getSubpackets(), null);
|
|
|
|
}
|
2018-06-02 21:21:35 +02:00
|
|
|
}
|
|
|
|
|
2018-07-12 11:07:53 +02:00
|
|
|
PGPPublicKeyRing publicKeys = ringGenerator.generatePublicKeyRing();
|
|
|
|
PGPSecretKeyRing secretKeys = ringGenerator.generateSecretKeyRing();
|
2018-07-16 17:16:16 +02:00
|
|
|
|
|
|
|
// TODO: Remove once BC 1.61 is released
|
|
|
|
secretKeys = KeyRingSubKeyFix.repairSubkeyPackets(secretKeys, null, null);
|
|
|
|
|
2018-07-12 11:07:53 +02:00
|
|
|
return new PGPKeyRing(publicKeys, secretKeys);
|
2018-06-02 21:21:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private PGPKeyPair generateKeyPair(KeySpec spec)
|
2018-06-04 14:50:09 +02:00
|
|
|
throws NoSuchProviderException, NoSuchAlgorithmException, PGPException,
|
|
|
|
InvalidAlgorithmParameterException {
|
2018-06-02 21:21:35 +02:00
|
|
|
KeyType type = spec.getKeyType();
|
|
|
|
KeyPairGenerator certKeyGenerator = KeyPairGenerator.getInstance(
|
|
|
|
type.getName(), BouncyCastleProvider.PROVIDER_NAME);
|
2018-06-04 14:50:09 +02:00
|
|
|
certKeyGenerator.initialize(type.getAlgorithmSpec());
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
// Create raw Key Pair
|
2018-06-04 14:50:09 +02:00
|
|
|
KeyPair keyPair = certKeyGenerator.generateKeyPair();
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
// Form PGP key pair
|
|
|
|
PGPKeyPair pgpKeyPair = new JcaPGPKeyPair(type.getAlgorithm().getAlgorithmId(),
|
2018-06-04 14:50:09 +02:00
|
|
|
keyPair, new Date());
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
return pgpKeyPair;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-02 21:40:59 +02:00
|
|
|
}
|