You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

cert_verify.h 4.0 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_FL_SERVER_CERT_VERIFY_H
  17. #define MINDSPORE_CCSRC_FL_SERVER_CERT_VERIFY_H
  18. #include <assert.h>
  19. #ifndef _WIN32
  20. #include <openssl/evp.h>
  21. #include <openssl/rsa.h>
  22. #include <openssl/x509v3.h>
  23. #include <openssl/err.h>
  24. #include <openssl/pem.h>
  25. #include <openssl/sha.h>
  26. #endif
  27. #include <iostream>
  28. #include <fstream>
  29. #include <string>
  30. #include "utils/log_adapter.h"
  31. #include "fl/server/common.h"
  32. namespace mindspore {
  33. namespace ps {
  34. namespace server {
  35. class CertVerify {
  36. public:
  37. static CertVerify &GetInstance() {
  38. static CertVerify instance;
  39. return instance;
  40. }
  41. CertVerify() {}
  42. ~CertVerify() = default;
  43. bool verifyCertAndSign(const std::string &flID, const std::string &timeStamp, const unsigned char *signData,
  44. const std::string &keyAttestation, const std::string &equipCert,
  45. const std::string &equipCACert, const std::string &rootFirstCAPath,
  46. const std::string &rootSecondCAPath, const std::string &equipCrlPath);
  47. static bool initRootCertAndCRL(const std::string rootFirstCaFilePath, const std::string rootSecondCaFilePath,
  48. const std::string equipCrlPath, uint64_t replay_attack_time_diff_);
  49. // verify valid of sign data
  50. bool verifyRSAKey(const std::string &keyAttestation, const uint8_t *srcData, const uint8_t *signData, int srcDataLen);
  51. void sha256Hash(const uint8_t *src, const int src_len, uint8_t *hash, const int len) const;
  52. // verify valid of time stamp of request
  53. bool verifyTimeStamp(const std::string &flID, const std::string &timeStamp) const;
  54. #ifndef _WIN32
  55. private:
  56. // read certificate from file path
  57. static X509 *readCertFromFile(const std::string &certPath);
  58. // read Certificate Revocation List from file absolute path
  59. static X509_CRL *readCrlFromFile(const std::string &crlPath);
  60. // read certificate from pem string
  61. X509 *readCertFromPerm(std::string cert);
  62. // verify valid of certificate time
  63. bool verifyCertTime(const X509 *cert) const;
  64. // verify valid of certificate chain
  65. bool verifyCAChain(const std::string &keyAttestation, const std::string &equipCert, const std::string &equipCACert,
  66. const std::string &rootFirstCAPath, const std::string &rootSecondCAPath);
  67. // verify valid of sign data
  68. bool verifyRSAKey(const std::string &keyAttestation, const unsigned char *signData, const std::string &flID,
  69. const std::string &timeStamp);
  70. // verify valid of equip certificate with CRL
  71. bool verifyCRL(const std::string &equipCert, const std::string &equipCrlPath);
  72. // verify valid of flID with sha256(equip cert)
  73. bool verifyEquipCertAndFlID(const std::string &flID, const std::string &equipCert);
  74. void sha256Hash(const std::string &src, uint8_t *hash, const int len) const;
  75. std::string toHexString(const unsigned char *data, const int len);
  76. bool verifyCertCommonName(const X509 *caCert, const X509 *subCert) const;
  77. bool verifyExtendedAttributes(const X509 *cert) const;
  78. bool verifyCertKeyID(const X509 *caCert, const X509 *subCert) const;
  79. bool verifyPublicKey(const X509 *keyAttestationCertObj, const X509 *equipCertObj, const X509 *equipCACertObj,
  80. const X509 *rootFirstCA, const X509 *rootSecondCA) const;
  81. #endif
  82. };
  83. } // namespace server
  84. } // namespace ps
  85. } // namespace mindspore
  86. #endif // MINDSPORE_CCSRC_FL_SERVER_CERT_VERIFY_H