spki
Class PublicKey

java.lang.Object
  |
  +--spki.BaseObj
        |
        +--spki.PublicKey

public class PublicKey
extends BaseObj
implements Key, Principal, SeqDefValue

This class represents a public signature key object. It provides everything a user needs to employ the key for checking signatures.

A collision-free hash of the SList canonical representation of an instance of this object yields a SPKI global identifier (and so does the SList itself, but a hash is much smaller). This can be done by calling the getKeyHash() method. An alternative is to use just the Base64-encoded hash value returned by the getKeyId() method.

Formal definition:

 <alg-id>:: <byte-string> | <uri> ;
 <pub-sig-alg-id>:: <alg-id> ;
 <pub-key>:: "(" "public-key" "(" <pub-sig-alg-id> <s-expr>+ ")" ")" ;
 

The RSA-keys S-Expression must contain the following s-expressions tokens: n and e, of the form (<token> <keydata>).

It also interfaces with the [I]JCA ([International] Java Cryptography Architecture) where possible, which means that we can use crypto-packages such as Systemic's Cryptix, which we do. This has some consequences for the signatures generated.

NOTE: For now, only String is supported for the algorithm-id. NOTE: only rsa-pkcs1-sha1 is supported for now.

TODO: support using URI as the algorithm-id?

Version:
$Id: PublicKey.java,v 1.20 2002/06/25 12:12:28 tep Exp $
Author:
Tep Narula <tep@win.trlabs.ca>, adapted from SPKI.SPKIPublicKey by Per Harald Myrvang <perm@pasta.cs.uit.no>
See Also:
Signature

Field Summary
private static String __ME
          This class's full name.
private  String cachedKeyId
          Cached key-id.
private  PublicKey jca_key
          The key, used by JCA.
private  Hash keyhash
          Hash of the public key.
static String SEXP_NAME
          This class S-Expression type name, used by toSExpression() and other methods.
private  Signature sig
          Signature object used for verification.
private  String sig_alg_id
          Signature algorithm-id.
private  String signature_provider
          Signature Provider ID.
 
Fields inherited from class spki.BaseObj
__ME, cache_sexp, cached_sexp, DEFAULT_HASH_ALG, locked, SEXP_NAME
 
Constructor Summary
private PublicKey()
          Don't use me!
  PublicKey(PublicKey pk)
          Create an immutable PublicKey from a java.security.PublicKey object.
  PublicKey(SExpression sexp)
          Create an immutable PublicKey object from an S-Expression.
 
Method Summary
 boolean equals(Principal that)
          Compare this object with another principal.
 String getAlgId()
          Get the SPKI encryption/signature algorithm id associated with the key.
 Key getJCAKey()
          Get a key object usable by [I]JCA packages.
 Hash getKeyHash()
          Get a Hash representing this key using the default hash algorithm.
 Hash getKeyHash(String hashAlg)
          Get a Hash representing this key using the specified hashing algorithm.
 String getKeyId()
          Get the key-id of this key.
 Principal toHashPrincipal()
          Convert this principal into a hash representation.
 SExpression toSExpression()
          Build an SExpression representing this object.
 boolean verify(byte[] input, byte[] sig_params)
          Verify a given input against a given signature using this public key.
 boolean verify(Hash hash, byte[] sig_params)
          Verify a given Hash object against a given signature value with this public key.
 boolean verify(Signature sig)
          Verify a given Signature using this public key.
 
Methods inherited from class spki.BaseObj
disableSExpressionCaching, enableSExpressionCaching, extractHashAlgId, getCachedSExpression, isLocked, isSListOfType, lock, providerToSPKI, setCachedSExpression, spkiToProvider, toString
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

__ME

private static final String __ME
This class's full name.

SEXP_NAME

public static final String SEXP_NAME
This class S-Expression type name, used by toSExpression() and other methods.

jca_key

private PublicKey jca_key
The key, used by JCA.

sig_alg_id

private String sig_alg_id
Signature algorithm-id.

signature_provider

private String signature_provider
Signature Provider ID.

sig

private Signature sig
Signature object used for verification.

keyhash

private Hash keyhash
Hash of the public key.

cachedKeyId

private String cachedKeyId
Cached key-id.
See Also:
getKeyId()
Constructor Detail

PublicKey

private PublicKey()
Don't use me!
See Also:
PublicKey(SExpression), PublicKey(java.security.PublicKey)

