ua_pki_certificate.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  3. *
  4. * Copyright 2018 (c) Mark Giraud, Fraunhofer IOSB
  5. */
  6. #include "ua_pki_certificate.h"
  7. #ifdef UA_ENABLE_ENCRYPTION
  8. #include <mbedtls/x509.h>
  9. #include <mbedtls/x509_crt.h>
  10. #endif
  11. /************/
  12. /* AllowAll */
  13. /************/
  14. static UA_StatusCode
  15. verifyAllowAll(void *verificationContext, const UA_ByteString *certificate) {
  16. return UA_STATUSCODE_GOOD;
  17. }
  18. static void
  19. deleteVerifyAllowAll(UA_CertificateVerification *cv) {
  20. }
  21. void UA_CertificateVerification_AcceptAll(UA_CertificateVerification *cv) {
  22. cv->verifyCertificate = verifyAllowAll;
  23. cv->deleteMembers = deleteVerifyAllowAll;
  24. }
  25. #ifdef UA_ENABLE_ENCRYPTION
  26. typedef struct {
  27. mbedtls_x509_crt certificateTrustList;
  28. mbedtls_x509_crl certificateRevocationList;
  29. } CertInfo;
  30. static UA_StatusCode
  31. certificateVerification_verify(void *verificationContext,
  32. const UA_ByteString *certificate) {
  33. CertInfo *ci = (CertInfo*)verificationContext;
  34. if(!ci)
  35. return UA_STATUSCODE_BADINTERNALERROR;
  36. /* Parse the certificate */
  37. mbedtls_x509_crt remoteCertificate;
  38. mbedtls_x509_crt_init(&remoteCertificate);
  39. int mbedErr = mbedtls_x509_crt_parse(&remoteCertificate, certificate->data,
  40. certificate->length);
  41. if(mbedErr) {
  42. /* char errBuff[300]; */
  43. /* mbedtls_strerror(mbedErr, errBuff, 300); */
  44. /* UA_LOG_WARNING(data->policyContext->securityPolicy->logger, UA_LOGCATEGORY_SECURITYPOLICY, */
  45. /* "Could not parse the remote certificate with error: %s", errBuff); */
  46. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  47. }
  48. /* Verify */
  49. mbedtls_x509_crt_profile crtProfile = {
  50. MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA1) | MBEDTLS_X509_ID_FLAG(MBEDTLS_MD_SHA256),
  51. 0xFFFFFF, 0x000000, 128 * 8 // in bits
  52. }; // TODO: remove magic numbers
  53. uint32_t flags = 0;
  54. mbedErr = mbedtls_x509_crt_verify_with_profile(&remoteCertificate,
  55. &ci->certificateTrustList,
  56. &ci->certificateRevocationList,
  57. &crtProfile, NULL, &flags, NULL, NULL);
  58. // TODO: Extend verification
  59. if(mbedErr) {
  60. /* char buff[100]; */
  61. /* mbedtls_x509_crt_verify_info(buff, 100, "", flags); */
  62. /* UA_LOG_ERROR(channelContextData->policyContext->securityPolicy->logger, */
  63. /* UA_LOGCATEGORY_SECURITYPOLICY, */
  64. /* "Verifying the certificate failed with error: %s", buff); */
  65. if(flags & MBEDTLS_X509_BADCERT_NOT_TRUSTED)
  66. return UA_STATUSCODE_BADCERTIFICATEUNTRUSTED;
  67. if(flags & MBEDTLS_X509_BADCERT_FUTURE ||
  68. flags & MBEDTLS_X509_BADCERT_EXPIRED)
  69. return UA_STATUSCODE_BADCERTIFICATETIMEINVALID;
  70. if(flags & MBEDTLS_X509_BADCERT_REVOKED ||
  71. flags & MBEDTLS_X509_BADCRL_EXPIRED)
  72. return UA_STATUSCODE_BADCERTIFICATEREVOKED;
  73. return UA_STATUSCODE_BADSECURITYCHECKSFAILED;
  74. }
  75. return UA_STATUSCODE_GOOD;
  76. }
  77. static void
  78. certificateVerification_deleteMembers(UA_CertificateVerification *cv) {
  79. CertInfo *ci = (CertInfo*)cv->context;
  80. if(!ci)
  81. return;
  82. mbedtls_x509_crt_free(&ci->certificateTrustList);
  83. mbedtls_x509_crl_free(&ci->certificateRevocationList);
  84. UA_free(ci);
  85. cv->context = NULL;
  86. }
  87. UA_StatusCode
  88. UA_CertificateVerification_Trustlist(UA_CertificateVerification *cv,
  89. const UA_ByteString *certificateTrustList,
  90. size_t certificateTrustListSize,
  91. const UA_ByteString *certificateRevocationList,
  92. size_t certificateRevocationListSize) {
  93. CertInfo *ci = (CertInfo*)malloc(sizeof(CertInfo));
  94. if(!ci)
  95. return UA_STATUSCODE_BADOUTOFMEMORY;
  96. mbedtls_x509_crt_init(&ci->certificateTrustList);
  97. mbedtls_x509_crl_init(&ci->certificateRevocationList);
  98. cv->context = (void*)ci;
  99. if(certificateTrustListSize > 0)
  100. cv->verifyCertificate = certificateVerification_verify;
  101. else
  102. cv->verifyCertificate = verifyAllowAll;
  103. cv->deleteMembers = certificateVerification_deleteMembers;
  104. int err = 0;
  105. for(size_t i = 0; i < certificateTrustListSize; i++) {
  106. err |= mbedtls_x509_crt_parse(&ci->certificateTrustList,
  107. certificateTrustList[i].data,
  108. certificateTrustList[i].length);
  109. }
  110. for(size_t i = 0; i < certificateRevocationListSize; i++) {
  111. err |= mbedtls_x509_crl_parse(&ci->certificateRevocationList,
  112. certificateRevocationList[i].data,
  113. certificateRevocationList[i].length);
  114. }
  115. if(err) {
  116. certificateVerification_deleteMembers(cv);
  117. return UA_STATUSCODE_BADINTERNALERROR;
  118. }
  119. return UA_STATUSCODE_GOOD;
  120. }
  121. #endif