static int set_hex(const char *in, unsigned char *out, int size) { int i, n; unsigned char j; i = size * 2; n = strlen(in); if (n > i) { BIO_printf(bio_err, "hex string is too long, ignoring excess\n"); n = i; /* ignore exceeding part */ } else if (n < i) { BIO_printf(bio_err, "hex string is too short, padding with zero bytes to length\n"); } memset(out, 0, size); for (i = 0; i < n; i++) { j = (unsigned char)*in++; if (!isxdigit(j)) { BIO_printf(bio_err, "non-hex digit\n"); return 0; } j = (unsigned char)OPENSSL_hexchar2int(j); if (i & 1) out[i / 2] |= j; else out[i / 2] = (j << 4); } return 1; }
/* * Same thing as OPENSSL_hexstr2buf() but enables us to pass the string in * 3 chunks */ static unsigned char *multihexstr2buf(const char *str[3], size_t *len) { size_t outer, inner, curr = 0; unsigned char *outbuf; size_t totlen = 0; /* Check lengths of all input strings are even */ for (outer = 0; outer < 3; outer++) { totlen += strlen(str[outer]); if ((totlen & 1) != 0) return NULL; } totlen /= 2; outbuf = OPENSSL_malloc(totlen); if (outbuf == NULL) return NULL; for (outer = 0; outer < 3; outer++) { for (inner = 0; str[outer][inner] != 0; inner += 2) { int hi, lo; hi = OPENSSL_hexchar2int(str[outer][inner]); lo = OPENSSL_hexchar2int(str[outer][inner + 1]); if (hi < 0 || lo < 0) { OPENSSL_free(outbuf); return NULL; } outbuf[curr++] = (hi << 4) | lo; } } *len = totlen; return outbuf; }
static int load_iv(char **fromp, unsigned char *to, int num) { int v, i; char *from; from = *fromp; for (i = 0; i < num; i++) to[i] = 0; num *= 2; for (i = 0; i < num; i++) { v = OPENSSL_hexchar2int(*from); if (v < 0) { PEMerr(PEM_F_LOAD_IV, PEM_R_BAD_IV_CHARS); return 0; } from++; to[i / 2] |= v << (long)((!(i & 1)) * 4); } *fromp = from; return 1; }
int a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size) { int i, j, k, m, n, again, bufsize; unsigned char *s = NULL, *sp; unsigned char *bufp; int num = 0, slen = 0, first = 1; bs->type = V_ASN1_INTEGER; bufsize = BIO_gets(bp, buf, size); for (;;) { if (bufsize < 1) goto err; i = bufsize; if (buf[i - 1] == '\n') buf[--i] = '\0'; if (i == 0) goto err; if (buf[i - 1] == '\r') buf[--i] = '\0'; if (i == 0) goto err; again = (buf[i - 1] == '\\'); for (j = 0; j < i; j++) { #ifndef CHARSET_EBCDIC if (!(((buf[j] >= '0') && (buf[j] <= '9')) || ((buf[j] >= 'a') && (buf[j] <= 'f')) || ((buf[j] >= 'A') && (buf[j] <= 'F')))) #else /* * This #ifdef is not strictly necessary, since the characters * A...F a...f 0...9 are contiguous (yes, even in EBCDIC - but * not the whole alphabet). Nevertheless, isxdigit() is faster. */ if (!isxdigit(buf[j])) #endif { i = j; break; } } buf[i] = '\0'; /* * We have now cleared all the crap off the end of the line */ if (i < 2) goto err; bufp = (unsigned char *)buf; if (first) { first = 0; if ((bufp[0] == '0') && (bufp[1] == '0')) { bufp += 2; i -= 2; } } k = 0; i -= again; if (i % 2 != 0) { ASN1err(ASN1_F_A2I_ASN1_INTEGER, ASN1_R_ODD_NUMBER_OF_CHARS); OPENSSL_free(s); return 0; } i /= 2; if (num + i > slen) { sp = OPENSSL_clear_realloc(s, slen, num + i * 2); if (sp == NULL) { ASN1err(ASN1_F_A2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE); OPENSSL_free(s); return 0; } s = sp; slen = num + i * 2; } for (j = 0; j < i; j++, k += 2) { for (n = 0; n < 2; n++) { m = OPENSSL_hexchar2int(bufp[k + n]); if (m < 0) { ASN1err(ASN1_F_A2I_ASN1_INTEGER, ASN1_R_NON_HEX_CHARACTERS); goto err; } s[num + j] <<= 4; s[num + j] |= m; } } num += i; if (again) bufsize = BIO_gets(bp, buf, size); else break; } bs->length = num; bs->data = s; return 1; err: ASN1err(ASN1_F_A2I_ASN1_INTEGER, ASN1_R_SHORT_LINE); OPENSSL_free(s); return 0; }
int a2i_ASN1_INTEGER(BIO *bp, ASN1_INTEGER *bs, char *buf, int size) { int i, j, k, m, n, again, bufsize; unsigned char *s = NULL, *sp; unsigned char *bufp; int num = 0, slen = 0, first = 1; bs->type = V_ASN1_INTEGER; bufsize = BIO_gets(bp, buf, size); for (;;) { if (bufsize < 1) goto err; i = bufsize; if (buf[i - 1] == '\n') buf[--i] = '\0'; if (i == 0) goto err; if (buf[i - 1] == '\r') buf[--i] = '\0'; if (i == 0) goto err; again = (buf[i - 1] == '\\'); for (j = 0; j < i; j++) { if (!ossl_isxdigit(buf[j])) { i = j; break; } } buf[i] = '\0'; /* * We have now cleared all the crap off the end of the line */ if (i < 2) goto err; bufp = (unsigned char *)buf; if (first) { first = 0; if ((bufp[0] == '0') && (bufp[1] == '0')) { bufp += 2; i -= 2; } } k = 0; i -= again; if (i % 2 != 0) { ASN1err(ASN1_F_A2I_ASN1_INTEGER, ASN1_R_ODD_NUMBER_OF_CHARS); OPENSSL_free(s); return 0; } i /= 2; if (num + i > slen) { sp = OPENSSL_clear_realloc(s, slen, num + i * 2); if (sp == NULL) { ASN1err(ASN1_F_A2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE); OPENSSL_free(s); return 0; } s = sp; slen = num + i * 2; } for (j = 0; j < i; j++, k += 2) { for (n = 0; n < 2; n++) { m = OPENSSL_hexchar2int(bufp[k + n]); if (m < 0) { ASN1err(ASN1_F_A2I_ASN1_INTEGER, ASN1_R_NON_HEX_CHARACTERS); goto err; } s[num + j] <<= 4; s[num + j] |= m; } } num += i; if (again) bufsize = BIO_gets(bp, buf, size); else break; } bs->length = num; bs->data = s; return 1; err: ASN1err(ASN1_F_A2I_ASN1_INTEGER, ASN1_R_SHORT_LINE); OPENSSL_free(s); return 0; }