Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support type for get/set methods of Object #131

Merged
merged 4 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
language: node_js
node_js:
# v10 is disabled cause Linux Segmentation fault
# https://travis-ci.org/PeculiarVentures/graphene/builds/458161752
# - "10"
- "8"
- "12"

env:
- CXX=g++
Expand Down
5 changes: 4 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,15 @@ declare namespace GraphenePkcs11 {
* destroys an object
*/
public destroy(): void;
public getAttribute(attr: string): any;
public getAttribute(name: string): any;
public getAttribute(type: number): Buffer;
public getAttribute(attrs: ITemplate): ITemplate;
public setAttribute(attrs: string, value: any): void;
public setAttribute(attrs: ITemplate): void;
public get(name: string): any;
public get(type: number): Buffer;
public set(name: string, value: any): void;
public set(type: number, value: any): void;
public toType<T extends SessionObject>(): T;
}

Expand Down
56 changes: 6 additions & 50 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@
"name": "graphene-pk11",
"version": "2.1.8",
"description": "A simple layer for interacting with PKCS #11 / PKCS11 / CryptoKI for Node in TypeScript",
"main": "./build/index.js",
"main": "./build/cjs/index.js",
"module": "./build/es2015/index.js",
"types": "index.d.ts",
"scripts": {
"clean": "rm -rf build/ coverage/ .nyc_output/ npm-debug.log npm-debug.log.*",
"test": "mocha",
"build": "tsc",
"clean": "rimraf build",
"build": "npm run build:module",
"build:module": "npm run build:cjs && npm run build:es2015",
"build:cjs": "tsc -p tsconfig.json --removeComments --module commonjs --outDir build/cjs",
"build:es2015": "tsc -p tsconfig.json --removeComments --module ES2015 --outDir build/es2015",
"rebuild": "npm run clean && npm run build",
"prepare": "npm run build",
"pub": "npm run build && npm version patch && npm publish && git push",
"pub": "npm run rebuild && npm version patch && npm publish && git push && git push --tags",
"sync": "git ac && git pull --rebase && git push",
"coverage": "nyc npm test",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
Expand All @@ -28,10 +33,11 @@
},
"devDependencies": {
"@types/mocha": "^8.0.0",
"@types/node": "^12.12.50",
"@types/node": "^12.12.53",
"coveralls": "^3.1.0",
"mocha": "^8.0.1",
"nyc": "^15.1.0",
"rimraf": "^3.0.2",
"ts-node": "^8.10.2",
"typescript": "^3.9.7"
},
Expand Down
70 changes: 40 additions & 30 deletions src/object.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as assert from "assert";
import * as pkcs11 from "pkcs11js";
import * as core from "./core";
import { Session } from "./session";
Expand Down Expand Up @@ -82,52 +83,61 @@ export class SessionObject extends core.HandleObject {
this.lib.C_DestroyObject(this.session.handle, this.handle);
}

public getAttribute(attr: string): any;
public getAttribute(type: number): Buffer;
public getAttribute(name: string): any;
public getAttribute(attrs: ITemplate): ITemplate;
public getAttribute(attrs: any): any {
let template: ITemplate;
if (typeof attrs === "string") {
public getAttribute(param: any): any {
if (core.isNumber(param)) {
// number
return this.lib.C_GetAttributeValue(this.session.handle, this.handle, [
{ type: param },
])[0].value;
} else if (core.isString(param)) {
// string
template = {};
(template as any)[attrs] = null;
} else {
// template
template = attrs;
const res = this.lib.C_GetAttributeValue(
this.session.handle,
this.handle,
Template.toPkcs11({ [param]: null }));

return Template.fromPkcs11(res)[param];
}
let tmpl = Template.toPkcs11(template);
// template

// get size of values of attributes
tmpl = this.lib.C_GetAttributeValue(this.session.handle, this.handle, tmpl);
const res = this.lib.C_GetAttributeValue(this.session.handle, this.handle, Template.toPkcs11(param));

if (typeof attrs === "string") {
return Template.fromPkcs11(tmpl)[attrs];
}
return Template.fromPkcs11(tmpl);
return Template.fromPkcs11(res);
}

public setAttribute(attrs: string, value: any): void;
public setAttribute(type: number, value: number | boolean | string | Buffer): void;
public setAttribute(name: string, value: any): void;
public setAttribute(attrs: ITemplate): void;
public setAttribute(attrs: any, value?: any): void {
if (core.isString(attrs)) {
const tmp: ITemplate = {};
(tmp as any)[attrs as string] = value;
attrs = tmp;
public setAttribute(param: any, value?: any): void {
let tmpl: pkcs11.Template = [];
if (core.isNumber(param)) {
// type: number
tmpl.push({ type: param, value });
} else if (core.isString(param)) {
// name: string, value: any
tmpl = Template.toPkcs11({ [param]: value });
} else {
// attrs: ITemplate
tmpl = Template.toPkcs11(param);
}
const tmpl = Template.toPkcs11(attrs);

this.lib.C_SetAttributeValue(this.session.handle, this.handle, tmpl);
}

public get(name: string): any {
const tmpl: any = {};
tmpl[name] = null;
return (this.getAttribute(tmpl) as any)[name];
public get(type: number): Buffer;
public get(name: string): any;
public get(param: any): any {
return this.getAttribute(param as any);
}

public set(name: string, value: any) {
const tmpl: any = {};
tmpl[name] = value;
this.setAttribute(tmpl);
public set(type: number, value: number | boolean | string | Buffer): void;
public set(name: string, value: any): void;
public set(param: any, value: any) {
this.setAttribute(param, value);
}

get class(): ObjectClass {
Expand Down
14 changes: 14 additions & 0 deletions test/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ context("Object", () => {
assert.equal(obj.getAttribute("label"), "new label");
});

it("set attribute by type", () => {
const obj = session.create({
class: graphene.ObjectClass.PUBLIC_KEY,
label: "label",
keyType: graphene.KeyType.RSA,
wrap: true,
modulus,
publicExponent: exponent,
});

obj.set(pkcs11.CKA_LABEL, "new label");
assert.equal(obj.get(pkcs11.CKA_LABEL), "new label");
});

it("set attribute by template", () => {
const obj = session.create({
class: graphene.ObjectClass.PUBLIC_KEY,
Expand Down
64 changes: 9 additions & 55 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,60 +1,14 @@
{
"compilerOptions": {
/* Basic Options */
"target": "es2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./build", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
"removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
"importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
"strictPropertyInitialization": false, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
"target": "es2015",
"module": "commonjs",
"outDir": "./build",
"moduleResolution": "node",
"removeComments": true,
"importHelpers": true,
"strict": true,
"strictPropertyInitialization": false,
"esModuleInterop": true
},
"exclude": [
"index.d.ts",
Expand Down