package org.openslx.imagemaster.db.models; import java.math.BigInteger; import java.security.NoSuchAlgorithmException; import java.security.PublicKey; import java.security.spec.InvalidKeySpecException; import org.apache.log4j.Logger; import org.openslx.encryption.AsymKeyHolder; public class LocalOrganization { private static final Logger LOGGER = Logger.getLogger( LocalOrganization.class ); private PublicKey publickey; private String publickeyString; private String name; /** * Get the public key of this organization, if known and valid. * * @return Public key, null on error or not known */ public PublicKey getPubkey() { if ( publickey == null && publickeyString != null ) { String parts[] = publickeyString.split( " " ); BigInteger mod = null; BigInteger exp = null; for ( int i = 0; i < parts.length; ++i ) { if ( parts[i].startsWith( "mod:" ) ) { // modulus found. mod = new BigInteger( parts[i].substring( 4 ) ); } if ( parts[i].startsWith( "exp:" ) ) { // exponent found. exp = new BigInteger( parts[i].substring( 4 ) ); } } if ( mod == null ) { LOGGER.error( "No modulus for building public key was found." ); return null; } if ( exp == null ) { LOGGER.error( "No public exponent for building public key was found." ); return null; } try { publickey = new AsymKeyHolder( null, exp, mod ).getPublicKey(); } catch ( InvalidKeySpecException | NoSuchAlgorithmException e ) { LOGGER.info( "PubKey of " + this.name + " is not valid.", e ); } catch ( NumberFormatException e ) { LOGGER.info( "PubKey of " + this.name + " is corrupted in database!", e ); } } return publickey; } }