|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: ?Description |
|
15 * |
|
16 */ |
|
17 |
|
18 #ifndef NATIVESECURECONNECTION_H |
|
19 #define NATIVESECURECONNECTION_H |
|
20 |
|
21 #include <logger.h> |
|
22 #include <sys/stat.h> |
|
23 #include <sys/types.h> |
|
24 #include <sys/socket.h> |
|
25 #include <netinet/in.h> |
|
26 #include <netdb.h> |
|
27 #include <fcntl.h> |
|
28 #include <unistd.h> |
|
29 #include <openssl/x509v3.h> |
|
30 #include <openssl/bio.h> |
|
31 #include <openssl/ssl.h> |
|
32 #include <openssl/err.h> |
|
33 #include <openssl/evp.h> |
|
34 #include <openssl/dsa.h> // for DSA functions |
|
35 #include <openssl/crypto.h> |
|
36 #include <openssl/evp.h> |
|
37 #include <time.h> |
|
38 |
|
39 #include "nativesocketconnection.h" |
|
40 #include "javaosheaders.h" |
|
41 |
|
42 namespace java |
|
43 { |
|
44 |
|
45 /** |
|
46 * This is the base class for Native Peer of secure sockets. |
|
47 * It derives from NativeSocketConnection native class, to reuse the socket |
|
48 * methods like getsocketoption, setsocketoption etc. |
|
49 * SecureConnectionImpl in java will make calls to this class to create a carry |
|
50 * out the all secure socket operations. |
|
51 * StreamConnectionBase in java will also call the readBytes() and writeBytes() |
|
52 * methods of this class to get/send data from the native secure socket buffer. |
|
53 * NativeStreamBase will have stored a handle to the Object and uses it to call |
|
54 * these methods. |
|
55 */ |
|
56 |
|
57 class NativeSecureConnection: public NativeSocketConnection |
|
58 { |
|
59 public: |
|
60 |
|
61 NativeSecureConnection(const char* aName, int aMode, const char* aHost, |
|
62 int aPort); |
|
63 |
|
64 //Methods inherited from NativeStreamBase |
|
65 |
|
66 /** |
|
67 * ReadBytes is called by StreamConnectionBase when the buffer at input |
|
68 * stream buffer runs out of data. |
|
69 * @param[in] aJavaBuffer: Byte array to which the data read is copied into |
|
70 * @return the number of bytes read. If error, then the |
|
71 * value of Ssl_get_error should be negated and returned. (return -error) |
|
72 */ |
|
73 OS_IMPORT int readBytes(JNIEnv& aJni, jbyteArray aJavaBuffer); |
|
74 |
|
75 /** |
|
76 * writeBytes is called by StreamConnectionBase when the output stream |
|
77 * buffer in java overflows |
|
78 * It is also called if flush() is called at the Java side. |
|
79 * @param[in] aJavaBuffer: A byte array of length aLength |
|
80 * @return the number of bytes written. If error, then the |
|
81 * value of Ssl_get_error should be negated and returned. (return -error) |
|
82 */ |
|
83 OS_IMPORT int writeBytes(JNIEnv& aJni, jbyteArray aJavaBuffer, int aOffset, |
|
84 int aLength); |
|
85 |
|
86 /** |
|
87 * StopReading is called by StreamConnectionBase once InputStream is closed |
|
88 * from the Application. |
|
89 */ |
|
90 OS_IMPORT void stopReading(); |
|
91 |
|
92 /** |
|
93 * StopWriting is called by StreamConnectionBase OutputStream is closed |
|
94 * from the Application. |
|
95 */ |
|
96 OS_IMPORT void stopWriting(); |
|
97 |
|
98 //Internal methods |
|
99 |
|
100 |
|
101 /** |
|
102 * InitialiseSslConnection is internal function |
|
103 * initializes Ssl library |
|
104 * creates Ssl context object and Ssl obejct |
|
105 */ |
|
106 void initialiseSslConnection(); |
|
107 |
|
108 /** |
|
109 * SecureHandshake is internal function - performs securehandshake |
|
110 * returns the open C API SSL_connect() return value |
|
111 * If error, then the value os Ssl_get_error should be negated and returned |
|
112 * (return -error) |
|
113 */ |
|
114 int secureHandshake(); |
|
115 |
|
116 /** |
|
117 * doHandshake is internal function to open a socket and secure it |
|
118 * returns the open C API SSL_connect() return value |
|
119 * If error, then the value os Ssl_get_error should be negated and returned |
|
120 * (return -error) |
|
121 */ |
|
122 int doHandshake(int aSocket, int aType, int aApn, int *err1, int * err2); |
|
123 |
|
124 // security info methods |
|
125 void getCertificateInformation(X509 *); |
|
126 |
|
127 OS_IMPORT char ** getSecurityInfo(); |
|
128 |
|
129 /** |
|
130 * securesocketWrite is internal function called by writeBytes() method. |
|
131 * @param[in] aWriteBuf: A character array of length len holding the data |
|
132 * to be written. |
|
133 * @return the number of bytes written. If error, then the |
|
134 * value of Ssl_get_error should be negated and returned. (return -errror) |
|
135 */ |
|
136 int secureSocketWrite(char *aWriteBuf, int aLen); |
|
137 |
|
138 /** |
|
139 * socketRead is internal function called by readBytes() method. |
|
140 * @param[in] aReadBuf: A character array of length len. |
|
141 * @return the number of bytes read. If error, then the |
|
142 * value of Ssl_get_error should be negated and returned. (return -error) |
|
143 */ |
|
144 int secureSocketRead(char *aReadBuf, int aLen); |
|
145 |
|
146 /** |
|
147 * SecureSocketClose is called by SocketConnectionImpl to close the socket |
|
148 * connection. |
|
149 * @return 0 on successful completion. If error, then the |
|
150 * value of errno should be negated and returned. (return -errno) |
|
151 */ |
|
152 int secureSocketClose(); |
|
153 |
|
154 X509* getCertificate(); |
|
155 |
|
156 ~NativeSecureConnection(); |
|
157 |
|
158 private: |
|
159 char* mName; |
|
160 char* mHost; |
|
161 char **mResult; |
|
162 int mMode; |
|
163 int mPort; |
|
164 BIO * mBio; |
|
165 SSL * mSslObj; |
|
166 SSL_CTX * mCtxObj; |
|
167 char* mSecureSocketBuffer; |
|
168 bool mIsSecureConnected; |
|
169 int mBytesRead; |
|
170 |
|
171 }; |
|
172 |
|
173 } |
|
174 #endif // NATIVESECURECONNECTION_H |