2021-10-07 15:48:52 +02:00
|
|
|
// SPDX-FileCopyrightText: 2018 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2018-07-18 18:23:06 +02:00
|
|
|
package org.pgpainless.algorithm;
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
import java.util.Map;
|
2020-01-09 20:49:12 +01:00
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
|
|
|
|
|
2021-04-25 13:28:33 +02:00
|
|
|
/**
|
|
|
|
* Enumeration of public key algorithms as defined in RFC4880.
|
|
|
|
*
|
|
|
|
* @see <a href="https://tools.ietf.org/html/rfc4880#section-9.1">RFC4880: Public-Key Algorithms</a>
|
|
|
|
*/
|
2018-06-02 21:21:35 +02:00
|
|
|
public enum PublicKeyAlgorithm {
|
|
|
|
|
2020-12-11 22:52:28 +01:00
|
|
|
/**
|
|
|
|
* RSA capable of encryption and signatures.
|
|
|
|
*/
|
2021-04-26 13:38:12 +02:00
|
|
|
RSA_GENERAL (PublicKeyAlgorithmTags.RSA_GENERAL, true, true),
|
2020-12-11 18:15:54 +01:00
|
|
|
|
|
|
|
/**
|
2020-12-11 22:52:28 +01:00
|
|
|
* RSA with usage encryption.
|
|
|
|
*
|
2020-12-11 18:15:54 +01:00
|
|
|
* @deprecated see https://tools.ietf.org/html/rfc4880#section-13.5
|
|
|
|
*/
|
2021-04-25 13:28:33 +02:00
|
|
|
@Deprecated
|
2021-04-26 13:38:12 +02:00
|
|
|
RSA_ENCRYPT (PublicKeyAlgorithmTags.RSA_ENCRYPT, false, true),
|
2020-12-11 18:15:54 +01:00
|
|
|
|
|
|
|
/**
|
2020-12-11 22:52:28 +01:00
|
|
|
* RSA with usage of creating signatures.
|
|
|
|
*
|
2020-12-11 18:15:54 +01:00
|
|
|
* @deprecated see https://tools.ietf.org/html/rfc4880#section-13.5
|
|
|
|
*/
|
2021-04-25 13:28:33 +02:00
|
|
|
@Deprecated
|
2021-04-26 13:38:12 +02:00
|
|
|
RSA_SIGN (PublicKeyAlgorithmTags.RSA_SIGN, true, false),
|
2020-12-11 18:15:54 +01:00
|
|
|
|
2020-12-11 22:52:28 +01:00
|
|
|
/**
|
|
|
|
* ElGamal with usage encryption.
|
|
|
|
*/
|
2021-04-26 13:38:12 +02:00
|
|
|
ELGAMAL_ENCRYPT (PublicKeyAlgorithmTags.ELGAMAL_ENCRYPT, false, true),
|
2020-12-11 18:15:54 +01:00
|
|
|
|
2020-12-11 22:52:28 +01:00
|
|
|
/**
|
|
|
|
* Digital Signature Algorithm.
|
|
|
|
*/
|
2021-04-26 13:38:12 +02:00
|
|
|
DSA (PublicKeyAlgorithmTags.DSA, true, false),
|
2020-12-11 22:52:28 +01:00
|
|
|
|
2018-06-02 21:21:35 +02:00
|
|
|
/**
|
2018-07-02 21:40:59 +02:00
|
|
|
* EC is deprecated.
|
2018-06-02 21:21:35 +02:00
|
|
|
* @deprecated use {@link #ECDH} instead.
|
|
|
|
*/
|
2021-04-25 13:28:33 +02:00
|
|
|
@Deprecated
|
2021-04-26 13:38:12 +02:00
|
|
|
EC (PublicKeyAlgorithmTags.EC, false, true),
|
2020-12-11 22:52:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Elliptic Curve Diffie-Hellman.
|
|
|
|
*/
|
2021-04-26 13:38:12 +02:00
|
|
|
ECDH (PublicKeyAlgorithmTags.ECDH, false, true),
|
2020-12-11 22:52:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Elliptic Curve Digital Signature Algorithm.
|
|
|
|
*/
|
2021-04-26 13:38:12 +02:00
|
|
|
ECDSA (PublicKeyAlgorithmTags.ECDSA, true, false),
|
2020-12-11 18:14:36 +01:00
|
|
|
|
|
|
|
/**
|
2020-12-11 22:52:28 +01:00
|
|
|
* ElGamal General.
|
|
|
|
*
|
2020-12-11 18:14:36 +01:00
|
|
|
* @deprecated see https://tools.ietf.org/html/rfc4880#section-13.8
|
|
|
|
*/
|
|
|
|
@Deprecated
|
2021-04-26 13:38:12 +02:00
|
|
|
ELGAMAL_GENERAL (PublicKeyAlgorithmTags.ELGAMAL_GENERAL, true, true),
|
2020-12-11 22:52:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Diffie-Hellman key exchange algorithm.
|
|
|
|
*/
|
2021-04-26 13:38:12 +02:00
|
|
|
DIFFIE_HELLMAN (PublicKeyAlgorithmTags.DIFFIE_HELLMAN, false, true),
|
2020-12-11 22:52:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Digital Signature Algorithm based on twisted Edwards Curves.
|
|
|
|
*/
|
2021-04-26 13:38:12 +02:00
|
|
|
EDDSA (PublicKeyAlgorithmTags.EDDSA, true, false),
|
2018-06-02 21:21:35 +02:00
|
|
|
;
|
|
|
|
|
2020-01-09 20:49:12 +01:00
|
|
|
private static final Map<Integer, PublicKeyAlgorithm> MAP = new ConcurrentHashMap<>();
|
2018-06-02 21:21:35 +02:00
|
|
|
|
|
|
|
static {
|
|
|
|
for (PublicKeyAlgorithm p : PublicKeyAlgorithm.values()) {
|
|
|
|
MAP.put(p.algorithmId, p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-25 13:28:33 +02:00
|
|
|
/**
|
|
|
|
* Return the {@link PublicKeyAlgorithm} that corresponds to the provided algorithm id.
|
|
|
|
* If an invalid id is provided, null is returned.
|
|
|
|
*
|
|
|
|
* @param id numeric algorithm id
|
|
|
|
* @return algorithm
|
|
|
|
*/
|
2018-06-02 21:21:35 +02:00
|
|
|
public static PublicKeyAlgorithm fromId(int id) {
|
|
|
|
return MAP.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
private final int algorithmId;
|
2021-04-26 13:38:12 +02:00
|
|
|
private final boolean signingCapable;
|
|
|
|
private final boolean encryptionCapable;
|
2018-06-02 21:21:35 +02:00
|
|
|
|
2021-04-26 13:38:12 +02:00
|
|
|
PublicKeyAlgorithm(int algorithmId, boolean signingCapable, boolean encryptionCapable) {
|
2018-06-02 21:21:35 +02:00
|
|
|
this.algorithmId = algorithmId;
|
2021-04-26 13:38:12 +02:00
|
|
|
this.signingCapable = signingCapable;
|
|
|
|
this.encryptionCapable = encryptionCapable;
|
2018-06-02 21:21:35 +02:00
|
|
|
}
|
|
|
|
|
2021-04-25 13:28:33 +02:00
|
|
|
/**
|
|
|
|
* Return the numeric identifier of the public key algorithm.
|
|
|
|
*
|
|
|
|
* @return id
|
|
|
|
*/
|
2018-06-02 21:21:35 +02:00
|
|
|
public int getAlgorithmId() {
|
|
|
|
return algorithmId;
|
|
|
|
}
|
2021-04-26 13:38:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if this public key algorithm is able to create signatures.
|
|
|
|
*
|
|
|
|
* @return true if can sign
|
|
|
|
*/
|
|
|
|
public boolean isSigningCapable() {
|
|
|
|
return signingCapable;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if this public key algorithm can be used as an encryption algorithm.
|
|
|
|
*
|
|
|
|
* @return true if can encrypt
|
|
|
|
*/
|
|
|
|
public boolean isEncryptionCapable() {
|
|
|
|
return encryptionCapable;
|
|
|
|
}
|
2018-06-02 21:21:35 +02:00
|
|
|
}
|