Return to pkcs7_data.c CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / strongswan / src / libstrongswan / plugins / pkcs7 |
1.1 misho 1: /* 2: * Copyright (C) 2012 Martin Willi 3: * Copyright (C) 2012 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 "pkcs7_data.h" 17: 18: #include <asn1/asn1.h> 19: #include <asn1/oid.h> 20: 21: typedef struct private_pkcs7_data_t private_pkcs7_data_t; 22: 23: /** 24: * Private data of a PKCS#7 signed-data container. 25: */ 26: struct private_pkcs7_data_t { 27: 28: /** 29: * Implements pkcs7_t. 30: */ 31: pkcs7_t public; 32: 33: /** 34: * Encoded data 35: */ 36: chunk_t content; 37: 38: /** 39: * Encoded PKCS#7 data 40: */ 41: chunk_t encoding; 42: }; 43: 44: METHOD(container_t, get_type, container_type_t, 45: private_pkcs7_data_t *this) 46: { 47: return CONTAINER_PKCS7_DATA; 48: } 49: 50: METHOD(container_t, create_signature_enumerator, enumerator_t*, 51: private_pkcs7_data_t *this) 52: { 53: return enumerator_create_empty(); 54: } 55: 56: METHOD(container_t, get_data, bool, 57: private_pkcs7_data_t *this, chunk_t *data) 58: { 59: chunk_t chunk; 60: 61: chunk = this->content; 62: if (asn1_unwrap(&chunk, &chunk) == ASN1_OCTET_STRING) 63: { 64: *data = chunk_clone(chunk); 65: return TRUE; 66: } 67: return FALSE; 68: } 69: 70: METHOD(container_t, get_encoding, bool, 71: private_pkcs7_data_t *this, chunk_t *data) 72: { 73: *data = chunk_clone(this->encoding); 74: return TRUE; 75: } 76: 77: METHOD(container_t, destroy, void, 78: private_pkcs7_data_t *this) 79: { 80: free(this->content.ptr); 81: free(this->encoding.ptr); 82: free(this); 83: } 84: 85: /** 86: * Create an empty container 87: */ 88: static private_pkcs7_data_t* create_empty() 89: { 90: private_pkcs7_data_t *this; 91: 92: INIT(this, 93: .public = { 94: .container = { 95: .get_type = _get_type, 96: .create_signature_enumerator = _create_signature_enumerator, 97: .get_data = _get_data, 98: .get_encoding = _get_encoding, 99: .destroy = _destroy, 100: }, 101: .get_attribute = (void*)return_false, 102: .create_cert_enumerator = (void*)enumerator_create_empty, 103: }, 104: ); 105: 106: return this; 107: } 108: 109: /** 110: * See header. 111: */ 112: pkcs7_t *pkcs7_data_load(chunk_t encoding, chunk_t content) 113: { 114: private_pkcs7_data_t *this = create_empty(); 115: 116: this->encoding = chunk_clone(encoding); 117: this->content = chunk_clone(content); 118: 119: return &this->public; 120: } 121: 122: /** 123: * See header. 124: */ 125: pkcs7_t *pkcs7_data_gen(container_type_t type, va_list args) 126: { 127: private_pkcs7_data_t *this; 128: chunk_t blob = chunk_empty; 129: 130: while (TRUE) 131: { 132: switch (va_arg(args, builder_part_t)) 133: { 134: case BUILD_BLOB: 135: blob = va_arg(args, chunk_t); 136: continue; 137: case BUILD_END: 138: break; 139: default: 140: return NULL; 141: } 142: break; 143: } 144: 145: if (blob.len) 146: { 147: this = create_empty(); 148: 149: this->content = asn1_wrap(ASN1_OCTET_STRING, "c", blob); 150: this->encoding = asn1_wrap(ASN1_SEQUENCE, "mm", 151: asn1_build_known_oid(OID_PKCS7_DATA), 152: asn1_wrap(ASN1_CONTEXT_C_0, "c", this->content)); 153: return &this->public; 154: } 155: return NULL; 156: }