1 10 11 package org.mule.extras.pgp; 12 13 import cryptix.message.Message; 14 import cryptix.message.MessageException; 15 import cryptix.message.SignedMessage; 16 import cryptix.pki.KeyBundle; 17 18 import org.mule.config.i18n.Messages; 19 import org.mule.umo.lifecycle.InitialisationException; 20 import org.mule.umo.security.SecurityException; 21 import org.mule.umo.security.UMOAuthentication; 22 import org.mule.umo.security.UMOSecurityContext; 23 import org.mule.umo.security.UMOSecurityContextFactory; 24 import org.mule.umo.security.UMOSecurityProvider; 25 import org.mule.umo.security.UnauthorisedException; 26 import org.mule.umo.security.UnknownAuthenticationTypeException; 27 28 31 public class PGPSecurityProvider implements UMOSecurityProvider 32 { 33 private String name = "PGPSecurityProvider"; 34 35 private PGPKeyRing keyManager; 36 37 private UMOSecurityContextFactory factory; 38 39 44 public void setName(String name) 45 { 46 this.name = name; 47 } 48 49 54 public String getName() 55 { 56 return name; 57 } 58 59 64 public UMOAuthentication authenticate(UMOAuthentication authentication) throws SecurityException 65 { 66 PGPAuthentication auth = (PGPAuthentication)authentication; 67 68 String userId = (String )auth.getPrincipal(); 69 70 if (userId == null) 71 { 72 throw new UnauthorisedException(new org.mule.config.i18n.Message(Messages.X_IS_NULL, "UserId")); 73 } 74 75 KeyBundle userKeyBundle = keyManager.getKeyBundle(userId); 76 77 if (userKeyBundle == null) 78 { 79 throw new UnauthorisedException(new org.mule.config.i18n.Message("pgp", 1, userId)); 80 } 81 82 Message msg = (Message)auth.getCredentials(); 83 84 if (!((msg != null) && msg instanceof SignedMessage)) 85 { 86 throw new UnauthorisedException(new org.mule.config.i18n.Message("pgp", 2)); 87 } 88 89 try 90 { 91 if (!((SignedMessage)msg).verify(userKeyBundle)) 92 { 93 throw new UnauthorisedException(new org.mule.config.i18n.Message("pgp", 3)); 94 } 95 } 96 catch (MessageException e) 97 { 98 throw new UnauthorisedException(new org.mule.config.i18n.Message("pgp", 4), e); 99 } 100 101 auth.setAuthenticated(true); 102 auth.setDetails(userKeyBundle); 103 104 return auth; 105 } 106 107 112 public boolean supports(Class aClass) 113 { 114 return PGPAuthentication.class.isAssignableFrom(aClass); 115 } 116 117 122 public UMOSecurityContext createSecurityContext(UMOAuthentication auth) 123 throws UnknownAuthenticationTypeException 124 { 125 return factory.create(auth); 126 } 127 128 133 public void initialise() throws InitialisationException 134 { 135 try 136 { 137 java.security.Security.addProvider(new cryptix.jce.provider.CryptixCrypto()); 138 java.security.Security.addProvider(new cryptix.openpgp.provider.CryptixOpenPGP()); 139 140 factory = new PGPSecurityContextFactory(); 141 } 142 catch (Exception e) 143 { 144 throw new InitialisationException(new org.mule.config.i18n.Message(Messages.FAILED_TO_CREATE_X, 145 "PGPProvider"), e); 146 } 147 } 148 149 public PGPKeyRing getKeyManager() 150 { 151 return keyManager; 152 } 153 154 public void setKeyManager(PGPKeyRing keyManager) 155 { 156 this.keyManager = keyManager; 157 } 158 } 159 | Popular Tags |