|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, Parse, LLC. All rights reserved. |
| 3 | + * |
| 4 | + * You are hereby granted a non-exclusive, worldwide, royalty-free license to use, |
| 5 | + * copy, modify, and distribute this software in source code or binary form for use |
| 6 | + * in connection with the web services and APIs provided by Parse. |
| 7 | + * |
| 8 | + * As with any software that integrates with the Parse platform, your use of |
| 9 | + * this software is subject to the Parse Terms of Service |
| 10 | + * [https://www.parse.com/about/terms]. This copyright notice shall be |
| 11 | + * included in all copies or substantial portions of the software. |
| 12 | + * |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 15 | + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 16 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 17 | + * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 18 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +#include <simplelink.h> |
| 23 | + |
| 24 | +#include <stdio.h> |
| 25 | + |
| 26 | +#include <parse_impl.h> |
| 27 | + |
| 28 | +void ensureCertificateFile(const unsigned char *certificate, unsigned int certificateLen) { |
| 29 | + long certificateFile = -1; |
| 30 | + |
| 31 | + short status = sl_FsOpen((unsigned char *)SSL_CA_CERT_FILE_NAME, FS_MODE_OPEN_READ, NULL, &certificateFile); |
| 32 | + if (status < 0) { |
| 33 | +#ifdef SSL_DEBUG |
| 34 | + DEBUG_PRINT("[Parse] Creating certificate file\r\n"); |
| 35 | +#endif /* SSL_DEBUG */ |
| 36 | + |
| 37 | + sl_FsClose(certificateFile, 0, 0, 0); |
| 38 | + certificateFile = -1; |
| 39 | + |
| 40 | + status = sl_FsOpen((unsigned char *)SSL_CA_CERT_FILE_NAME, |
| 41 | + FS_MODE_OPEN_CREATE(65536, _FS_FILE_OPEN_FLAG_COMMIT | _FS_FILE_PUBLIC_WRITE), |
| 42 | + NULL, &certificateFile); |
| 43 | + if (status < 0) { |
| 44 | +#ifdef SSL_DEBUG |
| 45 | + DEBUG_PRINT("[Parse] Error creating certificate file: %d\r\n", status); |
| 46 | + if (status == SL_FS_ERR_NO_AVAILABLE_BLOCKS) { |
| 47 | + DEBUG_PRINT("[Parse] No available blocks. Need to flash your device file system...\r\n"); |
| 48 | + } |
| 49 | +#endif /* SSL_DEBUG */ |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + sl_FsClose(certificateFile, 0, 0, 0); |
| 54 | + certificateFile = -1; |
| 55 | + |
| 56 | + status = sl_FsOpen((unsigned char *)SSL_CA_CERT_FILE_NAME, FS_MODE_OPEN_WRITE, NULL, &certificateFile); |
| 57 | + if (status < 0) { |
| 58 | +#ifdef SSL_DEBUG |
| 59 | + DEBUG_PRINT("[Parse] Error creating certificate file: %d\r\n", status); |
| 60 | + if (status == SL_FS_ERR_NO_AVAILABLE_BLOCKS) { |
| 61 | + DEBUG_PRINT("[Parse] No available blocks. Need to flash your device file system...\r\n"); |
| 62 | + } |
| 63 | +#endif /* SSL_DEBUG */ |
| 64 | + sl_FsClose(certificateFile, 0, 0, 0); |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + status = sl_FsWrite(certificateFile, 0, (unsigned char*)certificate, certificateLen); |
| 69 | + if (status < 0) { |
| 70 | +#ifdef SSL_DEBUG |
| 71 | + DEBUG_PRINT("[Parse] Error creating certificate file: %d\r\n", status); |
| 72 | + if (status == SL_FS_ERR_NO_AVAILABLE_BLOCKS) { |
| 73 | + DEBUG_PRINT("[Parse] No available blocks. Need to flash your device file system...\r\n"); |
| 74 | + } |
| 75 | +#endif /* SSL_DEBUG */ |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + sl_FsClose(certificateFile, 0, 0, 0); |
| 80 | +} |
0 commit comments