src/x509.c

changeset 17
4e3da35cc9ab
parent 16
0cefcdd5b635
child 18
2c6fbfe07883
equal deleted inserted replaced
16:0cefcdd5b635 17:4e3da35cc9ab
168 lua_pushnil(L); 168 lua_pushnil(L);
169 BIO_free(bio); 169 BIO_free(bio);
170 return 1; 170 return 1;
171 } 171 }
172 172
173 const char* hex_tab = "0123456789abcdef";
174 void to_hex(const char* in, int length, char* out) {
175 int i;
176 for (i = 0; i < length; i++) {
177 out[i*2] = hex_tab[(in[i] >> 4) & 0xF];
178 out[i*2+1] = hex_tab[(in[i]) & 0xF];
179 }
180 }
181
182 int meth_digest(lua_State* L)
183 {
184 X509 *cert;
185 unsigned int bytes;
186 unsigned char buffer[EVP_MAX_MD_SIZE];
187 char hex_buffer[EVP_MAX_MD_SIZE*2];
188 const EVP_MD *digest;
189 cert = luasec_to_x509(L, 1);
190 if(lua_gettop(L) < 2 || strcmp(luaL_checkstring(L, 1), "sha1") == 0)
191 {
192 digest = EVP_sha1();
193 }
194 else
195 {
196 lua_pushnil(L);
197 lua_pushstring(L, "digest algorithm not supported");
198 return 2;
199 }
200 if(!X509_digest(cert, digest, buffer, &bytes))
201 {
202 lua_pushnil(L);
203 lua_pushstring(L, "out of memory");
204 return 2;
205 }
206 to_hex((char*)buffer, bytes, hex_buffer);
207 lua_pushlstring(L, hex_buffer, bytes*2);
208 return 1;
209 }
210
173 /** 211 /**
174 * Certificate metamethods 212 * Certificate metamethods
175 */ 213 */
176 static luaL_Reg meta[] = { 214 static luaL_Reg meta[] = {
177 {"decode", meth_decode}, 215 {"decode", meth_decode},
178 {"pem", meth_pem}, 216 {"pem", meth_pem},
217 {"digest", meth_digest},
179 {NULL, NULL} 218 {NULL, NULL}
180 }; 219 };
181 220
182 LUASEC_API int luaopen_ssl_x509(lua_State *L) 221 LUASEC_API int luaopen_ssl_x509(lua_State *L)
183 { 222 {

mercurial