public static OctetString derivePublicKey(KeyPair keyPair) { return new OctetString(keyToBytes(keyPair.getPublic())); }
/** * Constructs a SnmpUsmDhObjectsMib instance without actually creating its * <code>ManagedObject</code> instances. This has to be done in a * sub-class constructor or after construction by calling * {@link #createMO(MOFactory moFactory)}. */ protected SnmpUsmDhObjectsMib() { //--AgentGen BEGIN=_DEFAULTCONSTRUCTOR dhOperations = new DHOperations(); //--AgentGen END }
public byte[] getSharedKey() { return DHOperations.computeSharedKey(keyAgreement, DHOperations.keyToBytes(keyPair.getPublic()), keyParameters); }
public static KeyPair createKeyPair(OctetString publicKeyOctets, OctetString privateKeyOctets, DHParameters dhParameters) { KeyFactory keyFactory = null; try { keyFactory = KeyFactory.getInstance(DIFFIE_HELLMAN); BigInteger y = bytesToBigInteger(publicKeyOctets.getValue()); DHPublicKeySpec dhPublicKeySpec = new DHPublicKeySpec(y, dhParameters.getPrime(), dhParameters.getGenerator()); PublicKey publicKey = keyFactory.generatePublic(dhPublicKeySpec); BigInteger x = bytesToBigInteger(privateKeyOctets.getValue()); DHPrivateKeySpec dhPrivateKeySpec = new DHPrivateKeySpec(x, dhParameters.getPrime(), dhParameters.getGenerator()); PrivateKey privateKey = keyFactory.generatePrivate(dhPrivateKeySpec); return new KeyPair(publicKey, privateKey); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } }
OctetString yl = DHOperations.derivePublicKey(keyInfo.getKeyPair()); OctetString z = new OctetString(yz.substring(yl.length(), yz.length())); if (keyInfo.getKeyAgreement() == null) { keyInfo.getInitializedKeyAgreement(); byte[] sharedKey = DHOperations.computeSharedKey(keyInfo.getKeyAgreement(), z.getValue(), dhParameters); byte[] usmKey = DHOperations.deriveKey(sharedKey, keyLength); keyInfo.setUsmKey(usmKey);
@Override public void prepare(SubRequest subRequest, MOTableRow row, MOTableRow changeSet, int column) { super.prepare(subRequest, row, changeSet, column); DHKeyInfo keyInfo = getDHKeyInfo(row, column); if (keyInfo == null || keyInfo.getKeyPair() == null || !(keyInfo.getKeyPair().getPublic() instanceof DHPublicKey)) { subRequest.setErrorStatus(SnmpConstants.SNMP_ERROR_WRONG_VALUE); } Variable variable = subRequest.getVariableBinding().getVariable(); if (variable instanceof OctetString) { OctetString yz = (OctetString)variable; OctetString yl = DHOperations.derivePublicKey(keyInfo.getKeyPair()); try { OctetString yr = yz.substring(0, yl.length()); if (!yr.equals(yl)) { subRequest.setErrorStatus(SnmpConstants.SNMP_ERROR_WRONG_VALUE); } } catch (IndexOutOfBoundsException ioobe) { subRequest.setErrorStatus(SnmpConstants.SNMP_ERROR_WRONG_VALUE); } } }
public KeyPair generatePublicKey(DHParameters dhParameters) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { keyParameters = dhParameters; keyPair = DHOperations.generatePublicKey(keyParameters); return keyPair; }
public KeyAgreement getInitializedKeyAgreement() { keyAgreement = DHOperations.getInitializedKeyAgreement(keyPair); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Created KeyAgreement: "+keyAgreement); } return keyAgreement; }
SecurityProtocols.getInstance().getAuthenticationProtocol(authProtocol); OctetString authKey = new OctetString(DHOperations.deriveKey(authSharedKey, authenticationProtocol.getDigestLength())); SecurityProtocols.getInstance().getPrivacyProtocol(privProtocol); OctetString privKey = new OctetString(DHOperations.deriveKey(privSharedKey, privacyProtocol.getMaxKeyLength()));
public static KeyPair createKeyPair(OctetString publicKeyOctets, OctetString privateKeyOctets, DHParameters dhParameters) { KeyFactory keyFactory = null; try { keyFactory = KeyFactory.getInstance(DIFFIE_HELLMAN); BigInteger y = bytesToBigInteger(publicKeyOctets.getValue()); DHPublicKeySpec dhPublicKeySpec = new DHPublicKeySpec(y, dhParameters.getPrime(), dhParameters.getGenerator()); PublicKey publicKey = keyFactory.generatePublic(dhPublicKeySpec); BigInteger x = bytesToBigInteger(privateKeyOctets.getValue()); DHPrivateKeySpec dhPrivateKeySpec = new DHPrivateKeySpec(x, dhParameters.getPrime(), dhParameters.getGenerator()); PrivateKey privateKey = keyFactory.generatePrivate(dhPrivateKeySpec); return new KeyPair(publicKey, privateKey); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } }
@Override public void get(SubRequest subRequest, MOTableRow row, int column) { DHKeyInfo keyInfo = getDHKeyInfo(row, column); if (keyInfo != null && getAccess().isAccessibleForRead()) { if (keyInfo.getKeyPair() == null) { try { keyInfo.generatePublicKey(dhParameters); } catch (NoSuchAlgorithmException e) { LOGGER.error("Failed to generate DH public key because Diffie-Hellman algorithm is not available", e); } catch (InvalidAlgorithmParameterException e) { LOGGER.error("Failed to generate DH public key because of invalid parameters", e); } } subRequest.getVariableBinding().setVariable(DHOperations.derivePublicKey(keyInfo.getKeyPair())); } else { subRequest.getStatus().setErrorStatus(SnmpConstants.SNMP_ERROR_NO_ACCESS); } subRequest.completed(); }
public static OctetString derivePrivateKey(KeyPair keyPair) { return new OctetString(keyToBytes(keyPair.getPrivate())); }
public static byte[] computeSharedKey(KeyAgreement keyAgreement, byte[] publicKey, DHParameters dhParameters) { if (keyAgreement == null) { return null; } try { KeyFactory keyFactory = KeyFactory.getInstance(DIFFIE_HELLMAN); BigInteger y = bytesToBigInteger(publicKey); DHPublicKeySpec dhPublicKeySpec = new DHPublicKeySpec(y, dhParameters.getPrime(), dhParameters.getGenerator()); PublicKey pubKey = keyFactory.generatePublic(dhPublicKeySpec); keyAgreement.doPhase(pubKey, true); byte[] secret = keyAgreement.generateSecret(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Computing shared key "+new OctetString(secret).toHexString()+" from public key "+ new OctetString(publicKey).toHexString()+" and parameters "+dhParameters); } return secret; } catch (Exception e) { throw new RuntimeException(e); } }
public static OctetString derivePrivateKey(KeyPair keyPair) { return new OctetString(keyToBytes(keyPair.getPrivate())); }
public static byte[] computeSharedKey(KeyAgreement keyAgreement, byte[] publicKey, DHParameters dhParameters) { if (keyAgreement == null) { return null; } try { KeyFactory keyFactory = KeyFactory.getInstance(DIFFIE_HELLMAN); BigInteger y = bytesToBigInteger(publicKey); DHPublicKeySpec dhPublicKeySpec = new DHPublicKeySpec(y, dhParameters.getPrime(), dhParameters.getGenerator()); PublicKey pubKey = keyFactory.generatePublic(dhPublicKeySpec); keyAgreement.doPhase(pubKey, true); byte[] secret = keyAgreement.generateSecret(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Computing shared key "+new OctetString(secret).toHexString()+" from public key "+ new OctetString(publicKey).toHexString()+" and parameters "+dhParameters); } return secret; } catch (Exception e) { throw new RuntimeException(e); } }
public static OctetString derivePublicKey(KeyPair keyPair) { return new OctetString(keyToBytes(keyPair.getPublic())); }