Commit dd8b4457 authored by michael.simon's avatar michael.simon
Browse files

Don't break on unsupported key type

parent 0a41f178
......@@ -25,10 +25,10 @@ public class OpenSshKeyDecoder implements Serializable {
private static final long serialVersionUID = 1L;
public OpenSshPublicKey decode(String opensshPublicKey) {
public OpenSshPublicKey decode(String opensshPublicKey) throws UnsupportedKeyTypeException {
OpenSshPublicKey key = new OpenSshPublicKey();
key.setBytes(getKeyBytes(opensshPublicKey));
getKeyBytes(key, opensshPublicKey);
try {
String type = decodeType(key);
......@@ -54,21 +54,23 @@ public class OpenSshKeyDecoder implements Serializable {
ECPublicKeySpec spec = new ECPublicKeySpec(ecPoint, ecParameterSpec);
key.setPublicKey(KeyFactory.getInstance("EC").generatePublic(spec));
} else {
throw new IllegalArgumentException("Unsupported key type " + type);
key.setDecoderResult("Unsupported key type");
}
return key;
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new IllegalArgumentException("Unable to decode public key", e);
key.setDecoderResult("Unable to decode public key");
return key;
}
}
private byte[] getKeyBytes(String opensshPublicKey) {
private void getKeyBytes(OpenSshPublicKey key, String opensshPublicKey) throws UnsupportedKeyTypeException {
for (String part : opensshPublicKey.split(" ")) {
if (Base64.isBase64(part) && part.startsWith("AAAA")) {
return Base64.decodeBase64(part);
key.setBaseDate(part);
key.setBytes(Base64.decodeBase64(part));
}
}
throw new IllegalArgumentException("no Base64 part to decode");
throw new UnsupportedKeyTypeException("no Base64 part to decode");
}
private String decodeType(OpenSshPublicKey key) {
......@@ -104,7 +106,7 @@ public class OpenSshKeyDecoder implements Serializable {
return new ECPoint(x, y);
}
ECParameterSpec getECParameterSpec(String identifier) {
ECParameterSpec getECParameterSpec(String identifier) throws UnsupportedKeyTypeException {
try {
// http://www.bouncycastle.org/wiki/pages/viewpage.action?pageId=362269#SupportedCurves(ECDSAandECGOST)-NIST(aliasesforSECcurves)
String name = identifier.replace("nist", "sec") + "r1";
......@@ -112,7 +114,7 @@ public class OpenSshKeyDecoder implements Serializable {
parameters.init(new ECGenParameterSpec(name));
return parameters.getParameterSpec(ECParameterSpec.class);
} catch (InvalidParameterSpecException | NoSuchAlgorithmException e) {
throw new IllegalArgumentException("Unable to get parameter spec for identifier " + identifier, e);
throw new UnsupportedKeyTypeException("Unable to get parameter spec for identifier " + identifier, e);
}
}
}
......@@ -7,6 +7,8 @@ public class OpenSshPublicKey {
private byte[] bytes;
private int decoderPos;
private PublicKey publicKey;
private String baseDate;
private String decoderResult;
public OpenSshPublicKey() {
super();
......@@ -40,4 +42,20 @@ public class OpenSshPublicKey {
public void setPublicKey(PublicKey publicKey) {
this.publicKey = publicKey;
}
public String getBaseDate() {
return baseDate;
}
public void setBaseDate(String baseDate) {
this.baseDate = baseDate;
}
public String getDecoderResult() {
return decoderResult;
}
public void setDecoderResult(String decoderResult) {
this.decoderResult = decoderResult;
}
}
package edu.kit.scc.webreg.ssh;
public class UnsupportedKeyTypeException extends Exception {
private static final long serialVersionUID = 1L;
public UnsupportedKeyTypeException() {
super();
}
public UnsupportedKeyTypeException(String arg0, Throwable arg1) {
super(arg0, arg1);
}
public UnsupportedKeyTypeException(String arg0) {
super(arg0);
}
public UnsupportedKeyTypeException(Throwable arg0) {
super(arg0);
}
}
......@@ -24,6 +24,7 @@ import edu.kit.scc.webreg.service.UserService;
import edu.kit.scc.webreg.session.SessionManager;
import edu.kit.scc.webreg.ssh.OpenSshKeyDecoder;
import edu.kit.scc.webreg.ssh.OpenSshPublicKey;
import edu.kit.scc.webreg.ssh.UnsupportedKeyTypeException;
@ManagedBean
@ViewScoped
......@@ -53,8 +54,13 @@ public class UserSshKeyManagementBean implements Serializable {
}
public void deployKey() {
OpenSshPublicKey key = keyDecoder.decode(newKey);
keyList.add(key);
OpenSshPublicKey key;
try {
key = keyDecoder.decode(newKey);
keyList.add(key);
} catch (UnsupportedKeyTypeException e) {
// happenes when there is not base64 part in key
}
}
public UserEntity getUser() {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment