1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file. 4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "net/quic/crypto/channel_id.h" 6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <openssl/bn.h> 8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <openssl/ec.h> 9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <openssl/ecdsa.h> 10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <openssl/obj_mac.h> 11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <openssl/sha.h> 12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "crypto/openssl_util.h" 14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "crypto/scoped_openssl_types.h" 15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)using base::StringPiece; 17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace net { 19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// static 21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)bool ChannelIDVerifier::Verify(StringPiece key, 22868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) StringPiece signed_data, 23868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) StringPiece signature) { 247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) return VerifyRaw(key, signed_data, signature, true); 257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)} 267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) 277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// static 287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool ChannelIDVerifier::VerifyRaw(StringPiece key, 297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) StringPiece signed_data, 307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) StringPiece signature, 317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) bool is_channel_id_signature) { 32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (key.size() != 32 * 2 || 33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) signature.size() != 32 * 2) { 34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return false; 35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch crypto::ScopedOpenSSL<EC_GROUP, EC_GROUP_free>::Type p256( 38868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1)); 39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (p256.get() == NULL) { 40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return false; 41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch crypto::ScopedBIGNUM x(BN_new()), y(BN_new()), r(BN_new()), s(BN_new()); 44868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ECDSA_SIG sig; 46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) sig.r = r.get(); 47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) sig.s = s.get(); 48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const uint8* key_bytes = reinterpret_cast<const uint8*>(key.data()); 507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) const uint8* signature_bytes = 517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) reinterpret_cast<const uint8*>(signature.data()); 52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (BN_bin2bn(key_bytes + 0, 32, x.get()) == NULL || 547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) BN_bin2bn(key_bytes + 32, 32, y.get()) == NULL || 557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) BN_bin2bn(signature_bytes + 0, 32, sig.r) == NULL || 567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) BN_bin2bn(signature_bytes + 32, 32, sig.s) == NULL) { 57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return false; 58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch crypto::ScopedOpenSSL<EC_POINT, EC_POINT_free>::Type point( 61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) EC_POINT_new(p256.get())); 62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (point.get() == NULL || 63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(), 64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) y.get(), NULL)) { 65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return false; 66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch crypto::ScopedEC_KEY ecdsa_key(EC_KEY_new()); 69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) if (ecdsa_key.get() == NULL || 70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) !EC_KEY_set_group(ecdsa_key.get(), p256.get()) || 71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) !EC_KEY_set_public_key(ecdsa_key.get(), point.get())) { 72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return false; 73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SHA256_CTX sha256; 76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SHA256_Init(&sha256); 777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (is_channel_id_signature) { 787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) SHA256_Update(&sha256, kContextStr, strlen(kContextStr) + 1); 797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) SHA256_Update(&sha256, kClientToServerStr, strlen(kClientToServerStr) + 1); 807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) } 81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SHA256_Update(&sha256, signed_data.data(), signed_data.size()); 82868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 83868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) unsigned char digest[SHA256_DIGEST_LENGTH]; 84868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) SHA256_Final(digest, &sha256); 85868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 86868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return ECDSA_do_verify(digest, sizeof(digest), &sig, ecdsa_key.get()) == 1; 87868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 88868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} // namespace net 90