/* * Check with the CA whether a challenge has been processed. * Note: we'll only do this a limited number of times, and pause for a * time between checks, but this happens in the caller. */ static int dochngcheck(struct conn *c, struct chng *chng) { int cc; long lc; struct jsmnn *j; dodbg("%s: status", chng->uri); if ((lc = nreq(c, chng->uri)) < 0) { warnx("%s: bad comm", chng->uri); return(0); } else if (200 != lc && 201 != lc && 202 != lc) { warnx("%s: bad HTTP: %ld", chng->uri, lc); buf_dump(&c->buf); return(0); } else if (NULL == (j = json_parse(c->buf.buf, c->buf.sz))) { warnx("%s: bad JSON object", chng->uri); buf_dump(&c->buf); return(0); } else if (-1 == (cc = json_parse_response(j))) { warnx("%s: bad response", chng->uri); buf_dump(&c->buf); json_free(j); return(0); } else if (cc > 0) chng->status = 1; json_free(j); return(1); }
/* * Look up directories from the certificate authority. */ static int dodirs(struct conn *c, const char *addr, struct capaths *paths) { struct jsmnn *j; long lc; int rc; j = NULL; rc = 0; dodbg("%s: directories", addr); if ((lc = nreq(c, addr)) < 0) warnx("%s: bad comm", addr); else if (200 != lc && 201 != lc) warnx("%s: bad HTTP: %ld", addr, lc); else if (NULL == (j = json_parse(c->buf.buf, c->buf.sz))) warnx("json_parse"); else if ( ! json_parse_capaths(j, paths)) warnx("%s: bad CA paths", addr); else rc = 1; if (0 == rc || verbose > 1) buf_dump(&c->buf); json_free(j); return(rc); }
/* * Submit our certificate to the CA. * This, upon success, will return the signed CA. */ static int docert(struct conn *c, const char *addr, const char *cert) { char *req; int rc; long lc; rc = 0; dodbg("%s: certificate", addr); if (NULL == (req = json_fmt_newcert(cert))) warnx("json_fmt_newcert"); else if ((lc = sreq(c, addr, req)) < 0) warnx("%s: bad comm", addr); else if (200 != lc && 201 != lc) warnx("%s: bad HTTP: %ld", addr, lc); else if (0 == c->buf.sz || NULL == c->buf.buf) warnx("%s: empty response", addr); else rc = 1; if (0 == rc || verbose > 1) buf_dump(&c->buf); free(req); return(rc); }
static int dorevoke(struct conn *c, const char *addr, const char *cert) { char *req; int rc; long lc; lc = 0; rc = 0; dodbg("%s: revocation", addr); if (NULL == (req = json_fmt_revokecert(cert))) warnx("json_fmt_revokecert"); else if ((lc = sreq(c, addr, req)) < 0) warnx("%s: bad comm", addr); else if (200 != lc && 201 != lc && 409 != lc) warnx("%s: bad HTTP: %ld", addr, lc); else rc = 1; if (409 == lc) warnx("%s: already revoked", addr); if (0 == rc || verbose > 1) buf_dump(&c->buf); free(req); return(rc); }
/* * Request a challenge for the given domain name. * This must happen for each name "alt". * On non-zero exit, fills in "chng" with the challenge. */ static int dochngreq(struct conn *c, const char *alt, struct chng *chng, const struct capaths *p) { int rc; char *req; long lc; struct jsmnn *j; j = NULL; rc = 0; dodbg("%s: req-auth: %s", p->newauthz, alt); if (NULL == (req = json_fmt_newauthz(alt))) warnx("json_fmt_newauthz"); else if ((lc = sreq(c, p->newauthz, req)) < 0) warnx("%s: bad comm", p->newauthz); else if (200 != lc && 201 != lc) warnx("%s: bad HTTP: %ld", p->newauthz, lc); else if (NULL == (j = json_parse(c->buf.buf, c->buf.sz))) warnx("%s: bad JSON object", p->newauthz); else if ( ! json_parse_challenge(j, chng)) warnx("%s: bad challenge", p->newauthz); else rc = 1; if (0 == rc || verbose > 1) buf_dump(&c->buf); json_free(j); free(req); return(rc); }
/* * Send to the CA that we want to authorise a new account. * This only happens once for a new account key. * Returns non-zero on success. */ static int donewreg(struct conn *c, const struct capaths *p) { int rc; char *req; long lc; rc = 0; dodbg("%s: new-reg", p->newreg); if (NULL == (req = json_fmt_newreg(URL_LICENSE))) warnx("json_fmt_newreg"); else if ((lc = sreq(c, p->newreg, req)) < 0) warnx("%s: bad comm", p->newreg); else if (200 != lc && 201 != lc) warnx("%s: bad HTTP: %ld", p->newreg, lc); else if (NULL == c->buf.buf || 0 == c->buf.sz) warnx("%s: empty response", p->newreg); else rc = 1; if (0 == rc || verbose > 1) buf_dump(&c->buf); free(req); return(rc); }
/* * Request the full chain certificate. */ static int dofullchain(struct conn *c, const char *addr) { int rc; long lc; rc = 0; dodbg("%s: full chain", addr); if ((lc = nreq(c, addr)) < 0) warnx("%s: bad comm", addr); else if (200 != lc && 201 != lc) warnx("%s: bad HTTP: %ld", addr, lc); else rc = 1; if (0 == rc || verbose > 1) buf_dump(&c->buf); return(rc); }
/* * Note to the CA that a challenge response is in place. */ static int dochngresp(struct conn *c, const struct chng *chng, const char *th) { int rc; long lc; char *req; rc = 0; dodbg("%s: challenge", chng->uri); if (NULL == (req = json_fmt_challenge(chng->token, th))) warnx("json_fmt_challenge"); else if ((lc = sreq(c, chng->uri, req)) < 0) warnx("%s: bad comm", chng->uri); else if (200 != lc && 201 != lc && 202 != lc) warnx("%s: bad HTTP: %ld", chng->uri, lc); else rc = 1; if (0 == rc || verbose > 1) buf_dump(&c->buf); free(req); return(rc); }
/****************************************************************** * * GSE2.0 example driver (sts 14.4.2001) * ******************************************************************/ main() { int i=0,ierr,ii=-1,i3=1; long data[20],*data2; int n,n2=0; long chksum=0,chksum2=0; /* initialize checksum to zero! */ FILE *fp, *fp2; struct header head, head2; char hline[121],tline[82]=""; buf_init (); /* initialize the character buffer */ fp = fopen ("test.gse","w"); /* open the output file */ head.d_year=1998; /* now, collect example */ head.d_mon=4; /* header information */ head.d_day=27; /* into the header structure */ head.t_hour=9; /* */ head.t_min=29; /* */ head.t_sec=3.123; /* */ strcpy (head.station,"FELD "); /* */ strcpy (head.channel,"SHZ"); /* */ strcpy (head.auxid,"VEL "); /* we have velocity data */ strcpy (head.datatype,"CM6"); /* */ head.n_samps=n=13; /* */ head.samp_rate=62.5; /* */ head.calib=1.; /* */ head.calper=6.283; /* that's ~ 2pi for our convenience */ strcpy (head.instype,"LE-3D ");/* */ head.hang=-1.0; /* */ head.vang=0.; /* header completed */ printf(">>>> %4d/%02d/%02d %02d:%02d:%06.3f %-5s %-3s %-4s %-3s %8d %11.6f %10.4e %7.3f %-6s %5.1f %4.1f\n", head.d_year, head.d_mon, head.d_day, head.t_hour, head.t_min, head.t_sec, head.station, head.channel, head.auxid, head.datatype, head.n_samps, head.samp_rate, head.calib, head.calper, head.instype, head.hang, head.vang); data[0]=13; printf("%d %ld\n",i,data[0]); for (i=1;i<n;i++) // let's create some data (13 points) { i3=i3*ii; data[i]=i3*data[i-1]*4+5; printf("%d %ld\n",i,data[i]); } /* 1st, compute the checksum */ chksum=labs(check_sum (data,n,chksum)); diff_2nd (data,n,0); /* 2nd, compute the 2nd differences */ ierr=compress_6b (data,n); /* 3rd, character-encode the data */ printf("error status after compression: %d\n",ierr); write_header(fp,&head); /* 4th, write the header to the output file */ buf_dump (fp); /* 5th, write the data to the output file */ fprintf (fp,"CHK2 %8ld\n\n",chksum); /* 6th, write checksum and closing line */ fclose(fp); /* close the output file */ buf_free (); /* clean up! */ /* That's all! Now, we can try to read the GSE2.0 file */ fp2 = fopen ("test.gse","r"); read_header(fp2,&head2); /* find and read the header line */ printf("<<<< %4d/%02d/%02d %02d:%02d:%06.3f %-5s %-3s %-4s %-3s %8d %11.6f %10.4e %7.3f %-6s %5.1f %4.1f\n", head2.d_year, head2.d_mon, head2.d_day, head2.t_hour, head2.t_min, head2.t_sec, head2.station, head2.channel, head2.auxid, head2.datatype, head2.n_samps, head2.samp_rate, head2.calib, head2.calper, head2.instype, head2.hang, head2.vang); data2 = (long *) calloc (head2.n_samps,sizeof(long)); /* allocate data vector */ n2 = decomp_6b (fp2, head2.n_samps, data2); /* read and decode the data */ printf("actual number of data read: %d\n",n2); rem_2nd_diff (data2, n2); /* remove second differences */ chksum=0; if (fgets(tline,82,fp2) == NULL) /* read next line (there might be */ { printf ("GSE: No CHK2 found before EOF\n"); /* an additional */ return; } /* blank line) */ if (strncmp(tline,"CHK2",4)) /* and look for CHK2 */ { if (fgets(tline,82,fp2) == NULL) /* read another line */ { printf ("GSE: No CHK2 found before EOF\n"); return; } } if (strncmp(tline,"CHK2",4)) { printf ("GSE: No CHK2 found!\n"); return; } sscanf(tline,"%*s %ld",&chksum); /* extract checksum */ chksum2 = check_sum (data2, n2, chksum2); /* compute checksum from data */ printf("checksum read : %ld \nchecksum computed: %ld\n",chksum,chksum2); for (i=0;i<n2;i++) /* print out data. We got velocity data */ { /* hence, we multiply by 2pi*calib/calper */ data2[i] = (long) data2[i] * 6.283 * head2.calib / head2.calper; printf("%d %ld \n",i,data2[i]); } close(fp2); free (data2); /* clean up */ }