static int get_key(char *getkey) { CURLcode res; char errorbuffer[CURL_ERROR_SIZE]; char request[MAX_URL]; struct curl_writer_ctx ctx; memset(&ctx,0,sizeof(ctx)); if(strncmp(getkey,"0x",2)==0) getkey+=2; fprintf(output,"KEY 0x%s BEGIN\n",getkey); sprintf(request,"%s://%s%s%s%s",opt->scheme,opt->host, opt->port?":":"",opt->port?opt->port:"",opt->path?opt->path:"/"); curl_easy_setopt(curl,CURLOPT_URL,request); curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,curl_writer); ctx.stream=output; curl_easy_setopt(curl,CURLOPT_FILE,&ctx); curl_easy_setopt(curl,CURLOPT_ERRORBUFFER,errorbuffer); res=curl_easy_perform(curl); if(res!=CURLE_OK) { fprintf(console,"gpgkeys: %s fetch error %d: %s\n",opt->scheme, res,errorbuffer); fprintf(output,"\nKEY 0x%s FAILED %d\n",getkey,curl_err_to_gpg_err(res)); } else { curl_writer_finalize(&ctx); if(!ctx.flags.done) { fprintf(console,"gpgkeys: no key data found for %s\n",request); fprintf(output,"\nKEY 0x%s FAILED %d\n", getkey,KEYSERVER_KEY_NOT_FOUND); } else fprintf(output,"\nKEY 0x%s END\n",getkey); } return curl_err_to_gpg_err(res); }
static int get_name(const char *getkey) { CURLcode res; char *request=NULL; char *searchkey_encoded; int ret=KEYSERVER_INTERNAL_ERROR; struct curl_writer_ctx ctx; memset(&ctx,0,sizeof(ctx)); searchkey_encoded=curl_easy_escape(curl,(char *)getkey,0); if(!searchkey_encoded) { fprintf(console,"gpgkeys: out of memory\n"); ret=KEYSERVER_NO_MEMORY; goto fail; } request=xtrymalloc(MAX_URL+60+strlen(searchkey_encoded)); if(!request) { fprintf(console,"gpgkeys: out of memory\n"); ret=KEYSERVER_NO_MEMORY; goto fail; } fprintf(output,"NAME %s BEGIN\n",getkey); strcpy(request,proto); strcat(request,"://"); strcat(request,opt->host); strcat(request,":"); strcat(request,port); strcat(request,opt->path); append_path(request,"/pks/lookup?op=get&options=mr&search="); strcat(request,searchkey_encoded); if(opt->action==KS_GETNAME) strcat(request,"&exact=on"); if(opt->verbose>2) fprintf(console,"gpgkeys: HTTP URL is `%s'\n",request); curl_easy_setopt(curl,CURLOPT_URL,request); curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,curl_writer); ctx.stream=output; curl_easy_setopt(curl,CURLOPT_FILE,&ctx); res=curl_easy_perform(curl); if(res!=CURLE_OK) { fprintf(console,"gpgkeys: HTTP fetch error %d: %s\n",res,errorbuffer); ret=curl_err_to_gpg_err(res); } else { curl_writer_finalize(&ctx); if(!ctx.flags.done) { fprintf(console,"gpgkeys: key %s not found on keyserver\n",getkey); ret=KEYSERVER_KEY_NOT_FOUND; } else { fprintf(output,"\nNAME %s END\n",getkey); ret=KEYSERVER_OK; } } fail: curl_free(searchkey_encoded); free(request); if(ret!=KEYSERVER_OK) fprintf(output,"\nNAME %s FAILED %d\n",getkey,ret); return ret; }
static int get_key(char *getkey) { CURLcode res; char request[MAX_URL+92]; char *offset; struct curl_writer_ctx ctx; size_t keylen; memset(&ctx,0,sizeof(ctx)); /* Build the search string. HKP only uses the short key IDs. */ if(strncmp(getkey,"0x",2)==0) getkey+=2; fprintf(output,"KEY 0x%s BEGIN\n",getkey); if(strlen(getkey)==32) { fprintf(console, "gpgkeys: HKP keyservers do not support v3 fingerprints\n"); fprintf(output,"KEY 0x%s FAILED %d\n",getkey,KEYSERVER_NOT_SUPPORTED); return KEYSERVER_NOT_SUPPORTED; } strcpy(request,proto); strcat(request,"://"); strcat(request,opt->host); strcat(request,":"); strcat(request,port); strcat(request,opt->path); /* request is MAX_URL+55 bytes long - MAX_URL covers the whole URL, including any supplied path. The 92 overcovers this /pks/... etc string plus the 8, 16, or 40 bytes of key id/fingerprint */ append_path(request,"/pks/lookup?op=get&options=mr&search=0x"); /* send only fingerprint, long key id, or short keyid. see: https://tools.ietf.org/html/draft-shaw-openpgp-hkp-00#section-3.1.1.1 */ keylen = strlen(getkey); if(keylen >= 40) offset=&getkey[keylen-40]; else if(keylen >= 16) offset=&getkey[keylen-16]; else if(keylen >= 8) offset=&getkey[keylen-8]; else offset=getkey; strcat(request,offset); if(opt->verbose>2) fprintf(console,"gpgkeys: HTTP URL is `%s'\n",request); curl_easy_setopt(curl,CURLOPT_URL,request); curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,curl_writer); ctx.stream=output; curl_easy_setopt(curl,CURLOPT_FILE,&ctx); res=curl_easy_perform(curl); if(res!=CURLE_OK) { fprintf(console,"gpgkeys: HTTP fetch error %d: %s\n",res,errorbuffer); fprintf(output,"\nKEY 0x%s FAILED %d\n",getkey,curl_err_to_gpg_err(res)); } else { curl_writer_finalize(&ctx); if(!ctx.flags.done) { fprintf(console,"gpgkeys: key %s not found on keyserver\n",getkey); fprintf(output,"\nKEY 0x%s FAILED %d\n", getkey,KEYSERVER_KEY_NOT_FOUND); } else fprintf(output,"\nKEY 0x%s END\n",getkey); } return KEYSERVER_OK; }
static int get_key(char *getkey) { CURLcode res; char request[MAX_URL+60]; char *offset; struct curl_writer_ctx ctx; memset(&ctx,0,sizeof(ctx)); /* Build the search string. HKP only uses the short key IDs. */ if(strncmp(getkey,"0x",2)==0) getkey+=2; fprintf(output,"KEY 0x%s BEGIN\n",getkey); if(strlen(getkey)==32) { fprintf(console, "gpgkeys: HKP keyservers do not support v3 fingerprints\n"); fprintf(output,"KEY 0x%s FAILED %d\n",getkey,KEYSERVER_NOT_SUPPORTED); return KEYSERVER_NOT_SUPPORTED; } strcpy(request,"http://"); strcat(request,opt->host); strcat(request,":"); if(opt->port) strcat(request,opt->port); else strcat(request,"11371"); strcat(request,opt->path); /* request is MAX_URL+55 bytes long - MAX_URL covers the whole URL, including any supplied path. The 60 overcovers this /pks/... etc string plus the 8 bytes of key id */ append_path(request,"/pks/lookup?op=get&options=mr&search=0x"); /* fingerprint or long key id. Take the last 8 characters and treat it like a short key id */ if(strlen(getkey)>8) offset=&getkey[strlen(getkey)-8]; else offset=getkey; strcat(request,offset); if(opt->verbose>2) fprintf(console,"gpgkeys: HTTP URL is `%s'\n",request); curl_easy_setopt(curl,CURLOPT_URL,request); curl_easy_setopt(curl,CURLOPT_WRITEFUNCTION,curl_writer); ctx.stream=output; curl_easy_setopt(curl,CURLOPT_FILE,&ctx); res=curl_easy_perform(curl); if(res!=CURLE_OK) { fprintf(console,"gpgkeys: HTTP fetch error %d: %s\n",res,errorbuffer); fprintf(output,"\nKEY 0x%s FAILED %d\n",getkey,curl_err_to_gpg_err(res)); } else { curl_writer_finalize(&ctx); if(!ctx.flags.done) { fprintf(console,"gpgkeys: key %s not found on keyserver\n",getkey); fprintf(output,"\nKEY 0x%s FAILED %d\n", getkey,KEYSERVER_KEY_NOT_FOUND); } else fprintf(output,"\nKEY 0x%s END\n",getkey); } return KEYSERVER_OK; }