Tizen 2.1 base
[framework/web/wrt-commons.git] / modules / vcore / src / vcore / OCSPUtil.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @author      Tomasz Morawski(t.morawski@samsung.com)
18  * @version     0.1
19  * @brief
20  */
21
22 #include <openssl/ocsp.h>
23
24 /*
25  * This function is needed to fix "Invalid conversion from void* to unsigned char*"
26  * C++ compiler error during calling i2d_OCSP_REQUEST_bio macro
27  */
28 int convertToBuffer(OCSP_REQUEST *req, char **buf, int *size) {
29     BIO *req_mem_bio;
30     BUF_MEM req_bmem;
31
32     /*
33      * size and membuffer for request
34      */
35     *size = i2d_OCSP_REQUEST(req, NULL);
36     *buf = (char*) malloc(*size);
37
38     if (!*buf)
39         return 0;
40
41     /* copy request into buffer */
42     req_bmem.length = 0;
43     req_bmem.data = *buf;
44     req_bmem.max = *size;
45
46     /*
47      * create a new buffer using openssl
48      */
49     req_mem_bio = BIO_new(BIO_s_mem());
50
51     if (!req_mem_bio) {
52         /*
53          * creation failed, return
54          */
55         free(*buf);
56         *buf = NULL;
57         return 0;
58     }
59
60     BIO_set_mem_buf(req_mem_bio, &req_bmem, BIO_NOCLOSE);
61
62     /*
63      * prepare request
64      */
65     if (i2d_OCSP_REQUEST_bio(req_mem_bio, req) <= 0) {
66         free(*buf);
67         *buf = NULL;
68         BIO_free_all(req_mem_bio);
69         return 0;
70     }
71
72     /*
73      *  check consistency
74      */
75     if (*size != ((int)req_bmem.length) || req_bmem.length != req_bmem.max)
76     {
77         free(*buf);
78         *buf = NULL;
79         BIO_free_all(req_mem_bio);
80         return 0;
81     }
82
83     /*
84      * free all reserved memory
85      */
86     BIO_free_all(req_mem_bio);
87
88     /*
89      * and return success
90      */
91     return 1;
92 }