Package org.picketlink.identity.federation.ws.trust

Examples of org.picketlink.identity.federation.ws.trust.BinarySecretType


        if (WSTrustConstants.KEY_TYPE_SYMMETRIC.equalsIgnoreCase(keyType.toString())) {
            // symmetric key case: if client entropy is found, compute a key. If not, generate a new key.
            requestedProofToken = new RequestedProofTokenType();

            byte[] serverSecret = WSTrustUtil.createRandomSecret((int) keySize / 8);
            BinarySecretType serverBinarySecret = new BinarySecretType();
            serverBinarySecret.setType(WSTrustConstants.BS_TYPE_NONCE);
            serverBinarySecret.setValue(Base64.encodeBytes(serverSecret).getBytes());

            byte[] clientSecret = null;
            EntropyType clientEntropy = request.getEntropy();
            if (clientEntropy != null) {
                clientSecret = Base64.decode(new String(WSTrustUtil.getBinarySecret(clientEntropy)));
View Full Code Here


                } else if (tag.equals(WSTrustConstants.ENTROPY)) {
                    subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
                    EntropyType entropy = new EntropyType();
                    subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
                    if (StaxParserUtil.matches(subEvent, WSTrustConstants.BINARY_SECRET)) {
                        BinarySecretType binarySecret = new BinarySecretType();
                        Attribute typeAttribute = subEvent.getAttributeByName(new QName("", "Type"));
                        if (typeAttribute != null) {
                            binarySecret.setType(StaxParserUtil.getAttributeValue(typeAttribute));
                        }

                        if (!StaxParserUtil.hasTextAhead(xmlEventReader))
                            throw logger.parserExpectedTextValue("binary secret value");

                        binarySecret.setValue(StaxParserUtil.getElementText(xmlEventReader).getBytes());
                        entropy.addAny(binarySecret);
                    }
                    responseToken.setEntropy(entropy);
                    EndElement endElement = StaxParserUtil.getNextEndElement(xmlEventReader);
                    StaxParserUtil.validate(endElement, WSTrustConstants.ENTROPY);
                } else if (tag.equals(WSTrustConstants.USE_KEY)) {
                    subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
                    UseKeyType useKeyType = new UseKeyType();
                    StaxParserUtil.validate(subEvent, WSTrustConstants.USE_KEY);

                    // We peek at the next start element as the stax source has to be in the START_ELEMENT mode
                    subEvent = StaxParserUtil.peekNextStartElement(xmlEventReader);
                    if (StaxParserUtil.matches(subEvent, X509CERTIFICATE)) {
                        Element domElement = StaxParserUtil.getDOMElement(xmlEventReader);
                        // Element domElement = getX509CertificateAsDomElement( subEvent, xmlEventReader );

                        useKeyType.add(domElement);
                        responseToken.setUseKey(useKeyType);
                    } else if (StaxParserUtil.matches(subEvent, KEYVALUE)) {
                        Element domElement = StaxParserUtil.getDOMElement(xmlEventReader);
                        useKeyType.add(domElement);
                        responseToken.setUseKey(useKeyType);
                    } else
                        throw logger.parserUnknownStartElement(StaxParserUtil.getStartElementName(subEvent), subEvent.getLocation());
                } else if (tag.equals(WSTrustConstants.REQUESTED_TOKEN_CANCELLED)) {
                    StaxParserUtil.getNextEndElement(xmlEventReader);
                    responseToken.setRequestedTokenCancelled(new RequestedTokenCancelledType());
                } else if (tag.equals(WSTrustConstants.REQUESTED_PROOF_TOKEN)) {
                    subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
                    RequestedProofTokenType requestedProofToken = new RequestedProofTokenType();
                    subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
                    if (StaxParserUtil.matches(subEvent, WSTrustConstants.BINARY_SECRET)) {
                        BinarySecretType binarySecret = new BinarySecretType();
                        Attribute typeAttribute = subEvent.getAttributeByName(new QName("", "Type"));
                        binarySecret.setType(StaxParserUtil.getAttributeValue(typeAttribute));

                        if (!StaxParserUtil.hasTextAhead(xmlEventReader))
                            throw logger.parserExpectedTextValue("binary secret value");

                        binarySecret.setValue(StaxParserUtil.getElementText(xmlEventReader).getBytes());
                        requestedProofToken.add(binarySecret);
                    } else if (StaxParserUtil.matches(subEvent, WSTrustConstants.COMPUTED_KEY)) {
                        ComputedKeyType computedKey = new ComputedKeyType();
                        if (!StaxParserUtil.hasTextAhead(xmlEventReader))
                            throw logger.parserExpectedTextValue("computed key algorithm");
View Full Code Here

        List<Object> entropyList = entropy.getAny();
        if (entropyList != null) {
            for (Object entropyObj : entropyList) {
                if (entropyObj instanceof BinarySecretType) {
                    BinarySecretType binarySecret = (BinarySecretType) entropyObj;
                    writeBinarySecretType(writer, binarySecret);
                }
            }
        }
        StaxUtil.writeEndElement(writer);
View Full Code Here

            StaxUtil.writeStartElement(this.writer, WSTrustConstants.PREFIX, WSTrustConstants.REQUESTED_PROOF_TOKEN,
                    WSTrustConstants.BASE_NAMESPACE);
            List<Object> theList = requestedProof.getAny();
            for (Object content : theList) {
                if (content instanceof BinarySecretType) {
                    BinarySecretType binarySecret = (BinarySecretType) content;
                    StaxUtil.writeStartElement(this.writer, WSTrustConstants.PREFIX, WSTrustConstants.BINARY_SECRET,
                            WSTrustConstants.BASE_NAMESPACE);
                    StaxUtil.writeAttribute(this.writer, WSTrustConstants.TYPE, binarySecret.getType());
                    StaxUtil.writeCharacters(this.writer, new String(binarySecret.getValue()));
                    StaxUtil.writeEndElement(this.writer);
                } else if (content instanceof ComputedKeyType) {
                    ComputedKeyType computedKey = (ComputedKeyType) content;
                    StaxUtil.writeStartElement(this.writer, WSTrustConstants.PREFIX, WSTrustConstants.COMPUTED_KEY,
                            WSTrustConstants.BASE_NAMESPACE);
                    StaxUtil.writeCharacters(this.writer, computedKey.getAlgorithm());
                    StaxUtil.writeEndElement(this.writer);
                } else
                    throw new ProcessingException(ErrorCodes.UNSUPPORTED_TYPE + content);
            }

            StaxUtil.writeEndElement(this.writer);
        }

        // write the server entropy, if available.
        if (response.getEntropy() != null) {
            EntropyType entropy = response.getEntropy();
            StaxUtil.writeStartElement(this.writer, WSTrustConstants.PREFIX, WSTrustConstants.ENTROPY,
                    WSTrustConstants.BASE_NAMESPACE);

            List<Object> entropyList = entropy.getAny();
            if (entropyList != null && entropyList.size() != 0) {
                for (Object entropyObj : entropyList) {
                    if (entropyObj instanceof BinarySecretType) {
                        BinarySecretType binarySecret = (BinarySecretType) entropyObj;
                        StaxUtil.writeStartElement(this.writer, WSTrustConstants.PREFIX, WSTrustConstants.BINARY_SECRET,
                                WSTrustConstants.BASE_NAMESPACE);
                        if (binarySecret.getType() != null) {
                            StaxUtil.writeAttribute(this.writer, WSTrustConstants.TYPE, binarySecret.getType());
                        }
                        StaxUtil.writeCharacters(this.writer, new String(binarySecret.getValue()));
                        StaxUtil.writeEndElement(this.writer);
                    }
                }
            }
            StaxUtil.writeEndElement(writer);
View Full Code Here

                } else if (tag.equals(WSTrustConstants.ENTROPY)) {
                    subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
                    EntropyType entropy = new EntropyType();
                    subEvent = StaxParserUtil.getNextStartElement(xmlEventReader);
                    if (StaxParserUtil.matches(subEvent, WSTrustConstants.BINARY_SECRET)) {
                        BinarySecretType binarySecret = new BinarySecretType();
                        Attribute typeAttribute = subEvent.getAttributeByName(new QName("", "Type"));
                        binarySecret.setType(StaxParserUtil.getAttributeValue(typeAttribute));

                        if (!StaxParserUtil.hasTextAhead(xmlEventReader))
                            throw new ParsingException(ErrorCodes.EXPECTED_TEXT_VALUE + "binary secret value");

                        binarySecret.setValue(StaxParserUtil.getElementText(xmlEventReader).getBytes());
                        entropy.addAny(binarySecret);
                    }
                    requestToken.setEntropy(entropy);
                    EndElement endElement = StaxParserUtil.getNextEndElement(xmlEventReader);
                    StaxParserUtil.validate(endElement, WSTrustConstants.ENTROPY);
View Full Code Here

    public static byte[] getBinarySecret(EntropyType entropy) {
        byte[] secret = null;

        for (Object obj : entropy.getAny()) {
            if (obj instanceof BinarySecretType) {
                BinarySecretType binarySecret = (BinarySecretType) obj;
                secret = binarySecret.getValue();
                break;
            }
        }
        return secret;
    }
View Full Code Here

        RequestSecurityTokenResponseCollection collection = (RequestSecurityTokenResponseCollection) baseResponse;
        RequestSecurityTokenResponse response = collection.getRequestSecurityTokenResponses().get(0);
        RequestedProofTokenType proofToken = response.getRequestedProofToken();
        assertNotNull("Unexpected null proof token", proofToken);
        assertTrue(proofToken.getAny().get(0) instanceof BinarySecretType);
        BinarySecretType serverBinarySecret = (BinarySecretType) proofToken.getAny().get(0);
        assertNotNull("Unexpected null secret", serverBinarySecret.getValue());
        // default key size is 128 bits (16 bytes).
        byte[] encodedSecret = serverBinarySecret.getValue();
        assertEquals("Unexpected secret size", 16, Base64.decode(encodedSecret, 0, encodedSecret.length).length);
    }
View Full Code Here

     */
    @Test
    public void testInvokeSAML20WithCombinedSymmetricKey() throws Exception {
        // create a 64-bit random client secret.
        byte[] clientSecret = WSTrustUtil.createRandomSecret(8);
        BinarySecretType clientBinarySecret = new BinarySecretType();
        clientBinarySecret.setType(WSTrustConstants.BS_TYPE_NONCE);
        clientBinarySecret.setValue(Base64.encodeBytes(clientSecret).getBytes());

        // set the client secret in the client entropy.
        EntropyType clientEntropy = new EntropyType();
        clientEntropy.addAny(clientBinarySecret);

        // create a token request specifying the key type, key size, and client entropy.
        RequestSecurityToken request = this.createRequest("testcontext", WSTrustConstants.ISSUE_REQUEST, null,
                "http://services.testcorp.org/provider2");
        request.setKeyType(URI.create(WSTrustConstants.KEY_TYPE_SYMMETRIC));
        request.setEntropy(clientEntropy);
        request.setKeySize(64);

        // invoke the token service.
        Source requestMessage = this.createSourceFromRequest(request);
        Source responseMessage = this.tokenService.invoke(requestMessage);
        BaseRequestSecurityTokenResponse baseResponse = (BaseRequestSecurityTokenResponse) new WSTrustParser()
                .parse(DocumentUtil.getSourceAsStream(responseMessage));

        // validate the security token response.
        AssertionType assertion = this.validateSAMLAssertionResponse(baseResponse, "testcontext", "jduke",
                SAMLUtil.SAML2_HOLDER_OF_KEY_URI);
        // validate the holder of key contents.
        SubjectConfirmationType subjConfirmation = assertion.getSubject().getConfirmation().get(0);
        this.validateHolderOfKeyContents(subjConfirmation, WSTrustConstants.KEY_TYPE_SYMMETRIC, null, false);

        RequestSecurityTokenResponseCollection collection = (RequestSecurityTokenResponseCollection) baseResponse;
        RequestSecurityTokenResponse response = collection.getRequestSecurityTokenResponses().get(0);
        RequestedProofTokenType proofToken = response.getRequestedProofToken();
        assertNotNull("Unexpected null proof token", proofToken);
        assertTrue(proofToken.getAny().get(0) instanceof ComputedKeyType);
        ComputedKeyType computedKey = (ComputedKeyType) proofToken.getAny().get(0);
        assertEquals("Unexpected computed key algorithm", WSTrustConstants.CK_PSHA1, computedKey.getAlgorithm());

        // server entropy must have been included in the response to allow reconstruction of the computed key.
        EntropyType serverEntropy = response.getEntropy();
        assertNotNull("Unexpected null server entropy");
        assertEquals("Invalid number of elements in server entropy", 1, serverEntropy.getAny().size());
        BinarySecretType serverBinarySecret = (BinarySecretType) serverEntropy.getAny().get(0);
        assertEquals("Unexpected binary secret type", WSTrustConstants.BS_TYPE_NONCE, serverBinarySecret.getType());
        assertNotNull("Unexpected null secret value", serverBinarySecret.getValue());
        // get the base64 decoded
        byte[] encodedSecret = serverBinarySecret.getValue();
        assertEquals("Unexpected secret size", 8, Base64.decode(encodedSecret, 0, encodedSecret.length).length);
    }
View Full Code Here

        assertEquals("http://services.testcorp.org/provider2", endpoint.getAddress().getValue());

        assertEquals(WSTrustConstants.BS_TYPE_SYMMETRIC, requestToken.getKeyType().toASCIIString());

        EntropyType entropy = requestToken.getEntropy();
        BinarySecretType binarySecret = (BinarySecretType) entropy.getAny().get(0);

        assertEquals(WSTrustConstants.BS_TYPE_NONCE, binarySecret.getType());
        assertEquals("M0/7qLpV49c=", new String(binarySecret.getValue()));

        // Now for the writing part
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        WSTrustRequestWriter rstWriter = new WSTrustRequestWriter(baos);
View Full Code Here

TOP

Related Classes of org.picketlink.identity.federation.ws.trust.BinarySecretType

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.