1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| void CtestDlg::OnBnClickedSelCert(){ unsigned char buf[4096]=""; CFileDialog dlg(TRUE,".der",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"文本文件(*.der)|*.der|所有文件(*.*)|*.*||"); if(dlg.DoModal()==IDOK){ CString strPath=dlg.GetPathName(); FILE* fp=fopen((LPSTR)(LPCSTR)strPath,"rb"); if(!fp) return; int nSize=fread(buf,1,4096,fp); AnsX509(buf,nSize); m_strCert=gstr; UpdateData(FALSE); }; }; void AnsX509(unsigned char* usrCertificate,unsigned long usrCertificateLen){ X509* x509Cert=NULL; unsigned char* pTmp=NULL; X509_NAME* issuer=NULL; X509_NAME* subject=NULL; int i,entriesNum; X509_NAME_ENTRY* name_entry; ASN1_INTEGER* Serial=NULL; long Nid; ASN1_TIME* time; EVP_PKEY* pubKey; long Version; unsigned char derpubkey[1024]; int derpubkeyLen; unsigned char msginfo[1024]; int msginfoLen; unsigned short* pUtf8=NULL; int nUtf8,rv; char szSign[256]; ULONG ulen=256; char szTmp[256]=""; pTmp=usrCertificate; x509Cert=d2i_X509(NULL,(const unsigned char**)&pTmp,usrCertificatelen); if(x509Cert==NULL) return; Version=X509_get_version(x509Cert); myprintf("X509 Version:V%ld\r\n",Version+1); Serial=X509_get_serialNumber(x509Cert); for(i=0;i<Serial->length;i++) myprintf("%02x",Serial->data[i]); myprintf("\r\n"); if(-1==get_SignatureAlgOid(x509Cert,szSign,&ulen)) return; myprintf("%s\r\n",szSign); issuer=X509_get_issuer_name(x509Cert); entriesNum=sk_X509_NAME_ENTRY_num(issuer->entries); for(i=0;i<entriesNum;i++){ name_entry=sk_X509_NAME_ENTRY_value(issuer->entries,i); Nid=OBJ_obj2nid(name_entry->object); if(name_entry->value->type==V_ASN1_UTF8STRING){ nUtf8=2*name_entry->value->length; pUtf8=(unsigned short*)malloc(nUtf8); memset(pUtf8,0,nUtf8); rv=MultiByteToWideChar(CP_UTF8,0,(char*)name_entry->value->data,name_entry->value->length,(LPWSTR)pUtf8,nUtf8); rv=WideCharToMultiByte(CP_ACP,0,(LPCWSTR)pUtf8,rv,(char*)msginfo,nUtf8,NULL,NULL); free(pUtf8); pUtf8=NULL; msginfoLen=rv; msginfo[msginfoLen]='\0'; } else{ msginfoLen=name_entry->value->length; memcpy(msginfo,name_entry->value->data,msginfoLen); msginfo[msginfoLen]='\0'; } switch(Nid){ case NID_countryName:{ myprintf("%s\r\n",msginfo); break; }; case NID_stateOrProvinceName:{ myprintf("%s\r\n",msginfo); break; }; case NID_localityName:{ myprintf("%s\r\n",msginfo); break; }; case NID_organizationName:{ myprintf("%s\r\n",msginfo); break; }; case NID_organizationUnitName:{ myprintf("%s\r\n",msginfo); break; }; case NID_commonName:{ myprintf("%s\r\n",msginfo); break; }; case NID_pkcs9_emailAddress:{ myprintf("%s\r\n",msginfo); break; }; }; }; subject=X509_get_subject_name(x509Cert); entriesNum=sk_X509_NAME_ENTRY_num(subject->entries); for(i=0;i<entiresNum;i++){ name_entry=sk_X509_NAME_ENTRY_value(subject->entries,i); Nid=OBJ_obj2nid(name_entry->object); if(name_entry->value->type==V_ASN1_UTF8STRING){ nUtf8=2*name_entry->value->length; pUtf8=(unsigned short*)malloc(nUtf8); rv=MultiByteToWideChar(CP_UTF8,0,(char*)name_entry->value->data,name_entry->value->length,(LPWSTR)pUtf8,nUtf8); rv=WideCharToMultiByte(CP_ACP,0,(LPCWSTR)pUtf8,rv,(char*)msginfo,nUtf8,NULL,NULL); free(pUtf8); pUtf8=NULL; msginfoLen=rv; msginfo[msginfoLen]='\0'; } else{ msginfoLen=name_entry->value->length; memcpy(msginfo,name_entry->value->data,msginfoLen); msginfo[msginfoLen]='\0'; }; switch(Nid){ case NID_countryName:{ myprintf("%s\r\n",msginfo); break; }; case NID_stateOrProvinceName:{ myprintf("%s\r\n",msginfo); break; }; case NID_localityName:{ myprintf("%s\r\n",msginfo); break; }; case NID_organizationName:{ myprintf("%s\r\n",msginfo); break; }; case NID_organizationalUnitName:{ myprintf("%s\r\n",msginfo); break; }; case NID_commonName:{ myprintf("%s\r\n",msginfo); break; }; case NID_pkcs9_emailAddress:{ myprintf("%s\r\n",msginfo); break; }; }; }; time=X509_get_notBefore(x509Cert); myprintf("%s\r\n",time->data); time=X509_get_notAfter(x509Cert); myprintf("%s\r\n",time->data); if(szSign[4]=='8'){ pubKey=X509_get_pubkey(x509Cert); if(!pubKey) goto end; pTmp=derpubkey; derpubkeyLen=i2d_PublicKey(pubKey,&pTmp); for(i=0;i<derpubkeyLen;i++){ if(i>0&&i%16==0) myprintf("\r\n"); myprintf("%02x",derpubkey[i]); }; }; end: X509_free(x509Cert); }; ULONG get_SignatureAlgOid(X509* x509Cert,LPSTR lpscOid,ULONG* pulLen){ char oid[128]={0}; ASN1_OBJECT* salg=NULL; if(!x509Cert) return -1; if(!pulLen) return -1; salg=x509Cert->sig_alg->algorithm; OBJ_obj2txt(oid,128,salg,1); if(!lpscOid){ *pulLen=strlen(oid)+1; return -1; }; if(*pulLen<strlen(oid)+1) return -1; strncpy(lpscOid,oid,*pulLen); *pulLen=strlen(oid)+1; return 0; }; CString gstr; void myprintf(const char* format,...){ va_list v1; char Buffer[2*MAX_PATH]={0}; LONG nRes; va_start(v1,format); vsprintf(Buffer,format,v1); CString str; str.Format("%s",Buffer); gstr+=str; };
|