Added: webservices/rampart/trunk/c/src/omxmlsec/saml/subject.c URL: http://svn.apache.org/viewvc/webservices/rampart/trunk/c/src/omxmlsec/saml/subject.c?rev=592652&view=auto ============================================================================== --- webservices/rampart/trunk/c/src/omxmlsec/saml/subject.c (added) +++ webservices/rampart/trunk/c/src/omxmlsec/saml/subject.c Wed Nov 7 00:22:44 2007 @@ -0,0 +1,548 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <saml.h> + + +AXIS2_EXTERN saml_named_id_t * AXIS2_CALL +saml_named_id_create(axutil_env_t *env) +{ + saml_named_id_t *named_id = AXIS2_MALLOC(env->allocator, sizeof(saml_named_id_t)); + if (named_id) + { + named_id->format = NULL; + named_id->name_qualifier = NULL; + named_id->name = NULL; + } + return named_id; +} + +#ifndef SAML_NAMED_ID_RESET +#define SAML_NAMED_ID_RESET(_named_id, _env) \ + if (_named_id->format) \ + { \ + AXIS2_FREE(_env->allocator, _named_id->format); \ + } \ + if (named_id->name_qualifier) \ + { \ + AXIS2_FREE(_env->allocator, _named_id->name_qualifier); \ + } \ + if (_named_id->name) \ + { \ + AXIS2_FREE(_env->allocator, _named_id->name); \ + } +#endif + + +AXIS2_EXTERN void AXIS2_CALL +saml_named_id_free(saml_named_id_t *named_id, axutil_env_t *env) +{ + if (named_id->format) + { + AXIS2_FREE(env->allocator, named_id->format); + } + if (named_id->name_qualifier) + { + AXIS2_FREE(env->allocator, named_id->name_qualifier); + } + if (named_id->name) + { + AXIS2_FREE(env->allocator, named_id->name); + } + AXIS2_FREE(env->allocator, named_id); +} + +AXIS2_EXTERN int AXIS2_CALL +saml_named_id_build(saml_named_id_t *named_id, axiom_node_t *node, + axutil_env_t *env) +{ + axutil_hash_t *attr_hash = NULL; + axutil_hash_index_t *hi = NULL; + axiom_element_t *element = NULL; + SAML_NAMED_ID_RESET(named_id, env); + if (axiom_node_get_node_type(node, env) != AXIOM_ELEMENT || (element = (axiom_element_t *)axiom_node_get_data_element(node, env)) == NULL) + { + return AXIS2_FAILURE; + } + attr_hash = axiom_element_get_all_attributes(element, env); + if (attr_hash == NULL) + { + return AXIS2_FAILURE; + } + for (hi = axutil_hash_first(attr_hash, env); hi != NULL; hi = axutil_hash_next(env, hi)) + { + void *v = NULL; + axutil_hash_this(hi, NULL, NULL, &v); + if (v) + { + axis2_char_t *local_name = NULL; + axiom_attribute_t *attr = (axiom_attribute_t*)v; + local_name = axiom_attribute_get_localname(attr, env); + if (0 == axutil_strcmp(local_name, SAML_NAME_QUALIFIER)) + { + named_id->name_qualifier = axiom_attribute_get_value(attr, env); + } + else if (0 == axutil_strcmp(local_name, SAML_FORMAT)) + { + named_id->format = axiom_attribute_get_value(attr, env); + } + else + { + return AXIS2_FAILURE; + } + } + } + if ((named_id->name = axiom_element_get_text(element, env, node)) == NULL) + { + return AXIS2_FAILURE; + } + return AXIS2_SUCCESS; +} + + +AXIS2_EXTERN axiom_node_t * AXIS2_CALL +saml_named_id_to_om(saml_named_id_t *id, axiom_node_t *parent, + axutil_env_t *env) +{ + axiom_element_t *e = NULL; + axiom_node_t *n = NULL; + axiom_namespace_t *ns = NULL; + axiom_attribute_t *attr = NULL; + ns = axiom_namespace_create(env, SAML_NMSP_URI, SAML_PREFIX); + e = axiom_element_create(env, parent, SAML_NAME_IDENTIFIER, ns, &n); + if (e) + { + if (id->format) + { + attr = axiom_attribute_create(env, SAML_FORMAT, id->format, NULL); + axiom_element_add_attribute(e, env, attr, n); + } + if (id->name_qualifier) + { + attr = axiom_attribute_create(env, SAML_NAME_QUALIFIER, id->name_qualifier, NULL); + axiom_element_add_attribute(e, env, attr, n); + } + if (id->name) + { + axiom_element_set_text(e, env, id->name, n); + } + } + return n; +} + +AXIS2_EXTERN axis2_char_t * AXIS2_CALL +saml_named_id_get_name(saml_named_id_t *id, axutil_env_t *env) +{ + return id->name; +} + +AXIS2_EXTERN axis2_char_t * AXIS2_CALL +saml_named_id_get_format(saml_named_id_t *id, axutil_env_t *env) +{ + return id->format; +} + +AXIS2_EXTERN axis2_char_t * AXIS2_CALL +saml_named_id_get_name_qualifier(saml_named_id_t *id, + axutil_env_t *env) +{ + return id->name_qualifier; +} + +AXIS2_EXTERN int AXIS2_CALL +saml_named_id_set_name(saml_named_id_t *id, axutil_env_t *env, + axis2_char_t *name) +{ + if (id->name) + { + AXIS2_FREE(env->allocator, id->name); + } + id->name = axutil_strdup(env, name); + return AXIS2_SUCCESS; +} + +AXIS2_EXTERN int AXIS2_CALL +saml_named_id_set_format(saml_named_id_t *id, axutil_env_t *env, + axis2_char_t *format) +{ + if (id->format) + { + AXIS2_FREE(env->allocator, id->format); + } + id->format = axutil_strdup(env, format); + return AXIS2_SUCCESS; +} + +AXIS2_EXTERN int AXIS2_CALL +saml_named_id_set_name_qualifier(saml_named_id_t *id, axutil_env_t *env, + axis2_char_t *qualifier) +{ + if (id->name_qualifier) + { + AXIS2_FREE(env->allocator, id->name_qualifier); + } + id->name_qualifier = axutil_strdup(env, qualifier); + return AXIS2_SUCCESS; +} + + +AXIS2_EXTERN saml_subject_t * AXIS2_CALL +saml_subject_create(axutil_env_t *env) +{ + saml_subject_t *subject = AXIS2_MALLOC(env->allocator, sizeof(saml_subject_t)); + if (subject) + { + subject->named_id = NULL; + subject->confirmation_data = NULL; + subject->confirmation_methods = NULL; + subject->key_info = NULL; + } + return subject; +} + +AXIS2_EXTERN void AXIS2_CALL +saml_subject_free(saml_subject_t *subject, axutil_env_t *env) +{ + /*if (subject->named_id) + { + saml_named_id_free(subject->named_id, env); + }*/ + if (subject->confirmation_methods) + { + axutil_array_list_free(subject->confirmation_methods, env); + } + if (subject->confirmation_data) + { + subject->confirmation_data = NULL; + } + if (subject->key_info) + { + subject->key_info = NULL; + } + AXIS2_FREE(env->allocator, subject); +} + +AXIS2_EXTERN int AXIS2_CALL +saml_subject_build(saml_subject_t *subject, axiom_node_t *node, + axutil_env_t *env) +{ + axiom_element_t *element = NULL; + axiom_node_t *cn = NULL, *ccn = NULL; + axiom_element_t *ce = NULL, *cce = NULL; + saml_auth_binding_t *auth_bind = NULL; + char *child_name = NULL; + axiom_child_element_iterator_t *ci = NULL, *cci = NULL; + short is_named_id = AXIS2_FALSE; + if (axiom_node_get_node_type(node, env) != AXIOM_ELEMENT || (element = (axiom_element_t *)axiom_node_get_data_element(node, env)) == NULL) + { + return AXIS2_FAILURE; + } + ci = axiom_element_get_child_elements(element, env, node); + if (ci) + { + while (AXIS2_TRUE == axiom_child_element_iterator_has_next(ci, env)) + { + cn = axiom_child_element_iterator_next(ci, env); + ce = axiom_node_get_data_element(cn, env); + if (0 == axutil_strcmp(axiom_element_get_localname(ce, env), SAML_NAME_IDENTIFIER)) + { + if (!subject->named_id) + { + subject->named_id = saml_named_id_create(env); + } + saml_named_id_build(subject->named_id, cn, env); + } + if (0 == axutil_strcmp(axiom_element_get_localname(ce, env), SAML_SUBJECT_CONFIRMATION)) + { + cci = axiom_element_get_child_elements(ce, env, cn); + if (cci) + { + while (AXIS2_TRUE == axiom_child_element_iterator_has_next(cci, env)) + { + ccn = axiom_child_element_iterator_next(cci, env); + cce = axiom_node_get_data_element(ccn, env); + if (0 == axutil_strcmp(axiom_element_get_localname(cce, env), SAML_CONFIRMATION_METHOD)) + { + if (!subject->confirmation_methods) + { + subject->confirmation_methods = axutil_array_list_create(env, SAML_ARRAY_LIST_DEF); + } + axutil_array_list_add(subject->confirmation_methods, env, axiom_element_get_text(cce, env, ccn)); + } + else if (0 == axutil_strcmp(axiom_element_get_localname(cce, env), SAML_SUBJECT_CONFIRMATION_DATA)) + { + subject->confirmation_data = ccn; + } + else if (0 == axutil_strcmp(axiom_element_get_localname(cce, env), SAML_KEY_INFO)) + { + subject->key_info = ccn; + } + else + { + return AXIS2_FAILURE; + } + } + } + } + } + } + else + { + return AXIS2_FAILURE; + } + return AXIS2_SUCCESS; +} + +AXIS2_EXTERN axiom_node_t * AXIS2_CALL +saml_subject_to_om(saml_subject_t *subject, axiom_node_t *parent, + axutil_env_t *env) +{ + int i = 0, size = 0; + axiom_element_t *e = NULL, *ce = NULL, *cce = NULL; + axiom_node_t *n = NULL, *cn = NULL, *ccn = NULL; + axiom_namespace_t *ns = NULL; + ns = axiom_namespace_create(env, SAML_NMSP_URI, SAML_PREFIX); + e = axiom_element_create(env, parent, SAML_SUBJECT, ns, &n); + if (e) + { + if (subject->named_id) + { + saml_named_id_to_om(subject->named_id, n, env); + } + ns = axiom_namespace_create(env, SAML_NMSP_URI, SAML_PREFIX); + ce = axiom_element_create(env, n, SAML_SUBJECT_CONFIRMATION, ns, &cn); + if (ce) + { + if (subject->confirmation_methods) + { + size = axutil_array_list_size(subject->confirmation_methods, env); + for (i = 0; i < size; i++) + { + ns = axiom_namespace_create(env, SAML_NMSP_URI, SAML_PREFIX); + cce = axiom_element_create(env, cn, SAML_CONFIRMATION_METHOD, ns, &ccn); + if (cce) + { + axiom_element_set_text(cce, env, axutil_array_list_get(subject->confirmation_methods, env, i), ccn); + } + } + } + if (subject->confirmation_data) + { + ns = axiom_namespace_create(env, SAML_NMSP_URI, SAML_PREFIX); + cce = axiom_element_create(env, cn, SAML_SUBJECT_CONFIRMATION_DATA, ns, &ccn); + if (cce) + { + axiom_node_add_child(cn, env, subject->confirmation_data); + } + } + if (subject->key_info) + { + ns = axiom_namespace_create(env, SAML_NMSP_URI, SAML_PREFIX); + cce = axiom_element_create(env, cn, SAML_KEY_INFO, ns, &ccn); + if (cce) + { + axiom_node_add_child(cn, env, subject->key_info); + } + } + } + } + return n; +} + +AXIS2_EXTERN saml_named_id_t * AXIS2_CALL +saml_subject_get_named_id(saml_subject_t *subject, axutil_env_t *env) +{ + return subject->named_id; +} + +AXIS2_EXTERN axutil_array_list_t * AXIS2_CALL +saml_subject_get_confirmation_methods(saml_subject_t *subject, + axutil_env_t *env) +{ + return subject->confirmation_methods; +} + +AXIS2_EXTERN axiom_node_t * AXIS2_CALL +saml_subject_get_confirmation_data(saml_subject_t *subject, axutil_env_t *env) +{ + return subject->confirmation_data; +} + +AXIS2_EXTERN axiom_node_t * AXIS2_CALL +saml_subject_get_key_info(saml_subject_t *subject, axutil_env_t *env) +{ + return subject->key_info; +} + +AXIS2_EXTERN int AXIS2_CALL +saml_subject_set_named_id(saml_subject_t *subject, + axutil_env_t *env, saml_named_id_t *named_id) +{ + if (subject->named_id) + { + saml_named_id_free(subject->named_id, env); + } + subject->named_id = named_id; + return AXIS2_SUCCESS; +} + +AXIS2_EXTERN int AXIS2_CALL +saml_subject_set_confirmation_methods(saml_subject_t *subject, + axutil_env_t *env, + axutil_array_list_t *list) +{ + int i = 0, size = 0; + axis2_char_t *val = NULL; + if (subject->confirmation_methods) + { + size = axutil_array_list_size(subject->confirmation_methods, env); + for (i = 0; i < size; i++) + { + val = axutil_array_list_get(subject->confirmation_methods, env, i); + if (val) + { + AXIS2_FREE(env->allocator, env); + } + } + axutil_array_list_free(subject->confirmation_methods, env); + subject->confirmation_methods = list; + } + else + { + subject->confirmation_methods = list; + } + return AXIS2_SUCCESS; +} + + +AXIS2_EXTERN int AXIS2_CALL +saml_subject_add_confirmation(saml_subject_t *subject, + axutil_env_t *env, axis2_char_t *sub_confirmation) +{ + if (!subject->confirmation_methods) + { + subject->confirmation_methods = axutil_array_list_create(env, SAML_ARRAY_LIST_DEF * 2); + } + axutil_array_list_add(subject->confirmation_methods, env, axutil_strdup(env, sub_confirmation)); + return AXIS2_SUCCESS; +} + +AXIS2_EXTERN int AXIS2_CALL +saml_subject_remove_subject_confiirmation(saml_subject_t *subject, + axutil_env_t *env, int index) +{ + axis2_char_t *val = NULL; + if (subject->confirmation_methods && axutil_array_list_size(subject->confirmation_methods, env) > index) + { + val = axutil_array_list_remove(subject->confirmation_methods, env, index); + if (val) + { + AXIS2_FREE(env->allocator, val); + } + return AXIS2_SUCCESS; + } + else + { + return AXIS2_FAILURE; + } +} + +AXIS2_EXTERN int AXIS2_CALL +saml_subject_set_key_info(saml_subject_t *subject, + axutil_env_t *env, axiom_node_t *node) +{ + if (subject->key_info) + { + axiom_node_free_tree(subject->key_info, env); + } + subject->key_info = node; + return AXIS2_SUCCESS; +} + +AXIS2_EXTERN saml_subject_stmt_t * AXIS2_CALL +saml_subject_stmt_create(axutil_env_t *env) +{ + saml_subject_stmt_t *stmt = AXIS2_MALLOC(env->allocator, sizeof(saml_subject_stmt_t)); + if (stmt) + { + if (!(stmt->subject = saml_subject_create(env))) + { + AXIS2_FREE(env->allocator, stmt); + return NULL; + } + } + return stmt; +} + +AXIS2_EXTERN void AXIS2_CALL +saml_subject_stmt_free(saml_subject_stmt_t *subject_stmt, + axutil_env_t *env) +{ + saml_subject_free(subject_stmt->subject, env); + AXIS2_FREE(env->allocator, subject_stmt); +} + +AXIS2_EXTERN int AXIS2_CALL +saml_subject_stmt_build(saml_subject_stmt_t *subject_stmt, + axiom_node_t *node, axutil_env_t *env) +{ + axiom_element_t *element = NULL; + axiom_node_t *first_enode = NULL; + axiom_element_t *first_element; + if (axiom_node_get_node_type(node, env) != AXIOM_ELEMENT || (element = (axiom_element_t *)axiom_node_get_data_element(node, env)) == NULL) + { + return AXIS2_FAILURE; + } + if ((first_element = axiom_element_get_first_element(element, env, node, &first_enode)) != NULL && 0 == axutil_strcmp(axiom_element_get_localname(element, env), SAML_SUBJECT)) + { + saml_subject_build(subject_stmt->subject, first_enode, env); + return AXIS2_SUCCESS; + } + return AXIS2_FAILURE; +} + +AXIS2_EXTERN axiom_node_t * AXIS2_CALL +saml_subject_stmt_to_om(saml_subject_stmt_t *subject_stmt, + axiom_node_t *parent, axutil_env_t *env) +{ + axiom_element_t *e = NULL; + axiom_node_t *n = NULL; + axiom_namespace_t *ns = NULL; + ns = axiom_namespace_create(env, SAML_NMSP_URI, SAML_PREFIX); + e = axiom_element_create(env, parent, SAML_SUBJECT_STATEMENT, ns, &n); + if (e) + { + saml_subject_to_om(subject_stmt->subject, n, env); + } + return n; +} + +AXIS2_EXTERN int AXIS2_CALL +saml_subject_stmt_set_subject(saml_subject_stmt_t *subject_stmt, + axutil_env_t *env, saml_subject_t *subject) +{ + saml_subject_free(subject_stmt->subject, env); + subject_stmt->subject = subject; + return AXIS2_SUCCESS; +} + +AXIS2_EXTERN saml_subject_t * AXIS2_CALL +saml_subject_stmt_get_subject(saml_subject_stmt_t *subject_stmt, + axutil_env_t *env) +{ + return subject_stmt->subject; +} \ No newline at end of file
Added: webservices/rampart/trunk/c/src/omxmlsec/saml/sutil.c URL: http://svn.apache.org/viewvc/webservices/rampart/trunk/c/src/omxmlsec/saml/sutil.c?rev=592652&view=auto ============================================================================== --- webservices/rampart/trunk/c/src/omxmlsec/saml/sutil.c (added) +++ webservices/rampart/trunk/c/src/omxmlsec/saml/sutil.c Wed Nov 7 00:22:44 2007 @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <saml.h> +#include <saml_req.h> + +AXIS2_EXTERN int AXIS2_CALL saml_util_set_sig_ctx_defaults(oxs_sign_ctx_t *sig_ctx, axutil_env_t *env, axis2_char_t *id) +{ + oxs_sign_part_t* sig_part = NULL; + oxs_transform_t *tr = NULL; + axutil_array_list_t *sig_parts = NULL, *trans = NULL; + axiom_attribute_t* att = NULL; + axiom_element_t *test = NULL; + + trans = axutil_array_list_create(env, SAML_ARRAY_LIST_DEF); + + /*create transform sor SAML XML signature with identifier*/ + tr = oxs_transforms_factory_produce_transform(env, OXS_HREF_TRANSFORM_ENVELOPED_SIGNATURE); + axutil_array_list_add(trans, env, tr); + + tr = oxs_transforms_factory_produce_transform(env, OXS_HREF_TRANSFORM_ENVELOPED_SIGNATURE); + axutil_array_list_add(trans, env, tr); + + sig_part = oxs_sign_part_create(env); + oxs_sign_part_set_digest_mtd(sig_part, env, OXS_HREF_SHA1); + + + oxs_sign_part_set_transforms(sig_part, env, trans); + oxs_sign_part_set_id_name(sig_part, env, id); + oxs_sign_part_set_sign_namespace(sig_part,env, NULL); + + sig_parts = axutil_array_list_create(env, SAML_ARRAY_LIST_DEF); + axutil_array_list_add(sig_parts, env, sig_part); + + /*create the specific sign context*/ + + oxs_sign_ctx_set_c14n_mtd(sig_ctx, env, OXS_HREF_XML_EXC_C14N); + oxs_sign_ctx_set_operation(sig_ctx, env, OXS_SIGN_OPERATION_SIGN); + oxs_sign_ctx_set_sign_mtd_algo(sig_ctx, env, OXS_HREF_RSA_SHA1); + oxs_sign_ctx_set_sign_parts(sig_ctx, env, sig_parts); + + return AXIS2_SUCCESS; +} \ No newline at end of file
