OpenSSL HMAC SHA 256 pour l'api REST de Windows Azure Storage
Bonjour,
J'essaye de mettre en oeuvre les requêtes http pour accéder à un blob storage du cloud Azure.
Pour cela je dois signer la requête en utilisant HMAC SHA 256.
D'après le message d'erreur que je reçois, la signature que j'envoie n'est pas bonne et je pense que j'ai mal compris le fonctionnement de HMAC_Init_ex et HMAC_Update.
Je suis sur une machine Ubuntu et j'utilise Qt Creator.
Voici mon code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
unsigned char* signature = (unsigned char*) ("GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:" + date + "\nx-ms-version:2009-09-19\n/monCompteStorage\ncomp:list").toStdString().c_str(); //date est un QString qui a la forme "Fri, 20 Apr 2012 11:16:11 GMT"
unsigned char* key = (unsigned char*) "***********************************"; //clé d'accès primaire de google qui fait 88 caractères
unsigned char* result;
unsigned int result_len = 44;
result = (unsigned char*)malloc(sizeof(char) * result_len);
ENGINE_load_builtin_engines();
ENGINE_register_all_complete();
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx, key, 88, EVP_sha256(), NULL);
HMAC_Update(&ctx, signature, 56);
HMAC_Final(&ctx, result, &result_len);
HMAC_CTX_cleanup(&ctx);
QByteArray array = QByteArray::fromRawData((char*)result, result_len);
array = array.toBase64();
qDebug() << array; |
Bonne journée à tous.
Update du code et réponse obtenue
J'ai changé le code (je pense que j'avais confondu la taille et le nombre de caractères :oops:)
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
unsigned char* signature = (unsigned char*) ("GET\n\n\n\n\n\n\n\n\n\n\n\nx-ms-date:" + date + "\nx-ms-version:2009-09-19\n/monCompteStorage\ncomp:list").toStdString().c_str(); //date est un QString qui a la forme "Fri, 20 Apr 2012 11:16:11 GMT"
unsigned char* key = (unsigned char*) "***********************************"; //clé d'accès primaire d'Azure
unsigned char* result;
unsigned int result_len = 32;
result = (unsigned char*)malloc(sizeof(char) * result_len);
ENGINE_load_builtin_engines();
ENGINE_register_all_complete();
HMAC_CTX ctx;
HMAC_CTX_init(&ctx);
HMAC_Init_ex(&ctx, key, 32, EVP_sha256(), NULL);
HMAC_Update(&ctx, signature, 32);
HMAC_Final(&ctx, result, &result_len);
HMAC_CTX_cleanup(&ctx);
QByteArray array = QByteArray::fromRawData((char*)result, result_len);
array = array.toBase64();
qDebug() << array; |
Mais la signature n'est toujours pas bonne et je ne comprend toujours pas d'où doit venir l'argument len qu'il faut donner à int HMAC_Update.
La doc d'OpenSSL
Citation:
int HMAC_Init_ex(HMAC_CTX *ctx, const void *key, int key_len,
const EVP_MD *md, ENGINE *impl);
int HMAC_Update(HMAC_CTX *ctx, const unsigned char *data, int len);
int HMAC_Final(HMAC_CTX *ctx, unsigned char *md, unsigned int *len);
Je rajoute la réponse que je reçois d'Azure:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
<?xml version="1.0" encoding="utf-8"?>
<Error>
<Code>AuthenticationFailed</Code>
<Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:f34e06cc-3ed1-46a0-b0b4-4651d429517c
Time:2012-04-23T11:44:59.1146695Z</Message>
<AuthenticationErrorDetail>The MAC signature found in the HTTP request 'Fuqpu3bnt9bAIsAjI5eDctLukvcsaozmF4EO+lVwY9o=' is not the same as any computed signature. Server used following string to sign: 'GET
x-ms-date:Mon, 23 Apr 2012 11:45:17 GMT
/monCompteStorage/?comp=list'.</AuthenticationErrorDetail>
</Error> |