summaryrefslogtreecommitdiff
path: root/src/plugins/certmanager/src/com/motorolamobility/studio/android/certmanager/core/BackwardKeystoreManager.java
blob: 319e46c23146ada85ce993e2ebf8269d6a3fbf33 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.motorolamobility.studio.android.certmanager.core;

import java.io.File;
import java.util.List;

import org.eclipse.equinox.security.storage.ISecurePreferences;
import org.eclipse.equinox.security.storage.SecurePreferencesFactory;
import org.eclipse.equinox.security.storage.StorageException;

import com.motorola.studio.android.common.log.StudioLogger;
import com.motorola.studio.android.common.utilities.EclipseUtils;
import com.motorolamobility.studio.android.certmanager.i18n.CertificateManagerNLS;
import com.motorolamobility.studio.android.certmanager.ui.model.IKeyStoreEntry;
import com.motorolamobility.studio.android.certmanager.ui.model.KeyStoreNode;
import com.motorolamobility.studio.android.certmanager.ui.model.SigningAndKeysModelManager;

/**
 * Manages old MOTODEV keystore available at <user_home>\motodevstudio\tools\motodev.keystore  
 * (to keep backward compatibility)
 */
public class BackwardKeystoreManager
{
    /**
     * MOTODEV Studio directory.
     */
    private static final String MOTODEV_TOOLS_PATH = File.separator
            + "motodevstudio" + File.separator + "tools"; //$NON-NLS-1$ //$NON-NLS-2$

    /**
     * Path to user home directory.
     */
    private static final String USER_HOME_PATH = System.getProperty("user.home"); //$NON-NLS-1$

    /**
     * Full path where the files will be.
     */
    private static final String TOOLS_FULL_PATH = USER_HOME_PATH + MOTODEV_TOOLS_PATH;

    private static final String KS_FILENAME_NEW = TOOLS_FULL_PATH + File.separatorChar
            + "motodev.keystore"; //$NON-NLS-1$

    /**
     * The constant contains the keysotre type.
     */
    private static final String KS_TYPE = "JCEKS"; //$NON-NLS-1$

    /**
     * The constant contains the keystore password.
     */
    private static final String KS_DEFAULT_PASSWD = "passwd"; //$NON-NLS-1$

    /**
     * The constant contains the keystore password node on secure storage.
     */
    private static final String KS_PASSWD_NODE = "com.motorola.studio.platform.tools.sign.core"; //$NON-NLS-1$

    /**
     * The constant contains the keystore password attribute on secure storage.
     */
    private static final String KS_PASSWD_ATTRIBUTE = "kspasswd"; //$NON-NLS-1$

    /*
     * Removes old keystore password from Eclipse secure preferences (old format)
     * @return the stored password (before removal), 
     * {@link SigningCoreConstants#KS_DEFAULT_PASSWD} if found node {@link SigningCoreConstants#KS_PASSWD_NODE} but {@link SigningCoreConstants#KS_PASSWD_ATTRIBUTE} does not exist, 
     * null if node {@link SigningCoreConstants#KS_PASSWD_NODE} not found  
     */
    private String removeOldKeystorePassword()
    {
        String oldPassword = KS_DEFAULT_PASSWD;
        ISecurePreferences preferences = SecurePreferencesFactory.getDefault();
        ISecurePreferences node = null;
        try
        {
            if (preferences.nodeExists(KS_PASSWD_NODE))
            {
                //old format node exists = try to get from attribute
                node = preferences.node(KS_PASSWD_NODE);
                try
                {
                    oldPassword = node.get(KS_PASSWD_ATTRIBUTE, KS_DEFAULT_PASSWD);
                }
                catch (StorageException e)
                {
                    oldPassword = KS_DEFAULT_PASSWD;
                }
                node.remove(KS_PASSWD_ATTRIBUTE);
                preferences.remove(KS_PASSWD_NODE);
                node.flush();
            }
            else
            {
                //node already removed => password is in the new format
                oldPassword = null;
            }
        }
        catch (Exception e)
        {
            StudioLogger.error(BackwardKeystoreManager.class, e.getMessage(), e);
        }
        return oldPassword;
    }

    /**
     * Maps old keystore if the <user_home>\motodevstudio\tools\motodev.keystore file exists.
     */
    public void mapOldKeystore()
    {
        File motodevKeystoreFile = new File(KS_FILENAME_NEW);
        if (motodevKeystoreFile.exists())
        {
            //we found backward (old default MOTODEV keystore) => import it
            KeyStoreNode keyStoreNode = new KeyStoreNode(motodevKeystoreFile, KS_TYPE);

            try
            {
                SigningAndKeysModelManager.getInstance().mapKeyStore(keyStoreNode);

                //remove old password from keystore
                String oldPassword = removeOldKeystorePassword();

                if (oldPassword != null)
                {
                    //oldPassword was in old format => add password for keystore in the new format  
                    PasswordProvider passwordProvider =
                            new PasswordProvider(keyStoreNode.getFile());
                    passwordProvider.saveKeyStorePassword(oldPassword);

                    //for each child key (save the same password as the keystore to maintain backward compatibility)
                    List<IKeyStoreEntry> iKeyStoreEntries = keyStoreNode.getEntries(oldPassword);
                    if (iKeyStoreEntries != null)
                    {
                        for (IKeyStoreEntry keyStoreEntry : iKeyStoreEntries)
                        {
                            passwordProvider.savePassword(keyStoreEntry.getAlias(), oldPassword);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                //error
                EclipseUtils.showErrorDialog(CertificateManagerNLS.bind(
                        CertificateManagerNLS.KeystoreManagerView_ErrorImportingBackwardKeystore,
                        motodevKeystoreFile), e.getMessage());
            }
        }
    }
}