目标:使用SSH协议连接本地计算机(iPod)到unix服务器。
首先我已经建立了一个套接字连接进行通信,它已经完成了。
现在是与SSH协商的程序。
client to server -> http://@aix.polarhome.com/ssh with port : 775 (using GCDAsyncSocket)
成功连接
server to client -> SSH-1.99-OpenSSH-6.6
client to server -> SSH-2.0-OpenSSH_6.6
server to client -> list of algo
cleint to server ->
这个代码格式
if (parsedByteArray[2] == ACK && parsedByteArray[5] == SSH_MSG_KEXINIT) {
//1 SSH_MSG_KEXINIT
sendByte[writeIndex++] = SSH2_MSG_KEXINIT;
[self put32BitInteger:16 toPacket:sendByte fromIndex:writeIndex];
writeIndex += 4;
//2 cookie (16 random bytes)
for (int i = 0; i < 16; i++) {
sendByte[writeIndex++] = [self random_byte];
}
//3 kex_algorithms
NSString *kex_algorithms = [self kexAlgorithms];
[self put32BitInteger:kex_algorithms.length toPacket:sendByte fromIndex:writeIndex];
writeIndex += 4;
writeIndex = [self convertFromString:kex_algorithms toBytesAndFillArray:sendByte withCurrentIndex:writeIndex];
//4 server_host_key_algorithms
NSString *server_host_key_algorithms = [self serverHostKeyAlgorithms];
[self put32BitInteger:server_host_key_algorithms.length toPacket:sendByte fromIndex:writeIndex];
writeIndex += 4;
writeIndex = [self convertFromString:server_host_key_algorithms toBytesAndFillArray:sendByte withCurrentIndex:writeIndex];
//5 encryption_algorithms_client_to_server
NSString *encryption_algorithms = [self encryptionAlgorithms];
[self put32BitInteger:encryption_algorithms.length toPacket:sendByte fromIndex:writeIndex];
writeIndex += 4;
writeIndex = [self convertFromString:encryption_algorithms toBytesAndFillArray:sendByte withCurrentIndex:writeIndex];
//6 encryption_algorithms_server_to_client
[self put32BitInteger:encryption_algorithms.length toPacket:sendByte fromIndex:writeIndex];
writeIndex += 4;
writeIndex = [self convertFromString:encryption_algorithms toBytesAndFillArray:sendByte withCurrentIndex:writeIndex];
//7 mac_algorithms_client_to_server
NSString *mac_algorithms = [self macAlgorithms];
[self put32BitInteger:mac_algorithms.length toPacket:sendByte fromIndex:writeIndex];
writeIndex += 4;
writeIndex = [self convertFromString:mac_algorithms toBytesAndFillArray:sendByte withCurrentIndex:writeIndex];
//8 mac_algorithms_server_to_client
[self put32BitInteger:mac_algorithms.length toPacket:sendByte fromIndex:writeIndex];
writeIndex += 4;
writeIndex = [self convertFromString:mac_algorithms toBytesAndFillArray:sendByte withCurrentIndex:writeIndex];
//9 compression_algorithms_client_to_server
NSString *compression_algorithms = [self compressionAlgorithms];
[self put32BitInteger:compression_algorithms.length toPacket:sendByte fromIndex:writeIndex];
writeIndex += 4;
writeIndex = [self convertFromString:compression_algorithms toBytesAndFillArray:sendByte withCurrentIndex:writeIndex];
//10 compression_algorithms_server_to_client
[self put32BitInteger:compression_algorithms.length toPacket:sendByte fromIndex:writeIndex];
writeIndex += 4;
writeIndex = [self convertFromString:compression_algorithms toBytesAndFillArray:sendByte withCurrentIndex:writeIndex];
//11 languages_client_to_server
[self put32BitInteger:0 toPacket:sendByte fromIndex:writeIndex];
writeIndex += 4;
//12 languages_server_to_client
[self put32BitInteger:0 toPacket:sendByte fromIndex:writeIndex];
writeIndex += 4;
//13 first_kex_packet_follows
[self put32BitInteger:1 toPacket:sendByte fromIndex:writeIndex];
writeIndex += 4;
sendByte[writeIndex++] = 0; //FALSE
//14 0 (reserved for future extension) int32
[self put32BitInteger:0 toPacket:sendByte fromIndex:writeIndex];
writeIndex += 4;
[self sendSSHBinaryPacketPayload:sendByte toLength:writeIndex];
writeIndex = 0;
}
-(void)put32BitInteger:(NSInteger)val toPacket:(Byte *)packet fromIndex:(int)index{
//parse int value from 32 bit to 4 bytes and assign to packet.
Byte bArray[4];
bArray[3] = val;
bArray[2] = val >> 8;
bArray[1] = val >> 16;
bArray[0] = val >> 24;
for (int i = 0; i < 4; i++) {
packet[index + i] = bArray[i];
}
}
//SSH Encryption Algorithms
-(NSString *)kexAlgorithms{
return @"diffie-hellman-group-exchange-sha1";
}
-(NSString *)serverHostKeyAlgorithms{
return @"ssh-rsa";
}
-(NSString *)encryptionAlgorithms{
return @"aes256-ctr";
}
-(NSString *)macAlgorithms{
return @"hmac-sha2-256";
}
-(NSString *)compressionAlgorithms{
return @"none";
}
-(NSString *)languageAlgorithms{
return @"none";
}
server to client -> Packet corrupted.
请建议我,是不是正确的方法。我需要实施哪些更正。
我不清楚的事情是 1.版本字符串首先从服务器或客户端协商。这里服务器发送它和 2.我是否需要以普通方式或BPP(二进制数据包协议)发送密钥交换数据。
请帮帮我。
答案 0 :(得分:0)
1)不久前我正在为一个侧面项目做类似的事情。在尝试实现/理解握手过程时,我发现本书非常有用:
使用加密和PKI实施SSL / TLS 约书亚戴维斯
那本书提供了很好的示例代码和建议。
2)我注意到你发布的示例代码中,你似乎有很多用Objective-C编写的辅助函数,如果你实现你的辅助函数,你可能会更容易增加代码的可读性平原C.您还应该使用结构来保存数据包的数据结构,这将使您的生活更轻松。
3)根据您发布的示例代码,很难为您提供有关出错的帮助,您可以发布一个要点或更完整的代码段。