Kannel: Open Source WAP and SMS gateway  svn-r5335
pki.c
Go to the documentation of this file.
1 /* ====================================================================
2  * The Kannel Software License, Version 1.0
3  *
4  * Copyright (c) 2001-2018 Kannel Group
5  * Copyright (c) 1998-2001 WapIT Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Kannel Group (http://www.kannel.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Kannel" and "Kannel Group" must not be used to
28  * endorse or promote products derived from this software without
29  * prior written permission. For written permission, please
30  * contact org@kannel.org.
31  *
32  * 5. Products derived from this software may not be called "Kannel",
33  * nor may "Kannel" appear in their name, without prior written
34  * permission of the Kannel Group.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS
40  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
41  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
42  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
43  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
44  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
45  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
46  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Kannel Group. For more information on
51  * the Kannel Group, please see <http://www.kannel.org/>.
52  *
53  * Portions of this software are based upon software originally written at
54  * WapIT Ltd., Helsinki, Finland for the Kannel project.
55  */
56 
57 /*
58  * pki.c: PKI and certificate handling routines
59  *
60  */
61 
62 #include <stdio.h>
63 
64 #include "gwlib/gwlib.h"
65 
66 #if (HAVE_WTLS_OPENSSL)
67 
68 #include <openssl/rsa.h>
69 #include <openssl/evp.h>
70 #include <openssl/objects.h>
71 #include <openssl/x509.h>
72 #include <openssl/err.h>
73 #include <openssl/pem.h>
74 #include <openssl/ssl.h>
75 
76 #include "pki.h"
77 
78 void pki_init(void)
79 {
80  OpenSSL_add_all_algorithms();
81  ERR_load_crypto_strings();
82 }
83 
84 void pki_shutdown(void)
85 {
86  EVP_cleanup();
87 }
88 
89 
90 void get_cert_from_file(Octstr *s, X509 **x509)
91 {
92  char *filename;
93 
94  /* Check errors!!!! */
95  FILE* fp;
96 
97  /* Open the file specified by "s" */
99  fp = fopen(filename,"r");
100  if (fp == NULL) warning(0,"Can't read certificate %s", filename);
101 
102  /* Load up that there certificate */
103  *x509 = PEM_read_X509(fp,NULL,NULL,NULL);
104 
105  /* Close the file specified by "s" */
106  fclose(fp);
107 
108  if (x509 == NULL) {
109  ERR_print_errors_fp (stderr);
110  }
111 }
112 
113 void get_privkey_from_file(Octstr* s, RSA** priv_key, Octstr* passwd)
114 {
115  char *password;
116  char *filename;
117 
118  /* Check errors!!!! */
119  FILE* fp;
120 
122  password = passwd != NULL ? octstr_get_cstr(passwd) : NULL;
123 
124  /* Open the file specified by "s" */
125  fp = fopen(filename,"r");
126  if (fp == NULL) warning(0,"Can't read private key %s", filename);
127 
128  /* Load up that there certificate */
129  *priv_key = PEM_read_RSAPrivateKey(fp,NULL,NULL,password);
130 
131  /* Close the file specified by "s" */
132  fclose(fp);
133 
134  if (priv_key == NULL) {
135  ERR_print_errors_fp (stderr);
136  }
137 }
138 
139 void dump_cert(X509* x509)
140 {
141 
142 }
143 
144 
145 void dump_privkey(RSA* priv_key)
146 {
147 }
148 
149 #endif
void pki_shutdown(void)
void pki_init(void)
void get_cert_from_file(Octstr *s, X509 **x509)
#define octstr_get_cstr(ostr)
Definition: octstr.h:233
void get_privkey_from_file(Octstr *s, RSA **priv_key, Octstr *password)
unsigned char * password
Definition: test_cimd2.c:100
void dump_cert(X509 *x509)
void warning(int err, const char *fmt,...)
Definition: log.c:660
char filename[FILENAME_MAX+1]
Definition: log.c:171
Definition: octstr.c:118
void dump_privkey(RSA *priv_key)
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.