public void savePublicKey(SessionID sessionID, PublicKey pubKey) {
    if (sessionID == null) return;

    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pubKey.getEncoded());

    //  if (!Address.hasResource(fullUserId))
    //    return;

    this.store.setProperty(
        sessionID.getRemoteUserId() + ".publicKey", x509EncodedKeySpec.getEncoded());
    // Stash the associated fingerprint.  This saves calculating it in the future
    // and is useful for transferring rosters to other apps.
    try {
      String fingerprintString = new OtrCryptoEngineImpl().getFingerprint(pubKey);
      String verifiedToken =
          buildPublicKeyVerifiedId(sessionID.getRemoteUserId(), fingerprintString.toLowerCase());
      if (!this.store.hasProperty(verifiedToken)) this.store.setProperty(verifiedToken, false);

      this.store.setPropertyHex(
          sessionID.getRemoteUserId() + ".fingerprint", Hex.decode(fingerprintString));
      store.save();
    } catch (OtrCryptoException e) {
      e.printStackTrace();
    }
  }
  /**
   * Stores the public key for a specified user from sessionID
   *
   * @param sessionID sessionID to identifiy the owner of the key
   * @param pubKey the key which should be stored
   */
  public void savePublicKey(SessionID sessionID, PublicKey pubKey) {
    if (sessionID == null) return;

    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pubKey.getEncoded());

    String userID = sessionID.getUserID();
    this.store.setProperty(userID + ".publicKey", x509EncodedKeySpec.getEncoded());

    this.store.removeProperty(userID + ".publicKey.verified");
  }
 // internal implementation of generatePublic. See JCA doc
 private PublicKey generatePublic(KeySpec keySpec) throws GeneralSecurityException {
   if (keySpec instanceof X509EncodedKeySpec) {
     X509EncodedKeySpec x509Spec = (X509EncodedKeySpec) keySpec;
     return new RSAPublicKeyImpl(x509Spec.getEncoded());
   } else if (keySpec instanceof RSAPublicKeySpec) {
     RSAPublicKeySpec rsaSpec = (RSAPublicKeySpec) keySpec;
     return new RSAPublicKeyImpl(rsaSpec.getModulus(), rsaSpec.getPublicExponent());
   } else {
     throw new InvalidKeySpecException(
         "Only RSAPublicKeySpec " + "and X509EncodedKeySpec supported for RSA public keys");
   }
 }
    public RSAKeyPair() {
      KeyPairGenerator keyGen = null;
      try {
        keyGen = KeyPairGenerator.getInstance("RSA");
      } catch (NoSuchAlgorithmException e) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
      }
      keyGen.initialize(512);
      java.security.KeyPair keyPair = keyGen.genKeyPair();
      privateKey = keyPair.getPrivate();
      publicKey = keyPair.getPublic();

      X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());

      pubKeyStr = Base64.byteArrayToBase64(x509EncodedKeySpec.getEncoded());
    }
Beispiel #5
0
  public static void SaveKeyPair(String path, KeyPair keyPair) throws IOException {
    PrivateKey privateKey = keyPair.getPrivate();
    PublicKey publicKey = keyPair.getPublic();

    // Store Public Key.
    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
    FileOutputStream fos = new FileOutputStream(path + "/public.key");
    fos.write(x509EncodedKeySpec.getEncoded());
    fos.close();

    // Store Private Key.
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded());
    fos = new FileOutputStream(path + "/private.key");
    fos.write(pkcs8EncodedKeySpec.getEncoded());
    fos.close();
  }
  private void storeLocalPublicKey(String fullUserId, PublicKey pubKey) {

    String userId = Address.stripResource(fullUserId);

    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pubKey.getEncoded());

    this.store.setProperty(userId + ".publicKey", x509EncodedKeySpec.getEncoded());

    // Stash fingerprint for consistency.
    try {
      String fingerprintString = new OtrCryptoEngineImpl().getFingerprint(pubKey);
      this.store.setPropertyHex(userId + ".fingerprint", Hex.decode(fingerprintString));
    } catch (OtrCryptoException e) {
      e.printStackTrace();
    }

    store.save();
  }
  /**
   * Generate a local key pair. Be careful. If there is already an key pair, it will override it
   *
   * @param sessionID the sessionID that is identified with the local machine
   */
  public void generateLocalKeyPair(SessionID sessionID) {
    if (sessionID == null) return;

    String accountID = sessionID.getAccountID();
    KeyPair keyPair;
    try {
      keyPair = KeyPairGenerator.getInstance("DSA").genKeyPair();
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
      return;
    }

    // Store Public Key.
    PublicKey pubKey = keyPair.getPublic();
    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(pubKey.getEncoded());

    this.store.setProperty(accountID + ".publicKey", x509EncodedKeySpec.getEncoded());

    // Store Private Key.
    PrivateKey privKey = keyPair.getPrivate();
    PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privKey.getEncoded());

    this.store.setProperty(accountID + ".privateKey", pkcs8EncodedKeySpec.getEncoded());
  }
Beispiel #8
0
 /**
  * Generates X509 encoded public key bytes from a given public key.
  *
  * @param publicKey the public key
  * @return generated public key bytes
  * @throws Exception if any errors occur
  */
 public static byte[] generateX509PublicKey(PublicKey publicKey) throws Exception {
   X509EncodedKeySpec x509EncodedPublicKey =
       KEY_FACTORY.getKeySpec(publicKey, X509EncodedKeySpec.class);
   return x509EncodedPublicKey.getEncoded();
 }