Advertisement
Kaidul

qerqwwqe

Dec 22nd, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.51 KB | None | 0 0
  1. #include <jni.h>
  2. #include <stdio.h>
  3. #include <android/log.h>
  4. #include <android/asset_manager.h>
  5. #include <android/asset_manager_jni.h>
  6. #include <openssl/ssl.h>
  7. #include <openssl/asn1.h>
  8. #include <openssl/bio.h>
  9. #include <openssl/x509.h>
  10. #include <openssl/x509_vfy.h>
  11. #include <openssl/pem.h>
  12. #include <openssl/x509v3.h>
  13. #include <openssl/err.h>
  14. #include <openssl/conf.h>
  15. #include <string.h>
  16.  
  17. using std::string;
  18.  
  19. #define DEBUG_LOG_TAG "kaidul_SSL"
  20. #define SSL_ASSERT(X, ...) \
  21.     if(!(X)) { \
  22.         return 0; \
  23.     }
  24.  
  25. #define SSL_LOG(...) \
  26.     printf(__VA_ARGS__);
  27.  
  28. static int verify_certificate_hostname(X509 *cert, char *hostname) {
  29.   int                   extcount;
  30.   int           success = 0;
  31.   char                  name[256];
  32.   X509_NAME             *subj;
  33.   const char            *extstr;
  34.   CONF_VALUE            *nval;
  35.   X509_EXTENSION        *ext;
  36.   X509V3_EXT_METHOD     *meth;
  37.   STACK_OF(CONF_VALUE)  *val;
  38.   void          *ext_internal;
  39.  
  40.   if ((extcount = X509_get_ext_count(cert)) > 0) {
  41.     for (int i = 0; !success && i < extcount; i++) {
  42.       ext = X509_get_ext(cert, i);
  43.       extstr = OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(ext)));
  44.       if (!strcasecmp(extstr, "subjectAltName")) {
  45.         if (!(meth = X509V3_EXT_get(ext))) break;
  46.         const unsigned char *data = ext->value->data;
  47.     ext_internal = X509V3_EXT_d2i(ext);
  48.        
  49.     val = meth->i2v(meth, ext_internal, 0);
  50.         for (int j = 0;  j < sk_CONF_VALUE_num(val); j++) {
  51.           nval = sk_CONF_VALUE_value(val, j);
  52.           if (!strcasecmp(nval->name, "DNS") && !strcasecmp(nval->value, hostname)) {
  53.             success = 1;
  54.             break;
  55.           }
  56.         }
  57.       }
  58.     }
  59.   }
  60.    
  61.   if (!success && (subj = X509_get_subject_name(cert)) && X509_NAME_get_text_by_NID(subj, NID_commonName, name, sizeof(name)) > 0) {
  62.     name[sizeof(name) - 1] = '\0';
  63.     if (!strcasecmp(name, hostname)) success = 1;
  64.   }
  65.    
  66.   return success;
  67. }
  68.  
  69. static int certificate_verifier_callback(void *ctx, void *arg)
  70. {
  71.     SSL_LOG("Callback called for certificate verification.");
  72.  
  73.         OpenSSL_add_all_algorithms();
  74.     ERR_load_crypto_strings();
  75.    
  76.     char *hostname = "www.smartbabymonitor.ugrow.philips.com";
  77.         X509_STORE_CTX store_ctx = X509_STORE_CTX(*(X509_STORE_CTX*)ctx);
  78.     int rc = X509_verify_cert(&store_ctx);
  79.         X509 *cert = X509_STORE_CTX_get_current_cert(&store_ctx);
  80.         SSL_ASSERT(cert != NULL, "Server certificate invalid.");
  81.     SSL_LOG("Server Certificate valid.");
  82.    
  83.     int err = X509_STORE_CTX_get_error(&store_ctx);
  84.     switch(err) {
  85.         case X509_V_ERR_CERT_NOT_YET_VALID:
  86.         case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  87.                     SSL_ASSERT(false, "Certificate is not valid yet.");
  88.                     break;
  89.             case X509_V_ERR_CERT_HAS_EXPIRED:
  90.             case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  91.                     SSL_ASSERT(false, "Certificate is expired.");
  92.                     break;
  93.         case X509_V_ERR_CRL_NOT_YET_VALID:
  94.             SSL_ASSERT(false, "Certificate Revocation List is not yet valid.");
  95.                     break;
  96.             case X509_V_ERR_CRL_HAS_EXPIRED:
  97.             SSL_ASSERT(false, "Certificate Revocation List is expired.");
  98.                     break;
  99.         default:
  100.             break;
  101.     }
  102.     SSL_LOG("Certificate is up-to-date.");
  103.     SSL_LOG("CRL list checking success."); 
  104.  
  105.     /* Server Certificate hostnam+e verification */
  106.     SSL_ASSERT(verify_certificate_hostname(cert, hostname) == 1, "Hostname verification failed.");
  107.     SSL_LOG("Hostname verification success.");
  108.            
  109.        
  110.         const char *path = "/home/nayeem/Desktop/philips_trusted_cert.pem";
  111.         FILE* fp = fopen(path, "r");
  112.         if(!fp) {
  113.         SAFE_LOG("Unable to open local certificate.");
  114.         __android_log_print(ANDROID_LOG_DEBUG, DEBUG_LOG_TAG, "\nUnable to open local certificate.\n");
  115.             return 0;
  116.         }
  117.         fseek(fp, 0, SEEK_END);
  118.         size_t size = ftell(fp);
  119.         char* data = new char[size];
  120.         rewind(fp);
  121.         fread(data, sizeof(char), size, fp);
  122.         fclose(fp);
  123.  
  124.         BIO *bio = BIO_new(BIO_s_mem());
  125.         BIO_puts(bio, data);
  126.         delete[] data;
  127.  
  128.         X509 *cert2 = PEM_read_bio_X509(bio, NULL, NULL, NULL);
  129.         BIO_free(bio);
  130.        
  131.         if(!cert2) {
  132.                 X509_free(cert);
  133.                 return 0;
  134.         }
  135.  
  136.         EVP_PKEY *pkey = X509_get_pubkey(cert2);
  137.         int result = X509_verify(cert, pkey);
  138.        
  139.     if(result > 0) {
  140.     } else {
  141.     }
  142.  
  143.     EVP_PKEY_free(pkey);
  144.         X509_free(cert2);
  145.         X509_free(cert);
  146.          
  147.     return result == 1;
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement