Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8ae41c7e144401d31d706895432d2a33e094a948 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*******************************************************************************
 * Copyright (c) 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Julien HENRY - Linux implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.security.linux;

import java.security.SecureRandom;
import javax.crypto.spec.PBEKeySpec;
import org.eclipse.equinox.internal.security.auth.AuthPlugin;
import org.eclipse.equinox.internal.security.linux.nls.LinuxPasswordProviderMessages;
import org.eclipse.equinox.internal.security.storage.Base64;
import org.eclipse.equinox.security.storage.provider.IPreferencesContainer;
import org.eclipse.equinox.security.storage.provider.PasswordProvider;

public class LinuxPasswordProvider extends PasswordProvider {

  /**
   * The length of the randomly generated password in bytes
   */
  private static final int PASSWORD_LENGTH = 64;

  private native String getMasterPassword() throws SecurityException;

  private native void saveMasterPassword(String password) throws SecurityException;

  static {
    System.loadLibrary("keystorelinuxnative"); //$NON-NLS-1$
  }

  @Override
  public PBEKeySpec getPassword(IPreferencesContainer container, int passwordType) {

    boolean newPassword = (passwordType & CREATE_NEW_PASSWORD) != 0;
    boolean passwordChange = (passwordType & PASSWORD_CHANGE) != 0;

    if (!newPassword && !passwordChange) {
      try {
        return new PBEKeySpec(getMasterPassword().toCharArray());
      } catch (SecurityException e) {
        AuthPlugin.getDefault().logError(LinuxPasswordProviderMessages.getMasterPasswordError, e);
        return null;
      }
    }

    byte[] rawPassword = new byte[PASSWORD_LENGTH];
    SecureRandom random = new SecureRandom();
    random.setSeed(System.currentTimeMillis());
    random.nextBytes(rawPassword);
    String newPasswordString = Base64.encode(rawPassword);

    // add info message in the log
    AuthPlugin.getDefault().logMessage(LinuxPasswordProviderMessages.newMasterPasswordGenerated);

    try {
      saveMasterPassword(newPasswordString);
      return new PBEKeySpec(newPasswordString.toCharArray());
    } catch (SecurityException e) {
      AuthPlugin.getDefault().logError(LinuxPasswordProviderMessages.saveMasterPasswordError, e);
      return null;
    }
  }

}

Back to the top