List of usage examples for java.security KeyFactory getInstance
public static KeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:com.vmware.demo.SamlService.java
public String validateSAMLResponse(String samlResponse, String samlCert) throws Exception { String decodedString = ""; try {//from ww w. j a va 2 s.com decodedString = decodeSAMLResponse(samlResponse); InputStream inputStream = new ByteArrayInputStream(decodedString.getBytes("UTF-8")); // Parse XML BasicParserPool parserPoolManager = new BasicParserPool(); parserPoolManager.setNamespaceAware(true); parserPoolManager.setIgnoreElementContentWhitespace(true); Document document = parserPoolManager.parse(inputStream); Element metadataRoot = document.getDocumentElement(); QName qName = new QName(metadataRoot.getNamespaceURI(), metadataRoot.getLocalName(), metadataRoot.getPrefix()); // Unmarshall document Unmarshaller unmarshaller = Configuration.getUnmarshallerFactory().getUnmarshaller(qName); Response response = (Response) unmarshaller.unmarshall(metadataRoot); Issuer issuer = response.getIssuer(); logger.info("Parsed response. Issued:" + response.getIssueInstant().toString() + ", issuer: " + issuer.getValue()); java.security.cert.X509Certificate jX509Cert = SamlUtils.parsePemCertificate(samlCert); if (null == jX509Cert) { logger.info("Failed to parse cert. " + samlCert); return ""; } PublicKey publicCert = jX509Cert.getPublicKey(); logger.info("Extracted cert. Cert:" + publicCert); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicCert.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); logger.debug("Key created by provider: " + keyFactory.getProvider().toString()); // Setup validation BasicX509Credential publicCredential = new BasicX509Credential(); publicCredential.setPublicKey(publicKey); SignatureValidator signatureValidator = new SignatureValidator(publicCredential); Signature signature = response.getSignature(); // Validate try { signatureValidator.validate(signature); logger.info("Assertion signature validated."); } catch (ValidationException e) { logger.error("Failed to validate signature of assertion", e); throw e; } // Get decryption key RSAPrivateKey privateKey = null; BasicX509Credential decryptionCredential = new BasicX509Credential(); decryptionCredential.setPrivateKey(privateKey); StaticKeyInfoCredentialResolver skicr = new StaticKeyInfoCredentialResolver(decryptionCredential); // Decrypt assertion Decrypter decrypter = new Decrypter(null, skicr, new InlineEncryptedKeyResolver()); if (response.getEncryptedAssertions().isEmpty()) { logger.info("Nothing to decrypt in assertion."); } else { Assertion decryptedAssertion; try { decryptedAssertion = decrypter.decrypt(response.getEncryptedAssertions().get(0)); logger.info("Assertion decryption succeeded."); } catch (DecryptionException e) { logger.error("Failed to decrypt assertion", e); throw e; } // Extract attributes, log in output List<AttributeStatement> attributeStatements = decryptedAssertion.getAttributeStatements(); for (int i = 0; i < attributeStatements.size(); i++) { List<Attribute> attributes = attributeStatements.get(i).getAttributes(); for (int x = 0; x < attributes.size(); x++) { String strAttributeName = attributes.get(x).getDOM().getAttribute("Name"); List<XMLObject> attributeValues = attributes.get(x).getAttributeValues(); for (int y = 0; y < attributeValues.size(); y++) { String strAttributeValue = attributeValues.get(y).getDOM().getTextContent(); logger.info(strAttributeName + " = " + strAttributeValue); } } } } } catch (Exception ex) { logger.error("Failed to validate assertion", ex); throw ex; } return decodedString; }
From source file:com.goodhustle.ouyaunitybridge.OuyaUnityActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); OuyaController.init(this); // Initialize ouyaFacade ouyaFacade = OuyaFacade.getInstance(); ouyaFacade.init(this, DEVELOPER_ID); userManager = UserManager.getInstance(this); playerStates = new ControllerState[OuyaController.MAX_CONTROLLERS]; for (int i = 0; i < OuyaController.MAX_CONTROLLERS; i++) { playerStates[i] = new ControllerState(); }/* w ww .j a v a 2 s.c o m*/ // Create the UnityPlayer mUnityPlayer = new UnityPlayer(this); int glesMode = mUnityPlayer.getSettings().getInt("gles_mode", 1); boolean trueColor8888 = false; mUnityPlayer.init(glesMode, trueColor8888); setContentView(R.layout.main); // Add the Unity view FrameLayout layout = (FrameLayout) findViewById(R.id.unityLayout); LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); layout.addView(mUnityPlayer.getView(), 0, lp); // Set the focus RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainLayout); mainLayout.setFocusableInTouchMode(true); // Attempt to restore the product and receipt list from the savedInstanceState Bundle if (savedInstanceState != null) { if (savedInstanceState.containsKey(PRODUCTS_INSTANCE_STATE_KEY)) { Parcelable[] products = savedInstanceState.getParcelableArray(PRODUCTS_INSTANCE_STATE_KEY); mProductList = new ArrayList<Product>(products.length); for (Parcelable product : products) { mProductList.add((Product) product); } addProducts(); } if (savedInstanceState.containsKey(RECEIPTS_INSTANCE_STATE_KEY)) { Parcelable[] receipts = savedInstanceState.getParcelableArray(RECEIPTS_INSTANCE_STATE_KEY); mReceiptList = new ArrayList<Receipt>(receipts.length); for (Parcelable receipt : receipts) { mReceiptList.add((Receipt) receipt); } addReceipts(); } } // Request the product list if it could not be restored from the savedInstanceState Bundle if (mProductList == null) { requestProducts(); } // Create a PublicKey object from the key data downloaded from the developer portal. try { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(APPLICATION_KEY); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); mPublicKey = keyFactory.generatePublic(keySpec); } catch (Exception e) { Log.e(LOG_TAG, "Unable to create encryption key", e); } }
From source file:net.sourceforge.msscodefactory.cfasterisk.v2_4.CFAsteriskXMsgRqstHandler.CFAsteriskXMsgRqstLogInHandler.java
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { final String S_ProcName = "startElement"; ICFAsteriskSchemaObj schemaObj = null; CFAsteriskXMsgSchemaMessageFormatter schemaFormatter = null; try {//from w ww. j av a 2s. co m // Common XML Attributes String attrId = null; // Request Attributes String attrLoginId = null; String attrDeviceName = null; String attrDevEncPWHash = null; String attrClusterName = null; String attrTenantName = null; // Attribute Extraction String attrLocalName; int numAttrs; int idxAttr; final String S_LocalName = "LocalName"; assert qName.equals("RqstLogIn"); CFAsteriskXMsgRqstHandler xmsgRqstHandler = (CFAsteriskXMsgRqstHandler) getParser(); if (xmsgRqstHandler == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "getParser()"); } schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); schemaObj = xmsgRqstHandler.getSchemaObj(); if (schemaObj == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "getParser().getSchemaObj()"); } schemaObj.connect(); // Extract Attributes numAttrs = attrs.getLength(); for (idxAttr = 0; idxAttr < numAttrs; idxAttr++) { attrLocalName = attrs.getLocalName(idxAttr); if (attrLocalName.equals("Id")) { if (attrId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("LoginId")) { if (attrLoginId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrLoginId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("DeviceName")) { if (attrDeviceName != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrDeviceName = attrs.getValue(idxAttr); } else if (attrLocalName.equals("DevEncPWHash")) { if (attrDevEncPWHash != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrDevEncPWHash = attrs.getValue(idxAttr); } else if (attrLocalName.equals("ClusterName")) { if (attrClusterName != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrClusterName = attrs.getValue(idxAttr); } else if (attrLocalName.equals("TenantName")) { if (attrTenantName != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrTenantName = attrs.getValue(idxAttr); } else { throw CFLib.getDefaultExceptionFactory().newUnrecognizedAttributeException(getClass(), S_ProcName, getParser().getLocationInfo(), attrLocalName); } } // Ensure that required attributes have values if ((attrLoginId == null) || (attrLoginId.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "LoginId"); } if ((attrDeviceName == null) || (attrDeviceName.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "DeviceName"); } if ((attrDevEncPWHash == null) || (attrDevEncPWHash.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "DevEncPWHash"); } if ((attrClusterName == null) || (attrClusterName.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "ClusterName"); } if ((attrTenantName == null) || (attrTenantName.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "TenantName"); } if (schemaObj.getAuthorization() != null) { throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "Back end database schema already authorized against database"); } if (schemaObj.isTransactionOpen()) { schemaObj.rollback(); } schemaObj.beginTransaction(); ICFSecuritySysClusterObj sysCluster = schemaObj.getSysClusterTableObj().readSysClusterByIdIdx(1, false); if (sysCluster == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "sysCluster"); } ICFSecurityClusterObj resolvedCluster = sysCluster.getRequiredContainerCluster(); if (resolvedCluster == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, "resolvedCluster"); } ICFSecuritySecUserObj authenticatingUser = schemaObj.getSecUserTableObj() .readSecUserByULoginIdx(attrLoginId, true); if (authenticatingUser == null) { throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(getClass(), S_ProcName, "Permission denied"); } ICFSecuritySecDeviceObj device = schemaObj.getSecDeviceTableObj() .readSecDeviceByIdIdx(authenticatingUser.getRequiredSecUserId(), attrDeviceName, true); if (device == null) { throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(getClass(), S_ProcName, "Permission denied"); } String pubKey = device.getOptionalPubKey(); if ((pubKey == null) || (pubKey.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "DevicePublicKey"); } byte wrapped[] = Base64.decodeBase64(pubKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(wrapped); KeyFactory kf = KeyFactory.getInstance("RSA"); if (kf == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "kf"); } PublicKey decodedPublicKey = kf.generatePublic(x509KeySpec); if (decodedPublicKey == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "DecodedPublicKey"); } byte decodedDevEncPWHash[] = Base64.decodeBase64(attrDevEncPWHash); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); if (cipher == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "cipher"); } cipher.init(Cipher.DECRYPT_MODE, decodedPublicKey); byte decryptedPWHash[] = cipher.doFinal(decodedDevEncPWHash); MessageDigest msgDigest = MessageDigest.getInstance("SHA-512"); msgDigest.update(decryptedPWHash); byte hash[] = msgDigest.digest(); byte encodedDoubleHash[] = Base64.encodeBase64(hash); String hashedAndEncodedPassword = new String(encodedDoubleHash); if (!hashedAndEncodedPassword.equals(authenticatingUser.getRequiredPasswordHash())) { throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(getClass(), S_ProcName, "Permission denied"); } ICFSecurityClusterObj useCluster = null; if (attrClusterName.equals("system")) { useCluster = schemaObj.getClusterTableObj().readClusterByUDomainNameIdx("system"); if (useCluster == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "ClusterSystem"); } attrTenantName = "system"; } else { useCluster = resolvedCluster; } ICFSecurityTenantObj useTenant = schemaObj.getTenantTableObj() .readTenantByUNameIdx(useCluster.getRequiredId(), attrTenantName); if (useTenant == null) { throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(getClass(), S_ProcName, "No such tenant \"" + attrTenantName + "\""); } ICFSecuritySecSessionObj systemSession = schemaObj.getSecSessionTableObj().newInstance(); ICFSecuritySecSessionEditObj editSystemSession = (ICFSecuritySecSessionEditObj) systemSession .beginEdit(); editSystemSession.setRequiredContainerSecUser(authenticatingUser); editSystemSession.setRequiredStart(Calendar.getInstance()); systemSession = editSystemSession.create(); editSystemSession.endEdit(); CFSecurityAuthorization auth = new CFSecurityAuthorization(); auth.setSecCluster(useCluster); auth.setSecTenant(useTenant); auth.setSecSession(systemSession); schemaObj.setAuthorization(auth); schemaObj.commit(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFAsteriskXMsgSchemaMessageFormatter.formatRspnLoggedIn("\n\t\t\t", schemaObj.getSecCluster().getRequiredId(), schemaObj.getSecCluster().getRequiredFullDomainName(), schemaObj.getSecTenant().getRequiredId(), schemaObj.getSecTenant().getRequiredTenantName(), schemaObj.getSecSession().getRequiredContainerSecUser().getRequiredSecUserId(), schemaObj.getSecSession().getRequiredContainerSecUser().getRequiredLoginId(), schemaObj.getSecSession().getRequiredSecSessionId()) + "\n" + schemaFormatter.formatRspnXmlPostamble(); ((CFAsteriskXMsgRqstHandler) getParser()).appendResponse(response); } catch (IllegalBlockSizeException e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFAsteriskXMsgRqstHandler xmsgRqstHandler = ((CFAsteriskXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFAsteriskXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (BadPaddingException e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFAsteriskXMsgRqstHandler xmsgRqstHandler = ((CFAsteriskXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFAsteriskXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (InvalidKeyException e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFAsteriskXMsgRqstHandler xmsgRqstHandler = ((CFAsteriskXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFAsteriskXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (NoSuchAlgorithmException e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFAsteriskXMsgRqstHandler xmsgRqstHandler = ((CFAsteriskXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFAsteriskXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (NoSuchPaddingException e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFAsteriskXMsgRqstHandler xmsgRqstHandler = ((CFAsteriskXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFAsteriskXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (InvalidKeySpecException e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFAsteriskXMsgRqstHandler xmsgRqstHandler = ((CFAsteriskXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFAsteriskXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (RuntimeException e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFAsteriskXMsgRqstHandler xmsgRqstHandler = ((CFAsteriskXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFAsteriskXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (Error e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFAsteriskXMsgRqstHandler xmsgRqstHandler = ((CFAsteriskXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFAsteriskXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } }
From source file:net.sourceforge.msscodefactory.cffreeswitch.v2_4.CFFreeSwitchXMsgRqstHandler.CFFreeSwitchXMsgRqstLogInHandler.java
public void startElement(String uri, String localName, String qName, Attributes attrs) throws SAXException { final String S_ProcName = "startElement"; ICFFreeSwitchSchemaObj schemaObj = null; CFFreeSwitchXMsgSchemaMessageFormatter schemaFormatter = null; try {/*from www . j av a 2 s . c o m*/ // Common XML Attributes String attrId = null; // Request Attributes String attrLoginId = null; String attrDeviceName = null; String attrDevEncPWHash = null; String attrClusterName = null; String attrTenantName = null; // Attribute Extraction String attrLocalName; int numAttrs; int idxAttr; final String S_LocalName = "LocalName"; assert qName.equals("RqstLogIn"); CFFreeSwitchXMsgRqstHandler xmsgRqstHandler = (CFFreeSwitchXMsgRqstHandler) getParser(); if (xmsgRqstHandler == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "getParser()"); } schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); schemaObj = xmsgRqstHandler.getSchemaObj(); if (schemaObj == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "getParser().getSchemaObj()"); } schemaObj.connect(); // Extract Attributes numAttrs = attrs.getLength(); for (idxAttr = 0; idxAttr < numAttrs; idxAttr++) { attrLocalName = attrs.getLocalName(idxAttr); if (attrLocalName.equals("Id")) { if (attrId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("LoginId")) { if (attrLoginId != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrLoginId = attrs.getValue(idxAttr); } else if (attrLocalName.equals("DeviceName")) { if (attrDeviceName != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrDeviceName = attrs.getValue(idxAttr); } else if (attrLocalName.equals("DevEncPWHash")) { if (attrDevEncPWHash != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrDevEncPWHash = attrs.getValue(idxAttr); } else if (attrLocalName.equals("ClusterName")) { if (attrClusterName != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrClusterName = attrs.getValue(idxAttr); } else if (attrLocalName.equals("TenantName")) { if (attrTenantName != null) { throw CFLib.getDefaultExceptionFactory().newUniqueIndexViolationException(getClass(), S_ProcName, S_LocalName, attrLocalName); } attrTenantName = attrs.getValue(idxAttr); } else { throw CFLib.getDefaultExceptionFactory().newUnrecognizedAttributeException(getClass(), S_ProcName, getParser().getLocationInfo(), attrLocalName); } } // Ensure that required attributes have values if ((attrLoginId == null) || (attrLoginId.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "LoginId"); } if ((attrDeviceName == null) || (attrDeviceName.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "DeviceName"); } if ((attrDevEncPWHash == null) || (attrDevEncPWHash.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "DevEncPWHash"); } if ((attrClusterName == null) || (attrClusterName.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "ClusterName"); } if ((attrTenantName == null) || (attrTenantName.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "TenantName"); } if (schemaObj.getAuthorization() != null) { throw CFLib.getDefaultExceptionFactory().newRuntimeException(getClass(), S_ProcName, "Back end database schema already authorized against database"); } if (schemaObj.isTransactionOpen()) { schemaObj.rollback(); } schemaObj.beginTransaction(); ICFSecuritySysClusterObj sysCluster = schemaObj.getSysClusterTableObj().readSysClusterByIdIdx(1, false); if (sysCluster == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "sysCluster"); } ICFSecurityClusterObj resolvedCluster = sysCluster.getRequiredContainerCluster(); if (resolvedCluster == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, "resolvedCluster"); } ICFSecuritySecUserObj authenticatingUser = schemaObj.getSecUserTableObj() .readSecUserByULoginIdx(attrLoginId, true); if (authenticatingUser == null) { throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(getClass(), S_ProcName, "Permission denied"); } ICFSecuritySecDeviceObj device = schemaObj.getSecDeviceTableObj() .readSecDeviceByIdIdx(authenticatingUser.getRequiredSecUserId(), attrDeviceName, true); if (device == null) { throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(getClass(), S_ProcName, "Permission denied"); } String pubKey = device.getOptionalPubKey(); if ((pubKey == null) || (pubKey.length() <= 0)) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "DevicePublicKey"); } byte wrapped[] = Base64.decodeBase64(pubKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(wrapped); KeyFactory kf = KeyFactory.getInstance("RSA"); if (kf == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "kf"); } PublicKey decodedPublicKey = kf.generatePublic(x509KeySpec); if (decodedPublicKey == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "DecodedPublicKey"); } byte decodedDevEncPWHash[] = Base64.decodeBase64(attrDevEncPWHash); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); if (cipher == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "cipher"); } cipher.init(Cipher.DECRYPT_MODE, decodedPublicKey); byte decryptedPWHash[] = cipher.doFinal(decodedDevEncPWHash); MessageDigest msgDigest = MessageDigest.getInstance("SHA-512"); msgDigest.update(decryptedPWHash); byte hash[] = msgDigest.digest(); byte encodedDoubleHash[] = Base64.encodeBase64(hash); String hashedAndEncodedPassword = new String(encodedDoubleHash); if (!hashedAndEncodedPassword.equals(authenticatingUser.getRequiredPasswordHash())) { throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(getClass(), S_ProcName, "Permission denied"); } ICFSecurityClusterObj useCluster = null; if (attrClusterName.equals("system")) { useCluster = schemaObj.getClusterTableObj().readClusterByUDomainNameIdx("system"); if (useCluster == null) { throw CFLib.getDefaultExceptionFactory().newNullArgumentException(getClass(), S_ProcName, 0, "ClusterSystem"); } attrTenantName = "system"; } else { useCluster = resolvedCluster; } ICFSecurityTenantObj useTenant = schemaObj.getTenantTableObj() .readTenantByUNameIdx(useCluster.getRequiredId(), attrTenantName); if (useTenant == null) { throw CFLib.getDefaultExceptionFactory().newInvalidArgumentException(getClass(), S_ProcName, "No such tenant \"" + attrTenantName + "\""); } ICFSecuritySecSessionObj systemSession = schemaObj.getSecSessionTableObj().newInstance(); ICFSecuritySecSessionEditObj editSystemSession = (ICFSecuritySecSessionEditObj) systemSession .beginEdit(); editSystemSession.setRequiredContainerSecUser(authenticatingUser); editSystemSession.setRequiredStart(Calendar.getInstance()); systemSession = editSystemSession.create(); editSystemSession.endEdit(); CFSecurityAuthorization auth = new CFSecurityAuthorization(); auth.setSecCluster(useCluster); auth.setSecTenant(useTenant); auth.setSecSession(systemSession); schemaObj.setAuthorization(auth); schemaObj.commit(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFFreeSwitchXMsgSchemaMessageFormatter.formatRspnLoggedIn("\n\t\t\t", schemaObj.getSecCluster().getRequiredId(), schemaObj.getSecCluster().getRequiredFullDomainName(), schemaObj.getSecTenant().getRequiredId(), schemaObj.getSecTenant().getRequiredTenantName(), schemaObj.getSecSession().getRequiredContainerSecUser().getRequiredSecUserId(), schemaObj.getSecSession().getRequiredContainerSecUser().getRequiredLoginId(), schemaObj.getSecSession().getRequiredSecSessionId()) + "\n" + schemaFormatter.formatRspnXmlPostamble(); ((CFFreeSwitchXMsgRqstHandler) getParser()).appendResponse(response); } catch (IllegalBlockSizeException e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFFreeSwitchXMsgRqstHandler xmsgRqstHandler = ((CFFreeSwitchXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFFreeSwitchXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (BadPaddingException e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFFreeSwitchXMsgRqstHandler xmsgRqstHandler = ((CFFreeSwitchXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFFreeSwitchXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (InvalidKeyException e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFFreeSwitchXMsgRqstHandler xmsgRqstHandler = ((CFFreeSwitchXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFFreeSwitchXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (NoSuchAlgorithmException e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFFreeSwitchXMsgRqstHandler xmsgRqstHandler = ((CFFreeSwitchXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFFreeSwitchXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (NoSuchPaddingException e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFFreeSwitchXMsgRqstHandler xmsgRqstHandler = ((CFFreeSwitchXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFFreeSwitchXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (InvalidKeySpecException e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFFreeSwitchXMsgRqstHandler xmsgRqstHandler = ((CFFreeSwitchXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFFreeSwitchXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (RuntimeException e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFFreeSwitchXMsgRqstHandler xmsgRqstHandler = ((CFFreeSwitchXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFFreeSwitchXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } catch (Error e) { if (schemaObj != null) { schemaObj.rollback(); schemaObj.setAuthorization(null); } CFFreeSwitchXMsgRqstHandler xmsgRqstHandler = ((CFFreeSwitchXMsgRqstHandler) getParser()); schemaFormatter = xmsgRqstHandler.getSchemaMessageFormatter(); String response = schemaFormatter.formatRspnXmlPreamble() + "\n" + "\t" + CFFreeSwitchXMsgSchemaMessageFormatter.formatRspnException("\n\t\t\t", e) + "\n" + schemaFormatter.formatRspnXmlPostamble(); xmsgRqstHandler.resetResponse(); xmsgRqstHandler.appendResponse(response); xmsgRqstHandler.setCaughtException(true); } }
From source file:net.jmhertlein.mcanalytics.api.auth.SSLUtil.java
public static PublicKey getPublicKeyFromInfo(SubjectPublicKeyInfo o) { try {//from w ww. j ava2 s . c o m byte[] bytes = o.getEncoded("X509"); return KeyFactory.getInstance("EC").generatePublic(new X509EncodedKeySpec(bytes)); } catch (NoSuchAlgorithmException | InvalidKeySpecException | IOException ex) { Logger.getLogger(SSLUtil.class.getName()).log(Level.SEVERE, null, ex); return null; } }
From source file:com.torresbueno.RSAEncryptionDecryptionUtil.java
public KeyFactory getKeyFactoryInstance() throws NoSuchAlgorithmException { if (keyFactoryInstance == null) { keyFactoryInstance = KeyFactory.getInstance("RSA"); }/* www. j a va 2 s .c o m*/ return keyFactoryInstance; }
From source file:cn.quickj.AbstractApplication.java
private void decryptQuickjLicense(String hex) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); byte[] encrypted = Hex.decodeHex(hex.toCharArray()); byte[] keydata = new byte[128]; System.arraycopy(encrypted, 0, keydata, 0, 128); String key = new String(Hex.encodeHex(keydata)); PublicKey pubKey = keyFactory .generatePublic(new RSAPublicKeySpec(new BigInteger(key, 16), new BigInteger("10001", 16))); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, pubKey); byte[] decrypted = new byte[encrypted.length]; int outputOffset = 0; for (int offset = 128; offset < encrypted.length;) { int inputLen = (encrypted.length - offset) > 128 ? 128 : (encrypted.length - offset); outputOffset += cipher.doFinal(encrypted, offset, inputLen, decrypted, outputOffset); offset += inputLen;/*from w ww. j a va2s . c om*/ } String licenseInfo = new String(decrypted, 0, outputOffset - 16, "utf8"); String[] s = licenseInfo.split("\\|"); hosts = s[1].split(","); endDate = new SimpleDateFormat("yyyy-MM-dd").parse(s[2]); byte[] md5 = new byte[16]; System.arraycopy(decrypted, outputOffset - 16, md5, 0, 16); licensePath = new String(Hex.encodeHex(md5)); }
From source file:net.jmhertlein.core.crypto.Keys.java
/** * Given an X509-formatted encoding of an RSA public key, returns the PublicKey object representing it * * @param bytes/* w w w .ja v a 2 s . c o m*/ * * @return the RSA public key, or null if the RSA algorithm is not available on the system */ public static PublicKey getRSAPublicKeyFromEncoded(byte[] bytes) { try { return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bytes)); } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { Logger.getLogger(Keys.class.getName()).log(Level.SEVERE, null, ex); return null; } }
From source file:XmldapCertsAndKeys.java
public static RSAPrivateKey getXmldapPrivateKey1() throws InvalidKeySpecException, NoSuchAlgorithmException { byte[] modulusBytes = { (byte) 0x00, (byte) 0xe2, (byte) 0x94, (byte) 0x9f, (byte) 0xaf, (byte) 0xd0, (byte) 0xa9, (byte) 0x36, (byte) 0x63, (byte) 0xfc, (byte) 0x15, (byte) 0xa8, (byte) 0x41, (byte) 0x5c, (byte) 0x4d, (byte) 0x14, (byte) 0x8d, (byte) 0x19, (byte) 0xd8, (byte) 0x05, (byte) 0xc8, (byte) 0xd5, (byte) 0xac, (byte) 0xf0, (byte) 0xb3, (byte) 0xe4, (byte) 0x72, (byte) 0xb8, (byte) 0x97, (byte) 0xa4, (byte) 0xeb, (byte) 0x43, (byte) 0x62, (byte) 0x6b, (byte) 0x04, (byte) 0x2b, (byte) 0x14, (byte) 0x02, (byte) 0xa4, (byte) 0xd0, (byte) 0xf8, (byte) 0x7e, (byte) 0xfd, (byte) 0x89, (byte) 0xd4, (byte) 0x89, (byte) 0x00, (byte) 0x27, (byte) 0xcd, (byte) 0xbb, (byte) 0x13, (byte) 0xfa, (byte) 0x3b, (byte) 0x99, (byte) 0x57, (byte) 0x4f, (byte) 0x14, (byte) 0xa5, (byte) 0xa3, (byte) 0xda, (byte) 0xd8, (byte) 0x81, (byte) 0x37, (byte) 0x83, (byte) 0x4f, (byte) 0x91, (byte) 0x15, (byte) 0x9b, (byte) 0x4e, (byte) 0x0e, (byte) 0x5a, (byte) 0xa8, (byte) 0x18, (byte) 0x61, (byte) 0x9a, (byte) 0x91, (byte) 0x23, (byte) 0x9d, (byte) 0xf4, (byte) 0xd3, (byte) 0x67, (byte) 0x0e, (byte) 0xe3, (byte) 0x61, (byte) 0xb1, (byte) 0xdb, (byte) 0x57, (byte) 0x25, (byte) 0x68, (byte) 0x10, (byte) 0xab, (byte) 0xca, (byte) 0x5a, (byte) 0x33, (byte) 0xd4, (byte) 0x9c, (byte) 0xe2, (byte) 0x75, (byte) 0x2e, (byte) 0x7b, (byte) 0x7d, (byte) 0x62, (byte) 0xab, (byte) 0xb2, (byte) 0xef, (byte) 0x9a, (byte) 0x34, (byte) 0x86, (byte) 0x48, (byte) 0xbe, (byte) 0x70, (byte) 0xf3, (byte) 0x83, (byte) 0x60, (byte) 0x95, (byte) 0x3e, (byte) 0x3c, (byte) 0x01, (byte) 0xca, (byte) 0x95, (byte) 0x1a, (byte) 0xbf, (byte) 0xbf, (byte) 0xe6, (byte) 0xfc, (byte) 0xbc, (byte) 0x09, (byte) 0xf4, (byte) 0xff }; byte[] exponentBytes = { (byte) 0x1d, (byte) 0xe6, (byte) 0xf1, (byte) 0x60, (byte) 0x19, (byte) 0x90, (byte) 0x8b, (byte) 0x4e, (byte) 0x0c, (byte) 0xb1, (byte) 0xaa, (byte) 0xff, (byte) 0xdd, (byte) 0x37, (byte) 0x8a, (byte) 0xf3, (byte) 0xc8, (byte) 0x2a, (byte) 0x5b, (byte) 0x31, (byte) 0x13, (byte) 0x09, (byte) 0xfc, (byte) 0xc6, (byte) 0x30, (byte) 0xea, (byte) 0xf6, (byte) 0xf3, (byte) 0x84, (byte) 0x5f, (byte) 0x4c, (byte) 0x08, (byte) 0x4c, (byte) 0x09, (byte) 0x43, (byte) 0xca, (byte) 0x23, (byte) 0x43, (byte) 0x2f, (byte) 0x14, (byte) 0xec, (byte) 0x65, (byte) 0x77, (byte) 0x70, (byte) 0x26, (byte) 0x18, (byte) 0x70, (byte) 0x28, (byte) 0x55, (byte) 0x7d, (byte) 0x20, (byte) 0x74, (byte) 0x07, (byte) 0x1b, (byte) 0x9f, (byte) 0xa3, (byte) 0x20, (byte) 0xed, (byte) 0x0b, (byte) 0xef, (byte) 0xb0, (byte) 0xb5, (byte) 0xeb, (byte) 0xcd, (byte) 0x2f, (byte) 0xcd, (byte) 0x4d, (byte) 0xde, (byte) 0x37, (byte) 0xe5, (byte) 0x86, (byte) 0x55, (byte) 0xf2, (byte) 0x34, (byte) 0xe7, (byte) 0xd9, (byte) 0xf7, (byte) 0xb3, (byte) 0x45, (byte) 0x2a, (byte) 0x92, (byte) 0x1b, (byte) 0x54, (byte) 0x49, (byte) 0x41, (byte) 0x81, (byte) 0xbd, (byte) 0xc0, (byte) 0x63, (byte) 0xd1, (byte) 0x86, (byte) 0x45, (byte) 0xe7, (byte) 0xe3, (byte) 0xb3, (byte) 0xf5, (byte) 0x77, (byte) 0x5f, (byte) 0x46, (byte) 0x93, (byte) 0x20, (byte) 0x19, (byte) 0x9a, (byte) 0x26, (byte) 0x9f, (byte) 0x48, (byte) 0x27, (byte) 0x4b, (byte) 0x93, (byte) 0xa7, (byte) 0x1c, (byte) 0xf2, (byte) 0x8a, (byte) 0x3b, (byte) 0xbe, (byte) 0x40, (byte) 0x85, (byte) 0x92, (byte) 0x8a, (byte) 0x3c, (byte) 0xfd, (byte) 0xeb, (byte) 0x18, (byte) 0x2e, (byte) 0x04, (byte) 0x69, (byte) 0xe5, (byte) 0xa1 }; BigInteger exponent = new BigInteger(1, exponentBytes); BigInteger modulus = new BigInteger(1, modulusBytes); RSAPrivateKeySpec ks = new RSAPrivateKeySpec(modulus, exponent); KeyFactory kf = KeyFactory.getInstance("RSA"); return (RSAPrivateKey) kf.generatePrivate(ks); }
From source file:com.hoccer.api.android.AsyncLinccer.java
public static PrivateKey getPrivateKeyFromSharedPreferences(Context context) throws IOException, InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException { SharedPreferences prefs = context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE); String defaultValue = ""; String storedValue = prefs.getString(PREF_PRIVATE_KEY, defaultValue); Log.v(LOG_TAG, "getPrivateKeyFromSharedPreferences, storedValue=" + storedValue); byte[] myEncodedPrivateKey = Base64.decode(storedValue); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(CryptoHelper.wrapRSA1024_PKCS8(myEncodedPrivateKey)); KeyFactory kf = KeyFactory.getInstance("RSA"); PrivateKey myPrivateKey = kf.generatePrivate(privSpec); return myPrivateKey; }