RTDECL(int) RTCrX509Certificate_ReadFromFile(PRTCRX509CERTIFICATE pCertificate, const char *pszFilename, uint32_t fFlags, PCRTASN1ALLOCATORVTABLE pAllocator, PRTERRINFO pErrInfo) { AssertReturn(!fFlags, VERR_INVALID_FLAGS); PCRTCRPEMSECTION pSectionHead; int rc = RTCrPemReadFile(pszFilename, 0, g_aCertificateMarkers, RT_ELEMENTS(g_aCertificateMarkers), &pSectionHead, pErrInfo); if (RT_SUCCESS(rc)) { RTCRX509CERTIFICATE TmpCert; RTASN1CURSORPRIMARY PrimaryCursor; RTAsn1CursorInitPrimary(&PrimaryCursor, pSectionHead->pbData, (uint32_t)RT_MIN(pSectionHead->cbData, UINT32_MAX), pErrInfo, pAllocator, RTASN1CURSOR_FLAGS_DER, RTPathFilename(pszFilename)); rc = RTCrX509Certificate_DecodeAsn1(&PrimaryCursor.Cursor, 0, &TmpCert, "Cert"); if (RT_SUCCESS(rc)) { rc = RTCrX509Certificate_CheckSanity(&TmpCert, 0, pErrInfo, "Cert"); if (RT_SUCCESS(rc)) { rc = RTCrX509Certificate_Clone(pCertificate, &TmpCert, &g_RTAsn1DefaultAllocator); if (RT_SUCCESS(rc)) { if (pSectionHead->pNext || PrimaryCursor.Cursor.cbLeft) rc = VINF_ASN1_MORE_DATA; } } RTCrX509Certificate_Delete(&TmpCert); } RTCrPemFreeSections(pSectionHead); } return rc; }
RTDECL(int) RTCrStoreCertAddFromFile(RTCRSTORE hStore, uint32_t fFlags, const char *pszFilename, PRTERRINFO pErrInfo) { AssertReturn(!fFlags, VERR_INVALID_FLAGS); #if 0 RTCRX509CERTIFICATES Certs; int rc = RTCrX509Certificates_ReadFromFile(pszFilename, 0, &Certs, pErrInfo); if (RT_SUCCESS(rc)) { for (uint32_t i = 0; i < Certs.cCerts; i++) { int rc2 = RTCrStoreCertAddEncoded(hStore, RTCRCERTCTX_F_ENC_X509_DER, RTASN1CORE_GET_RAW_ASN1_PTR(&Certs.paCerts[i].SeqCore.Asn1Core), RTASN1CORE_GET_RAW_ASN1_SIZE(&Certs.paCerts[i].SeqCore.Asn1Core), RT_SUCCESS(rc) ? pErrInfo : NULL); if (RT_FAILURE(rc2) && RT_SUCCESS(rc)) rc = rc2; } RTAsn1Destroy(&Certs.SetCore.Asn1Core); } return rc; #else PCRTCRPEMSECTION pSectionHead; int rc = RTCrPemReadFile(pszFilename, 0, g_aCertificateMarkers, RT_ELEMENTS(g_aCertificateMarkers), &pSectionHead, pErrInfo); if (RT_SUCCESS(rc)) { PCRTCRPEMSECTION pCurSec = pSectionHead; while (pCurSec) { int rc2 = RTCrStoreCertAddEncoded(hStore, RTCRCERTCTX_F_ENC_X509_DER, pCurSec->pbData, pCurSec->cbData, RT_SUCCESS(rc) ? pErrInfo : NULL); if (RT_FAILURE(rc2) && RT_SUCCESS(rc)) rc = rc2; pCurSec = pCurSec->pNext; } RTCrPemFreeSections(pSectionHead); } return rc; #endif }
RTDECL(int) RTCrX509Certificates_ReadFromFile(const char *pszFilename, uint32_t fFlags, PRTCRX509CERTIFICATES pCertificates, PRTERRINFO pErrInfo) { AssertReturn(!fFlags, VERR_INVALID_FLAGS); PCRTCRPEMSECTION pSectionHead; int rc = RTCrPemReadFile(pszFilename, 0, g_aCertificateMarkers, RT_ELEMENTS(g_aCertificateMarkers), &pSectionHead, pErrInfo); if (RT_SUCCESS(rc)) { pCertificates->Allocation PCRTCRPEMSECTION pCurSec = pSectionHead; while (pCurSec) { pCurSec = pCurSec->pNext; } RTCrPemFreeSections(pSectionHead); } return rc; }
RTDECL(int) RTCrPemReadFile(const char *pszFilename, uint32_t fFlags, PCRTCRPEMMARKER paMarkers, size_t cMarkers, PCRTCRPEMSECTION *ppSectionHead, PRTERRINFO pErrInfo) { AssertReturn(!fFlags, VERR_INVALID_FLAGS); size_t cbContent; uint8_t *pbContent; int rc = RTFileReadAllEx(pszFilename, 0, 64U*_1M, RTFILE_RDALL_O_DENY_WRITE, (void **)&pbContent, &cbContent); if (RT_SUCCESS(rc)) { PRTCRPEMSECTION pSection = (PRTCRPEMSECTION)RTMemAllocZ(sizeof(*pSection)); if (pSection) { /* * Try locate the first section. */ size_t offBegin, offEnd, offResume; PCRTCRPEMMARKER pMatch; if ( !rtCrPemIsBinaryFile(pbContent, cbContent) && rtCrPemFindMarkerSection(pbContent, cbContent, 0 /*offStart*/, paMarkers, cMarkers, &pMatch, &offBegin, &offEnd, &offResume) ) { PCRTCRPEMSECTION *ppNext = ppSectionHead; for (;;) { //pSection->pNext = NULL; pSection->pMarker = pMatch; //pSection->pbData = NULL; //pSection->cbData = 0; //pSection->pszPreamble = NULL; //pSection->cchPreamble = 0; *ppNext = pSection; ppNext = &pSection->pNext; /* Decode the section. */ /** @todo copy the preamble as well. */ rc = rtCrPemDecodeBase64(pbContent + offBegin, offEnd - offBegin, (void **)&pSection->pbData, &pSection->cbData); if (RT_FAILURE(rc)) { pSection->pbData = NULL; pSection->cbData = 0; break; } /* More sections? */ if ( offResume + 12 >= cbContent || offResume >= cbContent || !rtCrPemFindMarkerSection(pbContent, cbContent, offResume, paMarkers, cMarkers, &pMatch, &offBegin, &offEnd, &offResume) ) break; /* No. */ /* Ok, allocate a new record for it. */ pSection = (PRTCRPEMSECTION)RTMemAllocZ(sizeof(*pSection)); if (RT_UNLIKELY(!pSection)) { rc = VERR_NO_MEMORY; break; } } if (RT_SUCCESS(rc)) { RTFileReadAllFree(pbContent, cbContent); return rc; } RTCrPemFreeSections(*ppSectionHead); } else { /* * No PEM section found. Return the whole file as one binary section. */ //pSection->pNext = NULL; //pSection->pMarker = NULL; pSection->pbData = pbContent; pSection->cbData = cbContent; //pSection->pszPreamble = NULL; //pSection->cchPreamble = 0; *ppSectionHead = pSection; return VINF_SUCCESS; } } else rc = VERR_NO_MEMORY; RTFileReadAllFree(pbContent, cbContent); } *ppSectionHead = NULL; return rc; }