ByteArray PublicKey::getDerEncoded() throw (EncodeException) { BIO *buffer; int ndata, wrote; ByteArray ret; unsigned char *data; buffer = BIO_new(BIO_s_mem()); if (buffer == NULL) { throw EncodeException(EncodeException::BUFFER_CREATING, "PublicKey::getDerEncoded"); } wrote = i2d_PUBKEY_bio(buffer, this->key); if (!wrote) { BIO_free(buffer); throw EncodeException(EncodeException::DER_ENCODE, "PublicKey::getDerEncoded"); } ndata = BIO_get_mem_data(buffer, &data); if (ndata <= 0) { BIO_free(buffer); throw EncodeException(EncodeException::BUFFER_READING, "PublicKey::getDerEncoded"); } ret = ByteArray(data, ndata); BIO_free(buffer); return ret; }
std::string PublicKey::getPemEncoded() throw (EncodeException) { BIO *buffer; int ndata, wrote; std::string ret; ByteArray *retTemp; unsigned char *data; buffer = BIO_new(BIO_s_mem()); if (buffer == NULL) { throw EncodeException(EncodeException::BUFFER_CREATING, "PublicKey::getPemEncoded"); } wrote = PEM_write_bio_PUBKEY(buffer, this->key); if (!wrote) { BIO_free(buffer); throw EncodeException(EncodeException::PEM_ENCODE, "PublicKey::getPemEncoded"); } ndata = BIO_get_mem_data(buffer, &data); if (ndata <= 0) { BIO_free(buffer); throw EncodeException(EncodeException::BUFFER_READING, "PublicKey::getPemEncoded"); } retTemp = new ByteArray(data, ndata); ret = retTemp->toString(); delete retTemp; BIO_free(buffer); return ret; }
void Assembler::EncodeOperation(ast::Instruction& instr) { if (instr.op.type() == typeid(ast::Directive)){ return; } Op op = boost::get<Op>(instr.op); instr.encodedOp = EncodeOp(op); if (OneArg(op)){ //we're a jump if (instr.ArgB()){ //exactly one argument throw ArgumentException("One argument expected, got two or more.", instr.lineNumber); } if (ast::IsRegister(instr.ArgC())){ instr.encodedOp |= IBLIS_LS_MODE_BIT; } } else if (ThreeArgs(op)){ if (!instr.ArgA() || !instr.ArgB()){ throw ArgumentException("Three arguments expected.", instr.lineNumber); } if (!ast::IsRegister(instr.ArgC())){ throw EncodeException("C must be a register for arithmetic/compare operations.", instr.lineNumber); } if (!ast::IsRegister(instr.ArgA().get())){ instr.encodedOp |= IBLIS_LIT_A_BIT; } if (!ast::IsRegister(instr.ArgB().get())){ instr.encodedOp |= IBLIS_LIT_B_BIT; } } else { if (!instr.ArgB() || instr.ArgA()){ //exactly two arguments throw ArgumentException("Two arguments expected, got three.", instr.lineNumber); } if (ast::IsRegister(instr.ArgB().get())){ if (op == Op::CONST){ throw ArgumentException("Argument B for CONST cannot be a register reference.", instr.lineNumber); } instr.encodedOp |= IBLIS_LS_MODE_BIT; } else { if (op == Op::PUSH || op == Op::POP || op == Op::COPY){ throw EncodeException("Argument B for PUSH/POP/COPY must be a register.", instr.lineNumber); } } } }
ByteArray PublicKey::getKeyIdentifier() throw (EncodeException) { ByteArray ret; unsigned int size; X509_PUBKEY *pubkey = NULL; if(X509_PUBKEY_set(&pubkey, this->key) == 0) { throw EncodeException(EncodeException::UNKNOWN, "PublicKey::getKeyIdentifier"); } ret = ByteArray(EVP_MAX_MD_SIZE); EVP_Digest(pubkey->public_key->data, pubkey->public_key->length, ret.getDataPointer(), &size, EVP_sha1(), NULL); ret = ByteArray(ret.getDataPointer(), size); X509_PUBKEY_free(pubkey); return ret; //return ByteArray(digest, digestLen); /* ByteArray der = this->getDerEncoded(); MessageDigest md(MessageDigest::SHA1); MessageDigest::loadMessageDigestAlgorithms(); return md.doFinal(der);*/ }
PublicKey::PublicKey(std::string &pemEncoded) throw (EncodeException) : AsymmetricKey(NULL) { BIO *buffer; buffer = BIO_new(BIO_s_mem()); if (buffer == NULL) { throw EncodeException(EncodeException::BUFFER_CREATING, "PublicKey::PublicKey"); } if ((unsigned int)(BIO_write(buffer, pemEncoded.c_str(), pemEncoded.size())) != pemEncoded.size()) { BIO_free(buffer); throw EncodeException(EncodeException::BUFFER_WRITING, "PublicKey::PublicKey"); } this->key = PEM_read_bio_PUBKEY(buffer, NULL, NULL, NULL); if (this->key == NULL) { BIO_free(buffer); throw EncodeException(EncodeException::PEM_DECODE, "PublicKey::PublicKey"); } BIO_free(buffer); }
PublicKey::PublicKey(ByteArray &derEncoded) throw (EncodeException) : AsymmetricKey(NULL) { /* DER format support only RSA, DSA and EC. DH isn't supported */ BIO *buffer; buffer = BIO_new(BIO_s_mem()); if (buffer == NULL) { throw EncodeException(EncodeException::BUFFER_CREATING, "PublicKey::PublicKey"); } if ((unsigned int)(BIO_write(buffer, derEncoded.getDataPointer(), derEncoded.size())) != derEncoded.size()) { BIO_free(buffer); throw EncodeException(EncodeException::BUFFER_WRITING, "PublicKey::PublicKey"); } this->key = d2i_PUBKEY_bio(buffer, NULL); /* TODO: will the second parameter work fine ? */ if (this->key == NULL) { BIO_free(buffer); throw EncodeException(EncodeException::DER_DECODE, "PublicKey::PublicKey"); } BIO_free(buffer); }
CertificateRequestSPKAC* CertificateRequestFactory::fromSPKAC(std::string &path) throw (EncodeException, RandomException, NetscapeSPKIException) { STACK_OF(CONF_VALUE) *sk=NULL; LHASH_OF(CONF_VALUE) *parms=NULL; X509_REQ *req=NULL; CONF_VALUE *cv=NULL; NETSCAPE_SPKI *spki = NULL; X509_REQ_INFO *ri; char *type,*buf; EVP_PKEY *pktmp=NULL; X509_NAME *n=NULL; unsigned long chtype = MBSTRING_ASC; int i; long errline; int nid; CertificateRequestSPKAC* ret=NULL; /* * Load input file into a hash table. (This is just an easy * way to read and parse the file, then put it into a convenient * STACK format). */ parms=CONF_load(NULL,path.c_str(),&errline); if (parms == NULL) { throw EncodeException(EncodeException::BUFFER_READING, "CertificateRequestFactory::fromSPKAC"); } sk=CONF_get_section(parms, "default"); if (sk_CONF_VALUE_num(sk) == 0) { if (parms != NULL) CONF_free(parms); throw EncodeException(EncodeException::BUFFER_READING, "CertificateRequestFactory::fromSPKAC"); } /* * Now create a dummy X509 request structure. We don't actually * have an X509 request, but we have many of the components * (a public key, various DN components). The idea is that we * put these components into the right X509 request structure * and we can use the same code as if you had a real X509 request. */ req=X509_REQ_new(); if (req == NULL) { if (parms != NULL) CONF_free(parms); throw RandomException(RandomException::INTERNAL_ERROR, "CertificateRequestFactory::fromSPKAC"); } /* * Build up the subject name set. */ ri=req->req_info; n = ri->subject; for (i = 0; ; i++) { if (sk_CONF_VALUE_num(sk) <= i) break; cv=sk_CONF_VALUE_value(sk,i); type=cv->name; /* Skip past any leading X. X: X, etc to allow for * multiple instances */ for (buf = cv->name; *buf ; buf++) if ((*buf == ':') || (*buf == ',') || (*buf == '.')) { buf++; if (*buf) type = buf; break; } buf=cv->value; if ((nid=OBJ_txt2nid(type)) == NID_undef) { if (strcmp(type, "SPKAC") == 0) { spki = NETSCAPE_SPKI_b64_decode(cv->value, -1); if (spki == NULL) { if (parms != NULL) CONF_free(parms); throw EncodeException(EncodeException::BASE64_DECODE, "CertificateRequestFactory::fromSPKAC"); } } continue; } if (!X509_NAME_add_entry_by_NID(n, nid, chtype, (unsigned char *)buf, -1, -1, 0)) { if (parms != NULL) CONF_free(parms); if (spki != NULL) NETSCAPE_SPKI_free(spki); throw RandomException(RandomException::INTERNAL_ERROR, "CertificateRequestFactory::fromSPKAC"); } } if (spki == NULL) { if (parms != NULL) CONF_free(parms); throw NetscapeSPKIException(NetscapeSPKIException::SET_NO_VALUE, "CertificateRequestFactory::fromSPKAC"); } /* * Now extract the key from the SPKI structure. */ if ((pktmp=NETSCAPE_SPKI_get_pubkey(spki)) == NULL) { if (parms != NULL) CONF_free(parms); if (spki != NULL) NETSCAPE_SPKI_free(spki); throw NetscapeSPKIException(NetscapeSPKIException::SET_NO_VALUE, "CertificateRequestFactory::fromSPKAC"); } X509_REQ_set_pubkey(req,pktmp); EVP_PKEY_free(pktmp); ret = new CertificateRequestSPKAC(req, spki); return ret; }