@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DHParameters that = (DHParameters) o; if (getPrivateValueLength() != that.getPrivateValueLength()) return false; if (!getPrime().equals(that.getPrime())) return false; return getGenerator().equals(that.getGenerator()); }
/** * Encode this parameter set as BER octet string. * @return * the PKCS#3 encoded parameter octet string. If the encoding fails, {@code null} is returned. */ public OctetString encodeBER() { BigInteger prime = dhParameters.getPrime(); BigInteger generator = dhParameters.getGenerator(); int privateValueLength = dhParameters.getPrivateValueLength(); return DHParameters.encodeBER(prime, generator, privateValueLength); }
public void decodeBER(OctetString berValue) throws IOException { dhParameters = DHParameters.getDHParametersFromBER(berValue); }
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); } }
/** * Create a {@link DHParameters} instance that uses the Oakeley Group 1 Diffie Hellman parameters defined in * RFC 2409 (see also {@link DHGroups}). * @param id * the instance OID of the scalar instance (last sub-identifier should be * zero). * @param access * the maximum access level supported by this instance. * @param value * the initial value that defines the parameters as BER encoded OCTET STRING, * see {@link #decodeBER(OctetString)}. */ public UsmDHParametersImpl(OID id, MOAccess access, OctetString value) { super(id, access, value); if (value != null && value.length() > 0) { try { decodeBER(value); } catch (IOException e) { throw new IllegalArgumentException(value.toHexString()); } } else { dhParameters = new DHParameters(DHGroups.P1, DHGroups.G, AuthMD5.KEY_LENGTH * 8); } }
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); } }
public static DHParameters getDHParametersFromBER(OctetString berValue) throws IOException { BERInputStream inputStream = new BERInputStream(ByteBuffer.wrap(berValue.getValue())); BER.MutableByte sequenceType = new BER.MutableByte(); int sequenceLength = BER.decodeHeader(inputStream, sequenceType); long headerOffset = inputStream.getPosition(); BER.MutableByte integerType = new BER.MutableByte(); BigInteger prime = BER.decodeBigInteger(inputStream, integerType); BigInteger generator = BER.decodeBigInteger(inputStream, integerType); int privateValueLength = 0; if (inputStream.available() > 0 && inputStream.getPosition() < sequenceLength + headerOffset) { privateValueLength = BER.decodeInteger(inputStream, integerType); } return new DHParameters(prime, generator, privateValueLength); }
@Override public int hashCode() { int result = getPrime().hashCode(); result = 31 * result + getGenerator().hashCode(); result = 31 * result + getPrivateValueLength(); return result; }
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 DHParameters getDHParametersFromBER(OctetString berValue) throws IOException { BERInputStream inputStream = new BERInputStream(ByteBuffer.wrap(berValue.getValue())); BER.MutableByte sequenceType = new BER.MutableByte(); int sequenceLength = BER.decodeHeader(inputStream, sequenceType); long headerOffset = inputStream.getPosition(); BER.MutableByte integerType = new BER.MutableByte(); BigInteger prime = BER.decodeBigInteger(inputStream, integerType); BigInteger generator = BER.decodeBigInteger(inputStream, integerType); int privateValueLength = 0; if (inputStream.available() > 0 && inputStream.getPosition() < sequenceLength + headerOffset) { privateValueLength = BER.decodeInteger(inputStream, integerType); } return new DHParameters(prime, generator, privateValueLength); }
public void validate(MOValueValidationEvent validationEvent) { Variable newValue = validationEvent.getNewValue(); //--AgentGen BEGIN=usmDHParameters::validate if (newValue instanceof OctetString) { try { DHParameters.getDHParametersFromBER((OctetString) newValue); } catch (IOException e) { validationEvent.setValidationStatus(PDU.wrongEncoding); } } else { validationEvent.setValidationStatus(PDU.wrongType); } //--AgentGen END } }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; DHParameters that = (DHParameters) o; if (getPrivateValueLength() != that.getPrivateValueLength()) return false; if (!getPrime().equals(that.getPrime())) return false; return getGenerator().equals(that.getGenerator()); }
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 DHParameters getDHParameters() { try { return DHParameters.getDHParametersFromBER(new OctetString(usmDHParameters.getValue())); } catch (IOException e) { LOGGER.error("Unable to decode usmDHParameters value '"+usmDHParameters.getValue().toHexString()+"'", e); return null; } }
@Override public int hashCode() { int result = getPrime().hashCode(); result = 31 * result + getGenerator().hashCode(); result = 31 * result + getPrivateValueLength(); return result; }
public static KeyPair generatePublicKey(DHParameters dhParameters) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { DHParameterSpec dhParameterSpec = new DHParameterSpec(dhParameters.getPrime(), dhParameters.getGenerator(), dhParameters.getPrivateValueLength()); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(DIFFIE_HELLMAN); keyPairGenerator.initialize(dhParameterSpec); return keyPairGenerator.generateKeyPair(); }
public static KeyPair generatePublicKey(DHParameters dhParameters) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { DHParameterSpec dhParameterSpec = new DHParameterSpec(dhParameters.getPrime(), dhParameters.getGenerator(), dhParameters.getPrivateValueLength()); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(DIFFIE_HELLMAN); keyPairGenerator.initialize(dhParameterSpec); return keyPairGenerator.generateKeyPair(); }