Я ищу собственное решение OpenSSL для этой проблемы, и все остальные предложения НЕ помогли. Я пытаюсь создать пакет JWT, который можно использовать для входа в службы Google из кода C, поскольку я хочу написать приложение X.
Вот мой код:
#include
#include
#include
#include
#include
void getsha(char* msg,char* hash){
//This function wont work, yet it returns a 256 byte value as hash.
//I'm given a key. let's assume its this (but more extravagant).
char* pky="-----BEGIN PRIVATE KEY-----\nAAAAAAAAAAA==\n-----END PRIVATE KEY-----\n";
unsigned int psz=strlen(pky),msz=strlen(msg),len=0;
memset(hash,0,384); //wipe out output hash
OpenSSL_add_all_digests();
BIO* IO=BIO_new_mem_buf(pky,psz); //make a buffer?
struct evp_pkey_st* SK=PEM_read_bio_PrivateKey(IO,NULL,NULL,NULL);
//error out if function fails
if(!SK){
//we reach here if we use the example private key above, but if you replace it
//with a real private key, this test will pass.
BIO_free_all(IO);
EVP_PKEY_free(SK);
printf("Error getting key\n");
exit(-1);
}
EVP_MD_CTX *X=EVP_MD_CTX_create();
//We want SHA256 key
if(!EVP_SignInit(X,EVP_sha256())){
printf("Error getting digest\n");
exit(-1);
}
//We want to add our generated JWT
if(!EVP_SignUpdate(X,msg,msz)){
printf("Error updating key/digest\n");
exit(-1);
}
//Then we sign the key
//This function gives warnings if the string isn't an unsigned char
//but does that matter if I'm only feeding in base64 values?
EVP_SignFinal(X,(unsigned char*)hash,&len,SK);
//Free allocations
BIO_free_all(IO);
EVP_PKEY_free(SK);
EVP_MD_CTX_destroy(X);
}
char* b64enc(const char* in,char* res,const int bufsz){
//Use modified lookup table because today's base-64 standards are different when dealing with JWT
const char* lut="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
//get output pointer and size of input
int n=0,insz=strlen(in);
char *rp=res;
//exit if input buffer is too small but we made large buffers for our test so this shouldn't happen.
if((insz/3)*4 +2>bufsz){return NULL;}
//wipe out output buffer
memset(res,0,bufsz);
while(n
Подробнее здесь: [url]https://stackoverflow.com/questions/79218856/creating-jwt-packet-with-c-that-jwt-io-and-google-can-accept[/url]
Я ищу собственное решение OpenSSL для этой проблемы, и все остальные предложения НЕ помогли. Я пытаюсь создать пакет JWT, который можно использовать для входа в службы Google из кода C, поскольку я хочу написать приложение X. Вот мой код:[code]#include #include #include #include #include
void getsha(char* msg,char* hash){ //This function wont work, yet it returns a 256 byte value as hash. //I'm given a key. let's assume its this (but more extravagant). char* pky="-----BEGIN PRIVATE KEY-----\nAAAAAAAAAAA==\n-----END PRIVATE KEY-----\n"; unsigned int psz=strlen(pky),msz=strlen(msg),len=0; memset(hash,0,384); //wipe out output hash OpenSSL_add_all_digests(); BIO* IO=BIO_new_mem_buf(pky,psz); //make a buffer? struct evp_pkey_st* SK=PEM_read_bio_PrivateKey(IO,NULL,NULL,NULL); //error out if function fails if(!SK){ //we reach here if we use the example private key above, but if you replace it //with a real private key, this test will pass. BIO_free_all(IO); EVP_PKEY_free(SK); printf("Error getting key\n"); exit(-1); } EVP_MD_CTX *X=EVP_MD_CTX_create(); //We want SHA256 key if(!EVP_SignInit(X,EVP_sha256())){ printf("Error getting digest\n"); exit(-1); } //We want to add our generated JWT if(!EVP_SignUpdate(X,msg,msz)){ printf("Error updating key/digest\n"); exit(-1); } //Then we sign the key //This function gives warnings if the string isn't an unsigned char //but does that matter if I'm only feeding in base64 values? EVP_SignFinal(X,(unsigned char*)hash,&len,SK); //Free allocations BIO_free_all(IO); EVP_PKEY_free(SK); EVP_MD_CTX_destroy(X); }
char* b64enc(const char* in,char* res,const int bufsz){ //Use modified lookup table because today's base-64 standards are different when dealing with JWT const char* lut="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; //get output pointer and size of input int n=0,insz=strlen(in); char *rp=res; //exit if input buffer is too small but we made large buffers for our test so this shouldn't happen. if((insz/3)*4 +2>bufsz){return NULL;} //wipe out output buffer memset(res,0,bufsz); while(n