Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public Key abstraction layer |
| 3 | * |
| 4 | * Copyright (C) 2006-2013, Brainspark B.V. |
| 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
| 26 | #include "polarssl/config.h" |
| 27 | |
| 28 | #include "polarssl/rsa.h" |
| 29 | #include "polarssl/ecp.h" |
| 30 | #include "polarssl/pk.h" |
| 31 | |
| 32 | #include <stdlib.h> |
| 33 | |
| 34 | /* |
| 35 | * Initialise a pk_context |
| 36 | */ |
| 37 | void pk_init( pk_context *ctx ) |
| 38 | { |
| 39 | if( ctx == NULL ) |
| 40 | return; |
| 41 | |
| 42 | ctx->type = POLARSSL_PK_NONE; |
| 43 | ctx->data = NULL; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Free (the components of) a pk_context |
| 48 | */ |
| 49 | void pk_free( pk_context *ctx ) |
| 50 | { |
| 51 | if( ctx == NULL ) |
| 52 | return; |
| 53 | |
| 54 | switch( ctx->type ) |
| 55 | { |
| 56 | case POLARSSL_PK_NONE: |
| 57 | break; |
| 58 | |
| 59 | case POLARSSL_PK_RSA: |
| 60 | rsa_free( ctx->data ); |
| 61 | break; |
| 62 | |
| 63 | case POLARSSL_PK_ECKEY: |
| 64 | case POLARSSL_PK_ECKEY_DH: |
| 65 | ecp_keypair_free( ctx->data ); |
| 66 | break; |
| 67 | } |
| 68 | |
Manuel Pégourié-Gonnard | 1f73a65 | 2013-07-09 10:26:41 +0200 | [diff] [blame^] | 69 | free( ctx-> data ); |
| 70 | |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 71 | ctx->type = POLARSSL_PK_NONE; |
| 72 | ctx->data = NULL; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * Set a pk_context to a given type |
| 77 | */ |
| 78 | int pk_set_type( pk_context *ctx, pk_type_t type ) |
| 79 | { |
| 80 | size_t size = type == POLARSSL_PK_RSA ? sizeof( rsa_context ) |
| 81 | : type == POLARSSL_PK_ECKEY ? sizeof( ecp_keypair ) |
| 82 | : type == POLARSSL_PK_ECKEY_DH ? sizeof( ecp_keypair ) |
| 83 | : 0; |
| 84 | |
| 85 | if( size == 0 ) |
| 86 | return( 0 ); |
| 87 | |
| 88 | if( ( ctx->data = malloc( size ) ) == NULL ) |
Manuel Pégourié-Gonnard | 7a6c946 | 2013-07-09 10:04:07 +0200 | [diff] [blame] | 89 | return( POLARSSL_ERR_PK_MALLOC_FAILED ); |
Manuel Pégourié-Gonnard | 12e0ed9 | 2013-07-04 13:31:32 +0200 | [diff] [blame] | 90 | |
| 91 | memset( ctx->data, 0, size ); |
| 92 | ctx->type = type; |
| 93 | |
| 94 | return( 0 ); |
| 95 | } |