public DSAPublicKey cloneKey() { return new DSAPublicKey(this); }
@Override public byte[] getPubkeyBytes() { return pubKey.asBytes(); }
public synchronized void setPublicKey(DSAPublicKey pubKey) { if((this.pubKey != null) && (this.pubKey != pubKey) && !this.pubKey.equals(pubKey)) throw new IllegalArgumentException("Cannot reassign: was "+this.pubKey+" now "+pubKey); byte[] newKeyHash = pubKey.asBytesHash(); if(!Arrays.equals(newKeyHash, pubKeyHash)) throw new IllegalArgumentException("New pubKey hash does not match pubKeyHash: "+HexUtil.bytesToHex(newKeyHash)+" ( "+HexUtil.bytesToHex(pubKey.asBytesHash())+" != "+HexUtil.bytesToHex(pubKeyHash)+" for "+pubKey); this.pubKey = pubKey; this.cachedNodeKey = null; }
public void testSimple() throws IOException { final int keys = 10; PubkeyStore pk = new PubkeyStore(); new RAMFreenetStore<DSAPublicKey>(pk, keys); DSAGroup group = Global.DSAgroupBigA; Random random = new MersenneTwister(1010101); HashMap<ByteArrayWrapper, DSAPublicKey> map = new HashMap<ByteArrayWrapper, DSAPublicKey>(); for(int i=0;i<keys;i++) { DSAPrivateKey privKey = new DSAPrivateKey(group, random); DSAPublicKey key = new DSAPublicKey(group, privKey); byte[] hash = key.asBytesHash(); ByteArrayWrapper w = new ByteArrayWrapper(hash); map.put(w, key.cloneKey()); pk.put(hash, key, false); assertTrue(pk.fetch(hash, false, false, null).equals(key)); } int x = 0; for(Map.Entry<ByteArrayWrapper, DSAPublicKey> entry : map.entrySet()) { x++; assertTrue(pk.fetch(entry.getKey().get(), false, false, null).equals(entry.getValue())); } assert(x == keys); }
public static InsertableClientSSK createRandom(RandomSource r, String docName) { byte[] ckey = new byte[CRYPTO_KEY_LENGTH]; r.nextBytes(ckey); DSAGroup g = Global.DSAgroupBigA; DSAPrivateKey privKey = new DSAPrivateKey(g, r); DSAPublicKey pubKey = new DSAPublicKey(g, privKey); try { byte[] pkHash = SHA256.digest(pubKey.asBytes()); return new InsertableClientSSK(docName, pkHash, pubKey, privKey, ckey, Key.ALGO_AES_PCFB_256_SHA256); } catch (MalformedURLException e) { throw new Error(e); } }
public static KeyBlock createBlock(short keyType, byte[] keyBytes, byte[] headersBytes, byte[] dataBytes, byte[] pubkeyBytes) throws KeyVerifyException { byte type = (byte)(keyType >> 8); byte subtype = (byte)(keyType & 0xFF); if(type == NodeCHK.BASE_TYPE) { // For CHKs, the subtype is the crypto algorithm. return CHKBlock.construct(dataBytes, headersBytes, subtype); } else if(type == NodeSSK.BASE_TYPE) { DSAPublicKey pubKey; try { pubKey = new DSAPublicKey(pubkeyBytes); } catch (IOException e) { throw new KeyVerifyException("Failed to construct pubkey: "+e, e); } catch (CryptFormatException e) { throw new KeyVerifyException("Failed to construct pubkey: "+e, e); } NodeSSK key = new NodeSSK(pubKey.asBytesHash(), keyBytes, pubKey, subtype); return new SSKBlock(dataBytes, headersBytes, key, false); } else { throw new KeyVerifyException("No such key type "+Integer.toHexString(type)); } }
public void setPubKey(DSAPublicKey pubKey2) throws SSKVerifyException { if(pubKey == pubKey2) return; if(pubKey2 == null) return; if((pubKey == null) || !pubKey2.equals(pubKey)) { if(pubKey2 != null) { byte[] newPubKeyHash = SHA256.digest(pubKey2.asBytes()); if(Arrays.equals(pubKeyHash, newPubKeyHash)) { if(pubKey != null) { // same hash, yet different keys! Logger.error(this, "Found SHA-256 collision or something... WTF?"); throw new SSKVerifyException("Invalid new pubkey: "+pubKey2+" old pubkey: "+pubKey); } // Valid key, assign. } else { throw new SSKVerifyException("New pubkey has invalid hash"); } } pubKey = pubKey2; } }
@Override public byte[] getRoutingKey() { return asBytesHash(); }
@Override public boolean equals(Object o) { if(!(o instanceof SSKBlock)) return false; SSKBlock block = (SSKBlock)o; if(!block.pubKey.equals(pubKey)) return false; if(!block.nodeKey.equals(nodeKey)) return false; if(block.headersOffset != headersOffset) return false; if(block.hashIdentifier != hashIdentifier) return false; if(block.symCipherIdentifier != symCipherIdentifier) return false; // only compare some of the headers (see top) for (int i = 0; i < HEADER_COMPARE_TO; i++) { if (block.headers[i] != headers[i]) return false; } //if(!Arrays.equals(block.headers, headers)) return false; if(!Arrays.equals(block.data, data)) return false; return true; }
private ClientSSK(ClientSSK key) { this.cryptoAlgorithm = key.cryptoAlgorithm; this.docName = key.docName; if(key.pubKey != null) this.pubKey = key.pubKey.cloneKey(); else this.pubKey = null; pubKeyHash = key.pubKeyHash.clone(); cryptoKey = key.cryptoKey.clone(); ehDocname = key.ehDocname.clone(); hashCode = Fields.hashCode(pubKeyHash) ^ Fields.hashCode(cryptoKey) ^ Fields.hashCode(ehDocname) ^ docName.hashCode(); }
@Override public DSAPublicKey construct(byte[] data, byte[] headers, byte[] routingKey, byte[] fullKey, boolean canReadClientCache, boolean canReadSlashdotCache, BlockMetadata meta, DSAPublicKey ignored) throws KeyVerifyException { if(data == null) throw new PubkeyVerifyException("Need data to construct pubkey"); try { return DSAPublicKey.create(data); } catch (CryptFormatException e) { throw new PubkeyVerifyException(e); } }
public void put(byte[] hash, DSAPublicKey key, boolean isOldBlock) throws IOException { try { store.put(key, key.asPaddedBytes(), empty, false, isOldBlock); } catch (KeyCollisionException e) { Logger.error(this, "Impossible for PubkeyStore: "+e, e); } }
@Override public byte[] fingerprint() { synchronized(this) { if(fingerprint == null) fingerprint = fingerprint(new BigInteger[]{y}); return fingerprint; } }
public void testSimple() { final int keys = 10; PubkeyStore pk = new PubkeyStore(); new RAMFreenetStore<DSAPublicKey>(pk, keys); GetPubkey pubkeys = new SimpleGetPubkey(pk); DSAGroup group = Global.DSAgroupBigA; Random random = new MersenneTwister(1010101); HashMap<ByteArrayWrapper, DSAPublicKey> map = new HashMap<ByteArrayWrapper, DSAPublicKey>(); for(int i=0;i<keys;i++) { DSAPrivateKey privKey = new DSAPrivateKey(group, random); DSAPublicKey key = new DSAPublicKey(group, privKey); byte[] hash = key.asBytesHash(); ByteArrayWrapper w = new ByteArrayWrapper(hash); map.put(w, key.cloneKey()); pubkeys.cacheKey(hash, key, false, false, false, false, false); assertTrue(pubkeys.getKey(hash, false, false, null).equals(key)); } int x = 0; for(Map.Entry<ByteArrayWrapper, DSAPublicKey> entry : map.entrySet()) { x++; assertTrue(pubkeys.getKey(entry.getKey().get(), false, false, null).equals(entry.getValue())); } assert(x == keys); }
public static ClientKSK create(String keyword) { MessageDigest md256 = SHA256.getMessageDigest(); try { byte[] keywordHash; try { keywordHash = md256.digest(keyword.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new Error("Impossible: JVM doesn't support UTF-8: " + e, e); } MersenneTwister mt = new MersenneTwister(keywordHash); DSAPrivateKey privKey = new DSAPrivateKey(Global.DSAgroupBigA, mt); DSAPublicKey pubKey = new DSAPublicKey(Global.DSAgroupBigA, privKey); byte[] pubKeyHash = md256.digest(pubKey.asBytes()); try { return new ClientKSK(keyword, pubKeyHash, pubKey, privKey, keywordHash); } catch (MalformedURLException e) { throw new Error(e); } } finally { SHA256.returnMessageDigest(md256); } }
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);