{"record":{"id":"4fb53b8770a0d8bf","repo":"shadowsocks/shadowsocks-windows","slug":"openssl-fail-to-set-key-length","errorCode":null,"errorMessage":"openssl: fail to set key length","messagePattern":"openssl: fail to set key length","errorType":"exception","errorClass":"System.Exception","httpStatus":null,"severity":"critical","filePath":"shadowsocks-csharp/Encryption/AEAD/AEADOpenSSLEncryptor.cs","lineNumber":71,"sourceCode":"\r\n            if (isEncrypt)\r\n            {\r\n                _encryptCtx = ctx;\r\n            }\r\n            else\r\n            {\r\n                _decryptCtx = ctx;\r\n            }\r\n\r\n            DeriveSessionKey(isEncrypt ? _encryptSalt : _decryptSalt, _Masterkey,\r\n                isEncrypt ? _opensslEncSubkey : _opensslDecSubkey);\r\n\r\n            var ret = OpenSSL.EVP_CipherInit_ex(ctx, _cipherInfoPtr, IntPtr.Zero, null, null,\r\n                isEncrypt ? OpenSSL.OPENSSL_ENCRYPT : OpenSSL.OPENSSL_DECRYPT);\r\n            if (ret != 1) throw new System.Exception(\"openssl: fail to init ctx\");\r\n\r\n            ret = OpenSSL.EVP_CIPHER_CTX_set_key_length(ctx, keyLen);\r\n            if (ret != 1) throw new System.Exception(\"openssl: fail to set key length\");\r\n\r\n            ret = OpenSSL.EVP_CIPHER_CTX_ctrl(ctx, OpenSSL.EVP_CTRL_AEAD_SET_IVLEN,\r\n                nonceLen, IntPtr.Zero);\r\n            if (ret != 1) throw new System.Exception(\"openssl: fail to set AEAD nonce length\");\r\n\r\n            ret = OpenSSL.EVP_CipherInit_ex(ctx, IntPtr.Zero, IntPtr.Zero,\r\n                isEncrypt ? _opensslEncSubkey : _opensslDecSubkey,\r\n                null,\r\n                isEncrypt ? OpenSSL.OPENSSL_ENCRYPT : OpenSSL.OPENSSL_DECRYPT);\r\n            if (ret != 1) throw new System.Exception(\"openssl: cannot set key\");\r\n            OpenSSL.EVP_CIPHER_CTX_set_padding(ctx, 0);\r\n        }\r\n\r\n        public override void cipherEncrypt(byte[] plaintext, uint plen, byte[] ciphertext, ref uint clen)\r\n        {\r\n            OpenSSL.SetCtxNonce(_encryptCtx, _encNonce, true);\r\n            // buf: all plaintext\r\n            // outbuf: ciphertext + tag\r","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/shadowsocks/shadowsocks-windows/blob/891d971682eefcaa2e640258d3b352a3ad3b2233/shadowsocks-csharp/Encryption/AEAD/AEADOpenSSLEncryptor.cs#L53-L89","documentation":"Thrown from AEADOpenSSLEncryptor.InitCipher when EVP_CIPHER_CTX_set_key_length returns != 1. OpenSSL rejects a key length that does not match the cipher's defined key sizes; for fixed-key AEAD ciphers like AES-GCM the length must be exactly 16 or 32 bytes.","triggerScenarios":"keyLen (from EncryptorInfo.KeySize) does not equal the cipher's valid key size (e.g. 24 for AES-GCM); a buffer underlength so the derived key is short; a custom method with wrong KeySize metadata.","commonSituations":"EncryptorInfo.KeySize wrong for the method; master/subkey buffer shorter than declared; method string that maps to a cipher with a different key size than configured.","solutions":["Set EncryptorInfo.KeySize to a valid size for the cipher (AES-128-GCM=16, AES-256-GCM=32, chacha20-poly1305=32).","Confirm the derived subkey buffer is exactly keyLen bytes.","Ensure the method string selects the cipher whose key size matches KeySize."],"exampleFix":"// before\nret = OpenSSL.EVP_CIPHER_CTX_set_key_length(ctx, keyLen);\nif (ret != 1) throw new System.Exception(\"openssl: fail to set key length\");\n\n// after\nif (ret != 1)\n    throw new System.Exception($\"openssl: fail to set key length (keyLen={keyLen}); check EncryptorInfo.KeySize for {_method}\");","handlingStrategy":"validation","validationCode":"// Validate key length against the cipher before set_key_length\nint expected = _innerLibName.Contains(\"128\") ? 16 : _innerLibName.Contains(\"256\") ? 32 : -1;\nif (keyLen != expected) throw new ArgumentException($\"keyLen {keyLen} != expected {expected}\");","typeGuard":"bool KeyLenMatchesCipher(string innerName, int keyLen) =>\n    (innerName.Contains(\"128\") && keyLen == 16) ||\n    (innerName.Contains(\"256\") && keyLen == 32) ||\n    (innerName.StartsWith(\"chacha20\") && keyLen == 32);","tryCatchPattern":"try { OpenSSL.EVP_CIPHER_CTX_set_key_length(ctx, keyLen); }\ncatch (Exception ex) when (ex.Message.Contains(\"key length\"))\n{ /* fix EncryptorInfo.KeySize for this method */ }","preventionTips":["Set EncryptorInfo.KeySize to the cipher's exact key size.","Confirm the derived subkey buffer is keyLen bytes.","Match the method string to a cipher whose key size equals KeySize."],"tags":["encryption","aead","openssl","key","configuration"],"backgroundTag":null,"analyzedSha":"891d971682eefcaa2e640258d3b352a3ad3b2233","analyzedAt":"2026-08-13T10:12:34.434Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}