Return to hash_payload.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / strongswan / src / libcharon / encoding / payloads |
1.1 misho 1: /* 2: * Copyright (C) 2011 Martin Willi 3: * Copyright (C) 2011 revosec AG 4: * 5: * This program is free software; you can redistribute it and/or modify it 6: * under the terms of the GNU General Public License as published by the 7: * Free Software Foundation; either version 2 of the License, or (at your 8: * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. 9: * 10: * This program is distributed in the hope that it will be useful, but 11: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 12: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13: * for more details. 14: */ 15: 16: #include <stddef.h> 17: 18: #include "hash_payload.h" 19: 20: #include <encoding/payloads/encodings.h> 21: 22: typedef struct private_hash_payload_t private_hash_payload_t; 23: 24: /** 25: * Private data of an hash_payload_t object. 26: */ 27: struct private_hash_payload_t { 28: 29: /** 30: * Public hash_payload_t interface. 31: */ 32: hash_payload_t public; 33: 34: /** 35: * Next payload type. 36: */ 37: uint8_t next_payload; 38: 39: /** 40: * Reserved byte 41: */ 42: uint8_t reserved; 43: 44: /** 45: * Length of this payload. 46: */ 47: uint16_t payload_length; 48: 49: /** 50: * The contained hash value. 51: */ 52: chunk_t hash; 53: 54: /** 55: * either PLV1_HASH or PLV1_NAT_D 56: */ 57: payload_type_t type; 58: }; 59: 60: /** 61: * Encoding rules for an IKEv1 hash payload 62: */ 63: static encoding_rule_t encodings[] = { 64: /* 1 Byte next payload type, stored in the field next_payload */ 65: { U_INT_8, offsetof(private_hash_payload_t, next_payload) }, 66: { RESERVED_BYTE, offsetof(private_hash_payload_t, reserved) }, 67: /* Length of the whole payload*/ 68: { PAYLOAD_LENGTH, offsetof(private_hash_payload_t, payload_length) }, 69: /* Hash Data is from variable size */ 70: { CHUNK_DATA, offsetof(private_hash_payload_t, hash) }, 71: }; 72: 73: /* 74: 1 2 3 75: 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 76: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 77: ! Next Payload ! RESERVED ! Payload Length ! 78: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 79: ! ! 80: ~ Hash Data ~ 81: ! ! 82: +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ 83: */ 84: 85: METHOD(payload_t, verify, status_t, 86: private_hash_payload_t *this) 87: { 88: return SUCCESS; 89: } 90: 91: METHOD(payload_t, get_encoding_rules, int, 92: private_hash_payload_t *this, encoding_rule_t **rules) 93: { 94: *rules = encodings; 95: return countof(encodings); 96: } 97: 98: METHOD(payload_t, get_header_length, int, 99: private_hash_payload_t *this) 100: { 101: return 4; 102: } 103: 104: METHOD(payload_t, get_type, payload_type_t, 105: private_hash_payload_t *this) 106: { 107: return this->type; 108: } 109: 110: METHOD(payload_t, get_next_type, payload_type_t, 111: private_hash_payload_t *this) 112: { 113: return this->next_payload; 114: } 115: 116: METHOD(payload_t, set_next_type, void, 117: private_hash_payload_t *this, payload_type_t type) 118: { 119: this->next_payload = type; 120: } 121: 122: METHOD(payload_t, get_length, size_t, 123: private_hash_payload_t *this) 124: { 125: return this->payload_length; 126: } 127: 128: METHOD(hash_payload_t, set_hash, void, 129: private_hash_payload_t *this, chunk_t hash) 130: { 131: free(this->hash.ptr); 132: this->hash = chunk_clone(hash); 133: this->payload_length = get_header_length(this) + hash.len; 134: } 135: 136: METHOD(hash_payload_t, get_hash, chunk_t, 137: private_hash_payload_t *this) 138: { 139: return this->hash; 140: } 141: 142: METHOD2(payload_t, hash_payload_t, destroy, void, 143: private_hash_payload_t *this) 144: { 145: free(this->hash.ptr); 146: free(this); 147: } 148: 149: /* 150: * Described in header 151: */ 152: hash_payload_t *hash_payload_create(payload_type_t type) 153: { 154: private_hash_payload_t *this; 155: 156: INIT(this, 157: .public = { 158: .payload_interface = { 159: .verify = _verify, 160: .get_encoding_rules = _get_encoding_rules, 161: .get_header_length = _get_header_length, 162: .get_length = _get_length, 163: .get_next_type = _get_next_type, 164: .set_next_type = _set_next_type, 165: .get_type = _get_type, 166: .destroy = _destroy, 167: }, 168: .set_hash = _set_hash, 169: .get_hash = _get_hash, 170: .destroy = _destroy, 171: }, 172: .next_payload = PL_NONE, 173: .payload_length = get_header_length(this), 174: .type = type, 175: ); 176: return &this->public; 177: }