P2PWNC
Class P2PWNCCrypt

java.lang.Object
  extended byP2PWNC.P2PWNCProto
      extended byP2PWNC.P2PWNCCrypt

class P2PWNCCrypt
extends P2PWNCProto

This class provides cryptographic functionality, that is methods for performing encryption, decryption, digital signing and verification, etc. for the P2PWNC protocol. The standard SUNJCE provider is used.

Version:
0.0.1
Author:
Pantelis Frangoudis, Elias Efstathiou

Field Summary
static int P2PWNC_ALGO_ECC
          Elliptic Curve cryptosystem identifier.
static int P2PWNC_ALGO_RSA
          RSA cryptosystem identifier.
static int P2PWNC_MSG_CACK
          This message type corresponds to a P2PWNC "connection acknowledgement" message (CACK).
static int P2PWNC_MSG_CONN
          This message type corresponds to a P2PWNC "connect" message (CONN).
static int P2PWNC_MSG_RCPT
          This message type corresponds to a P2PWNC "receipt" message (RCPT).
static int P2PWNC_MSG_RREQ
          This message type corresponds to a P2PWNC "receipt request" message (RREQ).
static int P2PWNC_MSG_TEAR
          This message type corresponds to a P2PWNC "tear connection" message (TEAR).
static int P2PWNC_SERVER_PORT
          The standard port a service provider listens for client messages, as specified by the P2PWNC protocol).
 
Constructor Summary
(package private) P2PWNCCrypt()
          Default constructor (does nothing in special, since there are no fields to initialize)
 
Method Summary
 P2PWNCReceipt constructReceipt(P2PWNCCert cert, P2PWNCKey prov, int timestamp, int weight, P2PWNCKey key, boolean pub)
          Constructs and signs a P2PWNC receipt.
protected  byte[] cryptMessage(byte[] message, P2PWNCKey key, int cipherMode, boolean pub)
          Encrypts or decrypts a message using a specified P2PWNCKey ("key" parameter).
static void cryptTest(P2PWNCKey priv, P2PWNCKey pub, byte[] data)
           
protected  java.security.Key internalToKey(P2PWNCKey key, boolean pub)
          Constructs a Key object from the specified P2PWNCKey input parameter.
protected  java.security.spec.KeySpec internalToKeySpec(P2PWNCKey key, boolean pub)
          Converts from internal key representation to Java KeySpec.
static byte[] reverseByteOrder(byte[] arr)
          Reverses the order of the bytes in the input byte array.
protected  byte[] serializeReceiptInfo(P2PWNCCert cert, P2PWNCKey prov, int timestamp, int weight)
          Serializes the information contained in a P2PWNC receipt.
protected  P2PWNCSignature signMessage(P2PWNCKey key, byte[] message, boolean pub)
          Signs a message using the specified key.
protected  boolean verifySignature(P2PWNCSignature signature, P2PWNCKey key, byte[] message, boolean pub)
          Verifies a P2PWNCSignature.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

P2PWNC_SERVER_PORT

public static final int P2PWNC_SERVER_PORT
The standard port a service provider listens for client messages, as specified by the P2PWNC protocol).

See Also:
Constant Field Values

P2PWNC_ALGO_RSA

public static final int P2PWNC_ALGO_RSA
RSA cryptosystem identifier.

See Also:
Constant Field Values

P2PWNC_ALGO_ECC

public static final int P2PWNC_ALGO_ECC
Elliptic Curve cryptosystem identifier.

See Also:
Constant Field Values

P2PWNC_MSG_RCPT

public static final int P2PWNC_MSG_RCPT
This message type corresponds to a P2PWNC "receipt" message (RCPT).

See Also:
Constant Field Values

P2PWNC_MSG_CONN

public static final int P2PWNC_MSG_CONN
This message type corresponds to a P2PWNC "connect" message (CONN).

See Also:
Constant Field Values

P2PWNC_MSG_RREQ

public static final int P2PWNC_MSG_RREQ
This message type corresponds to a P2PWNC "receipt request" message (RREQ).

See Also:
Constant Field Values

P2PWNC_MSG_CACK

public static final int P2PWNC_MSG_CACK
This message type corresponds to a P2PWNC "connection acknowledgement" message (CACK).

See Also:
Constant Field Values

P2PWNC_MSG_TEAR

public static final int P2PWNC_MSG_TEAR
This message type corresponds to a P2PWNC "tear connection" message (TEAR).

See Also:
Constant Field Values
Constructor Detail

P2PWNCCrypt

P2PWNCCrypt()
Default constructor (does nothing in special, since there are no fields to initialize)

Method Detail

reverseByteOrder

public static byte[] reverseByteOrder(byte[] arr)
Reverses the order of the bytes in the input byte array.

Parameters:
arr - byte[] The byte array whose elements will be reversed.
Returns:
byte[] The reversed byte array.

internalToKeySpec

protected java.security.spec.KeySpec internalToKeySpec(P2PWNCKey key,
                                                       boolean pub)
Converts from internal key representation to Java KeySpec. This method constructs a KeySpec that corresponds to the input data. For examle, for an RSA public key, it will construct and return an RSAPublicKeySpec object whose public modulus will be the BigInteger representation of the key.data field (byte []) and whose public exponent will be 65537 (as specified by the P2PWNC protocol).

