~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/juti/java/com/sun/grid/security/login/GECAKeyManager.java

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 *
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 *
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 *
 
9
 *
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 *
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 *
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.GECAKeyManager.java
 
25
 *
 
26
 *   Copyright: 2006 by Sun Microsystems, Inc
 
27
 *
 
28
 *   All Rights Reserved.
 
29
 *
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
package com.sun.grid.security.login;
 
33
 
 
34
import java.io.File;
 
35
import java.io.FileInputStream;
 
36
import java.io.IOException;
 
37
import java.net.Socket;
 
38
import java.security.KeyStore;
 
39
import java.security.Principal;
 
40
import java.security.PrivateKey;
 
41
import java.security.cert.X509Certificate;
 
42
import java.util.logging.Level;
 
43
import java.util.logging.Logger;
 
44
import javax.net.ssl.KeyManagerFactory;
 
45
import javax.net.ssl.X509KeyManager;
 
46
 
 
47
/**
 
48
 * The GECAKeyManager handles the specific KeyManager properties
 
49
 * of a Grid Engine CSP system
 
50
 */
 
51
public class GECAKeyManager implements X509KeyManager {
 
52
 
 
53
    private final static Logger log = Logger.getLogger(GECAKeyManager.class.getName());
 
54
    private X509KeyManager keyManager;
 
55
 
 
56
    public GECAKeyManager() {
 
57
    }
 
58
            
 
59
    /**
 
60
     *  Creates a new instance of GECAKeyManager.
 
61
     *
 
62
     * @param serverKeystore keystore file of the daemon 
 
63
     * @param pw keystore password
 
64
     */
 
65
    public GECAKeyManager(File serverKeystore, char[] pw) throws SecurityException {
 
66
        setKeystore(serverKeystore, pw);
 
67
    }
 
68
    
 
69
    public synchronized void setKeystore(KeyStore serverKeystore, char[] pw) throws SecurityException {
 
70
        try {
 
71
            KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
 
72
            kmf.init(serverKeystore, pw);
 
73
            keyManager = (X509KeyManager)kmf.getKeyManagers()[0];
 
74
        } catch (Exception ex) {
 
75
            throw new SecurityException("Cannnot create keymanager", ex);
 
76
        }        
 
77
    }
 
78
    
 
79
    public synchronized void setKeystore(File serverKeystore, char[] pw) throws SecurityException {
 
80
        FileInputStream fi = null;
 
81
        try {
 
82
            log.log(Level.FINER, "loading keystore file {0}", serverKeystore);
 
83
            KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
 
84
            fi = new FileInputStream(serverKeystore);
 
85
            ks.load(fi, pw);
 
86
            setKeystore(ks, pw);
 
87
        } catch (Exception ex) {
 
88
            throw new SecurityException("Cannnot create keymanager", ex);
 
89
        } finally {
 
90
            try {
 
91
                fi.close();
 
92
            } catch (IOException ex) {
 
93
            }
 
94
        }
 
95
    }
 
96
    
 
97
    public synchronized void reset() {
 
98
        keyManager = null;
 
99
    }
 
100
            
 
101
 
 
102
    public synchronized String[] getClientAliases(String arg0, Principal[] arg1) {
 
103
        if(keyManager == null) {
 
104
            return new String[0];
 
105
        }
 
106
        return keyManager.getClientAliases(arg0, arg1);
 
107
    }
 
108
 
 
109
    public synchronized String chooseClientAlias(String[] keyType, Principal[] arg1, Socket arg2) {
 
110
        if(keyManager == null) {
 
111
            return null;
 
112
        }
 
113
        return keyManager.chooseClientAlias(keyType, arg1, arg2);
 
114
    }
 
115
 
 
116
    public synchronized String[] getServerAliases(String arg0, Principal[] arg1) {
 
117
        if(keyManager == null) {
 
118
            return new String[0];
 
119
        }
 
120
        return keyManager.getServerAliases(arg0, arg1);
 
121
    }
 
122
 
 
123
    public synchronized String chooseServerAlias(String arg0, Principal[] arg1, Socket arg2) {
 
124
        if(keyManager == null) {
 
125
            return null;
 
126
        }
 
127
        return keyManager.chooseServerAlias(arg0, arg1, arg2);
 
128
    }
 
129
 
 
130
    public synchronized X509Certificate[] getCertificateChain(String arg0) {
 
131
        if(keyManager == null) {
 
132
            return new X509Certificate[0];
 
133
        }
 
134
        return keyManager.getCertificateChain(arg0);
 
135
    }
 
136
 
 
137
    public synchronized PrivateKey getPrivateKey(String arg0) {
 
138
        if(keyManager == null) {
 
139
            return null;
 
140
        }
 
141
        return keyManager.getPrivateKey(arg0);
 
142
    }
 
143
}