static int snmp_translate(struct nf_conn *ct, int dir, struct sk_buff *skb) { struct iphdr *iph = ip_hdr(skb); struct udphdr *udph = (struct udphdr *)((__be32 *)iph + iph->ihl); u16 datalen = ntohs(udph->len) - sizeof(struct udphdr); char *data = (unsigned char *)udph + sizeof(struct udphdr); struct snmp_ctx ctx; int ret; if (dir == IP_CT_DIR_ORIGINAL) { ctx.from = ct->tuplehash[dir].tuple.src.u3.ip; ctx.to = ct->tuplehash[!dir].tuple.dst.u3.ip; } else { ctx.from = ct->tuplehash[!dir].tuple.src.u3.ip; ctx.to = ct->tuplehash[dir].tuple.dst.u3.ip; } if (ctx.from == ctx.to) return NF_ACCEPT; ctx.begin = (unsigned char *)udph + sizeof(struct udphdr); ctx.check = &udph->check; ret = asn1_ber_decoder(&nf_nat_snmp_basic_decoder, &ctx, data, datalen); if (ret < 0) { nf_ct_helper_log(skb, ct, "parser failed\n"); return NF_DROP; } return NF_ACCEPT; }
/** * pkcs7_parse_message - Parse a PKCS#7 message * @data: The raw binary ASN.1 encoded message to be parsed * @datalen: The size of the encoded message */ struct pkcs7_message *pkcs7_parse_message(const void *data, size_t datalen) { struct pkcs7_parse_context *ctx; struct pkcs7_message *msg = ERR_PTR(-ENOMEM); int ret; ctx = kzalloc(sizeof(struct pkcs7_parse_context), GFP_KERNEL); if (!ctx) goto out_no_ctx; ctx->msg = kzalloc(sizeof(struct pkcs7_message), GFP_KERNEL); if (!ctx->msg) goto out_no_msg; ctx->sinfo = kzalloc(sizeof(struct pkcs7_signed_info), GFP_KERNEL); if (!ctx->sinfo) goto out_no_sinfo; ctx->sinfo->sig = kzalloc(sizeof(struct public_key_signature), GFP_KERNEL); if (!ctx->sinfo->sig) goto out_no_sig; ctx->data = (unsigned long)data; ctx->ppcerts = &ctx->certs; ctx->ppsinfo = &ctx->msg->signed_infos; /* Attempt to decode the signature */ ret = asn1_ber_decoder(&pkcs7_decoder, ctx, data, datalen); if (ret < 0) { msg = ERR_PTR(ret); goto out; } ret = pkcs7_check_authattrs(ctx->msg); if (ret < 0) { msg = ERR_PTR(ret); goto out; } msg = ctx->msg; ctx->msg = NULL; out: while (ctx->certs) { struct x509_certificate *cert = ctx->certs; ctx->certs = cert->next; x509_free_certificate(cert); } out_no_sig: pkcs7_free_signed_info(ctx->sinfo); out_no_sinfo: pkcs7_free_message(ctx->msg); out_no_msg: kfree(ctx); out_no_ctx: return msg; }
/* * Parse an X.509 certificate */ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen) { struct x509_certificate *cert; struct x509_parse_context *ctx; long ret; ret = -ENOMEM; cert = kzalloc(sizeof(struct x509_certificate), GFP_KERNEL); if (!cert) goto error_no_cert; cert->pub = kzalloc(sizeof(struct public_key), GFP_KERNEL); if (!cert->pub) goto error_no_ctx; ctx = kzalloc(sizeof(struct x509_parse_context), GFP_KERNEL); if (!ctx) goto error_no_ctx; ctx->cert = cert; ctx->data = (unsigned long)data; /* Attempt to decode the certificate */ ret = asn1_ber_decoder(&x509_decoder, ctx, data, datalen); if (ret < 0) goto error_decode; /* Decode the public key */ ret = asn1_ber_decoder(&x509_rsakey_decoder, ctx, ctx->key, ctx->key_size); if (ret < 0) goto error_decode; kfree(ctx); return cert; error_decode: kfree(ctx); error_no_ctx: x509_free_certificate(cert); error_no_cert: return ERR_PTR(ret); }
/* * Parse a Microsoft Individual Code Signing blob */ int mscode_parse(void *_ctx, const void *content_data, size_t data_len, size_t asn1hdrlen) { struct pefile_context *ctx = _ctx; content_data -= asn1hdrlen; data_len += asn1hdrlen; pr_devel("Data: %zu [%*ph]\n", data_len, (unsigned)(data_len), content_data); return asn1_ber_decoder(&mscode_decoder, ctx, content_data, data_len); }
/** * rsa_parse_pub_key() - extracts an rsa private key from BER encoded buffer * and stores it in the provided struct rsa_key * * @rsa_key: struct rsa_key key representation * @key: key in BER format * @key_len: length of key * * Return: 0 on success or error code in case of error */ int rsa_parse_priv_key(struct rsa_key *rsa_key, const void *key, unsigned int key_len) { int ret; free_mpis(rsa_key); ret = asn1_ber_decoder(&rsaprivkey_decoder, rsa_key, key, key_len); if (ret < 0) goto error; return 0; error: free_mpis(rsa_key); return ret; }
static int qat_rsa_setkey(struct crypto_akcipher *tfm, const void *key, unsigned int keylen) { struct qat_rsa_ctx *ctx = akcipher_tfm_ctx(tfm); struct device *dev = &GET_DEV(ctx->inst->accel_dev); int ret; /* Free the old key if any */ if (ctx->n) dma_free_coherent(dev, ctx->key_sz, ctx->n, ctx->dma_n); if (ctx->e) dma_free_coherent(dev, ctx->key_sz, ctx->e, ctx->dma_e); if (ctx->d) { memset(ctx->d, '\0', ctx->key_sz); dma_free_coherent(dev, ctx->key_sz, ctx->d, ctx->dma_d); } ctx->n = NULL; ctx->e = NULL; ctx->d = NULL; ret = asn1_ber_decoder(&qat_rsakey_decoder, ctx, key, keylen); if (ret < 0) goto free; if (!ctx->n || !ctx->e) { /* invalid key provided */ ret = -EINVAL; goto free; } return 0; free: if (ctx->d) { memset(ctx->d, '\0', ctx->key_sz); dma_free_coherent(dev, ctx->key_sz, ctx->d, ctx->dma_d); ctx->d = NULL; } if (ctx->e) { dma_free_coherent(dev, ctx->key_sz, ctx->e, ctx->dma_e); ctx->e = NULL; } if (ctx->n) { dma_free_coherent(dev, ctx->key_sz, ctx->n, ctx->dma_n); ctx->n = NULL; ctx->key_sz = 0; } return ret; }
/* * Parse a Microsoft Individual Code Signing blob */ int mscode_parse(struct pefile_context *ctx) { const void *content_data; size_t data_len; int ret; ret = pkcs7_get_content_data(ctx->pkcs7, &content_data, &data_len, 1); if (ret) { pr_debug("PKCS#7 message does not contain data\n"); return ret; } pr_devel("Data: %zu [%*ph]\n", data_len, (unsigned)(data_len), content_data); return asn1_ber_decoder(&mscode_decoder, ctx, content_data, data_len); }
int PrintCerts(UINT8 *data, UINTN len, EFI_HANDLE image, CHAR16 *name) { EFI_SIGNATURE_LIST *CertList = (EFI_SIGNATURE_LIST *)data; EFI_SIGNATURE_DATA *Cert; EFI_GUID gX509 = EFI_CERT_X509_GUID; EFI_GUID gPKCS7 = EFI_CERT_PKCS7_GUID; EFI_GUID gRSA2048 = EFI_CERT_RSA2048_GUID; UINTN Index, count = 0, DataSize = len, CertCount; UINTN buflen; CHAR16 *ext; int status = 0; while ((DataSize > 0) && (DataSize >= CertList->SignatureListSize)) { count++; CertCount = (CertList->SignatureListSize - CertList->SignatureHeaderSize) / CertList->SignatureSize; Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) CertList + sizeof (EFI_SIGNATURE_LIST) + CertList->SignatureHeaderSize); // should all be X509 but just in case ... if (CompareGuid(&CertList->SignatureType, &gX509) == 0) ext = L"X509"; else if (CompareGuid(&CertList->SignatureType, &gPKCS7) == 0) ext = L"PKCS7"; else if (CompareGuid(&CertList->SignatureType, &gRSA2048) == 0) ext = L"RSA2048"; else ext = L"Unknown"; for (Index = 0; Index < CertCount; Index++) { if ( CertList->SignatureSize > 100 ) { Print(L"\nType: %s (GUID: %g)\n", ext, &Cert->SignatureOwner); buflen = CertList->SignatureSize-sizeof(EFI_GUID); status = asn1_ber_decoder(&x509_decoder, NULL, Cert->SignatureData, buflen); } Cert = (EFI_SIGNATURE_DATA *) ((UINT8 *) Cert + CertList->SignatureSize); } DataSize -= CertList->SignatureListSize; CertList = (EFI_SIGNATURE_LIST *) ((UINT8 *) CertList + CertList->SignatureListSize); } return status; }
/* * Parse an X.509 certificate */ struct x509_certificate *x509_cert_parse(const void *data, size_t datalen) { struct x509_certificate *cert; struct x509_parse_context *ctx; struct asymmetric_key_id *kid; long ret; ret = -ENOMEM; cert = kzalloc(sizeof(struct x509_certificate), GFP_KERNEL); if (!cert) goto error_no_cert; cert->pub = kzalloc(sizeof(struct public_key), GFP_KERNEL); if (!cert->pub) goto error_no_ctx; ctx = kzalloc(sizeof(struct x509_parse_context), GFP_KERNEL); if (!ctx) goto error_no_ctx; ctx->cert = cert; ctx->data = (unsigned long)data; /* Attempt to decode the certificate */ ret = asn1_ber_decoder(&x509_decoder, ctx, data, datalen); if (ret < 0) goto error_decode; /* Decode the AuthorityKeyIdentifier */ if (ctx->raw_akid) { pr_devel("AKID: %u %*phN\n", ctx->raw_akid_size, ctx->raw_akid_size, ctx->raw_akid); ret = asn1_ber_decoder(&x509_akid_decoder, ctx, ctx->raw_akid, ctx->raw_akid_size); if (ret < 0) { pr_warn("Couldn't decode AuthKeyIdentifier\n"); goto error_decode; } } /* Decode the public key */ ret = asn1_ber_decoder(&x509_rsakey_decoder, ctx, ctx->key, ctx->key_size); if (ret < 0) goto error_decode; /* Generate cert issuer + serial number key ID */ kid = asymmetric_key_generate_id(cert->raw_serial, cert->raw_serial_size, cert->raw_issuer, cert->raw_issuer_size); if (IS_ERR(kid)) { ret = PTR_ERR(kid); goto error_decode; } cert->id = kid; kfree(ctx); return cert; error_decode: kfree(ctx); error_no_ctx: x509_free_certificate(cert); error_no_cert: return ERR_PTR(ret); }