<p>I am using the basic-auth twitter API (<a href="https://groups.google.com/d/topic/twitter-development-talk/wD-isa6Q0qk/discussion" rel="noreferrer">no longer available</a>) to integrate twitter with my blog's commenting system.  The problem with this and many other web APIs out there is that they require the user's username and password to do anything useful.  I don't want to deal with the hassle and cost of installing a SSL certificate, but I also don't want passwords passed over the wire in clear text.</p>

<p>I guess my general question is: <strong>How can I send sensitive data over an insecure channel?</strong></p>

<p>This is my current solution and I'd like to know if there are any holes in it:</p>

<ol>
<li>Generate a random key on the server (I'm using php).</li>
<li>Save the key in a session and also output the key in a javascript variable.</li>
<li>On form submit, use <a href="http://tero.co.uk/des/" rel="noreferrer">Triple DES in javascript</a> with the key to encrypt the password.</li>
<li>On the server, decrypt the password using the key from the session and then destroy the session.</li>
</ol>

<p>The end result is that only the encrypted password is sent over the wire and the key is only used once and never sent with the password.  Problem solved?</p>

[{'answer_id': 38845, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 3, 'selected': False, 'text': '<p>Your method has a flaw - if someone were to intercept the transmission of the key to the user and the user\'s encrypted reply they could decrypt the reply and obtain the username/password of the user.</p>\n\n<p>However, there is a way to securely send information over an unsecure medium so long as the information is not capable of being modified in transit known as the <a href="http://en.wikipedia.org/wiki/Diffie-Hellman" rel="noreferrer">Diffie-Hellman algorithm</a>. Basically two parties are able to compute the shared key used to encrypt the data based on their conversations - yet an observer does not have enough information to deduce the key.</p>\n\n<p>Setting up the conversation between the client and the server can be tricky though, and much more time consuming than simply applying SSL to your site. You don\'t even have to pay for it - you can generate a self-signed certificate that provides the necessary encryption. This won\'t protect against man-in-the-middle attacks, but neither will the Diffie-Hellman algorithm.</p>\n'}, {'answer_id': 38850, 'author': 'Chris Upchurch', 'author_id': 2600, 'author_profile': 'https://Stackoverflow.com/users/2600', 'pm_score': 5, 'selected': False, 'text': "<blockquote>\n  <ol>\n  <li>Generate a random key on the server (I'm using php).</li>\n  <li>Save the key in a session and also output the key in a javascript variable.</li>\n  <li>On form submit, use Triple DES in javascript with the key to encrypt the password.</li>\n  </ol>\n</blockquote>\n\n<p>This avoids sending the password in the clear over the wire, but it requires you to send the key in the clear over the wire, which would allow anyone eavesdropping to decode the password.</p>\n\n<p>It's been said before and I'll say it again: don't try to make up your own cryptographic protocols!  There are established protocols out there for this kind of thing that have been created, peer reviewed, beat on, hacked on, poked and prodded by professionals, <strong>use them!</strong>  No one person is going to be able to come up with something better than the entire cryptographic and security community working together.</p>\n"}, {'answer_id': 38854, 'author': 'Till', 'author_id': 2859, 'author_profile': 'https://Stackoverflow.com/users/2859', 'pm_score': 2, 'selected': False, 'text': '<p>So how is this any more secure? Even though you might have secured browser&lt;>your server, what about the rest of the Internet (your server&lt;>twitter)?</p>\n\n<p>IMHO, it\'s unacceptable to ask for a username and password of another service and expect people to enter that. And if you care that much - don\'t integrate them until they get their act straight and re-enable <a href="http://oauth.net/" rel="nofollow noreferrer">OAuth</a>. (They supported it for a while, but disabled it a few months ago.)</p>\n\n<p>In the mean time, why not offer OpenID? Every Google, Yahoo!, VOX etc. account has one. People might not be aware of it but chances are really, really high that they already have OpenID. <a href="http://openid.net/get/" rel="nofollow noreferrer">Check this list</a> to see what I mean.</p>\n'}, {'answer_id': 38858, 'author': 'Jim McKeeth', 'author_id': 255, 'author_profile': 'https://Stackoverflow.com/users/255', 'pm_score': 2, 'selected': False, 'text': '<p>When the key is sent between the client and the server it is clear text and subject to interception.  Combine that with the encrypted text of the password and the password is decrypted.  </p>\n\n<p><a href="http://en.wikipedia.org/wiki/Diffie-Hellman" rel="nofollow noreferrer">Diffie-Hellman</a> is a good solution.  If you only need to authenticate them, and not actually transmit the password (because the password is already stored on the server) then you can use <a href="http://www.ietf.org/rfc/rfc2617.txt" rel="nofollow noreferrer">HTTP Digest Authentication</a>, or some variation there of.</p>\n'}, {'answer_id': 38879, 'author': 'erickson', 'author_id': 3474, 'author_profile': 'https://Stackoverflow.com/users/3474', 'pm_score': 3, 'selected': False, 'text': "<p>You don't have to have a certificate on your server; it's up to the client whether they are willing to talk to an unauthenticated server. Key agreement can still be performed to establish a private channel. It wouldn't be safe to send private credentials to an unauthenticated server though, which is why you don't see SSL used this way in practice.</p>\n\n<p>To answer your general question: <strong>you just send it.</strong> I think your <em>real</em> general question is: <em>&ldquo;How do I send sensitive data over an insecure channel&mdash;and keep it secure?&rdquo;</em> <strong>You can't.</strong></p>\n\n<p>It sounds like you've decided that security isn't worth the $10&ndash;20 per month a certificate would cost, and to protect Twitter passwords, that's probably true. So, why spend time to provide the illusion of security? Just make it clear to your users that their password will be sent in the clear and let them make their own choice.</p>\n"}, {'answer_id': 128204, 'author': 'Oli', 'author_id': 15296, 'author_profile': 'https://Stackoverflow.com/users/15296', 'pm_score': 1, 'selected': False, 'text': "<p>I've implemented a different approach</p>\n\n<ol>\n<li>Server: user name and password-hash stored in the database</li>\n<li>Server: send a challenge with the form to request the password, store it in the session with a timestamp and the client's IP address</li>\n<li>Client: hash the password, concat challenge|username|passwordhash, hash it again and post it to the server</li>\n<li>Server: verify timestamp, IP, do the same concatenation/hashing and compare it</li>\n</ol>\n\n<p>This applies to a password transmission. Using it for data means using the final hash as the encryption key for the plain text and generating a random initialization vector transmitted with the cipher text to the server.</p>\n\n<p>Any comments on this?</p>\n"}, {'answer_id': 254006, 'author': 'bmm6o', 'author_id': 10558, 'author_profile': 'https://Stackoverflow.com/users/10558', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>How can I send sensitive data over an\n  insecure channel</p>\n</blockquote>\n\n<p>With a pre-shared secret key.  This is what you attempt in your suggested solution, but you can't send that key over the insecure channel.  Someone mentioned DH, which will help you negotiate a key.  But the other part of what SSL does is provide authentication, to prevent man-in-the-middle attacks so that the client knows they are negotiating a key with the person they intend to communicate with.</p>\n\n<p>Chris Upchurch's advice is really the only good answer there is for 99.99% of engineers - don't do it.  Let someone else do it and use their solution (like the guys who wrote the SSL client/server).</p>\n\n<p>I think the ideal solution here would be to get Twitter to support OpenID and then use that.</p>\n"}, {'answer_id': 254045, 'author': 'Marcin', 'author_id': 21640, 'author_profile': 'https://Stackoverflow.com/users/21640', 'pm_score': 0, 'selected': False, 'text': "<p>An ssl certificate that is self-signed doesn't cost money. For a free twitter service, that is probably just fine for users.</p>\n"}, {'answer_id': 3794943, 'author': 'bad_man', 'author_id': 458298, 'author_profile': 'https://Stackoverflow.com/users/458298', 'pm_score': 0, 'selected': False, 'text': "<p>TO OLI</p>\n\n<p>In your approch for example i'm in the same subnet with same router, so i get the same ip as my collegues in my work. I open same url in browser, so server generates the timestamp with same ip, then i use tcp/ip dump to sniff the hashed or non hashed password from my collegues connection. I can sniff everything he sends. So i have all hashes from his form also you have timestamp(my) and same ip. So i send everything using post tool and hey i'm loggen in.</p>\n"}, {'answer_id': 8304476, 'author': 'Jack', 'author_id': 1070405, 'author_profile': 'https://Stackoverflow.com/users/1070405', 'pm_score': 0, 'selected': False, 'text': '<p>If you don\'t want to use SSL, why not try some other protocol, such as kerberos?</p>\n\n<p>A basic overview is here:\n<a href="http://www.kerberos.org/software/tutorial.html" rel="nofollow">http://www.kerberos.org/software/tutorial.html</a></p>\n\n<p>Or if you want to go somewhat more in depth, see\n<a href="http://www.hitmill.com/computers/kerberos.html" rel="nofollow">http://www.hitmill.com/computers/kerberos.html</a></p>\n'}, {'answer_id': 10237038, 'author': 'Dsyko', 'author_id': 381608, 'author_profile': 'https://Stackoverflow.com/users/381608', 'pm_score': 0, 'selected': False, 'text': '<p>I have a similar issue(wanting to encrypt data in forms without paying for an ssl certificate) so I did some hunting and found this project: <a href="http://www.jcryption.org/" rel="nofollow">http://www.jcryption.org/</a></p>\n\n<p>I haven\'t used it yet but it looks easy to implement and thought I\'d share it here in-case anyone else is looking for something like it and finds themselves on this page like I did.</p>\n'}, {'answer_id': 14853745, 'author': 'Kothar', 'author_id': 37416, 'author_profile': 'https://Stackoverflow.com/users/37416', 'pm_score': 2, 'selected': False, 'text': '<p><strong>APIs and OAuth</strong></p>\n\n<p>Firstly, as others have said, you shouldn\'t be using a user\'s password to access the API, you should be getting an <a href="https://dev.twitter.com/docs/auth/oauth/faq" rel="nofollow">OAuth token</a>. This will allow you to act on that user\'s behalf without needing their password. This is a common approach used by many APIs.</p>\n\n<p><strong>Key Exchange</strong></p>\n\n<p>If you need to solve the more general problem of exchanging information over insecure connections, there are several key exchange protocols as mentioned by other answers.</p>\n\n<p>In general key exchange algorithms are secure from eavesdroppers, but because they do not authenticate the identity of the users, they are vulnerable to man-in-the-middle attacks.</p>\n\n<p>From the Wikipedia page on <a href="http://en.wikipedia.org/wiki/Diffie%E2%80%93Hellman_key_exchange" rel="nofollow">Diffie Hellman</a>:</p>\n\n<blockquote>\n  <p>In the original description, the\n  Diffie–Hellman exchange by itself does not provide authentication of\n  the communicating parties and is thus vulnerable to a\n  man-in-the-middle attack. A person in the middle may establish two\n  distinct Diffie–Hellman key exchanges, one with Alice and the other\n  with Bob, effectively masquerading as Alice to Bob, and vice versa,\n  allowing the attacker to decrypt (and read or store) then re-encrypt\n  the messages passed between them. A method to authenticate the\n  communicating parties to each other is generally needed to prevent\n  this type of attack. Variants of Diffie-Hellman, such as <a href="http://en.wikipedia.org/wiki/Station-to-Station_protocol" rel="nofollow">STS</a>, may be\n  used instead to avoid these types of attacks.</p>\n</blockquote>\n\n<p>Even STS is insecure in some cases where an attacker is able to insert their own identity (signing key) in place of either the sender or receiver.</p>\n\n<p><strong>Identity and Authentication</strong></p>\n\n<p>This is exactly the problem SSL is designed to solve, by establishing a hierarchy of \'trusted\' signing authorities which have <em>in theory</em> verified who owns a domain name, etc, someone connecting to a website can verify that they are indeed communicating with that domain\'s server, and not with a man-in-the-middle who has placed themselves in between.</p>\n\n<p>You can create a self-signed certificate which will provide the necessary configuration to encrypt the connection, but will not protect you from man in the middle attacks for the same reason that unauthenticated Diffie-Hellman key exchange will not.</p>\n\n<p>You can get free SSL certificates valid for 1 year from <a href="https://www.startssl.com/" rel="nofollow">https://www.startssl.com/</a> - I use them for my personal sites. They\'re not quite as \'trusted\' whatever that means, since they only do automatic checks on people who apply for one, but it\'s free. There are also services which cost very little (£10/year from 123-Reg in the UK).</p>\n'}, {'answer_id': 36838355, 'author': 'Kadigan.KSB', 'author_id': 6250941, 'author_profile': 'https://Stackoverflow.com/users/6250941', 'pm_score': 1, 'selected': False, 'text': '<p>The problem with client-side javascript security is that the attacker can modify the javascript in transit to a simple {return input;} thereby rendering your elaborate security moot. Solution: use browser-provided (ie. not transmitted) RSA. From what I know, not available yet.</p>\n'}]
['https://Stackoverflow.com/questions/38838', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/744/']