public static InsertableClientSSK create(FreenetURI uri) throws MalformedURLException {
if(uri.getKeyType().equalsIgnoreCase("KSK"))
return ClientKSK.create(uri);
if(uri.getRoutingKey() == null)
throw new MalformedURLException("Insertable SSK URIs must have a private key!: "+uri);
if(uri.getCryptoKey() == null)
throw new MalformedURLException("Insertable SSK URIs must have a private key!: "+uri);
byte keyType;
byte[] extra = uri.getExtra();
if(uri.getKeyType().equals("SSK")) {
if(extra == null)
throw new MalformedURLException("Inserting pre-1010 keys not supported");
// Formatted exactly as ,extra on fetching
if(extra.length < 5)
throw new MalformedURLException("SSK private key ,extra too short");
if(extra[1] != 1) {
throw new MalformedURLException("SSK not a private key");
}
keyType = extra[2];
if(keyType != Key.ALGO_AES_PCFB_256_SHA256)
throw new MalformedURLException("Unrecognized crypto type in SSK private key");
}
else {
throw new MalformedURLException("Not a valid SSK insert URI type: "+uri.getKeyType());
}
// Allow docName="" for SSKs. E.g. GenerateSSK returns these; we want to be consistent.
// However, we recommend that you not use this, especially not for a freesite, as
// SSK@blah,blah,blah//filename is confusing for clients, browsers etc.
if(uri.getDocName() == null)
throw new MalformedURLException("SSK URIs must have a document name (to avoid ambiguity)");
DSAGroup g = Global.DSAgroupBigA;
DSAPrivateKey privKey;
try {
privKey = new DSAPrivateKey(new NativeBigInteger(1, uri.getRoutingKey()), g);
} catch(IllegalArgumentException e) {
// DSAPrivateKey is invalid
Logger.error(InsertableClientSSK.class, "Caught "+e, e);
throw new MalformedURLException("SSK private key (routing key) is invalid: " + e);
}
DSAPublicKey pubKey = new DSAPublicKey(g, privKey);
byte[] pkHash = pubKey.asBytesHash();
return new InsertableClientSSK(uri.getDocName(), pkHash, pubKey, privKey, uri.getCryptoKey(), keyType);
}