View Javadoc
1   /**
2    * Waffle (https://github.com/dblock/waffle)
3    *
4    * Copyright (c) 2010 - 2015 Application Security, Inc.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of the Eclipse Public License v1.0
8    * which accompanies this distribution, and is available at
9    * http://www.eclipse.org/legal/epl-v10.html
10   *
11   * Contributors:
12   *     Application Security, Inc.
13   */
14  package waffle.windows.auth.impl;
15  
16  import waffle.windows.auth.IWindowsCredentialsHandle;
17  
18  import com.sun.jna.platform.win32.Secur32;
19  import com.sun.jna.platform.win32.Sspi;
20  import com.sun.jna.platform.win32.Sspi.CredHandle;
21  import com.sun.jna.platform.win32.Sspi.TimeStamp;
22  import com.sun.jna.platform.win32.Win32Exception;
23  import com.sun.jna.platform.win32.WinError;
24  
25  /**
26   * Pre-existing credentials of a security principal. This is a handle to a previously authenticated logon data used by a
27   * security principal to establish its own identity, such as a password, or a Kerberos protocol ticket.
28   * 
29   * @author dblock[at]dblock[dot]org
30   */
31  public class WindowsCredentialsHandleImpl implements IWindowsCredentialsHandle {
32  
33      /** The principal name. */
34      private final String     principalName;
35      
36      /** The credentials type. */
37      private final int        credentialsType;
38      
39      /** The security package. */
40      private final String     securityPackage;
41      
42      /** The handle. */
43      private CredHandle handle;
44      
45      /** The client lifetime. */
46      private TimeStamp  clientLifetime;
47  
48      /**
49       * A new Windows credentials handle.
50       * 
51       * @param newPrincipalName
52       *            Principal name.
53       * @param newCredentialsType
54       *            Credentials type.
55       * @param newSecurityPackage
56       *            Security package.
57       */
58      public WindowsCredentialsHandleImpl(final String newPrincipalName, final int newCredentialsType,
59              final String newSecurityPackage) {
60          this.principalName = newPrincipalName;
61          this.credentialsType = newCredentialsType;
62          this.securityPackage = newSecurityPackage;
63      }
64  
65      /**
66       * Returns the current credentials handle.
67       * 
68       * @param securityPackage
69       *            Security package, eg. "Negotiate".
70       * @return A windows credentials handle
71       */
72      public static IWindowsCredentialsHandle getCurrent(final String securityPackage) {
73          final IWindowsCredentialsHandle handle = new WindowsCredentialsHandleImpl(null, Sspi.SECPKG_CRED_OUTBOUND,
74                  securityPackage);
75          handle.initialize();
76          return handle;
77      }
78  
79      /**
80       * Initialize a new credentials handle.
81       */
82      @Override
83      public void initialize() {
84          this.handle = new CredHandle();
85          this.clientLifetime = new TimeStamp();
86          final int rc = Secur32.INSTANCE.AcquireCredentialsHandle(this.principalName, this.securityPackage,
87                  this.credentialsType, null, null, null, null, this.handle, this.clientLifetime);
88          if (WinError.SEC_E_OK != rc) {
89              throw new Win32Exception(rc);
90          }
91      }
92  
93      /**
94       * Dispose of the credentials handle.
95       */
96      @Override
97      public void dispose() {
98          if (this.handle != null && !this.handle.isNull()) {
99              final int rc = Secur32.INSTANCE.FreeCredentialsHandle(this.handle);
100             if (WinError.SEC_E_OK != rc) {
101                 throw new Win32Exception(rc);
102             }
103         }
104     }
105 
106     /**
107      * Get CredHandle.
108      *
109      * @return the handle
110      */
111     @Override
112     public CredHandle getHandle() {
113         return this.handle;
114     }
115 }