1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| public class SshKeyValidator {
/**
*
* Enum Type of key.
*
*/
private enum TypeOfKey {
notDefined, openSSH, SSH2
}
/**
*
* @param inKey
* @return true if the key is valid.
* @throws IllegalSizeException
* @throws IllegalSshFormat
*/
public static boolean validateSshKey(final String inKey) throws IllegalSizeException, IllegalSshFormat {
TypeOfKey typeOfKey = TypeOfKey.notDefined;
try {
validateKeyByteContent(inKey);
BufferedReader reader = new BufferedReader(new StringReader(inKey));
String line = null;
StringBuilder builder = new StringBuilder();
line = reader.readLine();
// Check if it is a SSH2 Public Key
if (line != null && "---- BEGIN SSH2 PUBLIC KEY ----".equals(line)) {
typeOfKey = validateSSH2PublicKey(reader, builder);
} else {
// Check if it is a Open Public Key
typeOfKey = validateOpenPublicKey(typeOfKey, line, builder);
}
// Check if can be decoded from B64 en encoded to Hexa
byte[] keyB64Decoded = Base64.decode(builder.toString());
validateKeyLenght(typeOfKey, keyB64Decoded);
} catch (IOException e) {
throw new IllegalSshFormat("The ssh key is not valid", e);
}
return true;
}
/**
* @param inKey
* @throws IOException
*/
private static void validateKeyByteContent(final String inKey) throws IOException {
InputStream is = new ByteArrayInputStream(inKey.getBytes());
DataInputStream dis = new DataInputStream(is);
dis.readFully(inKey.getBytes());
dis.close();
}
/**
* @param reader
* @param builder
* @param isFooterPresent
* @return
* @throws IOException
* @throws IllegalSshFormat
*/
private static TypeOfKey validateSSH2PublicKey(BufferedReader reader, StringBuilder builder) throws IOException, IllegalSshFormat {
boolean isFooterPresent = false;
TypeOfKey typeOfKey;
String line;
typeOfKey = TypeOfKey.SSH2;
// Check if footer are present and get the B54 key
while ((line = reader.readLine()) != null) {
if (line.startsWith("Comment:")) {
continue;
} else if ("---- END SSH2 PUBLIC KEY ----".equals(line)) {
isFooterPresent = true;
break;
}
if (builder.length() == 0 && !line.startsWith("AAAAB3NzaC1yc2EAAAA")) {
throw new IllegalSshFormat("no valid rsa-ssh key");
}
builder.append(line);
}
if (!isFooterPresent) {
throw new IllegalSshFormat("Invalid Footer");
}
return typeOfKey;
}
/**
* @param typeOfKey
* @param line
* @param builder
* @return
* @throws IllegalSshFormat
*/
private static TypeOfKey validateOpenPublicKey(TypeOfKey typeOfKey, String line, StringBuilder builder) throws IllegalSshFormat {
if (line != null && line.startsWith("ssh-rsa AAAAB3NzaC1yc2EAAAA")) {
typeOfKey = TypeOfKey.openSSH;
line = line.replaceFirst("ssh-rsa ", "");
builder.append(line.substring(0, line.lastIndexOf(" ")));
} else {
throw new IllegalSshFormat("Invalid Header");
}
return typeOfKey;
}
/**
* @param typeOfKey
* @param keyB64Decoded
* @throws IllegalSizeException
*/
private static void validateKeyLenght(TypeOfKey typeOfKey, byte[] keyB64Decoded) throws IllegalSizeException {
// Check lenght of the Key
int keyLenght = (keyB64Decoded.length - 20) * 8;
if (typeOfKey == TypeOfKey.openSSH) {
keyLenght = keyLenght - 8;
}
if (keyLenght < 2048 || keyLenght > 4104) {// 4104 = lenght of ssh 4096
throw new IllegalSizeException("Invalid lenght: should be [2048 >= lenght <= 4096]");
}
}
/**
* IllegalSizeException Thrown when the size of the sshKey is not correct.
*
* @author same
*
* @since 1.2.5
*
*/
public static class IllegalSizeException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1340984138460417925L;
/**
* @param message
* @param cause
*/
public IllegalSizeException(String message, Throwable cause) {
super(message, cause);
}
/**
* @param message
*/
public IllegalSizeException(String message) {
super(message);
}
}
/**
*
* IllegalSshFormat Thrown when the format of the sshKey is not correct..
*
* @author same
*
* @since 1.2.5
*
*/
public static class IllegalSshFormat extends Exception {
/**
*
*/
private static final long serialVersionUID = -1714326896531848932L;
/**
* @param message
* @param cause
*/
public IllegalSshFormat(String message, Throwable cause) {
super(message, cause);
}
/**
* @param message
*/
public IllegalSshFormat(String message) {
super(message);
}
}
} |
Partager