/src/strongswan/src/libstrongswan/plugins/pkcs7/pkcs7_data.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2012 Martin Willi |
3 | | * |
4 | | * Copyright (C) secunet Security Networks AG |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify it |
7 | | * under the terms of the GNU General Public License as published by the |
8 | | * Free Software Foundation; either version 2 of the License, or (at your |
9 | | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, but |
12 | | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
13 | | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
14 | | * for more details. |
15 | | */ |
16 | | |
17 | | #include "pkcs7_data.h" |
18 | | |
19 | | #include <asn1/asn1.h> |
20 | | #include <asn1/oid.h> |
21 | | |
22 | | typedef struct private_pkcs7_data_t private_pkcs7_data_t; |
23 | | |
24 | | /** |
25 | | * Private data of a PKCS#7 signed-data container. |
26 | | */ |
27 | | struct private_pkcs7_data_t { |
28 | | |
29 | | /** |
30 | | * Implements pkcs7_t. |
31 | | */ |
32 | | pkcs7_t public; |
33 | | |
34 | | /** |
35 | | * Encoded data |
36 | | */ |
37 | | chunk_t content; |
38 | | |
39 | | /** |
40 | | * Encoded PKCS#7 data |
41 | | */ |
42 | | chunk_t encoding; |
43 | | }; |
44 | | |
45 | | METHOD(container_t, get_type, container_type_t, |
46 | | private_pkcs7_data_t *this) |
47 | 0 | { |
48 | 0 | return CONTAINER_PKCS7_DATA; |
49 | 0 | } |
50 | | |
51 | | METHOD(container_t, create_signature_enumerator, enumerator_t*, |
52 | | private_pkcs7_data_t *this) |
53 | 0 | { |
54 | 0 | return enumerator_create_empty(); |
55 | 0 | } |
56 | | |
57 | | METHOD(container_t, get_data, bool, |
58 | | private_pkcs7_data_t *this, chunk_t *data) |
59 | 0 | { |
60 | 0 | chunk_t chunk; |
61 | |
|
62 | 0 | chunk = this->content; |
63 | | |
64 | | /* The content field is optional */ |
65 | 0 | if (chunk.len == 0) |
66 | 0 | { |
67 | 0 | *data = chunk_empty; |
68 | 0 | return TRUE; |
69 | 0 | } |
70 | 0 | if (asn1_unwrap(&chunk, &chunk) == ASN1_OCTET_STRING) |
71 | 0 | { |
72 | 0 | *data = chunk_clone(chunk); |
73 | 0 | return TRUE; |
74 | 0 | } |
75 | 0 | return FALSE; |
76 | 0 | } |
77 | | |
78 | | METHOD(container_t, get_encoding, bool, |
79 | | private_pkcs7_data_t *this, chunk_t *data) |
80 | 0 | { |
81 | 0 | *data = chunk_clone(this->encoding); |
82 | 0 | return TRUE; |
83 | 0 | } |
84 | | |
85 | | METHOD(container_t, destroy, void, |
86 | | private_pkcs7_data_t *this) |
87 | 0 | { |
88 | 0 | free(this->content.ptr); |
89 | 0 | free(this->encoding.ptr); |
90 | 0 | free(this); |
91 | 0 | } |
92 | | |
93 | | /** |
94 | | * Create an empty container |
95 | | */ |
96 | | static private_pkcs7_data_t* create_empty() |
97 | 0 | { |
98 | 0 | private_pkcs7_data_t *this; |
99 | |
|
100 | 0 | INIT(this, |
101 | 0 | .public = { |
102 | 0 | .container = { |
103 | 0 | .get_type = _get_type, |
104 | 0 | .create_signature_enumerator = _create_signature_enumerator, |
105 | 0 | .get_data = _get_data, |
106 | 0 | .get_encoding = _get_encoding, |
107 | 0 | .destroy = _destroy, |
108 | 0 | }, |
109 | 0 | .get_attribute = (void*)return_false, |
110 | 0 | .create_cert_enumerator = (void*)enumerator_create_empty, |
111 | 0 | }, |
112 | 0 | ); |
113 | |
|
114 | 0 | return this; |
115 | 0 | } |
116 | | |
117 | | /** |
118 | | * See header. |
119 | | */ |
120 | | pkcs7_t *pkcs7_data_load(chunk_t encoding, chunk_t content) |
121 | 0 | { |
122 | 0 | private_pkcs7_data_t *this = create_empty(); |
123 | |
|
124 | 0 | this->encoding = chunk_clone(encoding); |
125 | 0 | this->content = chunk_clone(content); |
126 | |
|
127 | 0 | return &this->public; |
128 | 0 | } |
129 | | |
130 | | /** |
131 | | * See header. |
132 | | */ |
133 | | pkcs7_t *pkcs7_data_gen(container_type_t type, va_list args) |
134 | 0 | { |
135 | 0 | private_pkcs7_data_t *this; |
136 | 0 | chunk_t blob = chunk_empty; |
137 | |
|
138 | 0 | while (TRUE) |
139 | 0 | { |
140 | 0 | switch (va_arg(args, builder_part_t)) |
141 | 0 | { |
142 | 0 | case BUILD_BLOB: |
143 | 0 | blob = va_arg(args, chunk_t); |
144 | 0 | continue; |
145 | 0 | case BUILD_END: |
146 | 0 | break; |
147 | 0 | default: |
148 | 0 | return NULL; |
149 | 0 | } |
150 | 0 | break; |
151 | 0 | } |
152 | | |
153 | 0 | if (blob.len) |
154 | 0 | { |
155 | 0 | this = create_empty(); |
156 | |
|
157 | 0 | this->content = asn1_wrap(ASN1_OCTET_STRING, "c", blob); |
158 | 0 | this->encoding = asn1_wrap(ASN1_SEQUENCE, "mm", |
159 | 0 | asn1_build_known_oid(OID_PKCS7_DATA), |
160 | 0 | asn1_wrap(ASN1_CONTEXT_C_0, "c", this->content)); |
161 | 0 | return &this->public; |
162 | 0 | } |
163 | 0 | return NULL; |
164 | 0 | } |