1 /*
2 * GStreamer gstreamer-aesenc
3 *
4 * Copyright, LCC (C) 2015 RidgeRun, LCC <carsten.behling@ridgerun.com>
5 * Copyright, LCC (C) 2016 RidgeRun, LCC <jose.jimenez@ridgerun.com>
6 * Copyright (C) 2020 Nice, Contact: Rabindra Harlalka <Rabindra.Harlalka@nice.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * Alternatively, the contents of this file may be used under the
27 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
28 * which case the following provisions apply instead of the ones
29 * mentioned above:
30 *
31 * This library is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU Library General Public
33 * License as published by the Free Software Foundation; either
34 * version 2 of the License, or (at your option) any later version.
35 *
36 * This library is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 * Library General Public License for more details.
40 *
41 * You should have received a copy of the GNU Library General Public
42 * License along with this library; if not, write to the
43 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
44 * Boston, MA 02110-1335, USA.
45 */
46
47 /**
48 * SECTION:element-aesenc
49 *
50 * AES encryption
51 *
52 * ## Example
53 *
54 * |[
55 * echo "This is an AES crypto test ... " > plain.txt && \
56 * gst-launch-1.0 filesrc location=plain.txt ! \
57 * aesenc key=1f9423681beb9a79215820f6bda73d0f iv=e9aa8e834d8d70b7e0d254ff670dd718 ! \
58 * aesdec key=1f9423681beb9a79215820f6bda73d0f iv=e9aa8e834d8d70b7e0d254ff670dd718 ! \
59 * filesink location=dec.txt && \
60 * cat dec.txt
61 *
62 * ]|
63 *
64 * Since: 1.20
65 */
66
67 #ifdef HAVE_CONFIG_H
68 # include <config.h>
69 #endif
70
71 #include <gst/gst.h>
72 #include <gst/base/gstbasetransform.h>
73 #include <string.h>
74 #include "gstaeshelper.h"
75 #include "gstaesenc.h"
76
77 GST_DEBUG_CATEGORY_STATIC (gst_aes_enc_debug);
78 #define GST_CAT_DEFAULT gst_aes_enc_debug
79 G_DEFINE_TYPE_WITH_CODE (GstAesEnc, gst_aes_enc, GST_TYPE_BASE_TRANSFORM,
80 GST_DEBUG_CATEGORY_INIT (gst_aes_enc_debug, "aesenc", 0,
81 "aesenc AES encryption element")
82 );
83 GST_ELEMENT_REGISTER_DEFINE (aesenc, "aesenc", GST_RANK_PRIMARY,
84 GST_TYPE_AES_ENC);
85
86 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
87 GST_PAD_SINK,
88 GST_PAD_ALWAYS,
89 GST_STATIC_CAPS ("ANY")
90 );
91
92 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
93 GST_PAD_SRC,
94 GST_PAD_ALWAYS,
95 GST_STATIC_CAPS ("ANY")
96 );
97
98 static void gst_aes_enc_set_property (GObject * object, guint prop_id,
99 const GValue * value, GParamSpec * pspec);
100 static void gst_aes_enc_get_property (GObject * object, guint prop_id,
101 GValue * value, GParamSpec * pspec);
102
103 static GstFlowReturn gst_aes_enc_transform (GstBaseTransform * base,
104 GstBuffer * inbuf, GstBuffer * outbuf);
105 static GstFlowReturn gst_aes_enc_prepare_output_buffer (GstBaseTransform * base,
106 GstBuffer * inbuf, GstBuffer ** outbuf);
107
108 static gboolean gst_aes_enc_start (GstBaseTransform * base);
109 static gboolean gst_aes_enc_stop (GstBaseTransform * base);
110 static gboolean
111 gst_aes_enc_sink_event (GstBaseTransform * base, GstEvent * event);
112
113 /* aes_enc helper functions */
114 static gboolean gst_aes_enc_openssl_init (GstAesEnc * filter);
115 static void gst_aes_enc_finalize (GObject * object);
116
117 /* GObject vmethod implementations */
118
119 /* initialize class */
120 static void
gst_aes_enc_class_init(GstAesEncClass * klass)121 gst_aes_enc_class_init (GstAesEncClass * klass)
122 {
123 GObjectClass *gobject_class;
124 GstElementClass *gstelement_class;
125
126 gobject_class = (GObjectClass *) klass;
127 gstelement_class = (GstElementClass *) klass;
128
129 gobject_class->set_property = gst_aes_enc_set_property;
130 gobject_class->get_property = gst_aes_enc_get_property;
131 gobject_class->finalize = gst_aes_enc_finalize;
132
133 gst_type_mark_as_plugin_api (GST_TYPE_AES_CIPHER, 0);
134
135 /**
136 * GstAesEnc:cipher
137 *
138 * AES cipher mode (key length and mode)
139 * Currently, 128 and 256 bit keys are supported,
140 * in "cipher block chaining" (CBC) mode
141 *
142 * Since: 1.20
143 */
144 g_object_class_install_property (gobject_class, PROP_CIPHER,
145 g_param_spec_enum ("cipher",
146 "Cipher",
147 "cipher mode",
148 GST_TYPE_AES_CIPHER, GST_AES_DEFAULT_CIPHER_MODE,
149 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
150 GST_PARAM_MUTABLE_READY)));
151
152 /**
153 * GstAesEnc:serialize-iv
154 *
155 * If true, store initialization vector in first 16 bytes of first buffer
156 *
157 * Since: 1.20
158 */
159 g_object_class_install_property (gobject_class, PROP_SERIALIZE_IV,
160 g_param_spec_boolean ("serialize-iv", "Serialize IV",
161 "Store initialization vector in first 16 bytes of first buffer",
162 GST_AES_DEFAULT_SERIALIZE_IV,
163 G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY));
164
165 /**
166 * GstAesEnc:per-buffer-padding
167 *
168 * If true, each buffer will be padded using PKCS7 padding
169 * If false, only the final buffer in the stream will be padded
170 * (by OpenSSL) using PKCS7
171 *
172 * Since: 1.20
173 */
174 g_object_class_install_property (gobject_class, PROP_PER_BUFFER_PADDING,
175 g_param_spec_boolean ("per-buffer-padding", "Per buffer padding",
176 "If true, pad each buffer using PKCS7 padding scheme. Otherwise, only"
177 "pad final buffer",
178 GST_AES_PER_BUFFER_PADDING_DEFAULT,
179 G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY));
180
181 /**
182 * GstAesEnc:key
183 *
184 * AES encryption key (hexadecimal)
185 *
186 * Since: 1.20
187 */
188 g_object_class_install_property (gobject_class, PROP_KEY,
189 g_param_spec_string ("key", "Key",
190 "AES encryption key (in hexadecimal). Length (in bytes) must be equivalent to "
191 "the number of bits in the key length : "
192 "16 bytes for AES 128 and 32 bytes for AES 256",
193 (gchar *) GST_AES_DEFAULT_KEY,
194 G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY));
195
196 /**
197 * GstAesEnc:iv
198 *
199 * AES encryption initialization vector (hexadecimal)
200 *
201 * Since: 1.20
202 */
203 g_object_class_install_property (gobject_class, PROP_IV,
204 g_param_spec_string ("iv", "Iv",
205 "AES encryption initialization vector (in hexadecimal). "
206 "Length must equal AES block length (16 bytes)",
207 (gchar *) GST_AES_DEFAULT_IV,
208 G_PARAM_READWRITE | GST_PARAM_MUTABLE_READY));
209
210 gst_element_class_set_details_simple (gstelement_class,
211 "aesenc",
212 "Generic/Filter",
213 "AES buffer encryption",
214 "Rabindra Harlalka <Rabindra.Harlalka@nice.com>");
215
216 gst_element_class_add_pad_template (gstelement_class,
217 gst_static_pad_template_get (&src_template));
218 gst_element_class_add_pad_template (gstelement_class,
219 gst_static_pad_template_get (&sink_template));
220
221 GST_BASE_TRANSFORM_CLASS (klass)->transform =
222 GST_DEBUG_FUNCPTR (gst_aes_enc_transform);
223 GST_BASE_TRANSFORM_CLASS (klass)->prepare_output_buffer =
224 GST_DEBUG_FUNCPTR (gst_aes_enc_prepare_output_buffer);
225 GST_BASE_TRANSFORM_CLASS (klass)->start =
226 GST_DEBUG_FUNCPTR (gst_aes_enc_start);
227 GST_BASE_TRANSFORM_CLASS (klass)->sink_event =
228 GST_DEBUG_FUNCPTR (gst_aes_enc_sink_event);
229 GST_BASE_TRANSFORM_CLASS (klass)->stop = GST_DEBUG_FUNCPTR (gst_aes_enc_stop);
230 }
231
232 /* Initialize element
233 */
234 static void
gst_aes_enc_init(GstAesEnc * filter)235 gst_aes_enc_init (GstAesEnc * filter)
236 {
237 GST_INFO_OBJECT (filter, "Initializing plugin");
238 filter->cipher = GST_AES_DEFAULT_CIPHER_MODE;
239 filter->awaiting_first_buffer = TRUE;
240 filter->per_buffer_padding = GST_AES_PER_BUFFER_PADDING_DEFAULT;
241 g_mutex_init (&filter->encoder_lock);
242 }
243
244 static void
gst_aes_enc_finalize(GObject * object)245 gst_aes_enc_finalize (GObject * object)
246 {
247 GstAesEnc *filter = GST_AES_ENC (object);
248
249 g_mutex_clear (&filter->encoder_lock);
250 G_OBJECT_CLASS (gst_aes_enc_parent_class)->finalize (object);
251 }
252
253 static void
gst_aes_enc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)254 gst_aes_enc_set_property (GObject * object, guint prop_id,
255 const GValue * value, GParamSpec * pspec)
256 {
257 GstAesEnc *filter = GST_AES_ENC (object);
258
259 g_mutex_lock (&filter->encoder_lock);
260 /* no property may be set after first output buffer is prepared */
261 if (filter->locked_properties) {
262 GST_WARNING_OBJECT (filter,
263 "Properties cannot be set once buffers begin flowing in element. Ignored");
264 goto cleanup;
265 }
266 switch (prop_id) {
267 case PROP_CIPHER:
268 filter->cipher = g_value_get_enum (value);
269 filter->evp_cipher =
270 EVP_get_cipherbyname (gst_aes_cipher_enum_to_string (filter->cipher));
271 GST_DEBUG_OBJECT (filter, "cipher: %s",
272 gst_aes_cipher_enum_to_string (filter->cipher));
273 break;
274 case PROP_SERIALIZE_IV:
275 filter->serialize_iv = g_value_get_boolean (value);
276 GST_DEBUG_OBJECT (filter, "serialize iv: %s",
277 filter->serialize_iv ? "TRUE" : "FALSE");
278 break;
279 case PROP_PER_BUFFER_PADDING:
280 filter->per_buffer_padding = g_value_get_boolean (value);
281 GST_DEBUG_OBJECT (filter, "Per buffer padding: %s",
282 filter->per_buffer_padding ? "TRUE" : "FALSE");
283 break;
284 case PROP_KEY:
285 {
286 guint hex_len = gst_aes_hexstring2bytearray (GST_ELEMENT (filter),
287 g_value_get_string (value), filter->key);
288
289 if (!hex_len) {
290 GST_ERROR_OBJECT (filter, "invalid key");
291 goto cleanup;
292 }
293 GST_DEBUG_OBJECT (filter, "key: %s", g_value_get_string (value));
294 }
295 break;
296 case PROP_IV:
297 {
298 gchar iv_string[2 * GST_AES_BLOCK_SIZE + 1];
299 guint hex_len = gst_aes_hexstring2bytearray (GST_ELEMENT (filter),
300 g_value_get_string (value), filter->iv);
301
302 if (hex_len != GST_AES_BLOCK_SIZE) {
303 GST_ERROR_OBJECT (filter, "invalid initialization vector");
304 goto cleanup;
305 }
306 GST_DEBUG_OBJECT (filter, "iv: %s",
307 gst_aes_bytearray2hexstring (filter->iv, iv_string,
308 GST_AES_BLOCK_SIZE));
309 }
310 break;
311 default:
312 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
313 break;
314 }
315
316 cleanup:
317 g_mutex_unlock (&filter->encoder_lock);
318 }
319
320 static void
gst_aes_enc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)321 gst_aes_enc_get_property (GObject * object, guint prop_id,
322 GValue * value, GParamSpec * pspec)
323 {
324 GstAesEnc *filter = GST_AES_ENC (object);
325
326 switch (prop_id) {
327 case PROP_CIPHER:
328 g_value_set_enum (value, filter->cipher);
329 break;
330 case PROP_SERIALIZE_IV:
331 g_value_set_boolean (value, filter->serialize_iv);
332 break;
333 case PROP_PER_BUFFER_PADDING:
334 g_value_set_boolean (value, filter->per_buffer_padding);
335 break;
336 case PROP_KEY:
337 g_value_set_string (value, (gchar *) filter->key);
338 break;
339 case PROP_IV:
340 g_value_set_string (value, (gchar *) filter->iv);
341 break;
342 default:
343 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344 break;
345 }
346 }
347
348 static gboolean
gst_aes_enc_sink_event(GstBaseTransform * base,GstEvent * event)349 gst_aes_enc_sink_event (GstBaseTransform * base, GstEvent * event)
350 {
351 GstAesEnc *filter = GST_AES_ENC (base);
352 g_mutex_lock (&filter->encoder_lock);
353
354 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
355 GST_DEBUG_OBJECT (filter, "Received EOS on sink pad");
356 if (!filter->per_buffer_padding && !filter->awaiting_first_buffer) {
357 gint len;
358 GstBuffer *outbuf;
359 GstMapInfo outmap;
360
361 outbuf = gst_buffer_new_allocate (NULL, EVP_MAX_BLOCK_LENGTH, NULL);
362 if (outbuf == NULL) {
363 GST_DEBUG_OBJECT (filter,
364 "Failed to allocate a new buffer of length %d",
365 EVP_MAX_BLOCK_LENGTH);
366 goto buffer_fail;
367 }
368 if (!gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE)) {
369 GST_DEBUG_OBJECT (filter,
370 "gst_buffer_map on outbuf failed for final buffer.");
371 gst_buffer_unref (outbuf);
372 goto buffer_fail;
373 }
374 if (1 != EVP_CipherFinal_ex (filter->evp_ctx, outmap.data, &len)) {
375 GST_DEBUG_OBJECT (filter, "Could not finalize openssl encryption");
376 gst_buffer_unmap (outbuf, &outmap);
377 gst_buffer_unref (outbuf);
378 goto cipher_fail;
379 }
380 if (len == 0) {
381 GST_DEBUG_OBJECT (filter, "Not pushing final buffer as length is 0");
382 gst_buffer_unmap (outbuf, &outmap);
383 gst_buffer_unref (outbuf);
384 goto out;
385 }
386 GST_DEBUG_OBJECT (filter, "Pushing final buffer of length: %d", len);
387 gst_buffer_unmap (outbuf, &outmap);
388 gst_buffer_set_size (outbuf, len);
389 if (gst_pad_push (base->srcpad, outbuf) != GST_FLOW_OK) {
390 GST_DEBUG_OBJECT (filter, "Failed to push the final buffer");
391 goto push_fail;
392 }
393 } else {
394 GST_DEBUG_OBJECT (filter,
395 "Not pushing final buffer as we didn't have any input");
396 }
397 }
398
399 out:
400 g_mutex_unlock (&filter->encoder_lock);
401
402 return GST_BASE_TRANSFORM_CLASS (gst_aes_enc_parent_class)->sink_event (base,
403 event);
404
405 /* ERROR */
406 buffer_fail:
407 GST_ELEMENT_ERROR (filter, RESOURCE, FAILED, (NULL),
408 ("Failed to allocate or map buffer for writing"));
409 g_mutex_unlock (&filter->encoder_lock);
410
411 return FALSE;
412 cipher_fail:
413 GST_ELEMENT_ERROR (filter, STREAM, FAILED, ("Cipher finalization failed."),
414 ("Error while finalizing the stream"));
415 g_mutex_unlock (&filter->encoder_lock);
416
417 return FALSE;
418 push_fail:
419 GST_ELEMENT_ERROR (filter, CORE, PAD, (NULL),
420 ("Failed to push the final buffer"));
421 g_mutex_unlock (&filter->encoder_lock);
422
423 return FALSE;
424 }
425
426 /* GstBaseTransform vmethod implementations */
427 static GstFlowReturn
gst_aes_enc_transform(GstBaseTransform * base,GstBuffer * inbuf,GstBuffer * outbuf)428 gst_aes_enc_transform (GstBaseTransform * base,
429 GstBuffer * inbuf, GstBuffer * outbuf)
430 {
431 GstAesEnc *filter = GST_AES_ENC (base);
432 GstFlowReturn ret = GST_FLOW_ERROR;
433 GstMapInfo inmap, outmap;
434 guchar *plaintext;
435 gint plaintext_len;
436 guchar *ciphertext;
437 gint ciphertext_len;
438 gint out_len;
439
440 if (!gst_buffer_map (inbuf, &inmap, GST_MAP_READ)) {
441 GST_ELEMENT_ERROR (filter, RESOURCE, FAILED, (NULL),
442 ("Failed to map buffer for reading"));
443 goto cleanup;
444 }
445 if (!gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE)) {
446 gst_buffer_unmap (inbuf, &inmap);
447 GST_ELEMENT_ERROR (filter, RESOURCE, FAILED, (NULL),
448 ("Failed to map buffer for writing"));
449 goto cleanup;
450 }
451
452 /* ENCRYPTING */
453 plaintext = inmap.data;
454 plaintext_len = inmap.size;
455 if (filter->padding)
456 plaintext_len += filter->padding - GST_AES_BLOCK_SIZE;
457 ciphertext = outmap.data;
458 if (filter->awaiting_first_buffer) {
459 if (!EVP_CipherInit_ex (filter->evp_ctx, filter->evp_cipher, NULL,
460 filter->key, filter->iv, TRUE)) {
461 GST_ERROR_OBJECT (filter, "Could not initialize openssl cipher");
462 goto cleanup;
463 }
464 if (filter->serialize_iv) {
465 memcpy (ciphertext, filter->iv, GST_AES_BLOCK_SIZE);
466 ciphertext += GST_AES_BLOCK_SIZE;
467 }
468 }
469
470 /* encrypt unpadded buffer */
471 if (!EVP_CipherUpdate (filter->evp_ctx, ciphertext,
472 &ciphertext_len, plaintext, plaintext_len)) {
473 GST_ELEMENT_ERROR (filter, STREAM, FAILED, ("Cipher update failed."),
474 ("Error while updating openssl cipher"));
475 goto cleanup;
476 } else if (filter->padding) {
477 gint temp;
478 guint k;
479
480 /* PKCS7 padding */
481 memset (filter->padded_block, filter->padding, GST_AES_BLOCK_SIZE);
482 for (k = 0; k < GST_AES_BLOCK_SIZE - filter->padding; ++k)
483 filter->padded_block[k] = plaintext[plaintext_len + k];
484
485 /* encrypt padding buffer */
486 if (!EVP_CipherUpdate (filter->evp_ctx,
487 ciphertext + ciphertext_len, &temp,
488 filter->padded_block, GST_AES_BLOCK_SIZE)) {
489 GST_ELEMENT_ERROR (filter, STREAM, FAILED, ("Cipher update failed."),
490 ("Error while updating openssl cipher"));
491 goto cleanup;
492 } else {
493 g_assert (temp == GST_AES_BLOCK_SIZE);
494 ciphertext_len += GST_AES_BLOCK_SIZE;
495 plaintext_len += GST_AES_BLOCK_SIZE;
496 }
497 }
498 gst_buffer_unmap (inbuf, &inmap);
499 gst_buffer_unmap (outbuf, &outmap);
500
501 out_len = ciphertext_len + (filter->serialize_iv ? GST_AES_BLOCK_SIZE : 0);
502 gst_buffer_set_size (outbuf, out_len);
503 GST_LOG_OBJECT (filter,
504 "plaintext len: %d, ciphertext len: %d, padding: %d, output buffer length: %d",
505 plaintext_len, ciphertext_len, filter->padding, out_len);
506 ret = GST_FLOW_OK;
507
508 cleanup:
509 filter->awaiting_first_buffer = FALSE;
510
511 return ret;
512 }
513
514 static GstFlowReturn
gst_aes_enc_prepare_output_buffer(GstBaseTransform * base,GstBuffer * inbuf,GstBuffer ** outbuf)515 gst_aes_enc_prepare_output_buffer (GstBaseTransform * base,
516 GstBuffer * inbuf, GstBuffer ** outbuf)
517 {
518 GstAesEnc *filter = GST_AES_ENC (base);
519 GstBaseTransformClass *bclass = GST_BASE_TRANSFORM_GET_CLASS (base);
520 guint out_size = (guint) gst_buffer_get_size (inbuf);
521
522 g_mutex_lock (&filter->encoder_lock);
523 filter->locked_properties = TRUE;
524 if (filter->per_buffer_padding) {
525 /* pad to multiple of GST_AES_BLOCK_SIZE */
526 filter->padding =
527 GST_AES_BLOCK_SIZE - (out_size & (GST_AES_BLOCK_SIZE - 1));
528 out_size += filter->padding;
529 } else {
530 /* we need extra space at end of output buffer
531 * when we let OpenSSL handle PKCS7 padding */
532 out_size += GST_AES_BLOCK_SIZE;
533 }
534
535 /* add room for serialized IV at beginning of first output buffer */
536 if (filter->serialize_iv && filter->awaiting_first_buffer)
537 out_size += GST_AES_BLOCK_SIZE;
538 g_mutex_unlock (&filter->encoder_lock);
539
540 GST_LOG_OBJECT (filter,
541 "Input buffer size %d, output buffer size: %d. padding : %d",
542 (guint) gst_buffer_get_size (inbuf), out_size, filter->padding);
543 *outbuf = gst_buffer_new_allocate (NULL, out_size, NULL);
544 bclass->copy_metadata (base, inbuf, *outbuf);
545
546 return GST_FLOW_OK;
547 }
548
549 static gboolean
gst_aes_enc_start(GstBaseTransform * base)550 gst_aes_enc_start (GstBaseTransform * base)
551 {
552 GstAesEnc *filter = GST_AES_ENC (base);
553
554 GST_INFO_OBJECT (filter, "Starting");
555 if (!gst_aes_enc_openssl_init (filter)) {
556 GST_ERROR_OBJECT (filter, "OpenSSL initialization failed");
557 return FALSE;
558 }
559
560 GST_INFO_OBJECT (filter, "Start successful");
561
562 return TRUE;
563 }
564
565 static gboolean
gst_aes_enc_stop(GstBaseTransform * base)566 gst_aes_enc_stop (GstBaseTransform * base)
567 {
568 GstAesEnc *filter = GST_AES_ENC (base);
569
570 GST_INFO_OBJECT (filter, "Stopping");
571 EVP_CIPHER_CTX_free (filter->evp_ctx);
572
573 return TRUE;
574 }
575
576 /* AesEnc helper functions */
577 static gboolean
gst_aes_enc_openssl_init(GstAesEnc * filter)578 gst_aes_enc_openssl_init (GstAesEnc * filter)
579 {
580 GST_DEBUG_OBJECT (filter, "Initializing with %s",
581 OpenSSL_version (OPENSSL_VERSION));
582
583 filter->evp_cipher =
584 EVP_get_cipherbyname (gst_aes_cipher_enum_to_string (filter->cipher));
585 if (!filter->evp_cipher) {
586 GST_ERROR_OBJECT (filter, "Could not get cipher by name from openssl");
587 return FALSE;
588 }
589 if (!(filter->evp_ctx = EVP_CIPHER_CTX_new ()))
590 return FALSE;
591 GST_LOG_OBJECT (filter, "Initialization successful");
592
593 return TRUE;
594 }
595