public FreenetURI turnMySSKIntoUSK(FreenetURI uri) { if(uri.getKeyType().equals("SSK") && Arrays.equals(uri.getRoutingKey(), pubKeyHash) && Arrays.equals(uri.getCryptoKey(), cryptoKey) && Arrays.equals(uri.getExtra(), ClientSSK.getExtraBytes(cryptoAlgorithm)) && uri.getDocName() != null && uri.getDocName().startsWith(siteName)) { String doc = uri.getDocName(); doc = doc.substring(siteName.length()); if(doc.length() < 2 || doc.charAt(0) != '-') return uri; doc = doc.substring(1); long edition; try { edition = Long.parseLong(doc); } catch (NumberFormatException e) { Logger.normal(this, "Trying to turn SSK back into USK: "+uri+" doc="+doc+" caught "+e, e); return uri; } if(!doc.equals(Long.toString(edition))) return uri; return new FreenetURI("USK", siteName, uri.getAllMetaStrings(), pubKeyHash, cryptoKey, ClientSSK.getExtraBytes(cryptoAlgorithm), edition); } return uri; }
final FreenetURI originalURI = this; if(originalURI.isCHK()) { return originalURI; } else if(originalURI.isSSK() || originalURI.isUSK()) { FreenetURI newURI = originalURI; if(originalURI.isUSK()) newURI = newURI.sskForUSK(); InsertableClientSSK issk = InsertableClientSSK.create(newURI); newURI = issk.getURI(); if(originalURI.isUSK()) { newURI = newURI.uskForSSK(); newURI = newURI.setSuggestedEdition(originalURI.getSuggestedEdition()); } else if(originalURI.isKSK()) { return originalURI; } else { throw new IllegalArgumentException("Not implemented yet for this key type: " + getKeyType());
/** * Returns the request URI of this Sone. * * @return The request URI of this Sone */ @Nonnull public FreenetURI getRequestUri() { try { return new FreenetURI(getIdentity().getRequestUri()) .setKeyType("USK") .setDocName("Sone") .setMetaString(new String[0]) .setSuggestedEdition(latestEdition); } catch (MalformedURLException e) { throw new IllegalStateException( format("Identity %s's request URI is incorrect.", getIdentity()), e); } }
public ClientSSK(FreenetURI origURI) throws MalformedURLException { this(origURI.getDocName(), origURI.getRoutingKey(), origURI.getExtra(), null, origURI.getCryptoKey()); if(!origURI.getKeyType().equalsIgnoreCase("SSK")) throw new MalformedURLException(); }
public static USK create(FreenetURI uri) throws MalformedURLException { if(!uri.isUSK()) throw new MalformedURLException("Not a USK"); return new USK(uri.getRoutingKey(), uri.getCryptoKey(), uri.getExtra(), uri.getDocName(), uri.getSuggestedEdition()); }
/** * Generate a Sone URI from the given URI. * * @param uri * The URI to derive the Sone URI from * @return The derived URI */ public static FreenetURI create(String uri) { try { return new FreenetURI(uri).setDocName("Sone").setMetaString(new String[0]); } catch (MalformedURLException mue1) { /* this should never happen. */ logger.log(Level.WARNING, String.format("Could not create Sone URI from URI: %s", uri), mue1); return null; } }
/** Returns a <b>new</b> FreenetURI with a new suggested edition number. * Note that the suggested edition number is only valid for USKs. */ public FreenetURI setSuggestedEdition(long newEdition) { return new FreenetURI( keyType, docName, metaStr, routingKey, cryptoKey, extra, newEdition); }
FreenetURI uri; try { uri = new FreenetURI(key); Logger.normal(this, "Key: "+uri); } catch (MalformedURLException e2) { FreenetURI uri; try { uri = new FreenetURI(key); Logger.normal(this, "Key: "+uri); } catch (MalformedURLException e2) { FreenetURI uri; try { uri = new FreenetURI(key); } catch (MalformedURLException e2) { outsb.append("Malformed URI: ").append(key).append(" : ").append(e2); String fnam = uri.getDocName(); fnam = sanitize(fnam); if(fnam.length() == 0) { String[] split = line.split("#"); if(ssk) { insertURI = new FreenetURI(split[0]); line = split[1]; if(split.length > 2) try {
public FreenetURI getInstallerNonWindowsURI() { return updateURI.sskForUSK().setDocName( "installer-" + Version.buildNumber()); }
@Test public void notBeingAbleToFetchAKnownSoneDoesNotUpdateCore() { FreenetURI finalRequestUri = requestUri.sskForUSK() .setMetaString(new String[] { "sone.xml" }); soneDownloader.fetchSoneAction(sone).run(); verify(freenetInterface).fetchUri(finalRequestUri); verifyThatSoneStatusWasChangedToDownloadingAndBackTo(idle); verify(core, never()).updateSone(any(Sone.class)); }
try { if(binaryBlob) uri = new FreenetURI("CHK@"); else { String u = fs.get("URI"); if(u == null) throw new MessageInvalidException(ProtocolErrorMessage.MISSING_FIELD, "No URI", identifier, global); FreenetURI uu = new FreenetURI(u); String[] metas = uu.getAllMetaStrings(); if(metas != null && metas.length == 1) { fnam = metas[0]; uu = uu.setMetaString(null); throw new MessageInvalidException(ProtocolErrorMessage.MISSING_FIELD, "TargetURI missing but UploadFrom=redirect", identifier, global); try { redirectTarget = new FreenetURI(target); } catch (MalformedURLException e) { throw new MessageInvalidException(ProtocolErrorMessage.INVALID_FIELD, "Invalid TargetURI: "+e, identifier, global); if(uri.getRoutingKey() == null && !uri.isKSK()) targetFilename = fnam; else
static FreenetURI checkEmptySSK(FreenetURI uri, String filename, ClientContext context) { if("SSK".equals(uri.getKeyType()) && uri.getDocName() == null && uri.getRoutingKey() == null) { if(filename == null || filename.equals("")) filename = "key"; // SSK@ = use a random SSK. InsertableClientSSK key = InsertableClientSSK.createRandom(context.random, ""); return key.getInsertURI().setDocName(filename); } else { return uri; } }
private FreenetURI setupFreenetUri() { FreenetURI sskKey = mock(FreenetURI.class); FreenetURI keyWithDocName = mock(FreenetURI.class); FreenetURI keyWithMetaStrings = mock(FreenetURI.class); when(keyWithDocName.setMetaString(eq(new String[] { "sone.xml" }))).thenReturn(keyWithMetaStrings); when(sskKey.setDocName(eq("Sone-" + CURRENT_EDITION))).thenReturn(keyWithDocName); when(sone.getRequestUri().setKeyType(eq("SSK"))).thenReturn(sskKey); return keyWithMetaStrings; }
private void setupSone(Sone sone, Class<? extends Identity> identityClass) { Identity identity = mock(identityClass); InsertableClientSSK clientSSK = createRandom(new DummyRandomSource(), "WoT"); when(identity.getRequestUri()).thenReturn(clientSSK.getURI().toString()); when(identity.getId()).thenReturn("identity"); when(sone.getId()).thenReturn("identity"); when(sone.getIdentity()).thenReturn(identity); requestUri = clientSSK.getURI().setKeyType("USK").setDocName("Sone"); when(sone.getRequestUri()).thenAnswer(new Answer<FreenetURI>() { @Override public FreenetURI answer(InvocationOnMock invocation) throws Throwable { return requestUri; } }); when(sone.getTime()) .thenReturn(currentTimeMillis() - DAYS.toMillis(1)); }
private static String getForceValue(FreenetURI key, long time) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try{ bos.write(random); bos.write(key.toString().getBytes("UTF-8")); bos.write(Long.toString(time / FORCE_GRAIN_INTERVAL).getBytes("UTF-8")); } catch (IOException e) { throw new Error(e); } String f = HexUtil.bytesToHex(SHA256.digest(bos.toByteArray())); return f; }
public void checkUSK(FreenetURI uri, boolean persistent, boolean isMetadata) { try { FreenetURI uu; if(uri.isSSK() && uri.isSSKForUSK()) { uu = uri.setMetaString(null).uskForSSK(); } else if(uri.isUSK()) { uu = uri; } else { return; } USK usk = USK.create(uu); if(!isMetadata) context.uskManager.updateKnownGood(usk, uu.getSuggestedEdition(), context); else // We don't know whether the metadata is fetchable. // FIXME add a callback so if the rest of the request completes we updateKnownGood(). context.uskManager.updateSlot(usk, uu.getSuggestedEdition(), context); } catch (MalformedURLException e) { Logger.error(this, "Caught "+e, e); } catch (Throwable t) { // Don't let the USK hint cause us to not succeed on the block. Logger.error(this, "Caught "+t, t); } } }
public void testSskForUSK() throws MalformedURLException { FreenetURI uri1 = new FreenetURI(WANNA_USK_1); FreenetURI uri2 = new FreenetURI(WANNA_SSK_1); assertEquals(uri2, uri1.sskForUSK()); assertEquals(uri1, uri2.uskForSSK()); uri1.uskForSSK(); fail("no exception throw!"); } catch (IllegalStateException e) { uri2.sskForUSK(); fail("no exception throw!"); } catch (IllegalStateException e) { new FreenetURI(WANNA_CHK_1).sskForUSK(); fail("no exception throw!"); } catch (IllegalStateException e) { new FreenetURI(WANNA_CHK_1).uskForSSK(); fail("no exception throw!"); } catch (IllegalStateException e) { new FreenetURI( "SSK@5hH~39FtjA7A9~VXWtBKI~prUDTuJZURudDG0xFn3KA,GDgRGt5f6xqbmo-WraQtU54x4H~871Sho9Hz6hC-0RA,AQACAAE/Search-17XXXX/index_d51.xml") .sskForUSK(); fail("no exception throw!"); } catch (IllegalStateException e) { new FreenetURI(
FreenetURI key = new FreenetURI(request.getPartAsStringFailsafe("key", Short.MAX_VALUE)); String type = request.getPartAsStringFailsafe("content-type", 128); if (type == null) { HTMLNode content; String filenameHint = null; if (key.getKeyType().equals("CHK")) { String[] metas = key.getAllMetaStrings(); if ((metas != null) && (metas.length > 1)) { filenameHint = metas[0]; key = this.insert(block, filenameHint, false); content = ctx.getPageMaker().getInfobox("infobox-success", l10n("insertSucceededTitle"), contentNode, "successful-insert", false); String u = key.toString(); NodeL10n.getBase().addL10nSubstitution(content, "WelcomeToadlet.keyInsertedSuccessfullyWithKeyAndName", new String[]{"link", "name"}, content.addChild("br"); if (e.uri != null) { content.addChild("#", l10n("uriWouldHaveBeen", "uri", e.uri.toString())); String key; try { key = URLDecoder.decode(new FreenetURI(request.getPartAsStringFailsafe("key", Short.MAX_VALUE)).toURI("/").toString(), false); } catch (Exception e) { sendErrorPage(ctx, l10n("invalidURI"), l10n("invalidURILong"), e);
@Test public void returnedUriHasCorrectDocNameAndMetaStrings() { FreenetURI uri = createRandom(new DummyRandomSource(), "test-0").getURI().uskForSSK(); assertThat(create(uri.toString()).getDocName(), is("Sone")); assertThat(create(uri.toString()).getAllMetaStrings(), is(new String[0])); }
@Override public FreenetURI checkSource(String source) throws PluginNotFoundException { OfficialPluginDescription desc = node.getPluginManager().getOfficialPlugin(source); if(desc == null) throw new PluginNotFoundException("Not in the official plugins list: "+source); if(desc.uri != null) return desc.uri; else { return node.nodeUpdater.getURI().setDocName(source).setSuggestedEdition(desc.recommendedVersion).sskForUSK(); } }