|
| 1 | +/* MyTls13SecretCallback.java |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2024 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfSSL. |
| 6 | + * |
| 7 | + * wolfSSL is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * wolfSSL is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| 20 | + */ |
| 21 | + |
| 22 | +import java.io.FileWriter; |
| 23 | +import java.io.PrintWriter; |
| 24 | +import java.io.IOException; |
| 25 | + |
| 26 | +import com.wolfssl.WolfSSL; |
| 27 | +import com.wolfssl.WolfSSLSession; |
| 28 | +import com.wolfssl.WolfSSLTls13SecretCallback; |
| 29 | +import com.wolfssl.WolfSSLJNIException; |
| 30 | + |
| 31 | +/** |
| 32 | + * Example TLS 1.3 secret callback implementation. |
| 33 | + * |
| 34 | + * This is provided as an example only, and used with the example JNI |
| 35 | + * applications provided in this package. Users in production environments |
| 36 | + * should write their own implementation to conform to desired goals. |
| 37 | + */ |
| 38 | +class MyTls13SecretCallback implements WolfSSLTls13SecretCallback |
| 39 | +{ |
| 40 | + /* SSL keylog file to output secrets to */ |
| 41 | + private String sslKeyLogFile = "sslkeylog.log"; |
| 42 | + |
| 43 | + /** |
| 44 | + * Create new MyTls13SecretCallback using default "sslkeylog.log" file |
| 45 | + * path. |
| 46 | + */ |
| 47 | + public MyTls13SecretCallback() { |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Create new MyTls13SecretCallback object specifying SSL keylog file |
| 52 | + * path. |
| 53 | + * |
| 54 | + * @param keyLogFile path to output file (ex: sslkeylog.log) to use |
| 55 | + * for writing TLS 1.3 secrets into. |
| 56 | + */ |
| 57 | + public MyTls13SecretCallback(String keyLogFile) { |
| 58 | + this.sslKeyLogFile = keyLogFile; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Callback method for printing/saving TLS 1.3 secrets, for use |
| 63 | + * with Wireshark. Called by native wolfSSL when each secret is available. |
| 64 | + * |
| 65 | + * @param ssl the current SSL session object from which the |
| 66 | + * callback was initiated. |
| 67 | + * @param id Identifier specifying what type of secret this callback |
| 68 | + * is being called with, one of the following: |
| 69 | + * WolfSSL.CLIENT_EARLY_TRAFFIC_SECRET |
| 70 | + * WolfSSL.EARLY_EXPORTER_SECRET |
| 71 | + * WolfSSL.CLIENT_HANDSHAKE_TRAFFIC_SECRET |
| 72 | + * WolfSSL.SERVER_HANDSHAKE_TRAFFIC_SECRET |
| 73 | + * WolfSSL.CLIENT_TRAFFIC_SECRET |
| 74 | + * WolfSSL.SERVER_TRAFFIC_SECRET |
| 75 | + * WolfSSL.EXPORTER_SECRET |
| 76 | + * @param secret Current secret as byte array |
| 77 | + * @param ctx Optional user context if set |
| 78 | + * |
| 79 | + * @return 0 on success, otherwise negative if callback encounters |
| 80 | + * an error. |
| 81 | + */ |
| 82 | + public int tls13SecretCallback(WolfSSLSession ssl, int id, byte[] secret, |
| 83 | + Object ctx) { |
| 84 | + |
| 85 | + int i; |
| 86 | + String str = null; |
| 87 | + FileWriter fw = null; |
| 88 | + PrintWriter pw = null; |
| 89 | + byte[] clientRandom = null; |
| 90 | + |
| 91 | + try { |
| 92 | + /* Open FileWriter in append mode */ |
| 93 | + fw = new FileWriter(sslKeyLogFile, true); |
| 94 | + pw = new PrintWriter(fw); |
| 95 | + |
| 96 | + clientRandom = ssl.getClientRandom(); |
| 97 | + if (clientRandom == null || clientRandom.length == 0) { |
| 98 | + System.out.println("Error getting client random"); |
| 99 | + } |
| 100 | + |
| 101 | + /* Set secret label based on ID */ |
| 102 | + if (id == WolfSSL.CLIENT_EARLY_TRAFFIC_SECRET) { |
| 103 | + str = "CLIENT_EARLY_TRAFFIC_SECRET"; |
| 104 | + } else if (id == WolfSSL.EARLY_EXPORTER_SECRET) { |
| 105 | + str = "EARLY_EXPORTER_SECRET"; |
| 106 | + } else if (id == WolfSSL.CLIENT_HANDSHAKE_TRAFFIC_SECRET) { |
| 107 | + str = "CLIENT_HANDSHAKE_TRAFFIC_SECRET"; |
| 108 | + } else if (id == WolfSSL.SERVER_HANDSHAKE_TRAFFIC_SECRET) { |
| 109 | + str = "SERVER_HANDSHAKE_TRAFFIC_SECRET"; |
| 110 | + } else if (id == WolfSSL.CLIENT_TRAFFIC_SECRET) { |
| 111 | + str = "CLIENT_TRAFFIC_SECRET"; |
| 112 | + } else if (id == WolfSSL.SERVER_TRAFFIC_SECRET) { |
| 113 | + str = "SERVER_TRAFFIC_SECRET"; |
| 114 | + } else if (id == WolfSSL.EXPORTER_SECRET) { |
| 115 | + str = "EXPORTER_SECRET"; |
| 116 | + } else { |
| 117 | + pw.close(); |
| 118 | + return WolfSSL.TLS13_SECRET_CB_E; |
| 119 | + } |
| 120 | + |
| 121 | + pw.printf("%s ", str); |
| 122 | + for (i = 0; i < clientRandom.length; i++) { |
| 123 | + pw.printf("%02x", clientRandom[i]); |
| 124 | + } |
| 125 | + pw.printf(" "); |
| 126 | + for (i = 0; i < clientRandom.length; i++) { |
| 127 | + pw.printf("%02x", secret[i]); |
| 128 | + } |
| 129 | + pw.printf("\n"); |
| 130 | + |
| 131 | + pw.close(); |
| 132 | + |
| 133 | + return 0; |
| 134 | + |
| 135 | + } catch (IOException | WolfSSLJNIException e) { |
| 136 | + e.printStackTrace(); |
| 137 | + return WolfSSL.TLS13_SECRET_CB_E; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
0 commit comments