├── .gitignore ├── migrations ├── 1_initial_migration.js └── 2_deploy_contracts.js ├── contracts ├── Migrations.sol └── Asn1Decode.sol ├── truffle.js ├── truffle-config.js ├── package.json └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | 3 | -------------------------------------------------------------------------------- /migrations/1_initial_migration.js: -------------------------------------------------------------------------------- 1 | var Migrations = artifacts.require("./Migrations.sol"); 2 | 3 | module.exports = function(deployer) { 4 | deployer.deploy(Migrations); 5 | }; 6 | -------------------------------------------------------------------------------- /migrations/2_deploy_contracts.js: -------------------------------------------------------------------------------- 1 | var Asn1Decode = artifacts.require("Asn1Decode"); 2 | // var NodePtr = artifacts.require("NodePtr"); 3 | 4 | module.exports = function(deployer, network) { 5 | // I believe these are not needed since all the libraries are compiled into one because all their functions are declared as `internal` 6 | // In fact this library shouldn't even be deployed at all since it will be compiled into the contract that uses it 7 | // deployer.deploy(NodePtr); 8 | // deployer.link(NodePtr, Asn1Decode); 9 | 10 | deployer.deploy(Asn1Decode); 11 | }; 12 | -------------------------------------------------------------------------------- /contracts/Migrations.sol: -------------------------------------------------------------------------------- 1 | pragma solidity >=0.4.21 <0.6.0; 2 | 3 | contract Migrations { 4 | address public owner; 5 | uint public last_completed_migration; 6 | 7 | constructor() public { 8 | owner = msg.sender; 9 | } 10 | 11 | modifier restricted() { 12 | if (msg.sender == owner) _; 13 | } 14 | 15 | function setCompleted(uint completed) public restricted { 16 | last_completed_migration = completed; 17 | } 18 | 19 | function upgrade(address new_address) public restricted { 20 | Migrations upgraded = Migrations(new_address); 21 | upgraded.setCompleted(last_completed_migration); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /truffle.js: -------------------------------------------------------------------------------- 1 | /* 2 | * NB: since truffle-hdwallet-provider 0.0.5 you must wrap HDWallet providers in a 3 | * function when declaring them. Failure to do so will cause commands to hang. ex: 4 | * ``` 5 | * mainnet: { 6 | * provider: function() { 7 | * return new HDWalletProvider(mnemonic, 'https://mainnet.infura.io/') 8 | * }, 9 | * network_id: '1', 10 | * gas: 4500000, 11 | * gasPrice: 10000000000, 12 | * }, 13 | */ 14 | 15 | module.exports = { 16 | compilers: { 17 | solc: { 18 | version: "0.5.2" 19 | } 20 | }, 21 | solc: { 22 | optimizer: { 23 | enabled: true, 24 | runs: 200 25 | } 26 | } 27 | }; 28 | -------------------------------------------------------------------------------- /truffle-config.js: -------------------------------------------------------------------------------- 1 | /* 2 | * NB: since truffle-hdwallet-provider 0.0.5 you must wrap HDWallet providers in a 3 | * function when declaring them. Failure to do so will cause commands to hang. ex: 4 | * ``` 5 | * mainnet: { 6 | * provider: function() { 7 | * return new HDWalletProvider(mnemonic, 'https://mainnet.infura.io/') 8 | * }, 9 | * network_id: '1', 10 | * gas: 4500000, 11 | * gasPrice: 10000000000, 12 | * }, 13 | */ 14 | 15 | module.exports = { 16 | // See 17 | // compilers: { 18 | // solc: { 19 | // version: "0.5.2" 20 | // } 21 | // }, 22 | solc: { 23 | optimizer: { 24 | enabled: true, 25 | runs: 200 26 | } 27 | } 28 | }; 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "asn1-decode", 3 | "version": "1.0.0", 4 | "description": "Solidity functions for traversing an ASN1-encoded data structure", 5 | "directories": { 6 | "test": "test" 7 | }, 8 | "scripts": { 9 | "compile": "truffle compile", 10 | "migrate": "truffle migrate", 11 | "test": "truffle test" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/JonahGroendal/asn1-decode.git" 16 | }, 17 | "keywords": [ 18 | "asn1", 19 | "decoder", 20 | "Solidity" 21 | ], 22 | "author": "Jonah Groendal", 23 | "license": "UNLICENSED", 24 | "bugs": { 25 | "url": "https://github.com/JonahGroendal/asn1-decode/issues" 26 | }, 27 | "homepage": "https://github.com/JonahGroendal/asn1-decode#readme", 28 | "devDependencies": { 29 | "truffle": "^5.1.53" 30 | }, 31 | "dependencies": { 32 | "@ensdomains/dnssec-oracle": "github:JonahGroendal/dnssec-oracle" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Jonah Groendal 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /contracts/Asn1Decode.sol: -------------------------------------------------------------------------------- 1 | pragma solidity ^0.5.2; 2 | 3 | import "@ensdomains/dnssec-oracle/contracts/BytesUtils.sol"; 4 | 5 | library NodePtr { 6 | // Unpack first byte index 7 | function ixs(uint self) internal pure returns (uint) { 8 | return uint80(self); 9 | } 10 | // Unpack first content byte index 11 | function ixf(uint self) internal pure returns (uint) { 12 | return uint80(self>>80); 13 | } 14 | // Unpack last content byte index 15 | function ixl(uint self) internal pure returns (uint) { 16 | return uint80(self>>160); 17 | } 18 | // Pack 3 uint80s into a uint256 19 | function getPtr(uint _ixs, uint _ixf, uint _ixl) internal pure returns (uint) { 20 | _ixs |= _ixf<<80; 21 | _ixs |= _ixl<<160; 22 | return _ixs; 23 | } 24 | } 25 | 26 | library Asn1Decode { 27 | using NodePtr for uint; 28 | using BytesUtils for bytes; 29 | 30 | /* 31 | * @dev Get the root node. First step in traversing an ASN1 structure 32 | * @param der The DER-encoded ASN1 structure 33 | * @return A pointer to the outermost node 34 | */ 35 | function root(bytes memory der) internal pure returns (uint) { 36 | return readNodeLength(der, 0); 37 | } 38 | 39 | /* 40 | * @dev Get the root node of an ASN1 structure that's within a bit string value 41 | * @param der The DER-encoded ASN1 structure 42 | * @return A pointer to the outermost node 43 | */ 44 | function rootOfBitStringAt(bytes memory der, uint ptr) internal pure returns (uint) { 45 | require(der[ptr.ixs()] == 0x03, "Not type BIT STRING"); 46 | return readNodeLength(der, ptr.ixf()+1); 47 | } 48 | 49 | /* 50 | * @dev Get the root node of an ASN1 structure that's within an octet string value 51 | * @param der The DER-encoded ASN1 structure 52 | * @return A pointer to the outermost node 53 | */ 54 | function rootOfOctetStringAt(bytes memory der, uint ptr) internal pure returns (uint) { 55 | require(der[ptr.ixs()] == 0x04, "Not type OCTET STRING"); 56 | return readNodeLength(der, ptr.ixf()); 57 | } 58 | 59 | /* 60 | * @dev Get the next sibling node 61 | * @param der The DER-encoded ASN1 structure 62 | * @param ptr Points to the indices of the current node 63 | * @return A pointer to the next sibling node 64 | */ 65 | function nextSiblingOf(bytes memory der, uint ptr) internal pure returns (uint) { 66 | return readNodeLength(der, ptr.ixl()+1); 67 | } 68 | 69 | /* 70 | * @dev Get the first child node of the current node 71 | * @param der The DER-encoded ASN1 structure 72 | * @param ptr Points to the indices of the current node 73 | * @return A pointer to the first child node 74 | */ 75 | function firstChildOf(bytes memory der, uint ptr) internal pure returns (uint) { 76 | require(der[ptr.ixs()] & 0x20 == 0x20, "Not a constructed type"); 77 | return readNodeLength(der, ptr.ixf()); 78 | } 79 | 80 | /* 81 | * @dev Use for looping through children of a node (either i or j). 82 | * @param i Pointer to an ASN1 node 83 | * @param j Pointer to another ASN1 node of the same ASN1 structure 84 | * @return True iff j is child of i or i is child of j. 85 | */ 86 | function isChildOf(uint i, uint j) internal pure returns (bool) { 87 | return ( ((i.ixf() <= j.ixs()) && (j.ixl() <= i.ixl())) || 88 | ((j.ixf() <= i.ixs()) && (i.ixl() <= j.ixl())) ); 89 | } 90 | 91 | /* 92 | * @dev Extract value of node from DER-encoded structure 93 | * @param der The der-encoded ASN1 structure 94 | * @param ptr Points to the indices of the current node 95 | * @return Value bytes of node 96 | */ 97 | function bytesAt(bytes memory der, uint ptr) internal pure returns (bytes memory) { 98 | return der.substring(ptr.ixf(), ptr.ixl()+1 - ptr.ixf()); 99 | } 100 | 101 | /* 102 | * @dev Extract entire node from DER-encoded structure 103 | * @param der The DER-encoded ASN1 structure 104 | * @param ptr Points to the indices of the current node 105 | * @return All bytes of node 106 | */ 107 | function allBytesAt(bytes memory der, uint ptr) internal pure returns (bytes memory) { 108 | return der.substring(ptr.ixs(), ptr.ixl()+1 - ptr.ixs()); 109 | } 110 | 111 | /* 112 | * @dev Extract value of node from DER-encoded structure 113 | * @param der The DER-encoded ASN1 structure 114 | * @param ptr Points to the indices of the current node 115 | * @return Value bytes of node as bytes32 116 | */ 117 | function bytes32At(bytes memory der, uint ptr) internal pure returns (bytes32) { 118 | return der.readBytesN(ptr.ixf(), ptr.ixl()+1 - ptr.ixf()); 119 | } 120 | 121 | /* 122 | * @dev Extract value of node from DER-encoded structure 123 | * @param der The der-encoded ASN1 structure 124 | * @param ptr Points to the indices of the current node 125 | * @return Uint value of node 126 | */ 127 | function uintAt(bytes memory der, uint ptr) internal pure returns (uint) { 128 | require(der[ptr.ixs()] == 0x02, "Not type INTEGER"); 129 | require(der[ptr.ixf()] & 0x80 == 0, "Not positive"); 130 | uint len = ptr.ixl()+1 - ptr.ixf(); 131 | return uint(der.readBytesN(ptr.ixf(), len) >> (32-len)*8); 132 | } 133 | 134 | /* 135 | * @dev Extract value of a positive integer node from DER-encoded structure 136 | * @param der The DER-encoded ASN1 structure 137 | * @param ptr Points to the indices of the current node 138 | * @return Value bytes of a positive integer node 139 | */ 140 | function uintBytesAt(bytes memory der, uint ptr) internal pure returns (bytes memory) { 141 | require(der[ptr.ixs()] == 0x02, "Not type INTEGER"); 142 | require(der[ptr.ixf()] & 0x80 == 0, "Not positive"); 143 | uint valueLength = ptr.ixl()+1 - ptr.ixf(); 144 | if (der[ptr.ixf()] == 0) 145 | return der.substring(ptr.ixf()+1, valueLength-1); 146 | else 147 | return der.substring(ptr.ixf(), valueLength); 148 | } 149 | 150 | function keccakOfBytesAt(bytes memory der, uint ptr) internal pure returns (bytes32) { 151 | return der.keccak(ptr.ixf(), ptr.ixl()+1 - ptr.ixf()); 152 | } 153 | 154 | function keccakOfAllBytesAt(bytes memory der, uint ptr) internal pure returns (bytes32) { 155 | return der.keccak(ptr.ixs(), ptr.ixl()+1 - ptr.ixs()); 156 | } 157 | 158 | /* 159 | * @dev Extract value of bitstring node from DER-encoded structure 160 | * @param der The DER-encoded ASN1 structure 161 | * @param ptr Points to the indices of the current node 162 | * @return Value of bitstring converted to bytes 163 | */ 164 | function bitstringAt(bytes memory der, uint ptr) internal pure returns (bytes memory) { 165 | require(der[ptr.ixs()] == 0x03, "Not type BIT STRING"); 166 | // Only 00 padded bitstr can be converted to bytestr! 167 | require(der[ptr.ixf()] == 0x00); 168 | uint valueLength = ptr.ixl()+1 - ptr.ixf(); 169 | return der.substring(ptr.ixf()+1, valueLength-1); 170 | } 171 | 172 | function readNodeLength(bytes memory der, uint ix) private pure returns (uint) { 173 | uint length; 174 | uint80 ixFirstContentByte; 175 | uint80 ixLastContentByte; 176 | if ((der[ix+1] & 0x80) == 0) { 177 | length = uint8(der[ix+1]); 178 | ixFirstContentByte = uint80(ix+2); 179 | ixLastContentByte = uint80(ixFirstContentByte + length -1); 180 | } else { 181 | uint8 lengthbytesLength = uint8(der[ix+1] & 0x7F); 182 | if (lengthbytesLength == 1) 183 | length = der.readUint8(ix+2); 184 | else if (lengthbytesLength == 2) 185 | length = der.readUint16(ix+2); 186 | else 187 | length = uint(der.readBytesN(ix+2, lengthbytesLength) >> (32-lengthbytesLength)*8); 188 | ixFirstContentByte = uint80(ix+2+lengthbytesLength); 189 | ixLastContentByte = uint80(ixFirstContentByte + length -1); 190 | } 191 | return NodePtr.getPtr(ix, ixFirstContentByte, ixLastContentByte); 192 | } 193 | } 194 | --------------------------------------------------------------------------------