Parameters:
key - P2PWNCKey The P2PWNCKey from which a KeySpec will result.
pub - boolean True if key is public and false if it is a secret key.
Returns:
KeySpec The resulting KeySpoec object (can be an instance of the RSAPublicKeySpec, RSAPrivateKeySpec, ECPublicKeySpec or ECPrivateKeySpec classes).

internalToKey

protected java.security.Key internalToKey(P2PWNCKey key,
                                          boolean pub)
Constructs a Key object from the specified P2PWNCKey input parameter. This method calls internalToKeySpec to derive a KeySpec instance from the input P2PWNCKey object and then constructs a Key object using a KeyFactory.

Parameters:
key - P2PWNCKey The P2PWNCKey object from which a Key object will result.
pub - boolean True if key is public and false if it is secret.
Returns:
Key The resulting Key object.

cryptMessage

protected byte[] cryptMessage(byte[] message,
                              P2PWNCKey key,
                              int cipherMode,
                              boolean pub)
Encrypts or decrypts a message using a specified P2PWNCKey ("key" parameter).

Parameters:
message - byte[] The message to be encrypted/decrypted.
key - P2PWNCKey The P2PWNCKey key that will be used for the encryption/decryption operation. This key is first converted from P2PWNC internal representation to a JCE-specific object (for example RSAPubliKey) using the internalToKey method.
cipherMode - int Indicates whether the message is to be encrypted or decrypted. Legal values are Cipher.ENCRYPT_MODE and Cipher.DECRYPT_MODE.
pub - boolean True if key is public and false if it is secret.
Returns:
byte[] The encrypted/decrypted message.

signMessage

protected P2PWNCSignature signMessage(P2PWNCKey key,
                                      byte[] message,
                                      boolean pub)
Signs a message using the specified key. The operation works as follows; the message is first SHA-1 hashed and the the 20byte hash value is signed. The result is stored in a P2PWNCSignature object.

Parameters:
key - P2PWNCKey The key that will be used for signing.
message - byte[] The original byte array that is to be signed.
pub - boolean True if key is public.
Returns:
P2PWNCSignature The resulting P2PWNCSignature structure (contains the signed data).

verifySignature

protected boolean verifySignature(P2PWNCSignature signature,
                                  P2PWNCKey key,
                                  byte[] message,
                                  boolean pub)
Verifies a P2PWNCSignature. That is, extracts that P2PWNCSignature data, decrypts them, SHA-1 hashes the data the signature has resulted from and compares the hash values.

Parameters:
signature - P2PWNCSignature The P2PWNCSignature that is to be verified.
key - P2PWNCKey The key that will be used for signature verification.
message - byte[] The original data, from which the signature has resulted.
pub - boolean True if key is public.
Returns:
boolean True if verification succeeded, false otherwise.

serializeReceiptInfo

protected byte[] serializeReceiptInfo(P2PWNCCert cert,
                                      P2PWNCKey prov,
                                      int timestamp,
                                      int weight)
Serializes the information contained in a P2PWNC receipt. The result of this function is the byte array that will be signed. It contains the client certificate data (issuer team public key, user public key, team signature - these values are byte arrays and must be serialized strictly in the above order), the service provider public key, the session timestamp as an integer indicating the number of seconds that have elapsed since the Epoch and the receipt weight (an integer value of the traffic uploaded/downloaded from a client during a session, measured in bytes). The above integer values are serialized in big endian form.

Parameters:
cert - P2PWNCCert The client certificate that is included in the receipt.
prov - P2PWNCKey The service provider public key.
timestamp - int The session timestamp (number of seconds since the Epoch).
weight - int Session weight (traffic uploaded/downloaded by a client during the session, measured in bytes).
Returns:
byte[]

constructReceipt

public P2PWNCReceipt constructReceipt(P2PWNCCert cert,
                                      P2PWNCKey prov,
                                      int timestamp,
                                      int weight,
                                      P2PWNCKey key,
                                      boolean pub)
Constructs and signs a P2PWNC receipt. It creates a receipt using the input parameters. Then, it serializes these information (using serializeReceiptInfo), signs the resulting byte array and sets the receipt's receiptSignature field to the resulting object.

Parameters:
cert - P2PWNCCert Client certificate that will be included in the receipt.
prov - P2PWNCKey Service provider public key that will be included in the receipt.
timestamp - int The session timestamp (number of seconds since the Epoch).
weight - int Session weight (traffic uploaded/downloaded by a client during the session, measured in bytes).
key - P2PWNCKey The key that will be used to sign the receipt.
pub - boolean True if key is public.
Returns:
P2PWNCReceipt The resulting P2PWNCReceipt object. This can be passed to the receiptToTextMsg so as to create an RCPT text message.

cryptTest

public static void cryptTest(P2PWNCKey priv,
                             P2PWNCKey pub,
                             byte[] data)
                      throws java.security.NoSuchAlgorithmException,
                             java.security.InvalidKeyException,
                             java.security.SignatureException
Throws:
java.security.NoSuchAlgorithmException
java.security.InvalidKeyException
java.security.SignatureException