Sign Up
Log In
Log In
or
Sign Up
Places
All Projects
Status Monitor
Collapse sidebar
home:ruy_takata
apiSSL
apiSSL.cpp
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File apiSSL.cpp of Package apiSSL
/* * Copyright (C) 2012 -2013 Free Software Foundation, Inc. * Author: Serpro * * This file is part of apiSSL. * * apiSSL is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * apiSSL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include <openssl/bio.h> #include <openssl/x509.h> #include <openssl/pem.h> #include <openssl/ssl.h> #include <iostream> #include <string.h> #include <openssl/err.h> #include <openssl/rsa.h> #include <stdexcept> #include "apiSSL.h" apiSSL::apiSSL() { } apiSSL::apiSSL(const apiSSL& orig) { } apiSSL::~apiSSL() { } /** * Recebe um array de char e converte em base64. Armazena o valor resultante na variável cryptPassword que deve ser recuperada com o método getCryptPassword(). * @param input Array de char que será convertido em base64. * @param length tamanho do array de char que será convertido em base64. * @exception runtime_error */ void apiSSL::base64(unsigned char *input, int length) { BIO *bmem, *b64; BUF_MEM *bptr; char * buff; b64 = BIO_new(BIO_f_base64()); if (NULL == b64) { this->error = "apiSSL::base64(): "; this->error += "Erro ao alocar um novo BIO b64."; throw runtime_error(this->error); } bmem = BIO_new(BIO_s_mem()); if (NULL == bmem) { this->error = "apiSSL::base64(): "; this->error += "Erro ao alocar um novo BIO bmem."; BIO_free_all(b64); throw runtime_error(this->error); } b64 = BIO_push(b64, bmem); if (1 > BIO_write(b64, input, length)) { this->error = "apiSSL::base64(): "; this->error += "Possível erro a copiar buffer"; } if (1 != BIO_flush(b64)) { this->error = "apiSSL::base64(): "; this->error += "Erro ao efetuar BIO_flush()"; BIO_free_all(b64); throw runtime_error(this->error); } BIO_get_mem_ptr(b64, &bptr); try { buff = new char[bptr->length]; } catch (bad_alloc & ba) { this->error = "apiSSL::base64(): "; this->error += "Erro ao alocar memória: "; this->error += ba.what(); this->error += "."; BIO_free_all(b64); delete[] buff; throw runtime_error(this->error); } memcpy(buff, bptr->data, bptr->length - 1); buff[bptr->length - 1] = 0; BIO_free_all(b64); this->cryptPassword = buff; delete[] buff; } /** * Criptografa a string passada utilizando a chave pública presente no certificado. Depois de criptografar chama o método base64(). * @param pem Certificado no formato PEM lido do diretório. * @param pass String que será criptografada. * @exception runtime_error */ void apiSSL::cifrar(string pem, string pass) { SSL_load_error_strings(); X509 *cert; RSA *rsa; BIO *mem; unsigned char * to; int maxSize; int cryptedSize; mem = BIO_new(BIO_s_mem()); if (mem == NULL) { this->error = "apiSSL::cifrar(): "; this->error = "Erro ao criar um novo BIO."; throw runtime_error(this->error); } if (BIO_puts(mem, pem.c_str()) < 0) { this->error = "Erro ao copiar o certificado para o buffer"; BIO_free_all(mem); throw runtime_error(this->error); } cert = PEM_read_bio_X509(mem, NULL, 0, NULL); if (cert == NULL) { this->error = "apiSSL::cifrar(): "; this->error += "Erro ao ler o PEM"; BIO_free_all(mem); throw runtime_error(this->error); } BIO_free_all(mem); rsa = X509_get_pubkey(cert)->pkey.rsa; X509_free(cert); if (rsa == NULL) { this->error = "apiSSL::cifrar(): "; this->error += "Erro: "; this->error += ERR_func_error_string(ERR_get_error()); this->error += "."; throw runtime_error(this->error); } maxSize = RSA_size(rsa); try { to = new unsigned char[maxSize]; } catch (bad_alloc & ba) { this->error = "apiSSL::cifrar(): "; this->error += "Erro ao alocar memória: "; this->error += ba.what(); this->error += "."; RSA_free(rsa); throw runtime_error(this->error); } if (pass.size() > RSA_size(rsa) - 11) { pass.resize(RSA_size(rsa) - 11); } cryptedSize = RSA_public_encrypt(pass.size(), (unsigned char *) pass.c_str(), to, rsa, RSA_PKCS1_PADDING); if (cryptedSize == -1) { this->error = "apiSSL::cifrar(): "; this->error += "Erro: "; this->error += ERR_func_error_string(ERR_get_error()); this->error += "."; RSA_free(rsa); throw runtime_error(this->error); } RSA_free(rsa); try { base64(to, cryptedSize); } catch (exception &e) { this->error = "apiSSL::cifrar(): "; this->error += "Erro ao gerar senha em base64: "; this->error += e.what(); delete[] to; throw runtime_error(this->error); } delete[] to; } /** * Retorna a senha criptografada e convertida em base64. * @return String com a senha criptografada e convertida em formato base64. */ string apiSSL::getCryptPassword() { return this->cryptPassword; } /** * Retorna a descrição do erro ocorrido. * @return */ string apiSSL::getError() { return this->error; }
Locations
Projects
Search
Status Monitor
Help
OpenBuildService.org
Documentation
API Documentation
Code of Conduct
Contact
Support
@OBShq
Terms
openSUSE Build Service is sponsored by
The Open Build Service is an
openSUSE project
.
Sign Up
Log In
Places
Places
All Projects
Status Monitor