Changeset 2506bdb in terepaima for signHash/cryptotoken.cpp
- Timestamp:
- Oct 4, 2017, 4:08:55 PM (7 years ago)
- Branches:
- master
- Children:
- 2aa151c
- Parents:
- ce57b45
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
signHash/cryptotoken.cpp
rce57b45 r2506bdb 1 1 #include "cryptotoken.h" 2 3 #include <QSslCertificate> 2 4 3 5 #include <assert.h> … … 117 119 if ((rv == CKR_PIN_INCORRECT) || (rv == CKR_DATA_LEN_RANGE) || (rv == CKR_ARGUMENTS_BAD)) 118 120 plus = "El PIN introducido es incorrecto!"; 119 qDebug(qPrintable("C_login: " + error + plus));121 qDebug(qPrintable("C_login: " + error + " " +plus)); 120 122 return CK_INVALID_HANDLE; 121 123 } … … 456 458 457 459 closeSession(hSession); 460 finalize(); 458 461 459 462 return signature; … … 482 485 QStringList CryptoToken::getPrivateKeyLabels(QString pin) 483 486 { 487 qDebug("...CryptoToken::getPrivateKeyLabels"); 484 488 QStringList list; 485 489 … … 531 535 error = returnErrorToQString(rv); 532 536 qDebug("%s", qPrintable("C_FindObjectsInit: "+error)); 533 return list; 537 //return list; 538 throw std::runtime_error(qPrintable(error)); 534 539 } 535 540 … … 554 559 error = returnErrorToQString(rv); 555 560 qDebug(qPrintable("C_FindObjects: "+error)); 556 return list;557 561 //return list; 562 throw std::runtime_error(qPrintable(error)); 558 563 } 559 564 … … 566 571 error = returnErrorToQString(rv); 567 572 qDebug(qPrintable("C_FindObjectsFinal: "+error)); 568 return list; 573 //return list; 574 throw std::runtime_error(qPrintable(error)); 569 575 } 570 576 objectHandle.resize(objectCount); … … 573 579 if (objectHandle.size() == 0) 574 580 { 575 qDebug (" \nNingun a clave privada encontrada!\n");581 qDebug (" \nNingun objeto encontrado!\n"); 576 582 // cerrar la sesion 577 583 /*rv = C_CloseAllSessions(slotID); … … 579 585 qDebug ("\n"); 580 586 assert(rv==CKR_OK);*/ 581 return list; 587 //return list; 588 throw std::runtime_error(qPrintable(error)); 582 589 } 583 590 else 584 591 { 585 592 //qDebug (" \nObjeto encontrado (object=0x%04X)\n", hObject); 586 qDebug("se econtraron %d claves", objectHandle.size());593 qDebug("se econtraron %d objetos", objectHandle.size()); 587 594 //qDebug (" \nClave privada encontrada: (clave = 0x%4X)\n", hObject); 588 595 for (int i=0; i< objectHandle.size(); i++) 589 596 { 590 qDebug("clave encontrada: 0x%4X", objectHandle[i]); 597 qDebug("objeto encontrado: 0x%4X", objectHandle[i]); 598 list.append(getKeyLabel(hSession, objectHandle[i])); 591 599 } 600 } 601 602 closeSession(hSession); 603 finalize(); 604 605 return list; 606 } 607 608 QString CryptoToken::getKeyLabel(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE key) 609 { 610 qDebug("CryptoToken::getKeyLabel"); 611 612 CK_RV rv = CKR_OK; 613 CK_ATTRIBUTE keyid_attr[1] = { 614 { CKA_LABEL, NULL, 0 } 615 //{ CKA_VALUE, NULL, 0 } 616 }; 617 618 if ((rv = C_GetAttributeValue(hSession, key, keyid_attr, 1)) != CKR_OK) 619 { 620 //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la obtencion del atributo ID")); 621 qDebug("fallo la obtencion del atributo ID"); 622 return ""; 623 } 624 625 if ((keyid_attr[0].pValue = malloc(keyid_attr[0].ulValueLen)) == NULL) 626 { 627 //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la asignacion de memoria del atributo ID")); 628 qDebug("fallo la asignacion de memoria del atributo ID"); 629 return ""; 630 } 631 if ((rv = C_GetAttributeValue(hSession, key, keyid_attr, 1)) != CKR_OK) 632 { 633 //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la obtencion 2 del atributo ID")); 634 qDebug("fallo la obtencion 2 del atributo ID"); 635 return ""; 636 } 637 638 //qDebug("\n*-** VALOR DE ID: %s\n", keyid_attr[0].pValue); 639 qDebug("\n*-** VALOR DE atributo: %s\n", keyid_attr[0].pValue); 640 QString label = (const char *) keyid_attr[0].pValue; 641 642 qDebug(qPrintable(label)); 643 qDebug(qPrintable(QString::number(label.size()))); 644 645 return label; 646 } 647 648 649 QList<QStringList> CryptoToken::getCertificateCNandExpirationDate(QString pin) 650 { 651 qDebug("...CryptoToken::getCertificateCNandExpirationDate"); 652 QList<QStringList> list; 653 654 QString error(""); 655 CK_RV rv; 656 657 CK_SESSION_HANDLE hSession = CK_INVALID_HANDLE; 658 CK_SLOT_ID slotID; 659 660 // initialize criptoki 661 rv = C_Initialize(NULL_PTR); 662 if (rv != CKR_OK) 663 { 664 if (rv == CKR_DEVICE_ERROR) 665 { 666 qDebug("CKR_DEVICE_ERROR"); 667 error = returnErrorToQString(rv); 668 qDebug(qPrintable(error)); 669 throw std::runtime_error(qPrintable(error)); 670 } 671 qDebug("****"); 672 error = returnErrorToQString(rv); 673 qDebug(qPrintable(error)); 674 throw std::runtime_error(qPrintable(error)); 675 } 676 677 hSession = openSession((char *) qPrintable(pin), slotID); 678 if (hSession == CK_INVALID_HANDLE) 679 { 680 qDebug("Fallo ptr_SC->openSession"); 681 rv = C_Finalize(NULL_PTR); 682 qDebug("C_Finalize: rv = %x",rv); 683 assert(rv == CKR_OK); 684 throw std::runtime_error("Error openning a session inside the device"); 685 } 686 687 CK_OBJECT_CLASS objectClass = CKO_CERTIFICATE; 688 689 CK_OBJECT_HANDLE hObject = CK_INVALID_HANDLE; 690 CK_ATTRIBUTE searchAttribute = {CKA_CLASS, &objectClass, sizeof(objectClass)}; 691 CK_ULONG ulCount = 0; 692 693 rv = C_FindObjectsInit(hSession, &searchAttribute, 1); 694 qDebug ("C_FindObjectsInit rv = %x",rv); 695 qDebug ("\n"); 696 //assert(rv==CKR_OK); 697 if (rv != CKR_OK) 698 { 699 error = returnErrorToQString(rv); 700 qDebug("%s", qPrintable("C_FindObjectsInit: "+error)); 701 //return list; 702 throw std::runtime_error(qPrintable(error)); 703 } 704 705 CK_ULONG objectCount = 3; 706 std::vector<CK_OBJECT_HANDLE> objectHandle(objectCount); 707 708 // se realiza la busqueda 709 //rv = C_FindObjects(hSession, &hObject, 2, &ulCount); 710 rv = C_FindObjects(hSession, objectHandle.data(), objectHandle.size(), &objectCount); 711 712 qDebug ("C_FindObjects rv = %x",rv); 713 qDebug ("\n"); 714 //assert(rv==CKR_OK); 715 716 qDebug("*-*-*-*-*getPrivateKeyLabels VALOR DE objectCount %u: ", objectCount); 717 //if (rv != CKR_OK) 718 if ((rv != CKR_OK) )//|| (ulCount == 0)) 719 { 720 if (ulCount == 0) 721 qDebug("ulCount == 0"); 722 723 error = returnErrorToQString(rv); 724 qDebug(qPrintable("C_FindObjects: "+error)); 725 //return list; 726 throw std::runtime_error(qPrintable(error)); 727 } 728 729 rv = C_FindObjectsFinal(hSession); 730 qDebug ("C_FindObjectsFinal rv = %x",rv); 731 qDebug ("\n"); 732 //assert(rv==CKR_OK); 733 if (rv != CKR_OK) 734 { 735 error = returnErrorToQString(rv); 736 qDebug(qPrintable("C_FindObjectsFinal: "+error)); 737 //return list; 738 throw std::runtime_error(qPrintable(error)); 739 } 740 objectHandle.resize(objectCount); 741 742 //if (ulCount == 0) 743 if (objectHandle.size() == 0) 744 { 745 qDebug (" \nNingun objeto encontrado!\n"); 746 // cerrar la sesion 747 /*rv = C_CloseAllSessions(slotID); 748 qDebug("C_CloseAllSessions rv = %x", rv); 749 qDebug ("\n"); 750 assert(rv==CKR_OK);*/ 751 //return list; 752 throw std::runtime_error(qPrintable(error)); 753 } 754 else 755 { 756 //qDebug (" \nObjeto encontrado (object=0x%04X)\n", hObject); 757 qDebug("se econtraron %d objetos", objectHandle.size()); 758 //qDebug (" \nClave privada encontrada: (clave = 0x%4X)\n", hObject); 759 760 for (int i=0; i< objectHandle.size(); i++) 761 { 762 QStringList certInformation; 763 qDebug("objeto encontrado: 0x%4X", objectHandle[i]); 764 certInformation = getCertificateInformation(hSession, objectHandle[i]); 765 //list.append(getCertificateInformation(hSession, objectHandle[i])[0]); 766 //list.append(getCertificateInformation(hSession, objectHandle[i])[0]); 767 list.append(certInformation); 768 } 769 } 770 771 closeSession(hSession); 772 finalize(); 773 774 return list; 775 } 776 777 QStringList CryptoToken::getCertificateInformation(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE certificate) 778 { 779 qDebug("...getCertificateInformation..."); 780 781 CK_RV rv = CKR_OK; 782 CK_ATTRIBUTE keyid_attr[2] = { 783 { CKA_LABEL, NULL, 0 }, 784 { CKA_VALUE, NULL, 0 } 785 }; 786 787 QStringList list; 788 789 790 if ((rv = C_GetAttributeValue(hSession, certificate, keyid_attr, 2)) != CKR_OK) 791 { 792 //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la obtencion del atributo ID")); 793 qDebug("fallo la obtencion del atributo ID"); 592 794 return list; 593 795 } 594 796 797 if ((keyid_attr[1].pValue = malloc(keyid_attr[1].ulValueLen)) == NULL) 798 { 799 //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la asignacion de memoria del atributo ID")); 800 qDebug("fallo la asignacion de memoria del atributo ID"); 801 return list; 802 } 803 if ((rv = C_GetAttributeValue(hSession, certificate, keyid_attr, 2)) != CKR_OK) 804 { 805 //QMessageBox::critical(this,tr("UserAccess"),tr("fallo la obtencion 2 del atributo ID")); 806 qDebug("fallo la obtencion 2 del atributo ID"); 807 return list; 808 } 809 810 //qDebug("\n*-** VALOR DE ID: %s\n", keyid_attr[0].pValue); 811 //qDebug("\n*-** VALOR DE atributo: %s\n", keyid_attr[1].pValue); 812 //QString x = (const char *) keyid_attr[1].pValue; 813 814 //qDebug(qPrintable(x)); 815 //qDebug(qPrintable(QString::number(x.size()))); 816 817 818 //qDebug("prueba de generar un QSslCertificate"); 819 820 QSslCertificate qcert(QByteArray((const char *) keyid_attr[1].pValue, (int) keyid_attr[1].ulValueLen), QSsl::Der); 821 822 if (qcert.isNull()){ 823 qDebug("qcert es null"); 824 } 825 826 //qDebug("mira el contenido qcert"); 827 828 /* 829 QStringList list = qcert.subjectInfo(QSslCertificate::EmailAddress); 830 QStringList l2 = qcert.subjectInfo(QSslCertificate::DistinguishedNameQualifier); 831 QStringList l3 = qcert.subjectInfo( QSslCertificate::CommonName ); 832 QStringList l4 = qcert.subjectInfo( QSslCertificate::Organization ); 833 QStringList l5 = qcert.subjectInfo( QSslCertificate::LocalityName ); 834 QStringList l6 = qcert.subjectInfo( QSslCertificate::OrganizationalUnitName ); 835 QStringList l7 = qcert.subjectInfo( QSslCertificate::StateOrProvinceName ); 836 */ 837 list.append(qcert.subjectInfo( QSslCertificate::CommonName )); 838 list.append(qcert.expiryDate().date().toString("dd.MM.yyyy")); 839 840 //qDebug(qPrintable(qcert.effectiveDate().date().toString("dd.MM.yyyy"))); 841 //qDebug(qPrintable(qcert.expiryDate().date().toString("dd.MM.yyyy"))); 842 595 843 return list; 596 844 } 845 846 847 QList<QStringList> CryptoToken::getDeviceCertificates(QString pin) 848 { 849 qDebug("...getDeviceCertificates"); 850 851 852 QStringList labelList = getPrivateKeyLabels(pin); 853 854 QList<QStringList> nameExpirationList = getCertificateCNandExpirationDate(pin); 855 856 QList<QStringList> certificateInformationList; 857 858 for (int i=0; i<labelList.size(); i++) 859 { 860 QStringList l; 861 qDebug("... antes de insertar label"); 862 l.append(labelList[i]); 863 qDebug("... antes de insertar commonName"); 864 l.append(nameExpirationList[i][0]); 865 qDebug("... antes de insertar expirationDate"); 866 l.append(nameExpirationList[i][1]); 867 868 qDebug("... antes de insertar en certificateInformationList"); 869 certificateInformationList.append(l); 870 } 871 872 return certificateInformationList; 873 } 874 875 876 void CryptoToken::getCertificateAttributes(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hCert) 877 { 878 CK_ATTRIBUTE pTemplate[2] = { 879 //List your desired attributes here 880 { CKA_LABEL, NULL, 0 } 881 //{ CKA_LABEL, NULL, 0 } 882 }; 883 884 CK_RV rv = C_GetAttributeValue(hSession, hCert, pTemplate, /*pTemplateLen*/ 2); 885 if (rv == CKR_OK) { 886 //here you have your desired certificate attributes 887 qDebug("chequea los atributos"); 888 889 } 890 } 891 892 893 894 895 896 897 898 899 900 597 901 598 902 // slot para obtener informacion del modulo PKCS11 … … 672 976 } 673 977 978 979 980 674 981 QString CryptoToken::returnErrorToQString(CK_RV rv) 675 982 {
Note: See TracChangeset
for help on using the changeset viewer.