aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/theb/ssh/Pubkey.java
blob: cf3bd0da37dc7b8a91173e7d2ec22f09d8b6e449 (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
/*
 * Copyright (C) 2007 Kenny Root (kenny at the-b.org)
 * 
 * This file is part of Connectbot.
 *
 *  Connectbot is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Connectbot is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Connectbot.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.theb.ssh;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.util.concurrent.Semaphore;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Pubkey extends Activity {
	private static SecureRandom mSecRand = null;
	//private static KeyStore mKeyStore = null;
	private Thread kgThread = null;
	
	private static final int GATHER_ENTROPY = 0;
	
	protected Semaphore entropyGathered;
	protected String entropySeed;
	
	protected Button generateButton;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);	
        
        setContentView(R.layout.pubkey);
        
        generateButton = (Button) findViewById(R.id.generate);
        generateButton.setOnClickListener(mGenerateListener);
        
        Button okButton = (Button) findViewById(R.id.ok);
        okButton.setOnClickListener(mCommitListener);
        
        Button cancelButton = (Button) findViewById(R.id.cancel);
        cancelButton.setOnClickListener(mCancelListener);
    }
    
    final Runnable mKeyGen = new Runnable() {    	
		public void run() {
			// TODO Auto-generated method stub
    		Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    		try {
     			mSecRand = SecureRandom.getInstance("SHA1PRNG");
    			
    			entropyGathered = new Semaphore(0);
    			gatherEntropy();
    			entropyGathered.acquire();
    			mSecRand.setSeed(entropySeed.getBytes());
    			entropyGathered = null;
    			
    			KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA");
    			keyGen.initialize(512, mSecRand);
    			
    			KeyPair pair = keyGen.generateKeyPair();
    			PrivateKey priv = pair.getPrivate();
    			PublicKey pub = pair.getPublic();
    			
    			byte[] encPriv = priv.getEncoded();
    			byte[] encPub = pub.getEncoded();
    			Log.e("SSH/priv", new String(encPriv));
    			Log.d("SSH/pub", new String(encPub));
    		} catch (Exception ex) {
    			Log.e("SSH/keygen", ex.getMessage());
    		}			
		}
    };
    
    OnClickListener mGenerateListener = new OnClickListener() {
    	public void onClick(View v) {
    		kgThread = new Thread(mKeyGen);
    		kgThread.start();
    	}
    };
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            String data, Bundle extras)
	{
	    if (requestCode == GATHER_ENTROPY) {
	    	entropySeed = data;
	    	entropyGathered.release();
	    }
	}
    
    protected void gatherEntropy() {
    	generateButton.setEnabled(false);
		Intent intent = new Intent(this, TouchEntropy.class);
		startSubActivity(intent, GATHER_ENTROPY);
	}

	OnClickListener mCommitListener = new OnClickListener() {
    	public void onClick(View v) {
    		// When the user clicks, just finish this activity.
    		// onPause will be called, and we save our data there.
    		finish();
    	}
    };
    
    OnClickListener mCancelListener = new OnClickListener() {
    	public void onClick(View v) {
    		//cancelEdit();
    		finish();
    	}
    };
}