PublicKey

public PublicKey(SExpression sexp)
          throws SPKIException,
                 ParseException
Create an immutable PublicKey object from an S-Expression.
See Also:
BaseObj.BaseObj(SExpression)

PublicKey

public PublicKey(PublicKey pk)
          throws SPKIException
Create an immutable PublicKey from a java.security.PublicKey object. Note that the public signature algorithm id (<pub-sig-alg-id>) defaults in the case of RSA to rsa-pkcs1-sha1.
Parameters:
pk - Object implementing the java.security.PublicKey interface.
Throws:
SPKIException - if something went wrong.
Method Detail

toSExpression

public SExpression toSExpression()
                          throws SPKIException
Description copied from interface: SPKIObject
Build an SExpression representing this object.
Overrides:
toSExpression in class BaseObj
Tags copied from interface: SPKIObject
Returns:
SExpression object representing this object, or null.
Throws:
SPKIException - if something went wrong during conversion.

getAlgId

public String getAlgId()
Description copied from interface: Key
Get the SPKI encryption/signature algorithm id associated with the key.
Specified by:
getAlgId in interface Key
Tags copied from interface: Key
Returns:
String containing a SPKI algorithm-id.

getJCAKey

public Key getJCAKey()
Description copied from interface: Key
Get a key object usable by [I]JCA packages.
Specified by:
getJCAKey in interface Key
Tags copied from interface: Key
Returns:
java.security.Key object implementing, or null if this is a private key.

getKeyHash

public Hash getKeyHash()
                throws SPKIException
Description copied from interface: Key
Get a Hash representing this key using the default hash algorithm.
Specified by:
getKeyHash in interface Key
Tags copied from interface: Key
Returns:
Hash object representing this key.
Throws:
SPKIException - if something went wrong

getKeyHash

public Hash getKeyHash(String hashAlg)
                throws SPKIException
Get a Hash representing this key using the specified hashing algorithm. The Hash object is cached at first call, but if a subsequent call specifies a different hashing algorithm, then it caches and returns a new Hash object created with the specified algorithm.
Specified by:
getKeyHash in interface Key
Parameters:
hashAlg - hashing algorithm-id (currently "sha1" or "md5").
See Also:
Key.getKeyHash(String)

getKeyId

public String getKeyId()
                throws SPKIException
Description copied from interface: Key
Get the key-id of this key. Key-id is a Base64-encoded hash value, using the default hash algorithm.
Specified by:
getKeyId in interface Key
Tags copied from interface: Key
Returns:
String containing the key-id, or null.
Throws:
SPKIException - if something went wrong

equals

public boolean equals(Principal that)
Description copied from interface: Principal
Compare this object with another principal.
Specified by:
equals in interface Principal
Tags copied from interface: Principal
Parameters:
that - a principal to compare with.
Returns:
true if the two principals are equal, false otherwise.

toHashPrincipal

public Principal toHashPrincipal()
                          throws SPKIException
Description copied from interface: Principal
Convert this principal into a hash representation.
Specified by:
toHashPrincipal in interface Principal
Tags copied from interface: Principal
Returns:
the hash representation of this principal.
Throws:
SPKIException - if something went wrong during conversion.

verify

public boolean verify(Hash hash,
                      byte[] sig_params)
               throws SPKIException
Verify a given Hash object against a given signature value with this public key.
Parameters:
hash - the Hash object to be verified.
sig_params - the signature value to verify against the hash.
Returns:
true if the signature verified, false if not.
Throws:
SPKIException - if something went wrong.

verify

public boolean verify(byte[] input,
                      byte[] sig_params)
               throws SPKIException
Verify a given input against a given signature using this public key. It simply hashes the input byte-array and calls the verify(Hash,byte[]) method.
Parameters:
input - the data byte-array to be verified.
sig_params - the signature value to verify against the input.
Returns:
true if the signature verified, false if not.
Throws:
SPKIException - if something went wrong.
See Also:
verify(Hash,byte[])

verify

public boolean verify(Signature sig)
               throws SPKIException
Verify a given Signature using this public key. It's simply a front-end to the verify(Hash,byte[]) method.
Parameters:
sig - the Signature object to be verified.
Returns:
true if the Signature verified, false if not.
Throws:
SPKIException - if something went wrong.
See Also:
verify(Hash,byte[])