TokenBindingMessagePreparator extends ProtocolMessagePreparator<TokenBindingMessage> { @Override protected void prepareProtocolMessageContents() { message.setTokenbindingType(chooser.getConfig().getDefaultTokenBindingType().getTokenBindingTypeValue()); message.setKeyParameter(chooser.getConfig().getDefaultTokenBindingKeyParameters().get(0).getValue()); if (chooser.getConfig().getDefaultTokenBindingKeyParameters().get(0) == TokenBindingKeyParameters.ECDSAP256) { EllipticCurve curve = CurveFactory.getCurve(NamedGroup.SECP256R1); BigInteger privateKey = chooser.getConfig().getDefaultTokenBindingEcPrivateKey(); LOGGER.debug("Using private Key:" + privateKey); Point publicKey = curve.mult(privateKey, curve.getBasePoint()); message.setPoint(PointFormatter.toRawFormat(publicKey)); message.setPointLength(message.getPoint().getValue().length); ParametersWithRandom params = new ParametersWithRandom(new ECPrivateKeyParameters(privateKey, generateEcParameters()), new BadRandom(new Random(0), new byte[0])); ECDSASigner signer = new ECDSASigner(); signer.init(true, params); MessageDigest dig = null; try { dig = MessageDigest.getInstance("SHA-256"); } catch (NoSuchAlgorithmException ex) { throw new WorkflowExecutionException("Could not create SHA-256 digest", ex); } dig.update(generateToBeSigned()); BigInteger[] signature = signer.generateSignature(dig.digest()); message.setSignature(ArrayConverter.concatenate(ArrayConverter.bigIntegerToByteArray(signature[0]), ArrayConverter.bigIntegerToByteArray(signature[1]))); } else { message.setModulus(chooser.getConfig().getDefaultTokenBindingRsaModulus().toByteArray()); message.setModulusLength(message.getModulus().getValue().length); message.setPublicExponent(chooser.getConfig().getDefaultTokenBindingRsaPublicKey().toByteArray()); message.setPublicExponentLength(message.getPublicExponent().getValue().length); } TokenBindingMessageSerializer serializer = new TokenBindingMessageSerializer(message, chooser.getSelectedProtocolVersion()); message.setKeyLength(serializer.serializeKey().length); message.setExtensionBytes(new byte[0]); message.setExtensionLength(message.getExtensionBytes().getValue().length); message.setSignatureLength(message.getSignature().getValue().length); serializer = new TokenBindingMessageSerializer(message, ProtocolVersion.TLS12); message.setTokenbindingsLength(serializer.serializeBinding().length); } TokenBindingMessagePreparator(Chooser chooser, TokenBindingMessage message);   }
@Test public void testPrepareProtocolMessageContents() throws Exception { preparator.prepare(); Serializer serializer = new TokenBindingMessageSerializer(message, ProtocolVersion.TLS12); byte[] serialize = serializer.serialize(); TokenBindingMessageParser selfParser = new TokenBindingMessageParser(0, serialize, ProtocolVersion.TLS12, config); TokenBindingMessage selfParsed = selfParser.parse(); assertNotNull(selfParsed); String base64 = "AIkAAgBBQM9eQES_uxoyRn0DDoYLcWqvm6Oo3p0lI1s3fRjdIj6dw8wLDf0RWkxuyNAmgAQkUWxm8_JfwS8MziBYVuJ5ECcAQHF_HGcPiSv_X60y5Ql-AxoqaWzwqXvpStEBgY_IX8kT_qAHsb5h38ZuQoWOaZVgqlF1sa70B4GVXxmi2JkdJYcAAA"; byte[] decode = Base64.getUrlDecoder().decode(base64); TokenBindingMessageParser parser = new TokenBindingMessageParser(0, decode, ProtocolVersion.TLS12, config); TokenBindingMessage parsedMessage = parser.parse(); byte[] xBytes = new byte[32]; System.arraycopy(parsedMessage.getPoint().getValue(), 0, xBytes, 0, 32); LOGGER.debug("X:" + ArrayConverter.bytesToHexString(xBytes)); byte[] yBytes = new byte[32]; System.arraycopy(parsedMessage.getPoint().getValue(), 32, yBytes, 0, 32); LOGGER.debug("Y:" + ArrayConverter.bytesToHexString(yBytes)); BigInteger intX = new BigInteger(xBytes); LOGGER.debug("intx: " + intX); ASN1Integer x = new ASN1Integer(xBytes); LOGGER.debug("xasn1:" + x.getPositiveValue()); ASN1Integer y = new ASN1Integer(yBytes); ECDomainParameters generateEcParameters = generateEcParameters(); ECPublicKeyParameters ecPublicKeyParameters = new ECPublicKeyParameters(generateEcParameters.getCurve() .createPoint(x.getPositiveValue(), y.getPositiveValue()), generateEcParameters); LOGGER.debug("RAW X:" + ecPublicKeyParameters.getQ().getRawXCoord().toBigInteger()); LOGGER.debug("RAW Y:" + ecPublicKeyParameters.getQ().getRawYCoord().toBigInteger()); LOGGER.debug("Valid: " + ecPublicKeyParameters.getQ().isValid()); ECDSASigner signer = new ECDSASigner(); signer.init(false, ecPublicKeyParameters); byte[] signedContent = ArrayConverter .hexStringToByteArray("0002A5F86D4EA32D7B305774F6EA56DB444F4D70D777ABF77B810561935F3A96B9C2"); byte[] rBytes = new byte[32]; System.arraycopy(parsedMessage.getSignature().getValue(), 0, rBytes, 0, 32); byte[] sBytes = new byte[32]; System.arraycopy(parsedMessage.getSignature().getValue(), 32, sBytes, 0, 32); LOGGER.debug("r:" + ArrayConverter.bytesToHexString(rBytes)); LOGGER.debug("s:" + ArrayConverter.bytesToHexString(sBytes)); LOGGER.debug("r:" + new ASN1Integer(rBytes).getPositiveValue()); LOGGER.debug("s:" + new ASN1Integer(sBytes).getPositiveValue()); }