com.didisoft.pgp.net
Class LDAPClient

java.lang.Object
  extended by com.didisoft.pgp.net.LDAPClient

public class LDAPClient
extends java.lang.Object

Retrieves keys from LDAP servers that support the pgp ldap schema

Example usage:

 import java.io.ByteArrayInputStream;
 
 import com.didisoft.pgp.net.LDAPClient;
 import com.didisoft.pgp.KeyStore;
 
 public class DemoLDAP {
        public static void demo() throws Exception {
           // connect to LDAP server that supports pgp schema
           LDAPClient ldap = new LDAPClient("127.0.0.1");
     // search by part of the User Id
     ldap.setPartialMatchUserIds(true);
           byte[] keyBytes = ldap.getKeyByUserId("DidiSoft");
           // String keyInAscii = new String(keyBytes, "ASCII");

     if (keyBytes != null && keyBytes.length > 0) {
        KeyStore ks = new KeyStore(); // create an in-memory keystore
        ks.importKeyRing(new ByteArrayInputStream(keyBytes));
     }
        }
}
 


Field Summary
static int DEFAULT_LDAP_PORT
           
 
Constructor Summary
LDAPClient(java.lang.String host)
          Creates a new instance of the class
LDAPClient(java.lang.String host, int port)
          Creates a new instance of the class
LDAPClient(java.lang.String host, int port, java.lang.String username, java.lang.String password)
           
 
Method Summary
 byte[] getKeyByKeyId(long keyId)
          Tries to retrieve a key from an LDAP server searching by Key Id.
 byte[] getKeyByKeyIdHex(java.lang.String keyIdHex)
          Tries to retrieve a key from an LDAP server searching by hexadecimal Key Id.
 byte[] getKeyByUserId(java.lang.String userId)
          Tries to retrieve a key from an LDAP server searching by User Id.
 boolean isPartialMatchUserIds()
          Returns should keys be search by part of the User ID (true) or with the whole User ID only (false)
static java.lang.String padLeft(int s, int n)
           
 void setPartialMatchUserIds(boolean partialMatch)
          Sets should keys be search by part of the User ID (true) or with the whole User ID only (false)
 boolean submitKey(byte[] keyBytes)
          Uploads an OpenPGP key bytes into an LDAP server that supports the pgp schema
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_LDAP_PORT

public static final int DEFAULT_LDAP_PORT
See Also:
Constant Field Values
Constructor Detail

LDAPClient

public LDAPClient(java.lang.String host)
Creates a new instance of the class

Parameters:
host - host or IP address of the LDAP server that supports PGP schema

LDAPClient

public LDAPClient(java.lang.String host,
                  int port)
Creates a new instance of the class

Parameters:
host - host or IP address of the LDAP server that supports PGP schema
port - port of the LDAP server

LDAPClient

public LDAPClient(java.lang.String host,
                  int port,
                  java.lang.String username,
                  java.lang.String password)
Parameters:
host - host or IP address of the LDAP server that supports PGP schema
port - port of the LDAP server
username - LDAP user account (for example: uid=John.Doe,ou=People,dc=example,dc=com
password - LDAP user account password
Method Detail

isPartialMatchUserIds

public boolean isPartialMatchUserIds()
Returns should keys be search by part of the User ID (true) or with the whole User ID only (false)

Returns:
should keys be search by part of the User ID (true) or with the whole User ID only (false)
See Also:
setPartialMatchUserIds(boolean)

setPartialMatchUserIds

public void setPartialMatchUserIds(boolean partialMatch)
Sets should keys be search by part of the User ID (true) or with the whole User ID only (false)

Parameters:
partialMatch - if true, User ID parameters can be specified with part of the User ID when false the whole User ID must be specified
See Also:
isPartialMatchUserIds()

getKeyByKeyIdHex

public byte[] getKeyByKeyIdHex(java.lang.String keyIdHex)
                        throws java.io.IOException
Tries to retrieve a key from an LDAP server searching by hexadecimal Key Id.

Parameters:
keyIdHex - key Id in hexadecimal format (like "B29931AC")
Returns:
key serialized as byte array, or empty array if there is no such key
Throws:
java.io.IOException - in case of a network connection problem Example usage:
           LDAPClient ldap = new LDAPClient("localhost");
           byte[] keyBytes = ldap.getKeyByKeyIdHex("b22a15dd");
     if (keyBytes != null || keyBytes.length > 0) {
        KeyStore ks = new KeyStore(); // create an in-memory keystore
        ks.importKeyRing(new ByteArrayInputStream(keyBytes));
     }
 

getKeyByUserId

public byte[] getKeyByUserId(java.lang.String userId)
                      throws java.io.IOException
Tries to retrieve a key from an LDAP server searching by User Id.

Parameters:
userId - Whole or part of one of the User Id's of the key
Returns:
key serialized as byte array, or empty array if there is no such key
Throws:
java.io.IOException - in case of a network connection problem
See Also:
Example usage:
	   LDAPClient ldap = new LDAPClient("localhost");
	   ldap.getKeyByUserId("WellMed");
     if (keyBytes != null || keyBytes.length > 0) {
     	KeyStore ks = new KeyStore(); // create an in-memory keystore
     	ks.importKeyRing(new ByteArrayInputStream(keyBytes));
     }
 

getKeyByKeyId

public byte[] getKeyByKeyId(long keyId)
                     throws java.io.IOException
Tries to retrieve a key from an LDAP server searching by Key Id.

Parameters:
keyId - key Id
Returns:
key serialized as byte array, or empty array if there is no such key
Throws:
java.io.IOException - in case of a network connection problem

submitKey

public boolean submitKey(byte[] keyBytes)
                  throws java.io.IOException
Uploads an OpenPGP key bytes into an LDAP server that supports the pgp schema

Parameters:
keyBytes - OpenPGP key serialized as array of bytes
Returns:
true on success, false on error
Throws:
java.io.IOException - in case of an error Example:
        LDAPClient ldap = new LDAPClient("127.0.0.1");

  FileInputStream fIn = new FileInputStream("c:\\Test\\new3\\silverpub.asc");
  ByteArrayOutputStream bOut = new ByteArrayOutputStream();
  int i = -1;
  while ((i = fIn.read()) != -1) {
    bOut.write(i);
 }
 boolean submitted = ldap.submitKey(bOut.toByteArray());
 

padLeft

public static java.lang.String padLeft(int s,
                                       int n)


Copyright © 2006-2017 DidiSoft Ltd. All Rights Reserved.