/*1* Copyright (c) 2006 - 2007 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Portions Copyright (c) 2009 Apple Inc. All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13*14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* 3. Neither the name of the Institute nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include "hx_locl.h"3637/**38* @page page_peer Hx509 crypto selecting functions39*40* Peer info structures are used togeter with hx509_crypto_select() to41* select the best avaible crypto algorithm to use.42*43* See the library functions here: @ref hx509_peer44*/4546/**47* Allocate a new peer info structure an init it to default values.48*49* @param context A hx509 context.50* @param peer return an allocated peer, free with hx509_peer_info_free().51*52* @return An hx509 error code, see hx509_get_error_string().53*54* @ingroup hx509_peer55*/5657int58hx509_peer_info_alloc(hx509_context context, hx509_peer_info *peer)59{60*peer = calloc(1, sizeof(**peer));61if (*peer == NULL) {62hx509_set_error_string(context, 0, ENOMEM, "out of memory");63return ENOMEM;64}65return 0;66}676869static void70free_cms_alg(hx509_peer_info peer)71{72if (peer->val) {73size_t i;74for (i = 0; i < peer->len; i++)75free_AlgorithmIdentifier(&peer->val[i]);76free(peer->val);77peer->val = NULL;78peer->len = 0;79}80}8182/**83* Free a peer info structure.84*85* @param peer peer info to be freed.86*87* @ingroup hx509_peer88*/8990void91hx509_peer_info_free(hx509_peer_info peer)92{93if (peer == NULL)94return;95if (peer->cert)96hx509_cert_free(peer->cert);97free_cms_alg(peer);98memset(peer, 0, sizeof(*peer));99free(peer);100}101102/**103* Set the certificate that remote peer is using.104*105* @param peer peer info to update106* @param cert cerificate of the remote peer.107*108* @return An hx509 error code, see hx509_get_error_string().109*110* @ingroup hx509_peer111*/112113int114hx509_peer_info_set_cert(hx509_peer_info peer,115hx509_cert cert)116{117if (peer->cert)118hx509_cert_free(peer->cert);119peer->cert = hx509_cert_ref(cert);120return 0;121}122123/**124* Add an additional algorithm that the peer supports.125*126* @param context A hx509 context.127* @param peer the peer to set the new algorithms for128* @param val an AlgorithmsIdentier to add129*130* @return An hx509 error code, see hx509_get_error_string().131*132* @ingroup hx509_peer133*/134135int136hx509_peer_info_add_cms_alg(hx509_context context,137hx509_peer_info peer,138const AlgorithmIdentifier *val)139{140void *ptr;141int ret;142143ptr = realloc(peer->val, sizeof(peer->val[0]) * (peer->len + 1));144if (ptr == NULL) {145hx509_set_error_string(context, 0, ENOMEM, "out of memory");146return ENOMEM;147}148peer->val = ptr;149ret = copy_AlgorithmIdentifier(val, &peer->val[peer->len]);150if (ret == 0)151peer->len += 1;152else153hx509_set_error_string(context, 0, ret, "out of memory");154return ret;155}156157/**158* Set the algorithms that the peer supports.159*160* @param context A hx509 context.161* @param peer the peer to set the new algorithms for162* @param val array of supported AlgorithmsIdentiers163* @param len length of array val.164*165* @return An hx509 error code, see hx509_get_error_string().166*167* @ingroup hx509_peer168*/169170int171hx509_peer_info_set_cms_algs(hx509_context context,172hx509_peer_info peer,173const AlgorithmIdentifier *val,174size_t len)175{176size_t i;177178free_cms_alg(peer);179180peer->val = calloc(len, sizeof(*peer->val));181if (peer->val == NULL) {182peer->len = 0;183hx509_set_error_string(context, 0, ENOMEM, "out of memory");184return ENOMEM;185}186peer->len = len;187for (i = 0; i < len; i++) {188int ret;189ret = copy_AlgorithmIdentifier(&val[i], &peer->val[i]);190if (ret) {191hx509_clear_error_string(context);192free_cms_alg(peer);193return ret;194}195}196return 0;197}198199#if 0200201/*202* S/MIME203*/204205int206hx509_peer_info_parse_smime(hx509_peer_info peer,207const heim_octet_string *data)208{209return 0;210}211212int213hx509_peer_info_unparse_smime(hx509_peer_info peer,214heim_octet_string *data)215{216return 0;217}218219/*220* For storing hx509_peer_info to be able to cache them.221*/222223int224hx509_peer_info_parse(hx509_peer_info peer,225const heim_octet_string *data)226{227return 0;228}229230int231hx509_peer_info_unparse(hx509_peer_info peer,232heim_octet_string *data)233{234return 0;235}236#endif237238239