LCOV - code coverage report
Current view: top level - src/hed/libs/credential - testcertinfo.cpp (source / functions) Hit Total Coverage
Test: ARC cppunit test coverage for "2.0.0" Lines: 0 60 0.0 %
Date: 2012-04-25 Functions: 0 4 0.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : #ifdef HAVE_CONFIG_H
       2                 :            : #include <config.h>
       3                 :            : #endif
       4                 :            : 
       5                 :            : #include <stdlib.h>
       6                 :            : #include <stdexcept>
       7                 :            : #include <iostream>
       8                 :            : #include <string>
       9                 :            : #include <openssl/asn1.h>
      10                 :            : #include <openssl/pem.h>
      11                 :            : #include <openssl/x509.h>
      12                 :            : #include <openssl/x509v3.h>
      13                 :            : #include <openssl/pkcs12.h>
      14                 :            : #include <openssl/err.h>
      15                 :            : 
      16                 :            : #include "CertUtil.h"
      17                 :            : #include "Credential.h"
      18                 :            : 
      19                 :          0 :   X509_EXTENSION* CreateExtension(std::string& name, std::string& data, bool crit) {
      20                 :          0 :     X509_EXTENSION*   ext = NULL;
      21                 :          0 :     ASN1_OBJECT*      ext_obj = NULL;
      22                 :          0 :     ASN1_OCTET_STRING*  ext_oct = NULL;
      23                 :            : 
      24                 :          0 :     if(!(ext_obj = OBJ_nid2obj(OBJ_txt2nid((char *)(name.c_str()))))) {
      25                 :          0 :       std::cerr<<"Can not convert string into ASN1_OBJECT"<<std::endl;
      26                 :          0 :       return NULL;
      27                 :            :     }
      28                 :            : 
      29                 :          0 :     ext_oct = ASN1_OCTET_STRING_new();
      30                 :            : 
      31                 :          0 :     ext_oct->data = (unsigned char*) malloc(data.size());
      32                 :          0 :     memcpy(ext_oct->data, data.c_str(), data.size());
      33                 :          0 :     ext_oct->length = data.size();
      34                 :            : 
      35                 :          0 :     if (!(ext = X509_EXTENSION_create_by_OBJ(NULL, ext_obj, crit, ext_oct))) {
      36                 :          0 :       std::cerr<<"Can not create extension for proxy certificate"<<std::endl;
      37                 :          0 :       if(ext_oct) ASN1_OCTET_STRING_free(ext_oct);
      38                 :          0 :       if(ext_obj) ASN1_OBJECT_free(ext_obj);
      39                 :          0 :       return NULL;
      40                 :            :     }
      41                 :            : 
      42                 :          0 :     ext_oct = NULL;
      43                 :          0 :     return ext;
      44                 :            :   }
      45                 :            : 
      46                 :            : 
      47                 :          0 : int main(void) {
      48                 :            :    BIO* certbio;
      49                 :            :    FILE* file;
      50                 :          0 :    certbio = BIO_new(BIO_s_file());
      51                 :          0 :    file = fopen("./proxy1.pem", "r");
      52                 :          0 :    BIO_set_fp(certbio, file, BIO_NOCLOSE);
      53                 :            :    int res;
      54                 :            : 
      55                 :            :    X509* cert;
      56                 :          0 :    if(!(cert = PEM_read_bio_X509(certbio, NULL, NULL, NULL))) {
      57                 :          0 :       std::cerr<<"PEM_read_bio_X509 failed"<<std::endl;
      58                 :            :     }
      59                 :            : 
      60                 :            :     //if(!(d2i_X509_REQ_bio(reqbio, &req_))) {
      61                 :            :     //  credentialLogger.msg(ERROR, "Can't convert X509_REQ struct from DER encoded to internal form");
      62                 :            :     //  LogError(); return false;
      63                 :            :     //}
      64                 :            : 
      65                 :          0 :    Arc::Credential::InitProxyCertInfo();
      66                 :            : 
      67                 :          0 :    X509_EXTENSION* ext = NULL;
      68                 :            :    int certinfo_v3_NID, certinfo_v4_NID;
      69                 :            : 
      70                 :          0 :    ArcCredential::PROXYCERTINFO * cert_info = NULL;
      71                 :            :    //Get the PROXYCERTINFO from cert' extension
      72                 :          0 :    certinfo_v3_NID = OBJ_sn2nid("PROXYCERTINFO_V3");
      73                 :          0 :    certinfo_v4_NID = OBJ_sn2nid("PROXYCERTINFO_V4");
      74                 :            : 
      75                 :          0 :    res = X509_get_ext_by_NID(cert, certinfo_v3_NID, -1);
      76                 :          0 :    if (res == -1) X509_get_ext_by_NID(cert, certinfo_v4_NID, -1);
      77                 :            : 
      78                 :          0 :    if (res != -1) ext = X509_get_ext(cert,res);
      79                 :            :    
      80                 :          0 :    if (ext) cert_info = (ArcCredential::PROXYCERTINFO*) X509V3_EXT_d2i(ext);
      81                 :            : 
      82                 :            :    //X509V3_EXT_METHOD*  ext_method = X509V3_EXT_get_nid(certinfo_v3_NID);
      83                 :            :    //unsigned char* data = ext->value->data;
      84                 :            :    //cert_info = (ArcCredential::PROXYCERTINFO*)ext_method->d2i(NULL, (unsigned char **) &data, ext->value->length);
      85                 :            : 
      86                 :          0 :    if (cert_info == NULL) std::cerr<<"1. Can not convert DER encode PROXYCERTINFO extension to internal format"<<std::endl; 
      87                 :            : 
      88                 :          0 :    FILE* fp = fopen("./proxycertinfo1", "a");
      89                 :          0 :    PROXYCERTINFO_print_fp(fp, cert_info);
      90                 :            : 
      91                 :          0 :    int l = PROXYCERTINFO_get_path_length(cert_info);
      92                 :          0 :    std::cout<<"Path length: "<<l<<std::endl;
      93                 :            : 
      94                 :          0 :    const X509V3_EXT_METHOD* ext_method1 = X509V3_EXT_get_nid(certinfo_v3_NID);
      95                 :          0 :    int length = ext_method1->i2d(cert_info, NULL);
      96                 :          0 :    std::cout<<"Length of proxy cert info: "<<length<<std::endl;
      97                 :          0 :    unsigned char* data1 = NULL;
      98                 :          0 :    data1 = (unsigned char*) malloc(length);
      99                 :            : 
     100                 :            :    unsigned char* derdata;
     101                 :          0 :    derdata = data1;
     102                 :          0 :    length = ext_method1->i2d(cert_info, &derdata);
     103                 :          0 :    std::cout<<"Length of proxy cert info: "<<length<<" Data: "; for(int j =0; j< length; j++)std::cout<<data1[j];  std::cout<<std::endl;
     104                 :            :  
     105                 :            : 
     106                 :          0 :    std::cout<<"Original cert info: ";
     107                 :          0 :    for(int i = 0; i<length; i++) std::cout<<data1[i]; std::cout<<std::endl;
     108                 :          0 :    std::string ext_data((char*)data1, length); free(data1);
     109                 :          0 :    std::cout<<"Proxy cert info:" <<ext_data<<std::endl;
     110                 :          0 :    std::string cert_sn = "PROXYCERTINFO_V3";
     111                 :          0 :    X509_EXTENSION* ext2 = CreateExtension(cert_sn, ext_data, 1);
     112                 :            :   
     113                 :            : 
     114                 :            :    //ASN1_OCTET_STRING* ext_data = ASN1_OCTET_STRING_new();
     115                 :            :    //if(!ASN1_OCTET_STRING_set(ext_data, data1, length)) std::cerr<<"Error when set ext data"<<std::endl;
     116                 :            :    //free(data1);
     117                 :            :    //X509_EXTENSION* ext2 = X509_EXTENSION_create_by_NID(NULL, certinfo_v3_NID, 1, ext_data);
     118                 :            :    //ASN1_OCTET_STRING_free(ext_data);
     119                 :            : 
     120                 :            : 
     121                 :            :    ArcCredential::PROXYCERTINFO * cert_info2;
     122                 :          0 :    const X509V3_EXT_METHOD* ext_method2 = X509V3_EXT_get_nid(certinfo_v3_NID);
     123                 :          0 :    unsigned char* data2 = ext2->value->data;
     124                 :            : #if(OPENSSL_VERSION_NUMBER >= 0x0090800fL)
     125                 :          0 :    cert_info2 = (ArcCredential::PROXYCERTINFO*)ext_method2->d2i(NULL, (const unsigned char**) &data2, ext2->value->length);
     126                 :            : #else 
     127                 :            :    cert_info2 = (ArcCredential::PROXYCERTINFO*)ext_method2->d2i(NULL, (unsigned char**) &data2, ext2->value->length);
     128                 :            : #endif
     129                 :            :    //cert_info2 = (ArcCredential::PROXYCERTINFO*)X509V3_EXT_d2i(ext2);
     130                 :            :    
     131                 :          0 :    if (cert_info2 == NULL) std::cerr<<"2. Can not convert DER encode PROXYCERTINFO extension to internal format"<<std::endl;
     132                 :            : 
     133                 :            : 
     134                 :          0 :    FILE* fp1 = fopen("./proxycertinfo3", "a");
     135                 :          0 :    PROXYCERTINFO_print_fp(fp1, cert_info2);
     136                 :            : 
     137                 :            : 
     138                 :          0 : }
     139                 :          0 : 

Generated by: LCOV version 1.9