Ejemplo n.º 1
0
  public void testKeyGen() {
    RandomSource.getInstance().nextBoolean();
    byte src[] = new byte[200];
    RandomSource.getInstance().nextBytes(src);

    I2PAppContext ctx = I2PAppContext.getGlobalContext();
    for (int i = 0; i < 10; i++) {
      Object keys[] = KeyGenerator.getInstance().generatePKIKeypair();
      byte ctext[] = ctx.elGamalEngine().encrypt(src, (PublicKey) keys[0]);
      byte ptext[] = ctx.elGamalEngine().decrypt(ctext, (PrivateKey) keys[1]);
      assertTrue(DataHelper.eq(ptext, src));
    }

    Object obj[] = KeyGenerator.getInstance().generateSigningKeypair();
    SigningPublicKey fake = (SigningPublicKey) obj[0];
    for (int i = 0; i < 10; i++) {
      Object keys[] = KeyGenerator.getInstance().generateSigningKeypair();

      Signature sig = DSAEngine.getInstance().sign(src, (SigningPrivateKey) keys[1]);
      assertTrue(DSAEngine.getInstance().verifySignature(sig, src, (SigningPublicKey) keys[0]));
      assertFalse(DSAEngine.getInstance().verifySignature(sig, src, fake));
    }

    for (int i = 0; i < 1000; i++) {
      KeyGenerator.getInstance().generateSessionKey();
    }
  }
Ejemplo n.º 2
0
  /**
   * Verify that the signature matches the destination's signing public key.
   *
   * @return true only if the signature matches
   */
  public boolean verifySignature() {
    if (getSignature() == null) {
      if (_log.shouldLog(Log.WARN)) _log.warn("Signature is null!");
      return false;
    }
    if (getDestination() == null) {
      if (_log.shouldLog(Log.WARN)) _log.warn("Destination is null!");
      return false;
    }
    if (getCreationDate() == null) {
      if (_log.shouldLog(Log.WARN)) _log.warn("Date is null!");
      return false;
    }
    if (tooOld()) {
      if (_log.shouldLog(Log.WARN)) _log.warn("Too old!");
      return false;
    }
    byte data[] = getBytes();
    if (data == null) {
      if (_log.shouldLog(Log.WARN)) _log.warn("Bytes could not be found - wtf?");
      return false;
    }

    boolean ok =
        DSAEngine.getInstance()
            .verifySignature(getSignature(), data, getDestination().getSigningPublicKey());
    if (!ok) {
      if (_log.shouldLog(Log.WARN)) _log.warn("DSA signature failed!");
    }
    return ok;
  }
Ejemplo n.º 3
0
 /** @param sigprop The signature property to set */
 private void signIt(SigningPrivateKey spk, String sigprop) {
   if (props == null) throw new IllegalStateException();
   if (props.containsKey(sigprop)) throw new IllegalStateException();
   StringWriter buf = new StringWriter(1024);
   buf.append(name);
   buf.append(KV_SEPARATOR);
   buf.append(dest);
   try {
     writeProps(buf);
   } catch (IOException ioe) {
     throw new IllegalStateException(ioe);
   }
   Signature s = DSAEngine.getInstance().sign(DataHelper.getUTF8(buf.toString()), spk);
   if (s == null) throw new IllegalArgumentException("sig failed");
   props.setProperty(sigprop, s.toBase64());
 }
Ejemplo n.º 4
0
 /**
  * Sign as a "remove" line #!dest=dest#name=name#k1=v1#sig=sig...] Must have been constructed with
  * non-null properties.
  */
 public void signRemove(SigningPrivateKey spk) {
   if (props == null) throw new IllegalStateException();
   if (props.containsKey(PROP_SIG)) throw new IllegalStateException();
   props.setProperty(PROP_NAME, name);
   props.setProperty(PROP_DEST, dest);
   StringWriter buf = new StringWriter(1024);
   try {
     writeProps(buf);
   } catch (IOException ioe) {
     throw new IllegalStateException(ioe);
   }
   props.remove(PROP_NAME);
   props.remove(PROP_DEST);
   Signature s = DSAEngine.getInstance().sign(DataHelper.getUTF8(buf.toString()), spk);
   if (s == null) throw new IllegalArgumentException("sig failed");
   props.setProperty(PROP_SIG, s.toBase64());
 }
Ejemplo n.º 5
0
 /** Verify with the "olddest" property's public key using the "oldsig" property */
 public boolean hasValidInnerSig() {
   if (props == null || name == null || dest == null) return false;
   boolean rv = false;
   // don't cache result
   if (true) {
     StringWriter buf = new StringWriter(1024);
     String sig = props.getProperty(PROP_OLDSIG);
     String olddest = props.getProperty(PROP_OLDDEST);
     if (sig == null || olddest == null) return false;
     buf.append(name);
     buf.append(KV_SEPARATOR);
     buf.append(dest);
     try {
       writeProps(buf, true, true);
     } catch (IOException ioe) {
       // won't happen
       return false;
     }
     byte[] sdata = Base64.decode(sig);
     if (sdata == null) return false;
     Destination d;
     try {
       d = new Destination(olddest);
     } catch (DataFormatException dfe) {
       return false;
     }
     SigningPublicKey spk = d.getSigningPublicKey();
     SigType type = spk.getType();
     if (type == null) return false;
     Signature s;
     try {
       s = new Signature(type, sdata);
     } catch (IllegalArgumentException iae) {
       return false;
     }
     rv = DSAEngine.getInstance().verifySignature(s, DataHelper.getUTF8(buf.toString()), spk);
   }
   return rv;
 }
Ejemplo n.º 6
0
 /**
  * Sign the structure using the supplied private key
  *
  * @param signingKey SigningPrivateKey to sign with
  * @throws DataFormatException
  */
 public void signSessionConfig(SigningPrivateKey signingKey) throws DataFormatException {
   byte data[] = getBytes();
   if (data == null) throw new DataFormatException("Unable to retrieve bytes for signing");
   _signature = DSAEngine.getInstance().sign(data, signingKey);
 }